comparch.tex 170 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409
  1. \documentclass [a4paper,12pt]{article}
  2. \usepackage {graphicx}
  3. \usepackage {intfpc}
  4. \usepackage {longtable}
  5. %\usepackage {float}
  6. \usepackage{listings}
  7. \lstset{language=Delphi}
  8. \lstset{basicstyle=\sffamily\small}
  9. \lstset{commentstyle=\itshape}
  10. \lstset{keywordstyle=\bfseries}
  11. \lstset{frame=tb}
  12. % eats mem for longtable, but speeds up. Set to 10 for minimal mem req.
  13. \setcounter{LTchunksize}{200}
  14. % right aligned longtables
  15. \setlength\LTleft{0pt}
  16. \setlength\LTright\fill
  17. %\restylefloat{table}
  18. %\restylefloat{longtable}
  19. %\squeezetable
  20. \setcounter{totalnumber}{10}
  21. % This comes from the MDWTOOLS package and should discourage floating.
  22. \def\textfraction{0.1}
  23. \def\topfraction{0.9}
  24. \def\bottomfraction{0.9}
  25. \def\floatpagefraction{0.7}
  26. \def\fps@figure{htbp}
  27. \def\fpc@longtable{h}
  28. \title{FREE PASCAL}
  29. \title{Free Pascal 1.0.x Internal documentation}
  30. \author{Carl Eric Cod\`{e}re}
  31. \makeindex
  32. \begin{document}
  33. \maketitle
  34. \tableofcontents
  35. \newpage
  36. \listoffigures
  37. \newpage
  38. \textbf{TODO:}
  39. \begin{itemize}
  40. \item Explain architecture of the symbol table + API
  41. \item Describe in detail tsymtable, including all methods and fields
  42. \item Describe in detail procinfo (tprocinfo)
  43. \item Explain how a symbol is inserted into the symbol table (and how alignment requirements are met)
  44. \item Explain pparaitem
  45. \item Explain all symbol table fields
  46. \item Finish all internal routines definitions
  47. \item Architecture of the assembler generators + API
  48. \item Architecture of the PPU file and information
  49. \item Explain systems.pas
  50. \item Compiler DEFINES
  51. \item routine parsing and code generation algorithm
  52. \item (MvdV) OS specific stuff (like hardcoded linker includedirs)
  53. \end{itemize}
  54. \section{Introduction}
  55. \label{sec:introductionappendix}
  56. This document will describe the internal architecture of the Free Pascal
  57. Compiler version 1.0 release. This document is meant to be used as a guide
  58. for those who wish to understand how the compiler was created. Most of the
  59. architecture of the compiler described herein is based on the m68k version
  60. on the compiler, the i386 version of the compiler ressembles closely the
  61. m68k version, but there are subtle differences in the different interfaces.
  62. The architecture, and the different passes of the compiler are shown in
  63. figure \ref{fig1}.
  64. \begin{figure}
  65. \ifpdf
  66. % \epsfig{file=arch1d.pdf,width=\textwidth}
  67. \includegraphics{arch1.pdf}
  68. \else
  69. \includegraphics[width=6.45in,height=4.95in]{arch1.eps}
  70. \fi
  71. \caption{TTREE structure}
  72. \label{fig1}
  73. \end{figure}
  74. \section{Scanner / Tokenizer}
  75. The scanner and tokenizer is used to construct an input stream of tokens
  76. which will be fed to the parser. It is in this stage that the preprocessing
  77. is done, that all read compiler directives change the internal state
  78. variables of the compiler, and that all illegal characters found in the
  79. input stream cause an error.
  80. \subsection{Architecture}
  81. \label{subsec:architectureand}
  82. The general architecture of the scanner is show in figure \ref{fig2}
  83. \begin{figure}
  84. \ifpdf
  85. %\epsfig{file=arch2.png,width=\textwidth}
  86. \includegraphics{arch2.pdf}
  87. \else
  88. \includegraphics[width=5.87in,height=6.90in]{arch2.eps}
  89. \fi
  90. \caption{Possible tree Local compiler switches (tlocalswitches)}
  91. \label{fig2}
  92. \end{figure}
  93. Several types can be read from the input stream, a string, handled by
  94. readstring(), a numeric value, handled by readnumeric(), comments , compiler
  95. and preprocessor directives.
  96. \subsubsection{Input stream}
  97. \label{subsubsec:input}
  98. The input data is handled via the standard way of handling all the I/O in
  99. the compiler. That is to say, that it is a hook which can be overriden in
  100. \textbf{comphook.pas (do{\_}openinputfile)}, in case where another I/O
  101. method wants to be used.
  102. The default hook uses a non-buffered dos stream contained in
  103. \textbf{files.pas}
  104. \subsubsection{Preprocessor}
  105. \label{subsubsec:preprocessorhook}
  106. The scanner resolves all preprocessor directives and only gives to the
  107. parser the visible parts of the code (such as those which are included in
  108. conditional compilation). Compiler switches and directives are also saved in
  109. global variables while in the preprocessor, therefore this is part is
  110. completely independent of the parser.
  111. \paragraph{Conditional compilation (scandir.inc, scanner.pas)}
  112. The conditional compilation is handled via a preprocessor stack, where each
  113. directive is pushed on a stack, and popped when it is resolved. The actual
  114. implementation of the stack is a linked list of preprocessor directive
  115. items.
  116. \paragraph{Compiler switches (scandir.inc, switches.pas)}
  117. The compiler switches are handled via a lookup table which is linearly
  118. searched. Then another lookup table takes care of setting the appropriate
  119. bit flags and variables in the switches for this compilation process.
  120. \subsection{Scanner interface}
  121. \label{subsec:scanner}
  122. The parser only receives tokens as its input, where a token is a enumeration
  123. which indicates the type of the token, either a reserved word, a special
  124. character, an operator, a numeric constant, string, or an identifier.
  125. Resolution of the string into a token is done via lookup which searches the
  126. string table to find the equivalent token. This search is done using a
  127. binary search algorithm through the string table.
  128. In the case of identifiers, constants (including numeric values), the value
  129. is returned in the \textbf{pattern} string variable , with the appropriate
  130. return value of the token (numeric values are also returned as non-converted
  131. strings, with any special prefix included). In the case of operators, and
  132. reserved words, only the token itself must be assumed to be preserved. The
  133. read input string is assmued to be lost.
  134. Therefore the interface with the parser is with the \textbf{readtoken()}
  135. routine and the \textbf{pattern} variable.
  136. \subsubsection{Routines}
  137. \label{subsubsec:routinese}
  138. \begin{procedure}{ReadToken}
  139. \Declaration
  140. Procedure ReadToken;
  141. \Description
  142. Sets the global variable \textsf{token} to the current token read, and sets
  143. the \textsf{pattern} variable appropriately (if required).
  144. \end{procedure}
  145. % ?? :
  146. %\caption{: Symbol tables in memory}
  147. %\label{tab2}
  148. \subsubsection{Variables}
  149. \label{subsubsec:variablesglobal}
  150. \begin{variable}{Token}
  151. \Description
  152. Var Token : TToken;
  153. \Description
  154. Contains the contain token which was last read by a call to \seep{ReadToken}
  155. \SeeAlso
  156. \seep{ReadToken}
  157. \end{variable}
  158. %\caption{: Possible symbol table types (tsymboltabletype)}
  159. %\label{tab3}
  160. %\end{table}
  161. \begin{variable}{Pattern}
  162. \Declaration
  163. var Pattern : String;
  164. \Description
  165. Contains the string of the last pattern read by a call to
  166. \seep{ReadToken}
  167. \SeeAlso
  168. \seep{ReadToken}
  169. \end{variable}
  170. %\caption{: Symbol entry relationships (tsym)}
  171. %\label{tab4}
  172. \subsection{Assembler parser interface}
  173. \label{subsec:assembler}
  174. The inline assembler parser is completely separate from the pascal parser,
  175. therefore its scanning process is also completely independent. The scanner
  176. only takes care of the preprocessor part and comments, all the rest is
  177. passed character per character to the assembler parser via the
  178. \seef{AsmGetChar}() scanner routine.
  179. \begin{function}{AsmGetChar}
  180. \Declaration
  181. Function AsmGetChar: Char;
  182. \Description
  183. Returns the next character in the input stream.
  184. \end{function}
  185. %\caption{Possible symbol types (TSymTyp)}
  186. %\label{tab5}
  187. \section{The tree}
  188. \label{sec:mylabel2}
  189. \subsection{Architecture}
  190. \label{subsec:architecturenext}
  191. The tree is the basis of the compiler. When the compiler parses statements
  192. and blocks of code, they are converted to a tree representation. This tree
  193. representation is actually a doubly linked list. From this tree the code
  194. generation can easily be implemented.
  195. Assuming that you have the following pascal syntax:
  196. %\lstinline!x := x * y + (6 shl x);!
  197. \begin{center}
  198. $ x := x * y + (6\xspace shl \xspace x);$
  199. \end{center}
  200. The tree structure in picture \ref{fig3} will be built in memory, where each
  201. circle represents an element (a node ) in the tree:
  202. \begin{figure}
  203. \ifpdf
  204. %\epsfig{file=arch3.png,width=\textwidth}
  205. \includegraphics{arch3.pdf}
  206. \else
  207. \includegraphics[width=3.88in,height=3.65in]{arch3.eps}
  208. \fi
  209. \caption{Possible variable flags (tvaroptions)}
  210. \label{fig3}
  211. \end{figure}
  212. \subsection{Tree types}
  213. The following tree nodes are possible (of type \textsf{TTreeTyp):}
  214. \begin{longtable}{|l|p{10cm}|}
  215. %{|p{125pt}|p{316pt}|}
  216. \hline
  217. Tree type definition&
  218. Description \\
  219. \hline
  220. \endhead
  221. \hline
  222. \endfoot
  223. \textsf{addn}&
  224. \textsf{Represents the + operator} \\
  225. \textsf{muln}&
  226. \textsf{Represents the * operator} \\
  227. \textsf{subn}&
  228. \textsf{Represents the }\textsf{\textbf{-}}\textsf{ operator} \\
  229. \textsf{divn}&
  230. \textsf{Represents the }\textsf{\textbf{div}}\textsf{ operator} \\
  231. \textsf{symdifn}&
  232. \textsf{Represents the }\textsf{\textbf{><}}\textsf{ operator} \\
  233. \textsf{modn}&
  234. \textsf{Represents the }\textsf{\textbf{mod}}\textsf{ operator} \\
  235. \textsf{assignn}&
  236. \textsf{Represents the }\textsf{\textbf{:=}}\textsf{ operator (assignment)} \\
  237. \textsf{loadn}&
  238. \textsf{Represents the use of a variable} \\
  239. \textsf{rangen}&
  240. \textsf{Represents a numeric range (i.e 0..9)} \\
  241. \textsf{ltn}&
  242. \textsf{Represents the }\textsf{\textbf{<}}\textsf{ operator} \\
  243. \textsf{lten}&
  244. \textsf{Represents the }\textsf{\textbf{<=}}\textsf{ operator} \\
  245. \textsf{gtn}&
  246. \textsf{Represents the }\textsf{\textbf{>}}\textsf{ operator} \\
  247. \textsf{gten}&
  248. \textsf{Represents the }\textsf{\textbf{>=}}\textsf{ operator} \\
  249. \textsf{equaln}&
  250. \textsf{Represents the = operator} \\
  251. \textsf{unequaln}&
  252. \textsf{Represents the }\textsf{\textbf{<>}}\textsf{ operator} \\
  253. \textsf{inn}&
  254. \textsf{Represents the }\textsf{\textbf{in}}\textsf{ operator} \\
  255. \textsf{orn}&
  256. \textsf{Represents the }\textsf{\textbf{or}}\textsf{ operator} \\
  257. \textsf{xorn}&
  258. \textsf{Represents the }\textsf{\textbf{xor}}\textsf{ operator} \\
  259. \textsf{shrn}&
  260. \textsf{Represents the }\textsf{\textbf{shr}}\textsf{ operator} \\
  261. \textsf{shln}&
  262. \textsf{Represents the }\textsf{\textbf{shl}}\textsf{ operator} \\
  263. \textsf{slashn}&
  264. \textsf{Represents the / operator} \\
  265. \textsf{andn}&
  266. \textsf{Represents the }\textsf{\textbf{and}}\textsf{ operator} \\
  267. \textsf{subscriptn}&
  268. \textsf{Represents a field in an object or record} \\
  269. \textsf{derefn}&
  270. \textsf{Represents a pointer reference (such as the }\textsf{\textbf{\ }}\textsf{ operator)} \\
  271. \textsf{addrn}&
  272. \textsf{Represents the }\textsf{\textbf{@}}\textsf{ operator} \\
  273. \textsf{doubleaddrn}&
  274. \textsf{Represents the }\textsf{\textbf{@@}}\textsf{ operator} \\
  275. \textsf{ordconstn}&
  276. \textsf{Represents an ordinal constant} \\
  277. \textsf{typeconvn}&
  278. \textsf{Represents a typecast / type conversion} \\
  279. \textsf{calln}&
  280. \textsf{Represents a routine call} \\
  281. \textsf{callparan}&
  282. \textsf{Represents a parameter passed to a routine} \\
  283. \textsf{realconstn}&
  284. \textsf{Represents a floating point constant} \\
  285. \textsf{fixconstn}&
  286. \textsf{Represents a fixed point constant} \\
  287. \textsf{unaryminusn}&
  288. \textsf{Represents a sign change (e.g : -)} \\
  289. \textsf{asmn}&
  290. \textsf{Represents an assembler statement node} \\
  291. \textsf{vecn}&
  292. \textsf{Represents array indexing} \\
  293. \textsf{pointerconstn}&
  294. \textsf{Represents a pointer constant} \\
  295. \textsf{stringconstn}&
  296. \textsf{Represents a string constant} \\
  297. \textsf{funcretn}&
  298. \textsf{Represents the return function result variable (not loadn)} \\
  299. \textsf{selfn}&
  300. \textsf{Represents the self parameter (when is this parsed!)} \\
  301. \textsf{notn}&
  302. \textsf{Represents the }\textsf{\textbf{not}}\textsf{ operator} \\
  303. \textsf{inlinen}&
  304. \textsf{Represents one of the internal routines (writeln,ord, etc.)} \\
  305. \textsf{niln}&
  306. \textsf{Represents the }\textsf{\textbf{nil}}\textsf{ pointer } \\
  307. \textsf{erron}&
  308. \textsf{Represents error in parsing this node (used for error detection and correction)} \\
  309. \textsf{typen}&
  310. \textsf{Represents a type name (i.e typeof(obj)} \\
  311. \textsf{hnewn}&
  312. \textsf{Represents the }\textsf{\textbf{new }}\textsf{routine call on objects} \\
  313. \textsf{hdisposen}&
  314. \textsf{Represents the }\textsf{\textbf{dispose}}\textsf{ routine call on objects} \\
  315. \textsf{newn}&
  316. \textsf{Represents the }\textsf{\textbf{new}}\textsf{ routine call on non-objects} \\
  317. \textsf{simpledisposen}&
  318. \textsf{Represents the }\textsf{\textbf{dispose}}\textsf{ routine call on non-objects} \\
  319. \textsf{setelementn}&
  320. \textsf{Represents set elements (i.e : [a..b], [a,b,c]) (non-constant)} \\
  321. \textsf{setconstn}&
  322. \textsf{Represents set element constants i.e : [1..9], [1,2,3])} \\
  323. \textsf{blockn}&
  324. \textsf{Represents a block of statements} \\
  325. \textsf{statementn}&
  326. \textsf{One statement in a block of nodes} \\
  327. \textsf{loopn}&
  328. \textsf{Represents a loop (for, while, repeat) node} \\
  329. \textsf{ifn}&
  330. \textsf{Represents an }\textsf{\textbf{if}}\textsf{ statement} \\
  331. \textsf{breakn}&
  332. \textsf{Represents a }\textsf{\textbf{break}}\textsf{ statement} \\
  333. \textsf{continuen}&
  334. \textsf{Represents a }\textsf{\textbf{continue}}\textsf{ statement} \\
  335. \textsf{repeatn}&
  336. \textsf{Represents a }\textsf{\textbf{repeat }}\textsf{statement} \\
  337. \textsf{whilen}&
  338. \textsf{Represents a }\textsf{\textbf{while}}\textsf{ statement} \\
  339. \textsf{forn}&
  340. \textsf{Represents a }\textsf{\textbf{for}}\textsf{ statement} \\
  341. \textsf{exitn}&
  342. \textsf{Represents an }\textsf{\textbf{exit}}\textsf{ statement} \\
  343. \textsf{withn}&
  344. \textsf{Represents a }\textsf{\textbf{with}}\textsf{ statement} \\
  345. \textsf{casen}&
  346. \textsf{Represents a }\textsf{\textbf{case}}\textsf{ statement} \\
  347. \textsf{labeln}&
  348. \textsf{Represents a label statement} \\
  349. \textsf{goton}&
  350. \textsf{Represents a }\textsf{\textbf{goto}}\textsf{ statement} \\
  351. \textsf{simplenewn}&
  352. \textsf{Represents a }\textsf{\textbf{new}}\textsf{ statement } \\
  353. \textsf{tryexceptn}&
  354. \textsf{Represents a }\textsf{\textbf{try}}\textsf{ statement} \\
  355. \textsf{raisen}&
  356. \textsf{Represents a }\textsf{\textbf{raise}}\textsf{ statement} \\
  357. \textsf{\textit{switchesn}}&
  358. \textsf{\textit{Unused}} \\
  359. \textsf{tryfinallyn}&
  360. \textsf{Represents a }\textsf{\textbf{try..finally}}\textsf{ statement} \\
  361. \textsf{onn}&
  362. \textsf{Represents an }\textsf{\textbf{on..do}}\textsf{ statement} \\
  363. \textsf{isn}&
  364. \textsf{Represents the }\textsf{\textbf{is}}\textsf{ operator} \\
  365. \textsf{asn}&
  366. \textsf{Represents the }\textsf{\textbf{as}}\textsf{ typecast operator} \\
  367. \textsf{caretn}&
  368. \textsf{Represents the \ operator} \\
  369. \textsf{failn}&
  370. \textsf{Represents the }\textsf{\textbf{fail}}\textsf{ statement} \\
  371. \textsf{starstarn}&
  372. \textsf{Represents the }\textsf{\textbf{**}}\textsf{ operator (exponentiation)} \\
  373. \textsf{procinlinen}&
  374. \textsf{Represents an }\textsf{\textbf{inline}}\textsf{ routine} \\
  375. \textsf{arrayconstrucn}&
  376. \textsf{Represents a }\textsf{\textbf{[..]}}\textsf{ statement (array or sets)} \\
  377. \textsf{arrayconstructrangen}&
  378. \textsf{Represents ranges in [..] statements (array or sets)} \\
  379. \textsf{nothingn}&
  380. \textsf{Empty node} \\
  381. \textsf{loadvmtn}&
  382. \textsf{Load method table register} \\
  383. \hline
  384. %\end{tabular}
  385. \caption{Possible parameter types (tvarspez)}
  386. \label{tab6}
  387. \end{longtable}
  388. \subsection{Tree structure fields (tree.pas)}
  389. \label{subsec:mylabel2}
  390. Each element in a node is a pointer to a TTree structure, which is summarily
  391. explained and defined as follows:
  392. \begin{tabular*}{6.5in}{|l@{\extracolsep{\fill}}lp{8.0cm}|}
  393. \hline
  394. \textsf{TYPE}& & \\
  395. \xspace pTree = & \^{} TTree; & \\
  396. \xspace \textsf{TTree} = & \textbf{RECORD}& \\
  397. & \textsf{Error : Boolean;}& \\
  398. &\textsf{DisposeTyp : TDisposeTyp;}&
  399. \\
  400. &\textsf{Swaped : Boolean;}&
  401. Set to TRUE if the left and right nodes (fields) of this node have been swaped. \\
  402. & \textsf{VarStateSet : Boolean;}&
  403. \\
  404. & \textsf{Location : TLocation;}&
  405. Location information for this information (cf. Code generator) \\
  406. & \textsf{Registers32 : Longint;}&
  407. Number of general purpose registers required to evaluate this node \\
  408. & \textsf{RegistersFpu : Longint;}&
  409. Number of floating point registers required to evaluate this node \\
  410. & \textsf{Left : pTree;}&
  411. LEFT leaf of this node \\
  412. & \textsf{Right : pTree;}&
  413. RIGHT leaf of this node \\
  414. & \textsf{ResultType : pDef;}&
  415. Result type of this node \par (cf. Type definitions) \\
  416. & \textsf{FileInfo : TFilePosInfo;}&
  417. Line number information for this node creation in the original source code (for error management) \\
  418. & \textsf{LocalSwitches : TLocalSwitches;}&
  419. Local compiler switches used for code generation \par (Cf. \ref{fig1}) \\
  420. & \textsf{IsProperty : Boolean;}&
  421. TRUE if this is a property \\
  422. & \textsf{TreeType : TTreeTyp;}&
  423. Type of this tree (cf. \ref{tab1}) \\
  424. & \textsf{END;}& \\
  425. \hline
  426. \end{tabular*}
  427. %\caption{Possible definition types (tdeftype)}
  428. \begin{longtable}{|l|l|p{10cm}|}
  429. % p{126pt}|p{45pt}|p{319pt}|}
  430. \hline
  431. tlocalswitches & Switch & Description \\
  432. \hline
  433. \endhead
  434. \hline
  435. \endfoot
  436. \textsf{cs{\_}Check{\_}Overflow} & {\{}{\$}Q+{\}}&
  437. Code generator should emit overflow checking code \\
  438. \textsf{cs{\_}Check{\_}Range} & {\{}{\$}R+{\}}&
  439. Code generator should emit range checking code \\
  440. \textsf{cs{\_}Check{\_}IO} & {\{}{\$}I+{\}}&
  441. Code generator should emit I/O checking code \\
  442. \textsf{cs{\_}Check{\_}Object{\_}Ext} & N/A&
  443. Code generator should emit extended object access checks \\
  444. \textsf{\textit{cs{\_}OmitStackFrame}} & $N/A$ &
  445. \textit{Code generator should not emit frame{\_}pointer setup code
  446. in entry code} \\
  447. \textsf{cs{\_}Do{\_}Assertion} & {\{}{\$}C+{\}} &
  448. Code generator supports using the assert inline routine \\
  449. \textsf{cs{\_}Generate{\_}Rtti} & {\{}{\$}M+{\}} &
  450. Code generator should emit runtime type information \\
  451. \textsf{cs{\_}Typed{\_}Addresses} & {\{}{\$}T+{\}}&
  452. Parser emits typed pointer using the @ operator \\
  453. \textsf{cs{\_}Ansistrings} & {\{}{\$}H+{\}}&
  454. Parser creates an \textsf{ansistring} when an unspecified
  455. \textsf{String} type is declared instead of the default
  456. \textsf{ShortString} \\
  457. \textsf{cs{\_}Strict{\_}Var{\_}Strings} & {\{}{\$}V+{\}}&
  458. String types must be identical (same length) to be compatible \\
  459. \hline
  460. \caption{object definition flags (tobjectoptions)}
  461. \label{tab8}
  462. \end{longtable}
  463. \subsubsection{Additional fields}
  464. \label{subsubsec:additional}
  465. Depending on the tree type, some additional fields may be present in the
  466. tree node. This section describes these additional fields. Before accessing
  467. these additional fields, a check on the \textsf{treetype} should always be
  468. done to verify if not reading invalid memory ranges.
  469. \paragraph{AddN}\mbox{}
  470. \begin{longtable}{|l|p{10cm}|}
  471. \hline
  472. field & Description \\
  473. \hline
  474. \endhead
  475. \hline
  476. \endfoot
  477. \textsf{\textit{Use{\_}StrConcat : Boolean;}}&
  478. \textit{Currently unused (use for optimizations in future versions)} \\
  479. \hline
  480. \textsf{String{\_}Typ: TStringType;}&
  481. In the case where the + operator is applied on a string, this field indicates the string type. \\
  482. \hline
  483. \caption{Ordinal types (TBaseType)}
  484. \label{tab9}
  485. \end{longtable}
  486. \paragraph{CallParaN}\mbox{}
  487. \begin{longtable}{|l|p{10cm}|}
  488. \hline
  489. field & Description \\
  490. \hline
  491. \endhead
  492. \hline
  493. \endfoot
  494. \textsf{Is{\_}Colon{\_}Para : Boolean;}&
  495. Used for internal routines which can use optional format parameters
  496. (using colons). Is set to TRUE if this parameter was preceded by a
  497. colon (i.e : :1) \\
  498. \textsf{Exact{\_}Match{\_}Found : Boolean;}&
  499. Set to TRUE if the parameter type is exactly the same as the one
  500. expected by the routine. \\
  501. \textsf{ConvLevel1Found : Boolean;}&
  502. Set to TRUE if the parameter type requires a level 1 type conversion
  503. to conform to the parameter expected by the routine. \\
  504. \textsf{ConvLevel2Found : Boolean;}&
  505. Set to TRUE if the parameter type requires a level 2 type conversion
  506. to conform to the parameter expected by the routine. \\
  507. \textsf{HighTree : pTree;}& \\
  508. \hline
  509. \caption{Floating point types (TFloatType)}
  510. \label{tab10}
  511. \end{longtable}
  512. \paragraph{AssignN}\mbox{}
  513. \begin{longtable}{|l|p{10cm}|}
  514. \hline
  515. field & Description \\
  516. \hline
  517. \endhead
  518. \hline
  519. \endfoot
  520. \textsf{\textit{AssignTyp : TAssignTyp;}}&
  521. \textit{Currently unused (Used to be used for C-like assigns)} \\
  522. \textsf{\textit{Concat{\_}String : Boolean;}}&
  523. \textit{Currently unused (use for optimizations in future versions)}\\
  524. \hline
  525. \caption{Routine type information (TProcTypeOption)}
  526. \end{longtable}
  527. \paragraph{LoadN}\mbox{}
  528. \begin{longtable}{|l|p{10cm}|}
  529. \hline
  530. field & Description \\
  531. \hline
  532. \endhead
  533. \hline
  534. \endfoot
  535. \textsf{SymTableEntry : pSym;}&
  536. Symbol table entry for this symbol \\
  537. \textsf{SymTable : pSymTable;}&
  538. Symbol table in which this symbol is stored \\
  539. \textsf{Is{\_}Absolute : Boolean;}&
  540. set to TRUE if this variable is absolute \\
  541. \textsf{Is{\_}First : Boolean;}&
  542. set to TRUE if this is the first occurrence of the load for this
  543. variable (used with the varstate variable for optimizations) \\
  544. \hline
  545. \caption{Routine calling convention information (TProcCallOptions)}
  546. \label{tab12}
  547. \end{longtable}
  548. \paragraph{CallN}\mbox{}
  549. \begin{longtable}{|l|p{10cm}|}
  550. \hline
  551. field & Description \\
  552. \hline
  553. \endhead
  554. \hline
  555. \endfoot
  556. \textsf{SymTableProcEntry : pProcSym;}&
  557. Symbol table entry for this routine \\
  558. \textsf{SymTableProc : pSymTable;}&
  559. Symbol table associated with a call (object symbol table or routine
  560. symbol table) \\
  561. \textsf{ProcDefinition : pAbstractProcDef;}&
  562. Type definition for this routine \\
  563. \textsf{MethodPointer : pTree;}&
  564. ????????? \\
  565. \textsf{\textit{No{\_}Check : Boolean;}}&
  566. \textit{Currently unused} \\
  567. \textsf{Unit{\_}Specific : Boolean;}&
  568. set to TRUE if the routine is imported in a unit specific way (for
  569. example: system.writeln()) \\
  570. \textsf{Return{\_}Value{\_}Used : Boolean}&
  571. set to TRUE if the routine is a function and that the return value
  572. is not used (in extended syntax parsing - {\$}X+) \\
  573. \textsf{\textit{Static{\_}Call : Boolean;}}&
  574. \textit{unused} \\
  575. \hline
  576. \caption{Routine options (TProcOptions)}
  577. \label{tab13}
  578. \end{longtable}
  579. \paragraph{addrn}\mbox{}
  580. \begin{longtable}{|l|p{10cm}|}
  581. \hline
  582. field & Description \\
  583. \hline
  584. \endhead
  585. \hline
  586. \endfoot
  587. \textsf{ProcVarLoad : Boolean;}&
  588. Set to TRUE if this is a procedural variable call \\
  589. \hline
  590. \caption{String types (TStringType)}
  591. \end{longtable}
  592. \paragraph{OrdConstN}\mbox{}
  593. \begin{longtable}{|l|p{10cm}|}
  594. \hline
  595. Field & Description \\
  596. \hline
  597. \endhead
  598. \hline
  599. \endfoot
  600. \textsf{Value : Longint;}&
  601. The numeric value of this constant node \\
  602. \hline
  603. \caption{Possible set types (TSetType)}
  604. \end{longtable}
  605. \paragraph{RealConstN}\mbox{}
  606. \begin{longtable}{|l|p{10cm}|}
  607. \hline
  608. field & Description \\
  609. \hline
  610. \endhead
  611. \hline
  612. \endfoot
  613. \textsf{Value{\_}Real : Best{\_}Real;}&
  614. The numeric value of this constant node \\
  615. \textsf{Lab{\_}Real : pAsmLabel;}&
  616. The assembler label reference to this constant \\
  617. \hline
  618. \caption{Code generator operand sizes}\label{tab16}
  619. \end{longtable}
  620. \paragraph{FixConstN}\mbox{}
  621. \begin{longtable}{|l|p{10cm}|}
  622. \hline
  623. field & Description \\
  624. \hline
  625. \endhead
  626. \hline
  627. \endfoot
  628. \textsf{Value{\_}Fix : Longint;}&
  629. The numeric value of this constant node \\
  630. \hline
  631. \caption{Required target processor when compiling}
  632. \label{tab17}
  633. \end{longtable}
  634. \paragraph{FuncRetN}\mbox{}
  635. \begin{longtable}{|l|p{10cm}|}
  636. \hline
  637. field & Description \\
  638. \hline
  639. \endhead
  640. \hline
  641. \endfoot
  642. \textsf{FuncRetProcInfo : Pointer; (pProcInfo)}&
  643. Pointer to procedure information \\
  644. \textsf{RetType : TType;}& Indicates the return type of the function \\
  645. \textsf{Is{\_}First{\_}FuncRet : Boolean;}& \\
  646. \hline
  647. \caption{General defines for compiling system unit}
  648. \label{tab18}
  649. \end{longtable}
  650. \paragraph{SubscriptN}\mbox{}
  651. \begin{longtable}{|l|p{10cm}|}
  652. \hline
  653. field & Description \\
  654. \hline
  655. \endhead
  656. \hline
  657. \endfoot
  658. \textsf{vs : pVarSym;}&
  659. Symbol table entry for this variable (a field of
  660. object/class/record) \\
  661. \hline
  662. \caption{Debugging defines when compiling system unit}
  663. \end{longtable}
  664. \paragraph{RaiseN}\mbox{}
  665. \begin{longtable}{|l|p{10cm}|}
  666. \hline
  667. field & Description \\
  668. \hline
  669. \endhead
  670. \hline
  671. \endfoot
  672. \textsf{FrameTree : pTree;} & Exception frame tree (code in Raise statement)
  673. \end{longtable}
  674. \paragraph{VecN}\mbox{}
  675. \begin{longtable}{|l|p{10cm}|}
  676. \hline
  677. field & Description \\
  678. \hline
  679. \endhead
  680. \hline
  681. \endfoot
  682. \textsf{MemIndex : Boolean;} & Set to TRUE if Mem[Seg:Ofs] directive is parsed \\
  683. \textsf{MemSeg : Boolean;} & Set to TRUE if Mem[Seg:Ofs] directive is parsed \\
  684. \textsf{CallUnique: Boolean;} &
  685. \label{tab21}
  686. \end{longtable}
  687. \paragraph{StringConstN}\mbox{}
  688. \begin{longtable}{|l|p{10cm}|}
  689. \hline
  690. field & Description \\
  691. \hline
  692. \endhead
  693. \hline
  694. \endfoot
  695. \textsf{Value{\_}Str : pChar;} & The constant value of the string \\
  696. \textsf{Length : Longint;} & Length of the string in bytes (or in characters???) \\
  697. \textsf{Lab{\_}Str : pAsmLabel;} & The assembler label reference to this constant \\
  698. \textsf{StringType : TStringType;}& The string type (short, long, ansi, wide)
  699. \label{tab22}
  700. \end{longtable}
  701. \paragraph{TypeConvN}\mbox{}
  702. \begin{longtable}{|l|p{10cm}|}
  703. \hline
  704. field & Description \\
  705. \hline
  706. \endhead
  707. \hline
  708. \endfoot
  709. \textsf{ConvType: TConvertType;}& Indicates the conversion type to do \\
  710. \textsf{Explizit : Boolean;}&
  711. set to TRUE if this was an explicit conversion (with explicit
  712. typecast, or calling one of the internal conversion routines)
  713. \label{tab23}
  714. \end{longtable}
  715. \paragraph{TypeN}\mbox{}
  716. \begin{longtable}{|l|p{10cm}|}
  717. \hline
  718. field & Description \\
  719. \hline
  720. \endhead
  721. \hline
  722. \endfoot
  723. \textsf{TypeNodeType : pDef;}& \\
  724. \textsf{TypeNodeSym : pTypeSym;}&
  725. \label{tab24}
  726. \end{longtable}
  727. \paragraph{InlineN}\mbox{}
  728. \begin{longtable}{|l|p{10cm}|}
  729. \hline
  730. field & Description \\
  731. \hline
  732. \endhead
  733. \hline
  734. \endfoot
  735. \textsf{InlineNumber: Byte;} & Indicates the internal routine called (Cgf. code generator) \\
  736. \textsf{InlineConst : Boolean;} &
  737. One or more of the parameters to this inline routine call contains
  738. constant values
  739. \label{tab25}
  740. \end{longtable}
  741. \paragraph{ProcInlineN}\mbox{}
  742. Inline nodes are created when a routine is declared as being inline. The
  743. routine is actually inlined when the following conditions are satisfied:
  744. It is called within the same module
  745. The appropriate compiler switch to support inline is activated
  746. It is a non-method routine (a standard procedure or function)
  747. Otherwise a normal call is made, ignoring the inline directive. In the case
  748. where a routine is inlined, all parameters , return values and local
  749. variables of the inlined routine are actually allocated in the stack space
  750. of the routine which called the inline routine.
  751. \begin{longtable}{|l|p{10cm}|}
  752. \hline
  753. field & Description \\
  754. \hline
  755. \endhead
  756. \hline
  757. \endfoot
  758. \textsf{InlineTree : pTree;}&
  759. The complete tree for this inline procedure \\
  760. \textsf{InlineProcsym : pProcSym;}&
  761. Symbol table entry for this procedure \\
  762. \textsf{RetOffset : Longint;}&
  763. Return offset in parent routine stack space \\
  764. \textsf{Para{\_}Offset : Longint;}&
  765. Parameter start offset in parent routine stack space \\
  766. \textsf{Para{\_}Size : Longint;}&
  767. Parameter size in the parent routine stack space
  768. \label{tab26}
  769. \end{longtable}
  770. \paragraph{SetConstN}\mbox{}
  771. \begin{longtable}{|l|p{10cm}|}
  772. \hline
  773. field & Description \\
  774. \hline
  775. \endhead
  776. \hline
  777. \endfoot
  778. \textsf{Value{\_}Set : pConstSet;}& The numeric value of this constant node \\
  779. \textsf{Lab{\_}Set : pAsmLabel;} & The assembler label reference to this constant
  780. \label{tab27}
  781. \end{longtable}
  782. \paragraph{LoopN}\mbox{}
  783. \begin{longtable}{|l|p{10cm}|}
  784. \hline
  785. field & Description \\
  786. \hline
  787. \endhead
  788. \hline
  789. \endfoot
  790. & \\
  791. & \\
  792. &
  793. \end{longtable}
  794. \paragraph{AsmN}\mbox{}
  795. \begin{longtable}{|l|p{10cm}|}
  796. \hline
  797. field & Description \\
  798. \hline
  799. \endhead
  800. \hline
  801. \endfoot
  802. \textsf{p{\_}Asm : pAasmOutput;}&
  803. The instruction tree created by the assembler parser \\
  804. \textsf{Object{\_}Preserved : Boolean;}&
  805. set to FALSE if the Self{\_}Register was modified in the asm statement.
  806. \label{tab29}
  807. \end{longtable}
  808. \paragraph{CaseN}\mbox{}
  809. \begin{longtable}{|l|p{10cm}|}
  810. \hline
  811. field & Description \\
  812. \hline
  813. \endhead
  814. \hline
  815. \endfoot
  816. \textsf{Nodes : pCaserecord;}&
  817. Tree for each of the possible case in the case statement \\
  818. \textsf{ElseBlock : pTree;}&
  819. Else statement block tree
  820. \label{tab30}
  821. \end{longtable}
  822. \paragraph{LabelN, GotoN}\mbox{}
  823. \begin{longtable}{|l|p{10cm}|}
  824. \hline
  825. field & Description \\
  826. \hline
  827. \endhead
  828. \hline
  829. \endfoot
  830. \textsf{LabelNr : pAsmLabel;} & Assembler label associated with this statement \\
  831. \textsf{ExceptionBlock : ptree;}& ???????? \\
  832. \textsf{LabSym : pLabelSym;} & Symbol table entry for this label
  833. \label{tab31}
  834. \end{longtable}
  835. \paragraph{WithN}\mbox{}
  836. \begin{longtable}{|l|p{10cm}|}
  837. \hline
  838. field & Description \\
  839. \hline
  840. \endhead
  841. \hline
  842. \endfoot
  843. \textsf{WithSymTables : pWithSymTable;} & \\
  844. \textsf{TableCount : Longint;} & \\
  845. \textsf{WithReference : pReference;} & \\
  846. \textsf{IsLocal : Boolean;} &
  847. \label{tab32}
  848. \end{longtable}
  849. \paragraph{OnN}\mbox{}
  850. \begin{longtable}{|l|p{10cm}|}
  851. \hline
  852. field & Description \\
  853. \hline
  854. \endhead
  855. \hline
  856. \endfoot
  857. \textsf{ExceptSymTable : pSymtable;}& \\
  858. \textsf{ExceptType : pObjectdef;}&
  859. \label{tab33}
  860. \end{longtable}
  861. \paragraph{ArrayConstructorN}\mbox{}
  862. \begin{longtable}{|l|p{10cm}|}
  863. \hline
  864. field & Description \\
  865. \hline
  866. \endhead
  867. \hline
  868. \endfoot
  869. \textsf{CArgs : Boolean;} & \\
  870. \textsf{CArgSwap : Boolean;} & \\
  871. \textsf{ForceVaria : Boolean;} & \\
  872. \textsf{NoVariaAllowed : Boolean;} & \\
  873. \textsf{ConstructorDef : pDef;} &
  874. \label{tab34}
  875. \end{longtable}
  876. \section{Symbol tables}
  877. \label{sec:symbol}
  878. \subsection{Architecture}
  879. \label{subsec:architecturesructord}
  880. The symbol table contains all definitions for all symbols in the compiler.
  881. It also contains all type
  882. \noindent
  883. information for all symbols encountered during the parsing process. All
  884. symbols and definitions are streamable, and are used within PPU files to
  885. avoid recompiling everything to verify if all symbols are valid.
  886. There are different types of symbol tables, all of which maybe active at one
  887. time or another depending on the context of the parser.
  888. An architectural overview of the interaction between the symbol tables, the
  889. symbol entries and the definition entries is displayed in figure \ref{fig4}
  890. \begin{figure}
  891. \ifpdf
  892. \includegraphics{arch4.pdf}
  893. %\epsfig{file=arch4.png,width=\textwidth}
  894. \else
  895. \includegraphics[width=6.29in,height=3.29in]{arch4.eps}
  896. \fi
  897. \label{fig4}
  898. \caption{Interactions between symbol tables}
  899. \end{figure}
  900. As can be seen, the symbol table entries in the symbol table are done using
  901. the fast hashing algorithm with a hash dictionary.
  902. \subsection{The Symbol table object}
  903. \label{subsec:mylabel3}
  904. All symbol tables in the compiler are from this type of object, which
  905. contains fields for the total size of the data in the symbol table, and
  906. methods to read and write the symbol table into a stream. The start of the
  907. linked list of active symbol tables is the \textbf{symtablestack} variable.
  908. \begin{tabular*}{6.5in}{|l@{\extracolsep{\fill}}lp{6,5cm}|}
  909. \hline
  910. \textsf{TYPE} & & \\
  911. \xspace \textsf{pSymTable} &= \^{} \textbf{TSymTable};& \\
  912. \xspace \textsf{TSymTable} &= \textbf{object} & \\
  913. & \textsf{Name : pString;}& \\
  914. & \textsf{DataSize : Longint;}&
  915. The total size of all the data in this symbol table (after the data has been aligned). Only valid for certain types of symbol tables. \\
  916. & \textsf{DataAlignment : Longint;}& \\
  917. & \textsf{SymIndex : pIndexArray;}& \\
  918. & \textsf{DefIndex : pIndexArray;}& \\
  919. & \textsf{SymSearch : pDictionary;}& \\
  920. & \textsf{Next : pSymtable;}&
  921. Points to the next symbol table in the linked list of active symbol tables. \\
  922. & \textsf{DefOwner : pDef;}&
  923. The owner definition (only valid in the cases of objects and records, this points to the definition of that object or record). \\
  924. & \textsf{Address{\_}Fixup : Longint}& \\
  925. & \textsf{UnitId : Word;}& \\
  926. & \textsf{SymTableLevel : Byte;}& \\
  927. & \textsf{SymTableType :TSymTableType;}&
  928. Indicates the type of this symbol table (\ref{fig2}). \\
  929. &\textsf{end;}& \\
  930. \hline
  931. \end{tabular*}
  932. The type of possible symbol tables are shown in the following diagram:
  933. \begin{longtable}{|l|p{10cm}|}
  934. \hline
  935. field & Description \\
  936. \hline
  937. \endhead
  938. \hline
  939. \endfoot
  940. TSymTableType& Description \\
  941. \textsf{InvalidSymTable}&
  942. Default value when the symbol table is created and its type is not defined. Used for debugging purposes \\
  943. \textsf{WithSymTable}&
  944. All symbols accessed in a with statement \\
  945. \textsf{StaticSymTable}& \\
  946. \textsf{GlobalSymTable}& \\
  947. \textsf{UnitSymTable}&
  948. Linked list of units symbol used (all or unit?). The linked list is
  949. composed of \textsf{tunitsym} structures. \\
  950. \textsf{ObjectSymTable}& \\
  951. \textsf{RecordSymTable}&
  952. Contains all symbols within a record statement \\
  953. \textsf{MacroSymTable}&
  954. Holds all macros currently in scope. \\
  955. \textsf{LocalSymTable}&
  956. Hold symbols for all local variables of a routine \\
  957. \textsf{ParaSymTable}&
  958. Holds symbols for all parameters of a routine (the actual parameter declaration symbols) \\
  959. \textsf{InlineParaSymTable}&
  960. Holds all parameter symbols for the current inline routine \\
  961. \textsf{InlineLocalSymTable}&
  962. Holds all local symbols for the current inline routine \\
  963. \textsf{Stt{\_}ExceptSymTable}& \\
  964. \textsf{StaticPPUSymTable}&
  965. \label{tab36}
  966. \end{longtable}
  967. \subsection{Inserting symbols into a symbol table}
  968. \label{subsec:inserting}
  969. To add a symbol into a specific symbol table, that's symbol table's
  970. \textsf{Insert} method is called, which in turns call the
  971. \textsf{Insert{\_}In{\_}Data} method of that symbol.
  972. \textsf{Insert{\_}In{\_}Data}, depending on the symbol type, adjusts the
  973. alignment and sizes of the data and actually creates the data entry in the
  974. correct segment.
  975. \begin{figure}
  976. \ifpdf
  977. %\epsfig{file=arch5.png,width=\textwidth}
  978. \includegraphics{arch5.pdf}
  979. \else
  980. \includegraphics[width=1.51in,height=5.51in]{arch5.eps}
  981. \fi
  982. \label{fig5}
  983. \caption{Inserting into the tree}
  984. \end{figure}
  985. \subsection{Symbol table interface}
  986. \subsubsection{Routines}
  987. \label{subsubsec:routinesable}
  988. \begin{functionl}{Search{\_}a{\_}Symtable}{searchasymtable}
  989. \Declaration
  990. Function Search{\_}a{\_}Symtable(Const Symbol:String; \\
  991. SymTableType : TSymTableType):pSym;
  992. \Description
  993. Search for a symbol \textsf{Symbol} in a specified symbol table
  994. \textsf{SymTableType}. Returns \textsf{NIL} if the symbol table is not
  995. found, and also if the symbol cannot be found in the desired symbol table.
  996. \end{functionl}
  997. \begin{procedure}{GetSym}
  998. \Declaration
  999. Procedure GetSym(Const S : StringId; NotFoundError: Boolean);
  1000. \Description
  1001. Search all the active symbol tables for the symbol \textsf{s},setting the
  1002. global variable \textsf{SrSym} to the found symbol, or to \textsf{nil} if
  1003. the symbol was not found. \textsf{notfounderror} should be set to TRUE if
  1004. the routine must give out an error when the symbol is not found.
  1005. \end{procedure}
  1006. \begin{function}{GlobalDef}
  1007. \Declaration
  1008. Function GlobalDef(Const S : String) : pDef;
  1009. \Description
  1010. Returns a pointer to the definition of the fully qualified type symbol
  1011. \textsf{S}, or \textsf{NIL} if not found.
  1012. \Notes
  1013. It is fully qualified, in that the symbol \textsf{system.byte}, for example,
  1014. will be fully resolved to a unit and byte type component The symbol must
  1015. have a global scope, and it must be a type symbol, otherwise \textsf{NIL}
  1016. will be returned..
  1017. \end{function}
  1018. \subsubsection{Variables}
  1019. \label{subsubsec:variablesly}
  1020. \begin{variable}{SrSym}
  1021. \Declaration
  1022. Var SrSym : pSym;
  1023. \Description
  1024. This points to the symbol entry found, when calling \textsf{getsym}.
  1025. \end{variable}
  1026. \begin{variable}{SrSymTable}
  1027. \Declaration
  1028. Var SrSymTable : pSymTable;
  1029. \Description
  1030. This points to the symbol table of the symbol \seevar{SrSym} when calling
  1031. \seep{GetSym}.
  1032. \end{variable}
  1033. \section{Symbol entries}
  1034. \label{sec:mylabel3}
  1035. \subsection{Architecture}
  1036. \label{subsec:architecturees}
  1037. There are different possible types of symbols, each one having different
  1038. fields then the others. Each symbol type has a specific signature to
  1039. indicate what kind of entry it is. Each entry in the symbol table is
  1040. actually one of the symbol entries described in the following sections. The
  1041. relationship between a symbol entry, a type definition, and the type name
  1042. symbol entry is shown in figure \ref{fig6}.
  1043. \begin{figure}
  1044. \ifpdf
  1045. \includegraphics{arch6.pdf}
  1046. %\epsfig{file=arch6.png,width=\textwidth}
  1047. \else
  1048. \includegraphics[width=5.51in,height=4.51in]{arch6.eps}
  1049. \fi
  1050. \label{fig6}
  1051. \caption{relation between symbol entry and type definition and name}
  1052. \end{figure}
  1053. \subsection{Symbol entry types}
  1054. \label{subsec:symbol}
  1055. \subsubsection{Base symbol type (TSym)}
  1056. \label{subsubsec:mylabel1}
  1057. All entries in the symbol table are derived from this base object which
  1058. contains information on the symbol type as well as information on the owner
  1059. of this symbol entry.
  1060. \begin{tabular*}{6.5in}{|l@{\extracolsep{\fill}}lp{9cm}|}
  1061. \hline
  1062. \textsf{TYPE} & & \\
  1063. \xspace pSym = & \^{} TSym; & \\
  1064. \xspace \textsf{TSym} = & \textbf{Object}(TSymTableEntry) & \\
  1065. & \textsf{SymOptions : TSymOptions;}& Indicate the access scope of the symbol \\
  1066. & \textsf{FileInfo : tFilePosInfo;}& \\
  1067. & \textsf{Refs : Longint;}&
  1068. Indicates how many times this label is refered in the parsed code (is only used with variable and assembler label symbols). \\
  1069. &\textsf{LastRef : pRef;}& \\
  1070. &\textsf{DefRef : pRef;}& \\
  1071. &\textsf{LastWritten : pRef;}& \\
  1072. &\textsf{RefCount : Longint;}& \\
  1073. &\textsf{Typ : tSymTyp;}& Indicates the symbol type (Cf. \ref{tab2}(. \\
  1074. &\textsf{IsStabWritten : Boolean;}& \\
  1075. &\textsf{end; }&\\
  1076. \hline
  1077. \end{tabular*}
  1078. \begin{longtable}{|l|p{10cm}|}
  1079. \hline
  1080. TSymTyp & Description \\
  1081. \hline
  1082. \endhead
  1083. \hline
  1084. \endfoot
  1085. \textsf{AbstractSym}&
  1086. This is a special abstract symbol (this should never occur) \\
  1087. \textsf{VarSym}&
  1088. This symbol is a variable declaration in the \textsf{var} section, or a \textsf{var} parameter. \\
  1089. \textsf{TypeSym}&
  1090. This symbol is a type name \\
  1091. \textsf{ProcSym}&
  1092. This symbol is a routine or method name \\
  1093. \textsf{UnitSym}&
  1094. This symbol is a unit name \\
  1095. \textsf{\textit{ProgramSym}}&
  1096. \textit{This symbol is the main program name} \\
  1097. \textsf{ConstSym}&
  1098. This symbol is a constant \\
  1099. \textsf{EnumSym}&
  1100. This symbol is an enumeration symbol (an element in an enumeration) \\
  1101. \textsf{TypedConstSym}&
  1102. This symbol is pre-initialized variable (pascal typed constant) \\
  1103. \textsf{ErrorSym}&
  1104. This symbol is created for error generation \\
  1105. \textsf{SysSym}&
  1106. This symbol represents an inlined system unit routine \\
  1107. \textsf{LabelSym}&
  1108. This symbol represents a label in a \textsf{label} pascal declaration \\
  1109. \textsf{AbsoluteSym}&
  1110. This symbol represents an the symbol following an \textsf{absolute} variable declaration \\
  1111. \textsf{PropertySym}&
  1112. This symbol is a property name \\
  1113. \textsf{FuncRetSym}&
  1114. This symbol is the name of the return value for functions \\
  1115. \textsf{MacroSym}&
  1116. This symbol is a macro symbol (just like {\#}define in C)
  1117. \end{longtable}
  1118. \subsubsection{label symbol (TLabelSym)}
  1119. \label{subsubsec:label}
  1120. The label symbol table entry is only created when a pascal label is declared
  1121. via the label keyword. The object has the following fields which are
  1122. available for use publicly:
  1123. \begin{tabular*}{6.5in}{|l@{\extracolsep{\fill}}lp{9cm}|}
  1124. \hline
  1125. \textsf{TYPE} & & \\
  1126. \xspace pLabelSym = & \^{} TLabelSym; & \\
  1127. \xspace \textsf{TLabelSym} = & \textbf{Object}(TSym) & \\
  1128. & \textsf{Used : Boolean}&
  1129. Set to TRUE if this pascal label is used using a \textsf{goto} or in an assembler statement \\
  1130. & \textsf{Defined: Boolean}&
  1131. Set to TRUE if this label has been declared \\
  1132. & \textsf{Lab : pAsmLabel}&
  1133. Points to the actual assembler label structure which will be emitted by the code generator \\
  1134. & \textsf{Code : Pointer}& \\
  1135. & \textsf{end;}& \\
  1136. \hline
  1137. \end{tabular*}
  1138. \subsubsection{unit symbol (TUnitSym)}
  1139. \label{subsubsec:mylabel2}
  1140. The unit symbol is created and added to the symbol table each time that the
  1141. uses clause is parsed and a unit name is found, it is also used when
  1142. compiling a unit, with the first entry in that symbol table being the unit
  1143. name being compiled. The unit symbol entry is actual part of a linked list
  1144. which is used in the unit symbol table.
  1145. \begin{tabular*}{6.5in}{|l@{\extracolsep{\fill}}lp{7cm}|}
  1146. \hline
  1147. \textsf{TYPE} & & \\
  1148. \xspace pUnitSym = & \^{} TUnitSym; & \\
  1149. \xspace \textsf{TUnitSym} = & \textbf{Object}(TSym) & \\
  1150. & \textsf{UnitSymTable:pUnitSymTable}&
  1151. Pointer to the global symbol table for that unit, containing entries for each public? symbol in that unit \\
  1152. & \textsf{PrevSym : pUnitSym}&
  1153. Pointer to previous entry in the linked list \\
  1154. & \textsf{end;}& \\
  1155. \hline
  1156. \end{tabular*}
  1157. \subsubsection{macro symbol (TMacroSym)}
  1158. \label{subsubsec:macro}
  1159. The macro synbols are used in the preprocessor for conditional compilation
  1160. statements. There is one such entry created for each {\$}define directive,
  1161. it contains the value of the define (stored as a string).
  1162. \begin{tabular*}{6.5in}{|l@{\extracolsep{\fill}}lp{6cm}|}
  1163. \hline
  1164. \textsf{TYPE}& & \\
  1165. \xspace pMacroSym = & \^{} TMacroSym; & \\
  1166. \xspace \textsf{TMacroSym} = & \textbf{Object}(TSym) & \\
  1167. & \textsf{Defined : Boolean;}&
  1168. TRUE if the symbol has been defined with a \textsf{{\$}define}
  1169. directive, or false if it has been undefined with a
  1170. \textsf{{\$}undef} directive \\
  1171. & \textsf{Defined{\_}At{\_}Startup : Boolean;}&
  1172. TRUE if the symbol is a system wide define \\
  1173. & \textsf{Is{\_}Used: Boolean;}&
  1174. TRUE if the define has been used such as in a \textsf{{\$}ifdef}
  1175. directive. \\
  1176. & \textsf{BufText : pChar;}&
  1177. The actual string text of the define \\
  1178. & \textsf{BufLength : Longint;}&
  1179. The actual string length of the define \\
  1180. & \textsf{end;}& \\
  1181. \hline
  1182. \end{tabular*}
  1183. \subsubsection{error symbol (TErrorSym)}
  1184. \label{subsubsec:error}
  1185. This symbol is actually an empty symbol table entry. When the parser
  1186. encounters an error when parsing a symbol, instead of putting nothing in the
  1187. symbol table, it puts this symbol entry. This avoids illegal memory accesses
  1188. later in parsing.
  1189. \subsubsection{procedure symbol (TProcSym)}
  1190. \label{subsubsec:procedure}
  1191. The procedure symbol is created each time a routine is defined in the code.
  1192. This can be either a forward definition or the actual implementation of the
  1193. routine. After creation, the symbol is added into the appropriate symbol
  1194. table stack.
  1195. \begin{tabular*}{6.5in}{|l@{\extracolsep{\fill}}lp{8cm}|}
  1196. \hline
  1197. \textsf{TYPE}& & \\
  1198. \xspace pProcSym = & \^{} TProcSym; & \\
  1199. \xspace \textsf{TProcSym} = & \textbf{Object}(TSym) & \\
  1200. & \textsf{Is{\_}Global : Boolean}&
  1201. Set if the routine is exported by the unit \\
  1202. & \textsf{Definition : pProcDef}&
  1203. Procedure definition, including parameter information and return
  1204. values \\
  1205. & \textsf{end;}& \\
  1206. \hline
  1207. \end{tabular*}
  1208. \subsubsection{type symbol (TTypeSym)}
  1209. \label{subsubsec:mylabel3}
  1210. The type symbol is created each time a new type declaration is done, the
  1211. current symbol table stack is then inserted with this symbol. Furthermore,
  1212. each time the compiler compiles a module, the default base types are
  1213. initialized and added into the symbol table (\textbf{psystem.pas}) The type
  1214. symbol contains the name of a type, as well as a pointer to its type
  1215. definition.
  1216. \begin{tabular*}{6.5in}{|l@{\extracolsep{\fill}}lp{9cm}|}
  1217. \hline
  1218. \textsf{TYPE}& & \\
  1219. \xspace pTypeSym = & \^{} TTypeSym; & \\
  1220. \xspace \textsf{TTypeSym} = & \textbf{Object}(TSym) & \\
  1221. & \textsf{ResType : TType}&
  1222. Contains base type information as well as the type definition \\
  1223. & \textsf{end;}& \\
  1224. \hline
  1225. \end{tabular*}
  1226. \subsubsection{variable symbol (TVarSym)}
  1227. \label{subsubsec:variable}
  1228. Variable declarations, as well as parameters which are passed onto routines
  1229. are declared as variable symbol types. Access information, as well as type
  1230. information and optimization information are stored in this symbol type.
  1231. \begin{tabular*}{6.5in}{|l@{\extracolsep{\fill}}lp{8.5cm}|}
  1232. \hline
  1233. \textsf{TYPE}& & \\
  1234. \xspace pVarSym = & \^{} TVarSym; & \\
  1235. \xspace \textsf{TVarSym} = & \textbf{Object}(TSym) & \\
  1236. & \textsf{Reg: TRegister;}&
  1237. If the value is a register variable, the \textsf{reg} field will be
  1238. different then R{\_}NO \\
  1239. & \textsf{VarSpez : TVarSpez;}&
  1240. Indicates the variable type (parameters only) (Cf. \ref{tab4}). \\
  1241. & \textsf{Address : Longint;}&
  1242. In the case where the variable is a routine parameter, this
  1243. indicates the positive offset from the \textsf{frame{\_}pointer }to
  1244. access this variable. In the case of a local variable, this field
  1245. indicates the negative offset from the \textsf{frame{\_}pointer}. to
  1246. access this variable. \\
  1247. & \textsf{LocalVarSym : pVarSym;}& \\
  1248. & \textsf{VarType : TType;}&
  1249. Contains base type information as well as the type definition \\
  1250. & \textsf{VarOptions : TVarOptions;}&
  1251. Flags for this variable (Cf. \ref{tab3}) \\
  1252. & \textsf{VarState : TVarState}&
  1253. Indicates the state of the variable, if it's used or declared \\
  1254. & \textsf{end;}& \\
  1255. \hline
  1256. \end{tabular*}
  1257. \begin{longtable}{|l|p{10cm}|}
  1258. \hline
  1259. TVarOptions & Description \\
  1260. \hline
  1261. \endhead
  1262. \hline
  1263. \endfoot
  1264. \textsf{vo{\_}Regable}&
  1265. The variable can be put into a hardware general purpose register \\
  1266. \textsf{vo{\_}Is{\_}C{\_}Var}&
  1267. The variable is imported from a C module \\
  1268. \textsf{vo{\_}Is{\_}External}&
  1269. The variable is declared external \\
  1270. \textsf{vo{\_}Is{\_}Dll{\_}Var}&
  1271. The variable is a shared library variable \\
  1272. \textsf{vo{\_}Is{\_}Thread{\_}Var}&
  1273. The variable is declared as being thread safe \\
  1274. \textsf{vo{\_}FpuRegable}&
  1275. The variable can be put into a hardware floating point register \\
  1276. \textsf{vo{\_}Is{\_}Local{\_}Copy}& \\
  1277. \textsf{\textit{vo{\_}Is{\_}Const}}&
  1278. \textit{unused and useless} \\
  1279. \textsf{vo{\_}Is{\_}Exported}&
  1280. The variable is declared as exported in a dynamic link library
  1281. \end{longtable}
  1282. \begin{longtable}{|l|p{10cm}|}
  1283. \hline
  1284. TVarSpez & Description \\
  1285. \hline
  1286. \endhead
  1287. \hline
  1288. \endfoot
  1289. \textsf{vs{\_}Value}&
  1290. This is a value parameter \\
  1291. \textsf{vs{\_}Const}&
  1292. This is a constant parameter, property or array \\
  1293. \textsf{vs{\_}Var}&
  1294. This is a variable parameter
  1295. \end{longtable}
  1296. \subsubsection{property symbol}
  1297. \label{subsubsec:property}
  1298. \subsubsection{return value of function symbol}
  1299. \label{subsubsec:return}
  1300. \subsubsection{absolute declared variable}
  1301. \label{subsubsec:absolute}
  1302. \subsubsection{typed constant symbol}
  1303. \label{subsubsec:typed}
  1304. \subsubsection{constant symbol}
  1305. \label{subsubsec:constant}
  1306. \subsubsection{enumeration symbol}
  1307. \label{subsubsec:enumeration}
  1308. \subsubsection{program symbol}
  1309. \label{subsubsec:program}
  1310. \subsubsection{sys symbol}
  1311. \label{subsubsec:mylabel4}
  1312. \subsection{Symbol interface}
  1313. \label{subsec:mylabel5}
  1314. \section{Type information}
  1315. \label{sec:mylabel4}
  1316. \subsection{Architecture}
  1317. \label{subsec:architecturetionolbo}
  1318. A type declaration , which is the basis for the symbol table, since
  1319. inherently everything comes down to a type after parsing is a special
  1320. structure with two principal fields, which point to a symbol table entry
  1321. which is the type name, and the actual definition which gives the
  1322. information on other symbols in the type, the size of the type and other
  1323. such information.
  1324. \begin{tabular*}{6.5in}{|l@{\extracolsep{\fill}}lp{9cm}|}
  1325. \hline
  1326. \textsf{TYPE} & & \\
  1327. \xspace \textsf{TType} = & \textbf{Object} & \\
  1328. &\textsf{Sym : pSym;}&
  1329. Points to the symbol table of this type \\
  1330. & \textsf{Def : pDef;}&
  1331. Points to the actual definition of this type \\
  1332. &\textsf{end;}& \\
  1333. \hline
  1334. \end{tabular*}
  1335. \begin{figure}
  1336. \ifpdf
  1337. \includegraphics{arch7.pdf}
  1338. %\epsfig{file=arch7.png,width=\textwidth}
  1339. \else
  1340. \includegraphics[width=4.39in,height=3.56in]{arch7.eps}
  1341. \fi
  1342. \caption{Type symbol and definition relations}
  1343. \label{fig7}
  1344. \end{figure}
  1345. \subsection{Definition types}
  1346. Definitions represent the type information for all possible symbols which
  1347. can be encountered by the parser. The definition types are associated with
  1348. symbols in the symbol table, and are used by the parsing process (among
  1349. other things) to perform type checking.
  1350. The current possible definition types are enumerated in \textsf{TDefType}
  1351. and can have one of the following symbolic values:
  1352. \begin{longtable}{|l|p{10cm}|}
  1353. \hline
  1354. deftype of TDef object & Description \\
  1355. \hline
  1356. \endhead
  1357. \hline
  1358. \endfoot
  1359. \textsf{AbstractDef} & \\
  1360. \textsf{ArrayDef} & array type definition \\
  1361. \textsf{RecordDef} & record type definition \\
  1362. \textsf{PointerDef} & pointer type definition \\
  1363. \textsf{OrdDef} & ordinal (numeric value) type definition \\
  1364. \textsf{StringDef} & string type definition \\
  1365. \textsf{EnumDef} & enumeration type definition \\
  1366. \textsf{ProcDef} & procedure type definition \\
  1367. \textsf{ObjectDef} & object or class type definition \\
  1368. \textsf{ErrorDef} & error definition (empty, used for error recovery) \\
  1369. \textsf{FileDef} & file type definition \\
  1370. \textsf{FormalDef} & \\
  1371. \textsf{SetDef} & set type definition \\
  1372. \textsf{ProcVarDef} & procedure variable type definition \\
  1373. \textsf{FloatDef} & floating point type definition \\
  1374. \textsf{ClassrefDef} & \\
  1375. \textsf{ForwardDef} & \\
  1376. \end{longtable}
  1377. \subsubsection{base definition (TDef)}
  1378. \label{subsubsec:mylabel5}
  1379. All type definitions are based on this object. Therefore all derived object
  1380. all posess the fields in this object in addition to their own private
  1381. fields.
  1382. \begin{tabular*}{6.5in}{|l@{\extracolsep{\fill}}lp{7cm}|}
  1383. \hline
  1384. \textsf{TYPE} & & \\
  1385. \xspace pDef = & \^{} TDef; & \\
  1386. \xspace \textsf{TDef} = & \textbf{Object}(TSymTableEntry) & \\
  1387. &\textsf{TypeSym : pTypeSym;}&
  1388. Pointer to symbol table entry for this type definition \\
  1389. &\textsf{InitTable{\_}Label : pAsmLabel;}&
  1390. Label to initialization information (required for some complex types) \\
  1391. &\textsf{Rtti{\_}Label : pAsmLabel;}&
  1392. Label to the runtime type information. \\
  1393. &\textsf{NextGlobal : pDef;}& \\
  1394. &\textsf{PreviousGlobal : pDef;}& \\
  1395. &\textsf{SaveSize : Longint;}&
  1396. Size in bytes of the data definition \\
  1397. &\textsf{DefType : tDefType;}&
  1398. Indicates the definition type (see \ref{tab5}). \\
  1399. &\textsf{Has{\_}InitTable : Boolean;}& \\
  1400. &\textsf{Has{\_}Rtti : Boolean;}& \\
  1401. &\textsf{Is{\_}Def{\_}Stab{\_}Written : TDefStabStatus}&
  1402. Can be one of the following states : (\textsf{Not{\_}Written,
  1403. written, Being{\_}Written}) which indicates if the debug information
  1404. for this type has been defined or not. \\
  1405. &\textsf{GlobalNb : Longint;}&
  1406. Internal debug information type signature (each definition has a
  1407. numeric signature). \\
  1408. &\textsf{end;}& \\
  1409. \hline
  1410. \end{tabular*}
  1411. \subsubsection{file definition (TFileDef)}
  1412. \label{subsubsec:mylabel6}
  1413. The file definition can occur in only some rare instances, when a
  1414. \textsf{file of }\textsf{\textit{type}} is parsed, a file definition of that
  1415. type will be created. Furthermore, internally, a definition for a
  1416. \textbf{Text} file type and \textbf{untyped} File type are created when the
  1417. system unit is loaded. These types are always defined when compiling any
  1418. unit or program.
  1419. \begin{tabular*}{6.5in}{|l@{\extracolsep{\fill}}lp{8.5cm}|}
  1420. \hline
  1421. \textsf{TYPE}& & \\
  1422. \xspace pFileDef = & \^{} TFileDef; & \\
  1423. \xspace \textsf{TFileDef} = & \textbf{Object}(TDef) & \\
  1424. &\textsf{FileTyp : TFileTyp;}&
  1425. Indicates what type of file definition it is (\textsf{text},
  1426. \textsf{untyped} or \textsf{typed}). \\
  1427. &\textsf{TypedFileType : TType;}&
  1428. In the case of a typed file definition, definition of the type of
  1429. the file \\
  1430. &\textsf{end;}& \\
  1431. \hline
  1432. \end{tabular*}
  1433. \subsubsection{formal definition (TFormalDef)}
  1434. \label{subsubsec:formal}
  1435. \subsubsection{forward definition (TForwardDef)}
  1436. \label{subsubsec:forward}
  1437. The forward definition is created, when a type is declared before an actual
  1438. definition exists. This is the case, when, for example \textsf{type
  1439. pmyobject = \ tmyobject}, while \textsf{tmyobject} has yet to be defined.
  1440. \begin{tabular*}{6.5in}{|l@{\extracolsep{\fill}}lp{6.5cm}|}
  1441. \hline
  1442. \textsf{TYPE} & & \\
  1443. \xspace pForwardDef = & \^{} TForwardDef; & \\
  1444. \xspace \textsf{TForwardDef} = & \textbf{Object}(TDef) & \\
  1445. &\textsf{toSymName : String;}&
  1446. The symbol name for this forward declaration (the actual real
  1447. definition does not exist yet) \\
  1448. &\textsf{ForwardPos : TFilePosInfo;}&
  1449. Indicates file position where this forward definition was declared. \\
  1450. &\textsf{end;}& \\
  1451. \hline
  1452. \end{tabular*}
  1453. \subsubsection{error definition (TErrorDef)}
  1454. \label{subsubsec:mylabel7}
  1455. This definition is actually an empty definition entry. When the parser
  1456. encounters an error when parsing a definition instead of putting nothing in
  1457. the type for a symbol, it puts this entry. This avoids illegal memory
  1458. accesses later in parsing.
  1459. \subsubsection{pointer definition (TPointerDef)}
  1460. \label{subsubsec:pointer}
  1461. The pointer definition is used for distinguishing between different types of
  1462. pointers in the compiler, and are created at each \textsf{\ typename}
  1463. parsing construct found.
  1464. \begin{tabular*}{6.5in}{|l@{\extracolsep{\fill}}lp{9cm}|}
  1465. \hline
  1466. \textsf{TYPE} & & \\
  1467. \xspace pPointerDef = & \^{} TPointerDef; & \\
  1468. \xspace \textsf{TPointerDef} = & \textbf{Object}(TDef) & \\
  1469. &\textsf{Is{\_}Far : Boolean;}&
  1470. Used to indicate if this is a far pointer or not (this flag is
  1471. cpu-specific) \\
  1472. &\textsf{PointerType : TType;}&
  1473. This indicates to what type definition this pointer points to. \\
  1474. &\textsf{end;}& \\
  1475. \hline
  1476. \end{tabular*}
  1477. \subsubsection{object definition (TObjectDef)}
  1478. \label{subsubsec:object}
  1479. The object definition is created each time an object declaration is found in
  1480. the type declaration section.
  1481. \begin{tabular*}{6.5in}{|l@{\extracolsep{\fill}}lp{6cm}|}
  1482. \hline
  1483. \textsf{TYPE}& & \\
  1484. \xspace pObjectDef = & \^{} TObjectDef; & \\
  1485. \xspace \textsf{TObjectDef} = & \textbf{Object}(TDef) & \\
  1486. &\textsf{ChildOf : pObjectDef;}&
  1487. This is a pointer to the parent object definition. It is set to nil,
  1488. if this object definition has no parent. \\
  1489. &\textsf{ObjName : pString;}&
  1490. This is the object name \\
  1491. &\textsf{SymTable : pSymTable;}&
  1492. This is a pointer to the symbol table entries within this object. \\
  1493. &\textsf{PbjectOptions : TObjectOptions;}&
  1494. The options for this object, see the following table for the
  1495. possible options for the object. \\
  1496. &\textsf{VMT{\_}Offset : Longint;}&
  1497. This is the offset from the start of the object image in memory
  1498. where the virtual method table is located. \\
  1499. &\textsf{Writing{\_}Class{\_}Record{\_}Stab : Boolean;}& \\
  1500. &\textsf{end;}& \\
  1501. \hline
  1502. \end{tabular*}
  1503. \begin{longtable}{|l|p{10cm}|}
  1504. \hline
  1505. Object Options(TObjectOptions) & Description \\
  1506. \hline
  1507. \endhead
  1508. \hline
  1509. \endfoot
  1510. \textsf{oo{\_}Is{\_}Class}&
  1511. This is a delphi styled class declaration, and not a Turbo Pascal
  1512. object. \\
  1513. \textsf{oo{\_}Is{\_}Forward}&
  1514. This flag is set to indicate that the object has been declared in a
  1515. type section, but there is no implementation yet. \\
  1516. \textsf{oo{\_}Has{\_}Virtual}&
  1517. This object / class contains virtual methods \\
  1518. \textsf{oo{\_}Has{\_}Private}&
  1519. This object / class contains private fields or methods \\
  1520. \textsf{oo{\_}Has{\_}Protected}&
  1521. This object / class contains protected fields or methods \\
  1522. \textsf{oo{\_}Has{\_}Constructor}&
  1523. This object / class has a constructor method \\
  1524. \textsf{oo{\_}Has{\_}Destructor}&
  1525. This object / class has a destructor method \\
  1526. \textsf{oo{\_}Has{\_}VMT}&
  1527. This object / class has a virtual method table \\
  1528. \textsf{oo{\_}Has{\_}Msgstr}&
  1529. This object / class contains one or more message handlers \\
  1530. \textsf{oo{\_}Has{\_}Msgint}&
  1531. This object / class contains one or more message handlers \\
  1532. \textsf{oo{\_}Has{\_}Abstract}&
  1533. This object / class contains one or more abstract methods \\
  1534. \textsf{oo{\_}Can{\_}Have{\_}Published}&
  1535. the class has runtime type information, i.e. you can publish
  1536. properties \\
  1537. \textsf{oo{\_}CPP{\_}Class}&
  1538. the object/class uses an C++ compatible class layout \\
  1539. \textsf{oo{\_}Interface}&
  1540. this class is a delphi styled interface
  1541. \end{longtable}
  1542. \subsubsection{class reference definition (TClassRefDef)}
  1543. \label{subsubsec:class}
  1544. \subsubsection{array definition (TArrayDef)}
  1545. \label{subsubsec:array}
  1546. This definition is created when an array type declaration is parsed. It
  1547. contains all the information necessary for array type checking and code
  1548. generation.
  1549. \begin{tabular*}{6.5in}{|l@{\extracolsep{\fill}}lp{8.4cm}|}
  1550. \hline
  1551. \textsf{TYPE}& & \\
  1552. \xspace pArrayDef = & \^{} TArrayDef; & \\
  1553. \xspace \textsf{TArrayDef} = & \textbf{Object}(TDef) & \\
  1554. &\textsf{IsVariant : Boolean;}& \\
  1555. &\textsf{IsConstructor : Boolean;}& \\
  1556. &\textsf{RangeNr: Longint;}&
  1557. Label number associated with the index values when range checking is
  1558. on \\
  1559. &\textsf{LowRange : Longint;}&
  1560. The lower index range of the array definition \\
  1561. &\textsf{HighRange : Longint;}&
  1562. The higher index range of the array definition \\
  1563. &\textsf{ElementType : TType;}&
  1564. The type information for the elements of the array \\
  1565. &\textsf{RangeType : TType;}&
  1566. The type information for the index ranges of the array \\
  1567. &\textsf{IsArrayofConst : Boolean;}& \\
  1568. &\textsf{end;}& \\
  1569. \hline
  1570. \end{tabular*}
  1571. \subsubsection{record definition (TRecordDef)}
  1572. \label{subsubsec:record}
  1573. The record definition entry is created each time a record type declaration
  1574. is parsed. It contains the symbol table to the elements in the record.
  1575. \begin{tabular*}{6.5in}{|l@{\extracolsep{\fill}}lp{8.7cm}|}
  1576. \hline
  1577. \textsf{TYPE} & & \\
  1578. \xspace pRecordDef = & \^{} TRecordDef; & \\
  1579. \xspace \textsf{TRecordDef} = & \textbf{Object}(TDef) & \\
  1580. &\textsf{SymTable : PSymTable;}&
  1581. This is a pointer to the symbol table entries within this record. \\
  1582. &\textsf{end;}& \\
  1583. \hline
  1584. \end{tabular*}
  1585. \subsubsection{ordinal definition (TOrdDef)}
  1586. \label{subsubsec:ordinal}
  1587. This type definition is the one used for all ordinal values such as char,
  1588. bytes and other numeric integer type values. Some of the predefined type
  1589. definitions are automatically created and loaded when the compiler starts.
  1590. Others are created at compile time, when declared.
  1591. \begin{tabular*}{6.5in}{|l@{\extracolsep{\fill}}lp{9cm}|}
  1592. \hline
  1593. \textsf{TYPE} & & \\
  1594. \xspace pOrdDef = & \^{} TOrdDef; & \\
  1595. \xspace \textsf{TOrdDef} = & \textbf{Object}(TDef) & \\
  1596. &\textsf{Low : Longint;}&
  1597. The minimum value of this ordinal type \\
  1598. &\textsf{High : Longint;}&
  1599. The maximum value of this ordinal type \\
  1600. &\textsf{Typ : TBaseType;}&
  1601. The type of ordinal value (cf. \ref{fig3}) \\
  1602. &\textsf{end;}& \\
  1603. \hline
  1604. \end{tabular*}
  1605. \begin{longtable}{|l|p{10cm}|}
  1606. \hline
  1607. Base ordinal type (TBaseType) & Description \\
  1608. \hline
  1609. \endhead
  1610. \hline
  1611. \endfoot
  1612. \textsf{uauto} & user defined ordinal type definition \\
  1613. \textsf{uvoid} & Represents a void return value or node \\
  1614. \textsf{uchar} & ASCII character (1 byte) \\
  1615. \textsf{u8bit} & unsigned 8-bit value \\
  1616. \textsf{u16bit}& unsigned 16-bit value \\
  1617. \textsf{u32bit}& unsigned 32-bit value \\
  1618. \textsf{s16bit}& signed 16-bit value \\
  1619. \textsf{s32bit}& signed 32-bit value \\
  1620. \textsf{bool8bit}& boolean 8-bit value \\
  1621. \textsf{bool16bit}& boolean 16-bit value \\
  1622. \textsf{bool32bit}& boolean 32-bit value \\
  1623. \textsf{\textit{u64bit}}&
  1624. \textit{unsigned 64-bit value (not fully supported/tested)} \\
  1625. \textsf{s64bit}& signed 64-bit value \\\textsf{\textit{uwidechar}}&
  1626. \textit{Currently not supported and unused} \\
  1627. \end{longtable}
  1628. \subsubsection{float definition (TFloatDef)}
  1629. \label{subsubsec:float}
  1630. This type definition is the one used for all floating point values such as
  1631. SINGLE, DOUBLE. Some of the predefined type definitions are automatically
  1632. created and loaded when the compiler starts.
  1633. \begin{tabular*}{6.5in}{|l@{\extracolsep{\fill}}lp{9cm}|}
  1634. \hline
  1635. \textsf{TYPE} & & \\
  1636. \xspace pFloatDef = & \^{} TFloatDef; & \\
  1637. \xspace \textsf{TFloatDef} = & \textbf{Object}(TDef) & \\
  1638. &\textsf{Typ : TFloatType;}&
  1639. The type of floating point value (cf. \ref{tab6}). \\
  1640. &\textsf{end;}& \\
  1641. \hline
  1642. \end{tabular*}
  1643. \begin{longtable}{|l|p{10cm}|}
  1644. \hline
  1645. Base floating point type (TFloatType) & Description \\
  1646. \hline
  1647. \endhead
  1648. \hline
  1649. \endfoot
  1650. \textsf{s32real}& IEEE Single precision floating point value \\
  1651. \textsf{s64real}& IEEE Double precision floating point value \\
  1652. \textsf{s80real}&
  1653. Extended precision floating point value (cpu-specific,
  1654. usually maps to double) \\
  1655. \textsf{s64comp}& 63-bit signed value, using 1 bit for sign indication \\
  1656. \textsf{\textit{f16bit}}& \textit{Unsupported} \\
  1657. \textsf{\textit{f32bit}}& \textit{Unsupported} \\
  1658. \end{longtable}
  1659. \subsubsection{abstract procedure definition (tabstractprocdef)}
  1660. \label{subsubsec:abstract}
  1661. This is the base of all routine type definitions. This object is abstract,
  1662. and is not directly used in a useful way. The derived object of this object
  1663. are used for the actual parsing process.
  1664. \begin{tabular*}{6.5in}{|l@{\extracolsep{\fill}}lp{5.5cm}|}
  1665. \hline
  1666. \textsf{TYPE}& & \\
  1667. \xspace pAbstractProcDef = & \^{} TAbstractProcDef; & \\
  1668. \xspace \textsf{TAbstractProcDef} = & \textbf{Object}(TDef) & \\
  1669. &\textsf{SymtableLevel : byte;}& \\
  1670. &\textsf{Fpu{\_}Used : Byte;}&
  1671. Number of floating point registers used in this routine \\
  1672. &\textsf{RetType : TType;}&
  1673. Type information for the return value \par (uvoid if it returns nothing) \\
  1674. &\textsf{ProcTypeOption : TProcTypeOption;} &
  1675. Indicates the type of routine it is (cf \ref{tab7}). \\
  1676. &\textsf{ProcCallOptions : TProcCallOptions;} &
  1677. Indicates the calling convention of the routine (cf. \ref{tab8}). \\
  1678. &\textsf{ProcOptions : TProcOptions;}&
  1679. Indicates general procedure options. \par (cf. \ref{tab9}). \\
  1680. &\textsf{Para : pLinkedList;}&
  1681. This is a linked list of parameters (pparaitem list) \\
  1682. &\textsf{end;}& \\
  1683. \hline
  1684. \end{tabular*}
  1685. \begin{longtable}{|l|p{10cm}|}
  1686. \hline
  1687. Procedure options \par (TProcTypeOption)& Description \\
  1688. \hline
  1689. \endhead
  1690. \hline
  1691. \endfoot
  1692. \textsf{poType{\_}ProgInit}&
  1693. Routine is the program entry point (defined as `\textsf{main}' in
  1694. the compiler). \\
  1695. \textsf{poType{\_}UnitInit}&
  1696. Routine is the unit initialization code \par (defined as
  1697. unitname\textsf{{\_}init} in the compiler \\
  1698. \textsf{poType{\_}UnitFinalize}&
  1699. Routine is the unit exit code \par (defined as
  1700. unitname\textsf{{\_}finalize} in the compiler) \\
  1701. \textsf{poType{\_}Constructor}&
  1702. Routine is an object or class constructor \\
  1703. \textsf{poType{\_}Destructor}&
  1704. Routine is an object or class destructor \\
  1705. \textsf{poType{\_}Operator}&
  1706. Procedure is an operator \\
  1707. \end{longtable}
  1708. \begin{longtable}{|l|p{10cm}|}
  1709. \hline
  1710. call options \par (TProcCallOptions) & Description \\
  1711. \hline
  1712. \endhead
  1713. \hline
  1714. \endfoot
  1715. \textsf{poCall{\_}ClearStack}&
  1716. The routine caller clears the stack upon return \\
  1717. \textsf{poCall{\_}LeftRight}&
  1718. Send parameters to routine from left to right \\
  1719. \textsf{poCall{\_}Cdecl}&
  1720. Passing parameters is done using the GCC alignment scheme, passing
  1721. parameter values is directly copied into the stack space \\
  1722. \textsf{\textit{poCall{\_}Register}}&
  1723. \textit{unused (Send parameters via registers)} \\
  1724. \textsf{poCall{\_}StdCall}&
  1725. Passing parameters is done using GCC alignment scheme \\
  1726. \textsf{\textit{poCall{\_}SafeCall}}&
  1727. \textit{unused} \\
  1728. \textsf{\textit{poCall{\_}PalmOsSyscall}}&
  1729. \textit{unused} \\
  1730. \textsf{\textit{poCall{\_}System}}&
  1731. \textit{unused} \\
  1732. \textsf{poCall{\_}Inline}&
  1733. Routine is an inline assembler macro (not a true call) \\
  1734. \textsf{poCall{\_}InternProc}&
  1735. System unit code generator helper routine \\
  1736. \textsf{poCall{\_}InternConst}&
  1737. System unit code generator helper macro routine \\
  1738. \end{longtable}
  1739. \begin{longtable}{|l|p{10cm}|}
  1740. \hline
  1741. routine options (TProcOptions) & Description \\
  1742. \hline
  1743. \endhead
  1744. \hline
  1745. \endfoot
  1746. \textsf{po{\_}ClassMethod} & This is a class method \\
  1747. \textsf{po{\_}VirtualMethod }& This is a virtual method \\
  1748. \textsf{po{\_}AbstractMethod}& This is an abstract method \\
  1749. \textsf{po{\_}StaticMethod} & This is a static method \\
  1750. \textsf{po{\_}OverridingMethod}&
  1751. This is an overriden method (with po{\_}virtual flag usually) \\
  1752. \textsf{po{\_}MethodPointer}&
  1753. This is a method pointer (not a normal routine pointer) \\
  1754. \textsf{po{\_}ContainsSelf}&
  1755. self is passed explicitly as a parameter to the method \\
  1756. \textsf{po{\_}Interrupt}&
  1757. This routine is an interrupt handler \\
  1758. \textsf{po{\_}IOCheck}&
  1759. IO checking should be done after a call to the procedure \\
  1760. \textsf{po{\_}Assembler}&
  1761. The routine is in assembler \\
  1762. \textsf{po{\_}MsgStr}&
  1763. method for string message handling \\
  1764. \textsf{po{\_}MsgInt}&
  1765. method for int message handling \\
  1766. \textsf{po{\_}Exports}&
  1767. Routine has export directive \\
  1768. \textsf{po{\_}External}&
  1769. Routine is external (in other object or lib) \\
  1770. \textsf{po{\_}SaveStdRegs}&
  1771. Routine entry should save all registers used by GCC \\
  1772. \textsf{po{\_}SaveRegisters}&
  1773. Routine entry should save all registers \\
  1774. \textsf{po{\_}OverLoad}&
  1775. Routine is declared as being overloaded \\
  1776. \end{longtable}
  1777. \subsubsection{procedural variable definition (TProcVarDef)}
  1778. \label{subsubsec:procedural}
  1779. This definition is created when a procedure variable type is declared. It
  1780. gives information on the type of a procedure, and is used when assigning and
  1781. directly calling a routine through a pointer.
  1782. \begin{tabular*}{6.5in}{|l@{\extracolsep{\fill}}lp{7.8cm}|}
  1783. \hline
  1784. \textsf{TYPE} & & \\
  1785. \xspace pProcVarDef = & \^{} TProcVarDef; & \\
  1786. \xspace \textsf{TProcVarDef} = & \textbf{Object}(TAbstractProcDef) & \\
  1787. & \textsf{end;}&\\
  1788. \hline
  1789. \end{tabular*}
  1790. \subsubsection{procedure definition (TProcDef)}
  1791. \label{subsubsec:mylabel8}
  1792. When a procedure head is parsed, the definition of the routine is created.
  1793. Thereafter, other fields containing information on the definition of the
  1794. routine are populated as required.
  1795. \begin{tabular*}{6.5in}{|l@{\extracolsep{\fill}}lp{7.8cm}|}
  1796. \hline
  1797. \textsf{TYPE} & & \\
  1798. \xspace pProcDef = & \^{} TProcDef; & \\
  1799. \xspace \textsf{TProcDef} = & \textbf{Object}(TAbstractProcDef) & \\
  1800. &\textsf{ForwardDef : Boolean;}& TRUE if this is a forward definition \\
  1801. &\textsf{InterfaceDef: Boolean;}& \\
  1802. &\textsf{ExtNumber : Longint;}& \\
  1803. &\textsf{MessageInf : TMessageInf;}& \\
  1804. &\textsf{NextOverloaded : pProcDef;}& \\
  1805. &\textsf{FileInfo : TFilePosInfo;}&
  1806. Position in source code for the declaration of this routine. Used
  1807. for error management. \\
  1808. &\textsf{Localst : pSymTable;}& The local variables symbol table \\
  1809. &\textsf{Parast: pSymTable;}& The parameter symbol table \\
  1810. &\textsf{ProcSym : pProcSym;}& Points to owner of this definition \\
  1811. &\textsf{LastRef : pRef;}& \\
  1812. &\textsf{DefRef: pRef;}& \\
  1813. &\textsf{CrossRef : pRef;}& \\
  1814. &\textsf{LastWritten : pRef;}& \\
  1815. &\textsf{RefCount : Longint;}& \\
  1816. &\textsf{{\_}Class : ProbjectDef;}& \\
  1817. &\textsf{Code : Pointer;}&
  1818. The actual code for the routine (only for inlined routines) \\
  1819. &\textsf{UsedRegisters : TRegisterSet;}&
  1820. The set of registers used in this routine \\
  1821. &\textsf{HasForward : Boolean;}& \\
  1822. &\textsf{Count: Boolean;}& \\
  1823. &\textsf{Is{\_}Used : Boolean;}& \\
  1824. &\textsf{end;}& \\
  1825. \hline
  1826. \end{tabular*}
  1827. \subsubsection{string definition (TStringDef)}
  1828. \label{subsubsec:string}
  1829. This definition represents all string types as well as derived types. Some
  1830. of the default string type definitions are loaded when the compiler starts
  1831. up. Others are created at compile time as they are declared with a specific
  1832. length type.
  1833. \begin{tabular*}{6.5in}{|l@{\extracolsep{\fill}}lp{9cm}|}
  1834. \hline
  1835. \textsf{TYPE}& & \\
  1836. \xspace pStringDef = & \^{} TStringDef; & \\
  1837. \xspace \textsf{TStringDef} = & \textbf{Object}(TDef) & \\
  1838. &\textsf{String{\_}Typ : TStringType;}&
  1839. Indicates the string type definition (cf. \ref{tab10}) \\
  1840. &\textsf{Len : Longint;}&
  1841. This is the maximum length which can have the string \\
  1842. &\textsf{end;}& \\
  1843. \hline
  1844. \end{tabular*}
  1845. \begin{longtable}{|l|p{10cm}|}
  1846. \hline
  1847. String type \par (TStringType) & Description \\
  1848. \hline
  1849. \endhead
  1850. \hline
  1851. \endfoot
  1852. \textsf{st{\_}Default}&
  1853. Depends on current compiler switches, can either be a
  1854. st{\_}ShortString or st{\_}AnsiString \\
  1855. \textsf{st{\_}ShortString}&
  1856. short string (length byte followed by actual ASCII characters (1
  1857. byte/char)) \\
  1858. \textsf{st{\_}LongString}&
  1859. long string (length longint followed by actual ASCII characters (1
  1860. byte/char)) \\
  1861. \textsf{st{\_}AnsiString}&
  1862. long string garbage collected (pointer to a length, reference count
  1863. followed by actual ASCII characters (1 byte/char)) \\
  1864. \textsf{\textit{st{\_}WideString}}&
  1865. \textit{long string garbage collected (pointer to a length,
  1866. reference count followed by actual unicode characters (1
  1867. word/char))} \\
  1868. \end{longtable}
  1869. \subsubsection{enumeration definition (TEnumDef)}
  1870. \label{subsubsec:mylabel9}
  1871. An enumeration definition is created each time an enumeration is declared
  1872. and parsed. Each element in the enumeration will be added to the linked list
  1873. of symbols associated with this enumeration, and this symbol table will then
  1874. be attached to the enumeration definition.
  1875. \begin{tabular*}{6,5in}{|l@{\extracolsep{\fill}}lp{6,5cm}|}
  1876. \hline
  1877. \textsf{TYPE} & & \\
  1878. \xspace \textsf{pEnumDef} &= \^{} \textbf{TEnumDef};& \\
  1879. \xspace \textsf{TEnumDef} &= \textbf{object}(TDef) & \\
  1880. &\textsf{\textit{has{\_}jumps : boolean;}}&
  1881. \textit{Currently unused} \\
  1882. &\textsf{minval : longint;}&
  1883. Value of the first element in the enumeration \\
  1884. &\textsf{maxval : longint;}&
  1885. Value of the last element in the enumeration \\
  1886. &\textsf{firstenum : penumsym;}&
  1887. Pointer to a linked list of elements in the enumeration, each with
  1888. its name and value. \\
  1889. &\textsf{basedef : penumdef;}&
  1890. In the case where the enumeration is a subrange of another enumeration,
  1891. this gives information on the base range of the elements \\
  1892. &\textsf{end;}& \\
  1893. \hline
  1894. \end{tabular*}
  1895. \subsubsection{set definition (tsetdef)}
  1896. \label{subsubsec:mylabel10}
  1897. This definition is created when a set type construct is parsed (\textsf{set
  1898. of declaration}).
  1899. \begin{tabular*}{6.5in}{|l@{\extracolsep{\fill}}lp{6,5cm}|}
  1900. \hline
  1901. \textsf{TYPE} & & \\
  1902. \xspace \textsf{pSetDef} &= \^{} \textbf{TSetDef};& \\
  1903. \xspace \textsf{TSetDef} &= \textbf{object}(TDef) & \\
  1904. &\textsf{settype : tsettype;}&
  1905. Indicates the storage type of the set (Cf. \ref{tab11}). \\
  1906. &\textsf{elementtype : ttype;}&
  1907. Points the type definition and symbol table to the elements in the set. \\
  1908. &\textsf{end;}& \\
  1909. \hline
  1910. \end{tabular*}
  1911. \begin{longtable}{|l|p{10cm}|}
  1912. \hline
  1913. set type (tsettype) & Description \\
  1914. \hline
  1915. \endhead
  1916. \hline
  1917. \endfoot
  1918. \textsf{normset}&
  1919. Normal set of up to 256 elements (32 byte storage space required) \\
  1920. \textsf{smallset}&
  1921. Small set of up to 32 elements (4 byte storage space) \\
  1922. \textsf{\textit{varset}}&
  1923. \textit{Variable number of element set (storage size is dependent on number
  1924. of elements) (currently unused and unsupported)} \\
  1925. \end{longtable}
  1926. \subsection{Definition interface}
  1927. \label{subsec:definition}
  1928. \begin{function}{TDef.Size}
  1929. \Declaration
  1930. Function TDef.size : longint;
  1931. \Description
  1932. This method returns the true size of the memory space required in bytes for
  1933. this type definition (after alignment considerations).
  1934. \end{function}
  1935. \begin{function}{TDef.Alignment}
  1936. \Declaration
  1937. Function TDef.Alignment : longint;
  1938. \Description
  1939. This method returns the alignment of the data for complex types such as
  1940. records and objects, otherwise returns 0 or 1 (no alignment).
  1941. \end{function}
  1942. \section{The parser}
  1943. \label{sec:mylabel5}
  1944. The task of the parser is to read the token fed by the scanner, and make
  1945. sure that the pascal syntax is respected. It also populates the symbol
  1946. table, and creates the intermediate nodes (the tree) which will be used by
  1947. the code generator.
  1948. An overview of the parsing process, as well as its relationship with the
  1949. tree the type checker and the code generator is shown in the following
  1950. diagram:
  1951. \subsection{Module information}
  1952. \label{subsec:module}
  1953. Each module being compiled, be it a library , unit or main program has some
  1954. information which is required. This is stored in the tmodule object in
  1955. memory. To avoid recompilation of already compiled module, the dependencies
  1956. of the modules is stored in a PPU file, which makes it easier to determine
  1957. which modules to recompile.
  1958. \begin{longtable}{|l@{\extracolsep{\fill}}lp{7cm}|}
  1959. \hline
  1960. \endhead
  1961. \hline
  1962. \endfoot
  1963. %\begin{tabular*}
  1964. \textsf{TYPE}& & \\
  1965. \xspace pModule = & \^{} TModule; & \\
  1966. \xspace \textsf{TModule} = & \textbf{Object}(TLinkedList\_Item) & \\
  1967. &\textsf{ppufile : pppufile;}& Pointer to PPU file object (unit file) \\
  1968. &\textsf{crc : longint;}& CRC-32 bit of the whole PPU file \\
  1969. &\textsf{interface{\_}crc : longint;}& CRC-32 bit of the interface part of the PPU file \\
  1970. &\textsf{flags: longint;}& Unit file flags \\
  1971. &\textsf{compiled: boolean;}& TRUE if module is already compiled \\
  1972. &\textsf{do{\_}reload : boolean;} & TRUE if the PPU file must be reloaded \\
  1973. &\textsf{do{\_}assemble : boolean;} & Only assemble, don't recompile unit \\
  1974. &\textsf{sources{\_}avail : boolean;} & TRUE if all sources of module are available \\
  1975. &\textsf{sources{\_}checked : boolean;} & TRUE if the sources has already been checked \\
  1976. &\textsf{is{\_}unit: boolean;} & TRUE if this is a unit (otherwise a library or a main program) \\
  1977. &\textsf{in{\_}compile: boolean;} & module is currently being recompiled \\
  1978. &\textsf{in{\_}second{\_}compile: boolean;}& module is being compiled for second time \\
  1979. &\textsf{in{\_}second{\_}load: boolean;} & module is being reloaded a second time \\
  1980. &\textsf{in{\_}implementation : boolean;}& currently compiling implementation part (units only) \\
  1981. &\textsf{in{\_}global : boolean;} & currently compiling implementation part (units only) \\
  1982. &\textsf{recompile{\_}reason : trecompile{\_}reason;}& Reason why module should be recompiled \\
  1983. &\textsf{islibrary : boolean;}& TRUE if this module is a shared library \\
  1984. &\textsf{map : punitmap;} & Map of all used units for this unit \\
  1985. &\textsf{unitcount : word;} & Internal identifier of unit (for GDB support) \\
  1986. &\textsf{unit{\_}index : word;} & \\
  1987. &\textsf{globalsymtable : pointer;} & Symbol table for this module of externally visible symbols \\
  1988. &\textsf{localsymtable : pointer;} & Symbol table for this module of locally visible symbols \\
  1989. &\textsf{scanner : pointer;} & Scanner object pointer \\
  1990. &\textsf{loaded{\_}from : pmodule;} & Module which referred to this module \\
  1991. &\textsf{uses{\_}imports : boolean;} & TRUE if this module imports symbols from a shared library \\
  1992. &\textsf{imports : plinkedlist} & Linked list of imported symbols \\
  1993. &\textsf{{\_}exports : plinkedlist;} & Linked list of exported symbols (libraries only) \\
  1994. &\textsf{sourcefiles : pfilemanager;} & List of all source files for this module \\
  1995. &\textsf{resourcefiles : tstringcontainer;} & List of all resource files for this module \\
  1996. &\textsf{used{\_}units : tlinkedlist; } & Information on units used by this module (pused{\_}unit) \\
  1997. &\textsf{dependent{\_}units : tlinkedlist;}& \\
  1998. &\textsf{localunitsearchpath : TsearchPathList;}& Search path for obtaining module source code \\
  1999. &\textsf{localobjectsearchpath:TsearchPathList;}& \\
  2000. &\textsf{localincludesearchpath:TsearchPathList;}& Search path for includes for this module \\
  2001. &\textsf{locallibrarysearchpathTSearchPathList;}& \\
  2002. &\textsf{path : pstring;}& Path were module is located or created \\
  2003. &\textsf{outputpath : pstring;}& Path where object files (unit), executable (program) or shared library (library) is created \\
  2004. &\textsf{modulename : pstring;}& Name of the module in uppercase \\
  2005. &\textsf{objfilename : pstring;}& Full name of object file or executable file \\
  2006. &\textsf{asmfilename : pstring;}& Full name of the assembler file \\
  2007. &\textsf{ppufilename : pstring;}& Full name of the PPU file \\
  2008. &\textsf{staticlibfilename : pstring;}& Full name of the static library name (used when smart linking is used) \\
  2009. &\textsf{sharedlibfilename : pstring;}& Filename of the output shared library (in the case of a library) \\
  2010. &\textsf{exefilename : pstring;}& Filename of the output executable (in the case of a program) \\
  2011. &\textsf{asmprefix : pstring;}& Filename prefix of output assembler files when using smartlinking \\
  2012. &\textsf{mainsource : pstring;}& Name of the main source file \\
  2013. &\textsf{end;}& \\
  2014. %\end{tabular*}
  2015. \end{longtable}
  2016. \subsection{Parse types}
  2017. \label{subsec:parse}
  2018. \subsubsection{Entry}
  2019. \label{subsubsec:entry}
  2020. \begin{figure}
  2021. \ifpdf
  2022. \includegraphics{arch8.pdf}
  2023. %\epsfig{file=arch8.png,width=\textwidth}
  2024. \else
  2025. \includegraphics[width=4.99in,height=8.36in]{arch8.eps}
  2026. \fi
  2027. \label{fig8}
  2028. \caption{Parser - Scanner flow}
  2029. \end{figure}
  2030. \subsubsection{program or library parsing }
  2031. \subsubsection{unit parsing }
  2032. \label{subsubsec:mylabel12}
  2033. \subsubsection{routine parsing }
  2034. \label{subsubsec:routine}
  2035. \subsubsection{label declarations }
  2036. \label{subsubsec:mylabel13}
  2037. \subsubsection{constant declarations}
  2038. \label{subsubsec:mylabel14}
  2039. \subsubsection{type declarations}
  2040. \label{subsubsec:mylabel15}
  2041. \subsubsection{variable declarations}
  2042. \label{subsubsec:mylabel16}
  2043. \subsubsection{thread variable declarations}
  2044. \label{subsubsec:thread}
  2045. \subsubsection{resource string declarations}
  2046. \label{subsubsec:resource}
  2047. \subsubsection{exports declaration}
  2048. \label{subsubsec:exports}
  2049. \subsubsection{expression parsing }
  2050. \label{subsubsec:expression}
  2051. \subsubsection{typed constant declarations}
  2052. \label{subsubsec:mylabel17}
  2053. \subsection{Parser interface}
  2054. \label{subsec:parser}
  2055. \subsubsection{Routines}
  2056. \label{subsubsec:routinesnterfaceecla}
  2057. \subsubsection{Variables}
  2058. \label{subsubsec:variablesterfaceecla}
  2059. \paragraph{General}
  2060. \begin{variable}{AktProcSym}
  2061. \Declaration
  2062. Var aktprocsym : pProcSym;
  2063. \Description
  2064. Pointer to the symbol information for the routine currently being parsed.
  2065. \end{variable}
  2066. \begin{variable}{LexLevel}
  2067. \Declaration
  2068. var lexlevel : longint;
  2069. \Description
  2070. Level of code currently being parsed and compiled \par 0 = for main program
  2071. \par 1 = for subroutine \par 2 = for local / nested subroutines.
  2072. \end{variable}
  2073. \begin{variablel}{Current{\_}Module}{currentmodule}
  2074. \Declaration
  2075. var Current{\_}Module : pModule;
  2076. \Description
  2077. Information on the current module (program, library or unit) being compiled.
  2078. \end{variablel}
  2079. \paragraph{Ordinal definitions}
  2080. The following variables are default type definitions which are created each
  2081. time compilation begins (default system-unit definitions), these definitions
  2082. should always be valid:
  2083. \begin{variable}{VoidDef}
  2084. \Declaration
  2085. var VoidDef : pOrdDef;
  2086. \Description
  2087. Pointer to procedure???
  2088. \Notes
  2089. This is loaded as a default supported type for the compiler
  2090. \end{variable}
  2091. \begin{variable}{cCharDef}
  2092. \Declaration
  2093. var cCharDef : pOrdDef;
  2094. \Description
  2095. Type definition for a character (\textsf{char})
  2096. \Notes
  2097. This is loaded as a default supported type for the compiler
  2098. \end{variable}
  2099. \begin{variable}{cWideCharDef}
  2100. \Declaration
  2101. var cWideCharDef : pOrdDef;
  2102. \Description
  2103. Type definition for a unicode character (\textsf{widechar})
  2104. \Notes
  2105. This is loaded as a default supported type for the compiler
  2106. \end{variable}
  2107. \begin{variable}{BoolDef}
  2108. \Declaration
  2109. var BoolDef : pOrdDef;
  2110. \Description
  2111. Type definition for a boolean value (\textsf{boolean})
  2112. \Notes
  2113. This is loaded as a default supported type for the compiler
  2114. \end{variable}
  2115. \begin{variable}{u8BitDef}
  2116. \Declaration
  2117. var u8BitDef : pOrdDef;
  2118. \Description
  2119. Type definition for an 8-nit unsigned value (\textsf{byte})
  2120. \Notes
  2121. This is loaded as a default supported type for the compiler
  2122. \end{variable}
  2123. \begin{variable}{u16BitDef}
  2124. \Declaration
  2125. var u16BitDef : pOrdDef;
  2126. \Description
  2127. Type definition for an unsigned 16-bit value (\textsf{word})
  2128. \Notes
  2129. This is loaded as a default supported type for the compiler
  2130. \end{variable}
  2131. \begin{variable}{u32BitDef}
  2132. \Declaration
  2133. var u32BitDef : pOrdDef;
  2134. \Description
  2135. Type definition for an unsigned 32-bit value (\textsf{cardinal})
  2136. \Notes
  2137. This is loaded as a default supported type for the compiler
  2138. \end{variable}
  2139. \begin{variable}{s32BitDef}
  2140. \Declaration
  2141. var s32BitDef : pOrdDef;
  2142. \Description
  2143. Type definition for a signed 32-bit value (\textsf{longint})
  2144. \Notes
  2145. This is loaded as a default supported type for the compiler
  2146. \end{variable}
  2147. \begin{variable}{cu64BitDef}
  2148. \Declaration
  2149. var cu64BitDef : pOrdDef;
  2150. \Description
  2151. Type definition for an unsigned 64-bit value (\textsf{qword})
  2152. \Notes
  2153. This is loaded as a default supported type for the compiler
  2154. \end{variable}
  2155. \begin{variable}{cs64BitDef}
  2156. \Declaration
  2157. var cs64BitDef : pOrdDef;
  2158. \Description
  2159. Type definition for a signed 64-bit value (\textsf{int64})
  2160. \Notes
  2161. This is loaded as a default supported type for the compiler
  2162. \end{variable}
  2163. \paragraph{floating point definitions}
  2164. The following variables are default type definitions which are created each
  2165. time compilation begins (default system-unit definitions), these definitions
  2166. should always be valid:
  2167. \begin{variable}{s64FloatDef}
  2168. \Declaration
  2169. var s64FloatDef : pFloatDef;
  2170. \Description
  2171. Type definition for a 64-bit IEEE floating point type (\textsf{double})
  2172. \Notes
  2173. This is loaded as a default supported type for the compiler. This might not
  2174. actually really point to the double type if the cpu does not support it.
  2175. \end{variable}
  2176. \begin{variable}{s32FloatDef}
  2177. \Declaration
  2178. var s32FloatDef : pFloatDef;
  2179. \Description
  2180. Type definition for a 32-bit IEEE floating point type (\textsf{single})
  2181. \Notes
  2182. This is loaded as a default supported type for the compiler. This might not
  2183. actually really point to the single type if the cpu does not support it.
  2184. \end{variable}
  2185. \begin{variable}{s80FloatDef}
  2186. \Declaration
  2187. var s80FloatDef : pFloatDef;
  2188. \Description
  2189. Type definition for an extended floating point type (\textsf{extended})
  2190. \Notes
  2191. This is loaded as a default supported type for the compiler. This
  2192. might not actually really point to the extended type if the cpu does not
  2193. support it.
  2194. \end{variable}
  2195. \begin{variable}{s32FixedDef}
  2196. \Declaration
  2197. var s32FixedDef : pFloatDef;
  2198. \Description
  2199. Type definition for a fixed point 32-bit value (\textsf{fixed})
  2200. \Notes
  2201. This is loaded as a default supported type for the compiler. This is
  2202. not supported officially in FPC 1.0
  2203. \end{variable}
  2204. \clearpage
  2205. \paragraph{String definitions}
  2206. The following variables are default type definitions which are created each
  2207. time compilation begins (default system-unit definitions), these definitions
  2208. should always be valid:
  2209. \begin{variable}{cShortStringDef}
  2210. \Declaration
  2211. var cShortStringDef : pStringDef;
  2212. \Description
  2213. Type definition for a short string type (\textsf{shortstring})
  2214. \Notes
  2215. This is loaded as a default supported type for the compiler.
  2216. \end{variable}
  2217. \begin{variable}{cLongStringDef}
  2218. \Declaration
  2219. var cLongStringDef : pStringDef;
  2220. \Description
  2221. Type definition for a long string type (\textsf{\textit{longstring}})
  2222. \Notes
  2223. This is loaded as a default supported type for the compiler.
  2224. \end{variable}
  2225. \begin{variable}{cAnsiStringDef}
  2226. \Declaration
  2227. var cAnsiStringDef : pStringDef;
  2228. \Description
  2229. Type definition for an ansistring type (\textsf{ansistring})
  2230. \Notes
  2231. This is loaded as a default supported type for the compiler.
  2232. \end{variable}
  2233. \begin{variable}{cWideStringDef}
  2234. \Declaration
  2235. var cWideStringDef : pStringDef;
  2236. \Description
  2237. Type definition for an wide string type (\textsf{\textit{widestring}})
  2238. \Notes
  2239. This is loaded as a default supported type for the compiler.
  2240. \end{variable}
  2241. \begin{variable}{OpenShortStringDef}
  2242. \Declaration
  2243. var OpenShortStringDef : pStringDef;
  2244. \Description
  2245. Type definition for an open string type (\textsf{openstring})
  2246. \Notes
  2247. This is loaded as a default supported type for the compiler.
  2248. \end{variable}
  2249. \begin{variable}{OpenCharArrayDef}
  2250. \Declaration
  2251. var OpenCharArrayDef : pArrayDef;
  2252. \Description
  2253. Type definition for an open char array type(\textsf{openchararray})
  2254. \Notes
  2255. This is loaded as a default supported type for the compiler.
  2256. \end{variable}
  2257. \clearpage
  2258. \paragraph{Pointer definitions}
  2259. The following variables are default type definitions which are created each
  2260. time compilation begins (default system-unit definitions), these definitions
  2261. should always be valid:
  2262. \begin{variable}{VoidPointerDef}
  2263. \Declaration
  2264. var VoidPointerDef : pPointerDef;
  2265. \Description
  2266. Type definition for a pointer which can point to anything (\textsf{pointer})
  2267. \Notes
  2268. This is loaded as a default supported type for the compiler
  2269. \end{variable}
  2270. \begin{variable}{CharPointerDef}
  2271. \Declaration
  2272. var CharPointerDef : pPointerDef;
  2273. \Description
  2274. Type definition for a pointer which can point to characters (\textsf{pchar})
  2275. \Notes
  2276. This is loaded as a default supported type for the compiler
  2277. \end{variable}
  2278. \begin{variable}{VoidFarPointerDef}
  2279. \Declaration
  2280. var VoidFarPointerDef : pPointerDef;
  2281. \Description
  2282. Type definition for a pointer which can point to anything
  2283. (intra-segment) (\textsf{far pointer})
  2284. \Notes
  2285. This is loaded as a default supported type for the compiler
  2286. \end{variable}
  2287. \begin{variable}{cFormalDef}
  2288. \Declaration
  2289. var cFormalDef : pFormalDef;
  2290. \Notes
  2291. This is loaded as a default supported type for the compiler
  2292. \end{variable}
  2293. \paragraph{Other definitions}
  2294. \begin{variable}{cfFileDef}
  2295. \Declaration
  2296. var cfFileDef : pFileDef;
  2297. \Description This is the default file type (\textsf{file})
  2298. \Notes This is loaded as a default supported type for the compiler
  2299. \end{variable}
  2300. \section{The inline assembler parser}
  2301. \label{sec:mylabel6}
  2302. \section{The code generator}
  2303. \label{sec:mylabel7}
  2304. \subsection{Introduction}
  2305. \label{subsec:introductioneratorer}
  2306. The code generator is responsible for creating the assembler output in form
  2307. of a linked list, taking as input the node created in the parser and the
  2308. 1$^{st}$ pass. The following diagram shows an overview of the code generator
  2309. architecture:
  2310. \begin{figure}
  2311. \ifpdf
  2312. \includegraphics{arch9.pdf}
  2313. %\epsfig{file=arch9.png,width=\textwidth}
  2314. \else
  2315. \includegraphics[width=5.68in,height=1.76in]{arch9.eps}
  2316. \fi
  2317. \label{fig9}
  2318. \caption{Codegenerator architecture}
  2319. \end{figure}
  2320. The code generation is only done when a procedure body is parsed; the
  2321. interaction, between the 1$^{st}$ pass (type checking phase), the code
  2322. generation and the parsing process is show in the following diagram:
  2323. \begin{figure}
  2324. \ifpdf
  2325. \includegraphics{arch10.pdf}
  2326. %\epsfig{file=arch10.png,width=\textwidth}
  2327. \else
  2328. \includegraphics[width=6.95in,height=4.90in]{arch10.eps}
  2329. \fi
  2330. \label{fig10}
  2331. \caption{Interaction between codegeneration and the parsing process}
  2332. \end{figure}
  2333. The \textsf{secondpass()} is actually a simple dispatcher. Each possible
  2334. tree type node (Cf. Tree types) is associated with
  2335. a second pass routine which is called using a dispatch table.
  2336. \subsection{Locations (cpubase.pas)}
  2337. \label{subsec:locations}
  2338. The code generator uses the tree location component to indicate the location
  2339. where the current node operands are located. This is then used by the code
  2340. generator to generate the appropriate instruction, all depending on the
  2341. location of the operand. The possible operand locations:
  2342. \begin{longtable}{|l|p{10cm}|}
  2343. \hline
  2344. Location define & Description \\
  2345. \hline
  2346. \endhead
  2347. \hline
  2348. \endfoot
  2349. \textsf{LOC{\_}INVALID}&
  2350. Invalid location (should never occur) \\
  2351. \textsf{LOC{\_}FPU}&
  2352. Floating point registers \\
  2353. \textsf{LOC{\_}REGISTER}&
  2354. Integer registers \\
  2355. \textsf{LOC{\_}MEM}&
  2356. Memory Location \\
  2357. \textsf{LOC{\_}REFERENCE}&
  2358. Constant node with constant value \\
  2359. \textsf{LOC{\_}JUMP}&
  2360. Label operand \\
  2361. \textsf{LOC{\_}FLAGS}&
  2362. Flags operand \\
  2363. \textsf{LOC{\_}CREGISTER}&
  2364. Constant integer register (when operand is in this
  2365. location, it should be considered as read-only) \\
  2366. \end{longtable}
  2367. Depending on the location type, a variable structure is defined indicating
  2368. more information on the operand. This is used by the code generator to
  2369. generate the exact instructions.
  2370. \subsubsection{LOC{\_}INVALID}
  2371. \label{subsubsec:mylabel18}
  2372. This location does not contain any related information, when this location
  2373. occurs, it indicates that the operand location was not initially allocated
  2374. correctly. This indicates a problem in the compiler.
  2375. \subsubsection{LOC{\_}FPU}
  2376. \label{subsubsec:mylabel19}
  2377. This indicates a location in the coprocessor; this is platform dependant.
  2378. \paragraph{Stack based FPU}
  2379. Only one CPU uses a stack based FPU architecture, this is the intel 80x86
  2380. family of processors. When the operand is on the top of the stack, the
  2381. operand is of type LOC{\_}FPU.
  2382. \paragraph{Register based FPU}
  2383. When the floating point co-processor is register based, the following
  2384. field(s) are defined in the structure to indicate the current location of
  2385. the operand:
  2386. \begin{longtable}{|l|p{7cm}|}
  2387. \hline
  2388. Field & Description \\
  2389. \hline
  2390. \endhead
  2391. \hline
  2392. \endfoot
  2393. \textsf{fpuregister : tregister;}&
  2394. Indicates in what register the operand is located (a general purpose
  2395. register in emulation mode, and a floating point register when floating
  2396. point hardware is present) \\
  2397. \textsf{fpuregisterhigh, } \par \textsf{fpuregisterlow : tregister;}&
  2398. Indicates in what registers the operand are located (for emulation
  2399. support - these are general purpose registers)
  2400. \end{longtable}
  2401. \subsubsection{LOC{\_}REGISTER}
  2402. \label{subsubsec:mylabel20}
  2403. This fields indicates that the operand is located in a CPU register. It is
  2404. possible to allocate more then one register, if trying to access 64-bit
  2405. values on 32-bit wide register machines.
  2406. \begin{longtable}{|l|p{10cm}|}
  2407. \hline
  2408. Field & Description \\
  2409. \hline
  2410. \endhead
  2411. \hline
  2412. \endfoot
  2413. \textsf{register : tregister}&
  2414. Indicates in what register the operand is located. \\
  2415. \textsf{registerhigh : tregister;}&
  2416. High 32-bit of 64-bit virtual register (on 32-bit machines) \\
  2417. \textsf{registerlow : tregister;}&
  2418. Low 32-bit of 64-bit virtual register (on 32-bit machines)
  2419. \end{longtable}
  2420. \subsubsection{LOC{\_}MEM, LOC{\_}REFERENCE}
  2421. \label{subsubsec:mylabel21}
  2422. This either indicates an operand in memory, or a constant integer numeric
  2423. value. The fields for this type of operand is as follows:
  2424. \begin{longtable}{|l|p{10cm}|}
  2425. \hline
  2426. Field & Description \\
  2427. \hline
  2428. \endhead
  2429. \hline
  2430. \endfoot
  2431. \textsf{reference : treference;}&
  2432. Information on the location in memory
  2433. \end{longtable}
  2434. References are the basic building blocks of the code generator, every load
  2435. and store in memory is done via a reference. A reference type can either
  2436. point to a symbolic name, an assembler expression (base register + index
  2437. register + offset)*scale factor, as well as simply giving information on a
  2438. numeric value.
  2439. The treference consists of the following:
  2440. \begin{tabular*}{6.5in}{|l@{\extracolsep{\fill}}lp{6,5cm}|}
  2441. \hline
  2442. \textsf{TYPE} & & \\
  2443. \xspace \textsf{pReference} &= \^{} \textbf{TReference};& \\
  2444. \xspace \textsf{TReference} &= \textbf{packed Record} & \\
  2445. &\textsf{is{\_}immediate : boolean;}&
  2446. Indicates that this location points to a memory location, but to a
  2447. constant value (TRUE), which is located in the offset field. \\
  2448. &\textsf{segment : tregister;}& (cpu-specific) \\
  2449. &\textsf{base : tregister;}&
  2450. Base address register for assembler expression \\
  2451. &\textsf{index : tregister;}&
  2452. Index register for assembler expression \\
  2453. &\textsf{scalefactor : byte;}&
  2454. Multiplication factor for assembler expression (this field is
  2455. cpu-specific) \\
  2456. &\textsf{offset : longint;}&
  2457. Either an offset from base assembler address expression to add (if
  2458. is{\_}constant = FALSE) otherwise the numeric value of the operand \\
  2459. &\textsf{symbol : pasmsymbol;}&
  2460. Pointer to the symbol name string of the reference in case where it is
  2461. a symbolic reference \\
  2462. &\textsf{offsetfixup : longint;}& \\
  2463. &\textsf{options : trefoptions;}& \\
  2464. &\textsf{END;}& \\
  2465. \hline
  2466. \end{tabular*}
  2467. \subsubsection{LOC{\_}JUMP}
  2468. \label{subsubsec:mylabel22}
  2469. There are no fields associated with this location, it simply indicates that
  2470. it is a boolean comparison which must be done to verify the succeeding
  2471. operations. (i.e the processor zero flag is valid and gives information on
  2472. the result of the last operation).
  2473. \subsubsection{LOC{\_}FLAGS}
  2474. \label{subsubsec:mylabel23}
  2475. The operand is in the flags register. From this operand, the conditional
  2476. jumps can be done. This is processor dependant, but normally the flags for
  2477. all different comparisons should be present.
  2478. \begin{longtable}{|l|p{10cm}|}
  2479. \hline
  2480. Field & Description \\
  2481. \hline
  2482. \endhead
  2483. \hline
  2484. \endfoot
  2485. \textsf{resflags : tresflags;}&
  2486. This indicates the flag which must be verified for the actual jump
  2487. operation. \textsf{tresflags }is an enumeration of all possible
  2488. conditional flags which can be set by the processor. \\
  2489. \end{longtable}
  2490. \subsubsection{LOC{\_}CREGISTER}
  2491. \label{subsubsec:mylabel24}
  2492. This is a read-only register allocated somewhere else in the code generator.
  2493. It is used mainly for optimization purposes. It has the same fields as
  2494. LOC{\_}REGISTER, except that the registers associated with this location can
  2495. only be read from, and should never be modified directly.
  2496. \begin{longtable}{|l|p{10cm}|}
  2497. \hline
  2498. Field & Description \\
  2499. \hline
  2500. \endhead
  2501. \hline
  2502. \endfoot
  2503. \textsf{register : tregister}&
  2504. Indicates in what register the operand is located. \\
  2505. \textsf{registerhigh : tregister;}&
  2506. High 32-bit of 64-bit virtual register (on 32-bit machines) \\
  2507. \textsf{registerlow : tregister;}&
  2508. Low 32-bit of 64-bit virtual register (on 32-bit machines) \\
  2509. \end{longtable}
  2510. \subsubsection{LOCATION PUBLIC INTERFACE}
  2511. \label{subsubsec:location}
  2512. \begin{procedurel}{Del{\_}Location}{dellocation}
  2513. \Declaration
  2514. procedur Del{\_}Location(const L : TLocation);
  2515. \Description
  2516. If the location points to a LOC{\_}REGISTER or LOC{\_}CREGISTER, it frees up
  2517. the allocated register(s) associated with this location. If the location
  2518. points to LOC{\_}REFERENCE or LOC{\_}MEM, it frees up the the allocated base
  2519. and index registers associated with this node.
  2520. \end{procedurel}
  2521. \begin{procedurel}{Clear{\_}Location}{clearlocation}
  2522. \Declaration
  2523. procedure Clear{\_}location(var Loc : TLocation);
  2524. \Description
  2525. Sets the location to point to a LOC{\_}INVALID type.
  2526. \end{procedurel}
  2527. \begin{procedurel}{Set{\_}Location}{setlocation}
  2528. \Declaration
  2529. procedure Set{\_}Location(var Destloc,Sourceloc : TLocation);
  2530. \Description
  2531. The destination location now points to the destination location (now copy is
  2532. made, a simple pointer assignment)
  2533. \end{procedurel}
  2534. \begin{procedurel}{Swap{\_}Location}{swaplocation}
  2535. \Declaration
  2536. Procedure Swap{\_}Location(var Destloc,Sourceloc : TLocation);
  2537. \Description
  2538. Swap both location pointers.
  2539. \end{procedurel}
  2540. \subsection{Registers (cpubase.pas)}
  2541. \label{subsec:registers}
  2542. The code generator defines several types of registers which are categorized
  2543. by classes. All (except for the scratch register class) of these register
  2544. classes are allocated / freed on the fly, when the code is generated in the
  2545. code generator: The registers are defined in a special enumeration called
  2546. tregister. This enumeration contains all possible register defines for the
  2547. target architecture, and a possible definition could be as follows :
  2548. % FIXME this should be changed to something more TeXish
  2549. \textsf{tregister = (}
  2550. \textsf{{\{} general purpose registers {\}} }
  2551. \textsf{R{\_}NO,R{\_}D0,R{\_}D1,R{\_}D2,R{\_}D3,R{\_}D4,R{\_}D5,R{\_}D6,R{\_}D7,}
  2552. \textsf{{\{} address registers {\}}}
  2553. \textsf{R{\_}A0,R{\_}A1,R{\_}A2,R{\_}A3,R{\_}A4,R{\_}A5,R{\_}A6,R{\_}SP,}
  2554. \textsf{{\{} PUSH/PULL- quick and dirty hack {\}}}
  2555. \textsf{R{\_}SPPUSH,R{\_}SPPULL,}
  2556. \textsf{{\{} misc. and floating point registers {\}}}
  2557. \textsf{R{\_}CCR,R{\_}FP0,R{\_}FP1,R{\_}FP2,R{\_}FP3,R{\_}FP4,R{\_}FP5,R{\_}FP6,}
  2558. \textsf{R{\_}FP7,R{\_}FPCR,R{\_}SR,R{\_}SSP,R{\_}DFC,R{\_}SFC,R{\_}VBR,R{\_}FPSR,}
  2559. \textsf{{\{} other - not used {\}}}
  2560. \textsf{R{\_}DEFAULT{\_}SEG}
  2561. \textsf{);}
  2562. \subsubsection{integer registers}
  2563. \label{subsubsec:integer}
  2564. \textsf{intregs: array[1..maxintregs] of tregister;}
  2565. General purpose registers which can contain any data, usually integer
  2566. values. These can also be used, when no floating point coprocessor is
  2567. present, to hold values for floating point operations.
  2568. \subsubsection{address registers}
  2569. \label{subsubsec:address}
  2570. \textsf{addrregs: array[1..maxaddrregs] of tregister;}
  2571. Registers which are used to construct assembler address expressions, usually
  2572. the address registers are used as the base registers in these assembler
  2573. expressions.
  2574. \subsubsection{fpu registers}
  2575. \label{subsubsec:mylabel25}
  2576. \textsf{fpuregs: array[1..maxfpuregs] of tregister;}
  2577. Hardware floating point registers. These registers must at least be able to
  2578. load and store IEEE DOUBLE floating point values, otherwise they cannot be
  2579. considered as FPU registers. Not available on systems with no floating point
  2580. coprocessor.
  2581. \subsubsection{scratch registers}
  2582. \label{subsubsec:scratch}
  2583. \textsf{scratch{\_}regs: array[1..maxscratchregs] of tregister;}
  2584. These registers are used as scratch, and can be used in assembler statement
  2585. in the pascal code, without being saved. They will always be valid across
  2586. routine calls. These registers are sometimes temporarily allocated inside
  2587. code generator nodes, and then immediately freed (always inside the same
  2588. routine).
  2589. \subsection{Special registers (cpubase.pas)}
  2590. \label{subsec:special}
  2591. The code generator has special uses for certain types of registers. These
  2592. special registers are of course CPU dependant, but as an indication, the
  2593. following sections explains the uses of these special registers and their
  2594. defines.
  2595. \subsubsection{stack{\_}pointer}
  2596. \label{subsubsec:stack}
  2597. \textsf{const stack{\_}pointer = R{\_}A7}
  2598. This represents the stack pointer, an address register pointing to the
  2599. allocated stack area.
  2600. \subsubsection{frame{\_}pointer}
  2601. \label{subsubsec:frame}
  2602. \textsf{const frame{\_}pointer = R{\_}A6}
  2603. This represents the frame register which is used to access values in the
  2604. stack. This is usually also an address register.
  2605. \subsubsection{self{\_}pointer}
  2606. \label{subsubsec:mylabel26}
  2607. \textsf{const self{\_}pointer = R{\_}A5}
  2608. This represents the self register, which represents a pointer to the current
  2609. instance of a class or object.
  2610. \subsubsection{accumulator}
  2611. \label{subsubsec:accumulatorents}
  2612. \textsf{const accumulator = R{\_}D0}
  2613. The accumulator is used (except in the i386) as a scratch register, and also
  2614. for return value in functions (in the case where they are 32-bit or less).
  2615. In the case it is a 64-bit value (and the target processor only supports
  2616. 32-bit registers) , the result of the routine is stored in the accumulator
  2617. for the low 32-bit value, and in the scratch register
  2618. (\textsf{scratch{\_}register}) for the high 32-bit value.
  2619. \subsubsection{scratch register}
  2620. \label{subsubsec:mylabel27}
  2621. \textsf{const scratch{\_}reg = R{\_}D1}
  2622. This register is used in special circumstances by the code generator. It is
  2623. simply a define to one of the registers in the \textsf{scratch{\_}regs
  2624. }array.
  2625. \subsection{Instructions}
  2626. \label{subsec:instructionsr}
  2627. \subsection{Reference subsystem}
  2628. \label{subsec:reference}
  2629. \subsubsection{Architecture}
  2630. \label{subsubsec:architecturebsysteme}
  2631. As described before in the locations section, one of the possible locations
  2632. for an operand is a memory location, which is described in a special
  2633. structure \textsf{treference} (described earlier). This subsection describes
  2634. the interface available by the code generator for allocation and freeing
  2635. reference locations.
  2636. \subsubsection{Code generator interface}
  2637. \label{subsubsec:mylabel28}
  2638. \lstinline!Function NewReference(Const R : TReference) : pReference;!
  2639. \begin{procedure}{DisposeReference}
  2640. \Declaration
  2641. Procedure DisposeReference(Var R : pReference);
  2642. \Description
  2643. Disposes of the reference \textsf{R} and sets r to \textsf{NIL}
  2644. \Notes
  2645. Does not verify if \textsf{R} is assigned first.
  2646. \end{procedure}
  2647. \begin{function}{NewReference}
  2648. \Declaration
  2649. function NewReference(Const R : TReference) : pReference;
  2650. \Description
  2651. Allocates in the heap a copy of the reference \textsf{r} and returns that
  2652. allocated pointer.
  2653. \end{function}
  2654. \begin{functionl}{Del{\_}Reference}{delreference}
  2655. \Declaration
  2656. procedure Del{\_}Reference(Const Ref : tReference);
  2657. \Description
  2658. Free up all address registers allocated in this reference for the index and
  2659. base (if required).
  2660. \Notes
  2661. Does not free the reference symbol if it exists.
  2662. \end{functionl}
  2663. \begin{functionl}{New{\_}Reference}{resetreference}
  2664. \Declaration
  2665. Function New{\_}Reference(Base : TRegister;Offset : Longint) : PReference;
  2666. \Description
  2667. Allocates a reference pointer, clears all the fields to zero, and sets the
  2668. offset to the offset field and the base to the base fields of the newly
  2669. allocated reference. Returns this newly allocated reference.
  2670. \end{functionl}
  2671. \begin{procedurel}{Reset{\_}Reference}{resetreference}
  2672. \Declaration
  2673. procedure Reset{\_}Reference(var ref : treference);
  2674. \Description
  2675. Clears all fields of the reference.
  2676. \end{procedurel}
  2677. \subsection{The register allocator subsystem}
  2678. \label{subsec:mylabel7}
  2679. \subsubsection{Architecture}
  2680. \label{subsubsec:architecture}
  2681. This system allocates and deallocates registers, from a pool of free
  2682. registers. Each time the code generator requires a register for generating
  2683. assembler instructions, it either calls the register allocator subsystem to
  2684. get a free register or directly uses the scratch registers (which are never
  2685. allocated in a pool except in the optimization phases of the compiler).
  2686. The code generator when no longer referencing the register should deallocate
  2687. it so it can be used once again.
  2688. \subsubsection{Code generator interface (tgen.pas)}
  2689. \label{subsubsec:mylabel29}
  2690. The following interface routines are used by the code generator to allocate
  2691. and deallocate registers from the different register pools available to code
  2692. generator.
  2693. \paragraph{General purpose registers}
  2694. \begin{function}{GetRegister32}
  2695. \Declaration
  2696. function GetRegister32 : tregister;
  2697. \Description
  2698. Allocates and returns a general purpose (integer) register which can be used
  2699. in the code generator. The register, when no longer used should be
  2700. deallocated with ungetregister32() or ungetregister()
  2701. \Notes
  2702. On non 32-bit machines, this routine should return the normal register for
  2703. this machine (eg : 64-bit machines will alloate and return a 64-bit
  2704. register).
  2705. \end{function}
  2706. \begin{procedure}{GetRegisterPair}
  2707. \Declaration
  2708. procedure GetRegisterPair(var low, high : TRegister);
  2709. \Description
  2710. Returns a register pair to be used by the code generator when accessing
  2711. 64-bit values on 32-bit wide register machines.
  2712. \Notes
  2713. On machines which support 64-bit registers naturally, this routine should
  2714. never be used, it is intended for 32-bit machines only.par Some machines
  2715. support 64-bit integer operations using register 32-bit pairs in hardware,
  2716. but the allocated registers must be specific, this routine is here to
  2717. support these architectures.
  2718. \end{procedure}
  2719. \begin{procedure}{UngetRegister32}
  2720. \Declaration
  2721. Procedure UnGetRegister32(R : TRegister);
  2722. \Description
  2723. Deallocates a general purpose register which was previously allocated with
  2724. \seef{GetRegister32}().
  2725. \end{procedure}
  2726. \paragraph{Floating point registers}
  2727. \begin{function}{GetFloatRegister}
  2728. \Declaration
  2729. Function GetFloatRegister : tregister;
  2730. \Description
  2731. Allocates and returns a floating point register which can be used in the
  2732. code generator. The register, when no longer used should be deallocated with
  2733. ungetregister(). The register returned is a true floating point register (if
  2734. supported).
  2735. \Notes
  2736. This routine should only be used when floating point hardware is present in
  2737. the system. For emulation of floating point, the general purpose register
  2738. allocator / deallocator routines should be used instead.
  2739. \end{function}
  2740. \begin{function}{IsFloatsRegister}
  2741. \Declaration
  2742. Function IsFloatsRegister(R : TRegister): Boolean;
  2743. \Description
  2744. Returns TRUE if the register r is actually a floating point register,
  2745. otherwise returns FALSE. This is used when the location is LOC{\_}FPU on
  2746. machines which do not support true floating point registers.
  2747. \end{function}
  2748. \paragraph{Address registers}
  2749. \begin{function}{GetAdressReg}
  2750. \Declaration
  2751. Function GetAddressReg : tregister;
  2752. \Description
  2753. Allocates and returns an address register which can be used for address
  2754. related opcodes in the code generator. The register, when no longer used
  2755. should be deallocated with ungetregister()
  2756. \Notes
  2757. If there is no distinction between address registers, and general purpose
  2758. register in the architecture, this routine may simply call and return the
  2759. getregister32() result.
  2760. \end{function}
  2761. \begin{function}{IsAddressRegister}
  2762. \Declaration
  2763. Function IsAddressRegister(r : TRegister): Boolean;
  2764. \Description
  2765. Returns TRUE if the register r is actually an address register, otherwise
  2766. returns FALSE.
  2767. \Notes
  2768. If there is no distinction between address registers, and general purpose
  2769. register in the architecture, this routine may simply verify if this is a
  2770. general purpose register and return TRUE in that case.
  2771. \end{function}
  2772. \paragraph{Generic}
  2773. \begin{procedure}{UngetRegister}
  2774. \Declaration
  2775. procedure UngetRegister(r : TRegister);
  2776. \Description
  2777. Deallocates any register which was previously allocated with any of the
  2778. allocation register routines.
  2779. \end{procedure}
  2780. \begin{function}{SaveUsedRegisters}
  2781. \Declaration
  2782. procedure SaveUsedRegisters(var Saved : TSaved; ToSave: TRegisterset);
  2783. \Description
  2784. Saves in a temporary location all specified registers. On stack based
  2785. machines the registers are saved on the stack, otherwise they are saved in a
  2786. temporary memory location. The registers which were saved are stored in the
  2787. \textsf{saved} variable. The constant \textsf{ALL{\_}REGISTERS} passed to
  2788. the \textsf{tosave} parameter indicates to save all used registers.
  2789. \end{function}
  2790. \begin{function}{RestoreUsedRegisters}
  2791. \Declaration
  2792. procedure restoreusedregisters(Saved : TSaved);
  2793. \Description
  2794. Restores all saved registers from the stack (or a temporary memory
  2795. location). Free any temporary memory space allocated, if necessary.
  2796. \end{function}
  2797. \paragraph{Debugging}
  2798. \begin{function}{GetExplicitRegister32}
  2799. \Declaration
  2800. Function GetExplicitRegister32(r : tregister): tregister;
  2801. \Description
  2802. This routine allocates specifically the specified register \textsf{r }and
  2803. returns that register. The register to allocate can only be one of the
  2804. scratch registers.
  2805. \Notes
  2806. This routine is used for debugging purposes only. It should be used in
  2807. conjunctions with ungetregister32() to explicitly allocate and deallocate a
  2808. scratch register.
  2809. \end{function}
  2810. \subsection{Temporary memory allocator subsystem}
  2811. \label{subsec:temporary}
  2812. \subsubsection{Architecture}
  2813. \label{subsubsec:architecturemory}
  2814. Sometimes it is necessary to reserve temporary memory locations on the stack
  2815. to store intermediate results of statements. This is done by the temporary
  2816. management module.
  2817. Since entry and exit code for routines are added after the code for the
  2818. statements in the routine have been generated, temporary memory allocation
  2819. can be used `on the fly' in the case where temporary memory values are
  2820. required in the code generation phase of the routines being compiled. After
  2821. usage, the temporary memory space should be freed, so it can be reused if
  2822. necessary.
  2823. The temporary memory allocation is a linked list of entries containing
  2824. information where to access the data via a negative offset from the
  2825. frame{\_}pointer register. The linked list is only valid when compiling and
  2826. generating the code for the procedure bodies; it is reset and cleared each
  2827. time a new routine is compiled. There are currently three different types of
  2828. memory spaces in use : volatile (\textsf{tt{\_}normal}) which can be
  2829. allocated and freed any time in the procedure body, ansistring, which is
  2830. currently the same as volatile, except it only stored references to
  2831. ansistring's, and persistent (\textsf{tt{\_}persistent}) which are memory
  2832. blocks which are reserved throughout the routine duration; persistent
  2833. allocated space can never be reused in a procedure body, unless explicitly
  2834. released.
  2835. The temporary memory allocator guarantees to allocate memory space on the
  2836. stack at least on a 16-bit alignment boundary. The exact alignment depends
  2837. on the operating system required alignment.
  2838. \subsubsection{Temporary memory allocator interface (temp{\_}gen.pas)}
  2839. \label{subsubsec:temporary}
  2840. \paragraph{volatile / ansistring memory}
  2841. \begin{function}{GetTempOfSize}
  2842. \Declaration
  2843. function GetTempOfSize(Size : Longint) : Longint;
  2844. \Description
  2845. Allocates at least \textsf{size} bytes of temporary volatile memory on the
  2846. stack. The return value is the negative offset from the frame pointer where
  2847. this memory was allocated.
  2848. \Notes
  2849. The return offset always has the required alignment for the target system,
  2850. and can be used as an offset from the frame{\_}pointer to access the
  2851. temporary space.
  2852. \end{function}
  2853. \begin{procedure}{GetTempOfSizeReference}
  2854. \Declaration
  2855. procedure GetTempOfSizeReference(l : Longint;Var Ref : TReference);
  2856. \Description
  2857. This routine is used to assign and allocate extra temporary volatile memory
  2858. space on the stack from a reference. \textsf{l} is the size of the
  2859. persistent memory space to allocate, while \textsf{ref} is a reference entry
  2860. which will be set to the correct offset from the frame{\_}pointer register
  2861. base. The \textsf{offset} and \textsf{base} fields of \textsf{ref} will be
  2862. set appropriately in this routine, and can be considered valid on exit of
  2863. this routine.
  2864. \Notes
  2865. The return offset always has the required alignment for the target system.
  2866. \end{procedure}
  2867. \begin{procedure}{UnGetIfTemp}
  2868. \Declaration
  2869. procedure UnGetIfTemp(const ref : treference);
  2870. \Description
  2871. Frees a reference \textsf{ref} which was allocated in the volatile temporary
  2872. memory space.
  2873. \Notes
  2874. The freed space can later be reallocated and reused.
  2875. \end{procedure}
  2876. \begin{procedure}{GetTempAnsiStringReference}
  2877. \Declaration
  2878. procedure GetTempAnsiStringReference(Var Ref : TReference);
  2879. \Description
  2880. Allocates \textsf{ref }on the volatile memory space and sets the
  2881. \textsf{base} to the frame{\_}pointer register and \textsf{offset} to the
  2882. correct offset to access this allocated memory space.
  2883. \Notes
  2884. The return offset always has the required alignment for the target system.
  2885. \end{procedure}
  2886. \paragraph{persistent memory}
  2887. \begin{function}{GetTempOfSizePersistant}
  2888. \Declaration
  2889. function GetTempOfSizePersistant(Size : Longint) :Longint;
  2890. \Description
  2891. Allocates persistent storage space on the stack. return value is the
  2892. negative offset from the frame pointer where this memory was allocated.
  2893. \Notes
  2894. The return offset always has the required alignment for the target system.
  2895. \end{function}
  2896. \begin{function}{UngetPersistantTemp}
  2897. \Declaration
  2898. procedure UngetPersistantTemp(Pos : Longint);
  2899. \Description
  2900. Frees space allocated as being persistent. This persistent space can then
  2901. later be used and reallocated. \textsf{pos} is the offset relative to the
  2902. frame{\_}pointer of the persistent memory block to free.
  2903. \end{function}
  2904. \paragraph{utility routines}
  2905. \begin{procedure}{ResetTempGen}
  2906. \Declaration
  2907. procedure ResetTempGen;
  2908. \Description
  2909. Clear and free the complete linked list of temporary memory locations. The
  2910. list is set to nil.
  2911. \Notes
  2912. This routine is called each time a routine has been fully compiled.
  2913. \end{procedure}
  2914. \begin{procedure}{SetFirstTemp}
  2915. \Declaration
  2916. procedure SetFirstTemp(l : Longint);
  2917. \Description
  2918. This routine sets the start of the temporary local area (this value is a
  2919. negative offset from the frame{\_}pointer, which is located after the local
  2920. variables). Usually the start offset is the size of the local variables,
  2921. modified by any alignment requirements.
  2922. \Notes
  2923. This routine is called once before compiling a routine, it indicates the
  2924. start address where to allocate temporary memory space.
  2925. \end{procedure}
  2926. \begin{function}{GetFirstTempSize}
  2927. \Declaration
  2928. function GetFirstTempSize : longint;
  2929. \Description
  2930. Returns the total number of bytes allocated for local and temporary
  2931. allocated stack space. This value is aligned according to the target system
  2932. alignment requirements, even if the actual size is not aligned.
  2933. \Notes
  2934. This routine is used by the code generator to get the total number of bytes
  2935. to allocate locally (i.e the stackframe size) in the entry and exit code of
  2936. the routine being compiled.
  2937. \end{function}
  2938. \begin{function}{NormalTempToPersistant}
  2939. \Declaration
  2940. Procedure NormalTempToPersistant(Pos : Longint);
  2941. \Description
  2942. Searches the list of currently temporary memory allocated for the one with
  2943. the offset \textsf{pos, }and if found converts this temporary memory space
  2944. as persistent (can never be freed and reallocated).
  2945. \end{function}
  2946. \begin{function}{PersistantTempToNormal}
  2947. \Declaration
  2948. Procedure PersistantTempToNormal(Pos : Longint);
  2949. \Description
  2950. Searches the list of currently allocated persistent memory space as the
  2951. specified address \textsf{pos }, and if found converts this memory space to
  2952. normal volatile memory space which can be freed and reused.
  2953. \end{function}
  2954. \begin{function}{IsTemp}
  2955. \Declaration
  2956. function IsTemp(const Ref : TReference): boolean;
  2957. \Description
  2958. Returns TRUE if the reference \textsf{ref }is allocated in temporary
  2959. volatile memory space, otherwise returns FALSE.
  2960. \end{function}
  2961. \subsection{Assembler generation}
  2962. \label{subsec:mylabel8}
  2963. \subsubsection{Architecture}
  2964. \label{subsubsec:architectureneration}
  2965. The different architectures on the market today only support certain types
  2966. of operands as assembler instructions. The typical format of an assembler
  2967. instruction has the following format:
  2968. \begin{center}
  2969. \textsf{OPCODE [opr1,opr2[,opr3][\ldots ]]}
  2970. \end{center}
  2971. The opcode field is a mnemonic for a specific assembler instruction, such as
  2972. \textsf{MOV} on the 80x86, or \textsf{ADDX} on the 680x0. Furthermore, in
  2973. most cases, this mnemonic is followed by zero to three operands which can be
  2974. of the following types:
  2975. Possible Operand Types
  2976. \begin{itemize}
  2977. \item a LABEL or SYMBOL (to code or data)
  2978. \item a REGISTER (one of the predefined hardware registers)
  2979. \item a CONSTANT (an immediate value)
  2980. \item a MEMORY EXPRESSION (indirect addressing through offsets, symbols, and
  2981. address registers)
  2982. \end{itemize}
  2983. In the compiler, this concept of different operand types has been directly
  2984. defined for easier generation of assembler output. All opcodes generated by
  2985. the code generator are stored in a linked list of opcodes which contain
  2986. information on the operand types, The opcode and the size (which is
  2987. important to determine on what size the operand must be operated on) are
  2988. stored in that linked list.
  2989. The possible operand sizes for the code generator are as follows (a
  2990. enumeration of type \textsf{topsize}):
  2991. \begin{longtable}{|l|p{10cm}|}
  2992. \hline
  2993. Operand size enum (\textsf{topsize}) & Description \\
  2994. \hline
  2995. \endhead
  2996. \hline
  2997. \endfoot
  2998. \textsf{S{\_}B}& 8-bit integer operand \\
  2999. \textsf{S{\_}W}& 16-bit integer operand \\
  3000. \textsf{S{\_}L}& 32-bit integer operand \\
  3001. \textsf{S{\_}Q}& 64-bit integer operand \\
  3002. \textsf{S{\_}FS}& 32-bit IEEE 754 Single floating point operand \\
  3003. \textsf{S{\_}FL}& 64-bit IEEE 754 Double floating point operand \\
  3004. \textsf{S{\_}FX}& Extended point floating point operand (cpu-specific) \\
  3005. \textsf{S{\_}CPU}& A constant equal to one of the previous sizes (natural size of operands) \\
  3006. \end{longtable}
  3007. The possible operand types for the code generator are as follows (other
  3008. might be added as required by the target architecture):
  3009. \begin{longtable}{|l|p{10cm}|}
  3010. \hline
  3011. Operand type (\textsf{toptype}) & Description \\
  3012. \hline
  3013. \endhead
  3014. \hline
  3015. \endfoot
  3016. \textsf{top{\_}none}& No operand \\
  3017. \textsf{top{\_}reg}& Operand is a register \\
  3018. \textsf{top{\_}ref}& Operand is a reference (\textsf{treference} type) \\
  3019. \textsf{top{\_}symbol}& Operand is a symbol (reference or label) \\
  3020. \end{longtable}
  3021. The architecture specific opcodes are done in an enumeration of type
  3022. \textsf{tasmop}. An example of an enumeration for some of the opcodes of the
  3023. PowerPC 32-bit architecture is as follows:
  3024. \begin{lstlisting}{}
  3025. type tasmop = (a_add, a_add_, a_addo, a_addo_, a_addc, a_addc_, a_addco,
  3026. a_addco_,a_adde, a_adde_, a_addeo, a_addeo_, a_addi,
  3027. a_addic, a_addic_, a_addis \ldots
  3028. \end{lstlisting}
  3029. \subsubsection{Generic instruction generation interface}
  3030. \label{subsubsec:generic}
  3031. To independently generate code for different architectures, wrappers for the
  3032. most used instructions in the code generator have been created which are
  3033. totally independent of the target system.
  3034. \paragraph{Load / store instructions}
  3035. \begin{procedurel}{Emit\_Load\_Loc\_Reg}{EmitLoadLocReg}
  3036. \Declaration
  3037. Procedure Emit{\_}Load{\_}Loc{\_}Reg(src:tlocation;srcdef:pdef; dstdef : pdef; dst : tregister);
  3038. \Description
  3039. Loads an operand from the source location in \textsf{src }into the
  3040. destination register \textsf{dst }taking into account the source definition
  3041. and destination definition (sign-extension, zero extension depending on the
  3042. sign and size of the operands).
  3043. \Notes
  3044. The source location can only be in LOC{\_}REGISTER, LOC{\_}CREGISTER,
  3045. LOC{\_}MEM or LOC{\_}REFERENCE otherwise an internal error will occur. This
  3046. generic opcode does not work on floating point values, only integer values.
  3047. \end{procedurel}
  3048. \begin{procedure}{FloatLoad}
  3049. \Declaration
  3050. procedure FloatLoad(t : tFloatType;Ref : TReference; Var Location:TLocation);
  3051. \Description
  3052. This routine is to be called each time a location must be set to LOC{\_}FPU
  3053. and a value loaded into a FPU register
  3054. \Notes
  3055. The routine sets up the register field of LOC{\_}FPU correctly. The source
  3056. location can only be : LOC{\_}MEM or LOC{\_}REFERENCE. The destination
  3057. location is set to LOC{\_}FPU.
  3058. \end{procedure}
  3059. \begin{function}{FloatStore}
  3060. \Declaration
  3061. procedure FloatStore(t : TFloatType;var Location:TLocation; ref:TReference);
  3062. \Description
  3063. This routine is to be called when a value located in LOC{\_}FPU must be
  3064. stored into memory.
  3065. \Notes
  3066. The destination must be LOC{\_}REFERENCE or LOC{\_}MEM. This routine frees
  3067. the LOC{\_}FPU location \\
  3068. \end{function}
  3069. \begin{functionl}{emit{\_}mov{\_}ref{\_}reg64}{emitmovrefreg64}
  3070. \Declaration
  3071. Procedure Emit{\_}Mov{\_}Ref{\_}Reg64(r : TReference;rl,rh : TRegister);
  3072. \Description
  3073. This routine moves a 64-bit integer value stored in memory location
  3074. \textsf{r} into the low 32-bit register \textsf{rl} and the high 32-bit
  3075. register \textsf{rh}.
  3076. \end{functionl}
  3077. \paragraph{Load address}
  3078. \begin{functionl}{Emit{\_}Lea{\_}Loc{\_}Ref}{emitlealocref}
  3079. \Declaration
  3080. procedure Emit{\_}Lea{\_}Loc{\_}Ref(const t:TLocation;Const Ref:TReference; FreeTemp:Boolean);
  3081. \Description
  3082. Loads the address of the location \textsf{loc }and stores the result into
  3083. \textsf{ref}
  3084. \Notes
  3085. The store address \textsf{ref }should point to an allocated area at least
  3086. \textsf{sizeof(pointer)} bytes, otherwise unexpected code might be
  3087. generated.
  3088. \end{functionl}
  3089. \begin{functionl}{Emit{\_}Lea{\_}Loc{\_}Reg}{Emitlealocreg}
  3090. \Declaration
  3091. Procedure Emit{\_}Lea{\_}Loc{\_}Reg(const t:TLocation;Reg:TRegister;Freetemp:Boolean);
  3092. \Description
  3093. Loads the address of the location \textsf{loc }and stores the result into
  3094. ther target register \textsf{reg}
  3095. \end{functionl}
  3096. \paragraph{Label instructions}
  3097. \begin{procedure}{GetLabel}
  3098. \Declaration
  3099. procedure GetLabel(var l : pAsmLabel);
  3100. \Description
  3101. Returns a label associated with code. This label can then be used with the
  3102. instructions output by the code generator using the instruction generation
  3103. templates which require labels as parameters. The label itself can be
  3104. emitted to the assembler source by calling the \textsf{emitlab} routine.
  3105. \end{procedure}
  3106. \begin{procedure}{EmitLab}
  3107. \Declaration
  3108. procedure EmitLab(var l : pasmlabel);
  3109. \Description
  3110. Output the label \textsf{l }to the assembler instruction stream.
  3111. \Notes
  3112. The label should have been previously allocated with \textsf{getlabel.} The
  3113. output label will be of the form label: in the instruction stream. This
  3114. label is usually a jump target.
  3115. \end{procedure}
  3116. \begin{procedure}{EmitLabeled}
  3117. \Declaration
  3118. procedure EmitLabeled(op : tasmop; var l : pasmlabel);
  3119. \Description
  3120. Output the opcode \textsf{op }with the operand \textsf{l}
  3121. which is a previously allocated label.
  3122. \Notes
  3123. This routine is used to output jump instructions such as : jmp label, jne
  3124. label. The label should have been previously allocated with a call to
  3125. \textsf{getlabel}
  3126. \end{procedure}
  3127. \paragraph{Other instructions}
  3128. \begin{function}{EmitCall}
  3129. \Declaration
  3130. procedure EmitCall(const routine:string);
  3131. \Description
  3132. Emit a call instruction to an internal routine
  3133. \Parameters
  3134. routine = The name of the routine to call.
  3135. \end{function}
  3136. \begin{procedure}{ConcatCopy}
  3137. \Declaration
  3138. procedure ConcatCopy(Source,Dest : TReference;Size : Longint;DelSource : Boolean; loadref:boolean);
  3139. \Description
  3140. This routine copies \textsf{size} data from the \textsf{source} reference to the destination \textsf{dest} reference. \\
  3141. \Parameters
  3142. source = Source reference to copy from \par
  3143. dest = Depending on the value of loadref, either indicates a location where a
  3144. pointer to the data to copy is stored, or this reference directly the address
  3145. to copy to. \par
  3146. size = Number of bytes to copy \par delsource = TRUE if the source reference
  3147. should be freed in this routine \par loadref = TRUE if the source reference
  3148. contains a pointer to the address we wish to copy to, otherwise the reference
  3149. itself is the destination location to copy to.
  3150. \end{procedure}
  3151. \begin{procedurel}{Emit{\_}Flag2Reg}{emitflag2reg}
  3152. \Declaration
  3153. procedure Emit{\_}Flag2Reg(Flag:TResflags;HRegister:TRegister);
  3154. \Description
  3155. Sets the value of the register to 1 if the condition code flag in \textsf{flag}
  3156. is TRUE, otherwise sets the register to zero.
  3157. \Notes
  3158. The operand should be zero extended to the natural register size for the
  3159. target architecture.
  3160. \end{procedurel}
  3161. \subsubsection{Instruction generation interface}
  3162. \label{subsubsec:instruction}
  3163. \section{The assembler output}
  3164. \label{sec:mylabel8}
  3165. All code is generated via special linked lists of instructions. The base of
  3166. this is a special object, an abstract assembler which implements all
  3167. directives which are usually implemented in the different assemblers
  3168. available on the market . When the code generator and parser generates the
  3169. final output, it is generated as a linked list for each of the sections
  3170. available for the output assembler. Each entry in the linked list is either
  3171. an instruction, or one of the abstract directives for the assembler.
  3172. \begin{figure}
  3173. \ifpdf
  3174. \includegraphics{arch11.pdf}
  3175. %\epsfig{file=arch11.png,width=\textwidth}
  3176. \else
  3177. \includegraphics[width=5.67in,height=2.17in]{arch11.eps}
  3178. \fi
  3179. \label{fig11}
  3180. \caption{Assembler generation organisation}
  3181. \end{figure}
  3182. % FIXME
  3183. % If I don't do this, the assembler node table has a problem.
  3184. \clearpage
  3185. The different possible sections which are output are as follows:
  3186. \begin{center}
  3187. Section lists for the assembler output
  3188. \end{center}
  3189. \begin{longtable}{|l|p{10cm}|}
  3190. \hline
  3191. Internal section name & Description \\
  3192. \hline
  3193. \endhead
  3194. \hline
  3195. \endfoot
  3196. exparasmlist & temporary list \\
  3197. datasegment & initialized variables \\
  3198. codesegment & instructions and general code directives \\
  3199. debuglist & debugging information \\
  3200. withdebuglist & ??????????????? \\
  3201. consts & read only constants \\
  3202. importsection & imported symbols \\
  3203. exportsection & exported symbols \\
  3204. resourcesection & Resource data \\
  3205. rttilist & runtime type information data \\
  3206. resourcestringlist& resource string data
  3207. \end{longtable}
  3208. The following directives for the abstract assembler currently exist:
  3209. Abstract assembler node types:
  3210. \begin{longtable}{|l|p{10cm}|}
  3211. \hline
  3212. Node entry Type & Description \\
  3213. \hline
  3214. \endhead
  3215. \hline
  3216. \endfoot
  3217. ait{\_}none&
  3218. This entry in the linked list is invalid (this should
  3219. normally never occur) \\
  3220. ait{\_}direct&
  3221. Direct output to the resulting assembler file (as string) \\
  3222. ait{\_}string&
  3223. Shortstring with a predefined length \\
  3224. ait{\_}label&
  3225. Numbered assembler label used for jumps \\
  3226. ait{\_}comment&
  3227. Assembler output comment \\
  3228. ait{\_}instruction&
  3229. Processor specific instruction \\
  3230. ait{\_}datablock&
  3231. Unitialized data block (BSS) \\
  3232. ait{\_}symbol&
  3233. Entry represents a symbol (exported, imported, or other public
  3234. symbol type) \newline
  3235. Possible symbol types : NONE, EXTERNAL, LOCAL and GLOBAL \newline
  3236. eg : A symbol followed by an ait{\_}const{\_}32bit \\
  3237. ait{\_}symbol{\_}end &
  3238. Symbol end (for example the end of a routine) \\
  3239. ait{\_}const{\_}32bit&
  3240. Initialized 32-bit constant (without a symbol) \\
  3241. ait{\_}const{\_}16bit&
  3242. Initialized 16-bit constant (without a symbol) \\
  3243. ait{\_}const{\_}8bit&
  3244. Initialized 8-bit constant (without a symbol) \\
  3245. ait{\_}const{\_}symbol & ???????????? \\
  3246. ait{\_}real{\_}80bit (x86)&
  3247. Initialized 80-bit floating point constant (without symbol) \\
  3248. ait{\_}real{\_}64bit&
  3249. Initialized Double IEEE floating point constant (without symbol) \\
  3250. ait{\_}real{\_}32bit&
  3251. Initialized Single IEEE floating point constant (without symbol) \\
  3252. ait{\_}comp{\_}64bit (x86)&
  3253. Initialized 64-bit floating point integer (without symbol) \\
  3254. ait{\_}align&
  3255. Alignment directive \\
  3256. ait{\_}section&
  3257. Section directive \\
  3258. ait{\_}const{\_}rva (Win32)& \\
  3259. ait{\_}stabn &
  3260. stabs debugging information (numerical value) \\
  3261. ait{\_}stabs &
  3262. stabs debugging information (string) \\
  3263. ait{\_}force{\_}line&
  3264. stabs debugging line information \\
  3265. ait{\_}stab{\_}function{\_}name&
  3266. stabs debug information routine name \\
  3267. ait{\_}cut&
  3268. Cut in the assembler files (used for smartlinking) \\
  3269. ait{\_}regalloc&
  3270. Debugging information for the register allocator \\
  3271. ait{\_}marker & ???????????? \\
  3272. ait{\_}frame (Alpha)& \\
  3273. ait{\_}ent (Alpha)& \\
  3274. ait{\_}labeled{\_}instruction (m68k)& \\
  3275. ait{\_}dummy & Unused - should never appear
  3276. \end{longtable}
  3277. \section{The Runtime library}
  3278. \label{sec:mylabel9}
  3279. This section describes the requirements of the internal routines which MUST
  3280. be implemented for all relevant platforms to port the system unit to a new
  3281. architecture or operating system.
  3282. The following defines are available when compiling the runtime library:
  3283. \begin{longtable}{|l|p{10cm}|}
  3284. \hline
  3285. Define Name & Description \\
  3286. \hline
  3287. \endhead
  3288. \hline
  3289. \endfoot
  3290. i386 & Intel 80x86 family of processors (and compatibles) \\
  3291. m68k & Motorola 680x0 family of processors (excludes coldfire) \\
  3292. alpha & Alpha 21x64 family of processors \\
  3293. powerpc & Motorola / IBM 32-bit family of processors \\
  3294. sparc & SPARC v7 compatible processors
  3295. \end{longtable}
  3296. \begin{longtable}{|l|p{10cm}|}
  3297. \hline
  3298. Define name & Description \\
  3299. \hline
  3300. \endhead
  3301. \hline
  3302. \endfoot
  3303. RTLLITE&
  3304. Removes some extraneous routine from compilation (system unit
  3305. is minimal). Mvdv: Afaik the status of this is unknown \\
  3306. DEFAULT{\_}EXTENDED&
  3307. The runtime library routines dealing with fixed point values have the
  3308. \textsf{extended} type instead of the \textsf{real} type. \\
  3309. SUPPORT{\_}SINGLE&
  3310. The compiler supports the \textsf{single} floating point precision type \\
  3311. SUPPORT{\_}DOUBLE&
  3312. The compiler supports the \textsf{double }floating point precision type \\
  3313. SUPPORT{\_}EXTENDED&
  3314. The compiler supports the \textsf{extended }floating point
  3315. precision type \\
  3316. SUPPORT{\_}FIXED&
  3317. The compiler supports the \textsf{fixed} floating point precision type \\
  3318. HASWIDECHAR&
  3319. The compiler supported the \textsf{widechar} character type \\
  3320. INT64&
  3321. The compiler supports 64-bit integer operations \\
  3322. MAC{\_}LINEBREAK&
  3323. Text I/O uses Mac styled line break ({\#}13) instead of {\#}13{\#}10 \\
  3324. SHORT{\_}LINEBREAK&
  3325. Text I/O uses UNIX styled line breaks ({\#}10) instead of {\#}13{\#}10 \\
  3326. EOF{\_}CTRLZ&
  3327. A Ctrl-Z character in a text file is an EOF marker (UNIX mostly) \\
  3328. \end{longtable}
  3329. The following defines are used for fexpand definitions:
  3330. % FIXME Seem to miss a *nix symlink expand behaviour define.
  3331. \begin{longtable}{|l|p{10cm}|}
  3332. \hline
  3333. Define name & Description \\
  3334. \hline
  3335. \endhead
  3336. \hline
  3337. \endfoot
  3338. FPC{\_}EXPAND{\_}DRIVES&
  3339. Different devices with different names (as drives) are
  3340. supported \par (like DOS, Netware, etc\ldots ) \\
  3341. FPC{\_}EXPAND{\_}UNC&
  3342. Universal Naming convention support i.e \par $\backslash \backslash
  3343. < $server-name>$\backslash $<share-name>$\backslash $<directory/filename> \\
  3344. UNIX&
  3345. Unix style file names \\
  3346. FPC{\_}EXPAND{\_}VOLUMES&
  3347. Volume names (i.e. drive descriptions longer than 1
  3348. character) are supported. \\
  3349. FPC{\_}EXPAND{\_}TILDE&
  3350. Replaces the $\sim $ character, with the `HOME' directory
  3351. (mostly on UNIX platforms) \\
  3352. \end{longtable}
  3353. The following defines some debugging routines for the runtime library:
  3354. \begin{longtable}{|l|p{10cm}|}
  3355. \hline
  3356. Define Name & Description \\
  3357. \hline
  3358. \endhead
  3359. \hline
  3360. \endfoot
  3361. DEFINE NAME & Description \\
  3362. ANSISTRDEBUG & Add Debug routines for ansi string support \\
  3363. EXCDEBUG & Add Debug routines for exception debugging \\
  3364. LOGGING & Log the operations to a file \\
  3365. \end{longtable}
  3366. \subsection{Operating system hooks}
  3367. \label{subsec:operating}
  3368. This section contains information on all routines which should be hooked and
  3369. implemented to be able to compile and use the system unit for a new
  3370. operating system:
  3371. \begin{functionl}{System{\_}Exit}{systemexit}
  3372. \Declaration
  3373. procedure System{\_}Exit;
  3374. \Description
  3375. This routine is internally called by the system unit when the application
  3376. exits.
  3377. \Notes
  3378. This routine should actually exit the application. It should exit with the
  3379. error code specified in the \textsf{ExitCode} variable.
  3380. \Algorithm
  3381. Exit application with ExitCode value.
  3382. \end{functionl}
  3383. \begin{function}{ParamCount}
  3384. \Declaration
  3385. Function ParamCount : Longint;
  3386. \end{function}
  3387. \begin{procedure}{Randomize}
  3388. \Declaration
  3389. Procedure Randomize;
  3390. \Description
  3391. This routine initializes the built-in random generator with a random value.
  3392. \Notes
  3393. This routine is used by random
  3394. \Algorithm
  3395. Randseed := pseudo random 32-bit value
  3396. \end{procedure}
  3397. \begin{function}{GetHeapStart}
  3398. \Declaration
  3399. Function GetHeapStart : Pointer;
  3400. \Description
  3401. This routine should return a pointer to the start of the heap area.
  3402. \Notes
  3403. GetHeapStart := address of start of heap.
  3404. \end{function}
  3405. \begin{function}{GetHeapSize}
  3406. \Declaration
  3407. Function GetHeapSize : Longint;
  3408. \Description
  3409. This routine should return the total heap size in bytes
  3410. \Parameters
  3411. \Algorithm
  3412. GetHeapSize := total size of the initial heap area.
  3413. \end{function}
  3414. \begin{function}{sbrk}
  3415. \Declaration
  3416. Function sbrk(Size : Longint): Longint;
  3417. \Description
  3418. \end{function}
  3419. \begin{procedurel}{Do{\_}Close}{doclose}
  3420. \Declaration
  3421. Procedure Do{\_}Close(Handle : Longint);
  3422. \Description
  3423. This closes the file specified of the specified handle number.
  3424. \Parameters
  3425. handle = file handle of file to close
  3426. \Notes
  3427. This routine should close the specified file.
  3428. \end{procedurel}
  3429. \begin{functionl}{Do{\_}Erase}{doerase}
  3430. \Declaration
  3431. procedure Do{\_}Erase(p: pchar);
  3432. \Description
  3433. This erases the file specifed by p.
  3434. \Parameters
  3435. p = name of the file to erase
  3436. \Notes
  3437. \end{functionl}
  3438. The following variables should also be defined for each new operating
  3439. system, they are used by external units:
  3440. \noindent
  3441. argc : The number of command line arguments of the program
  3442. \noindent
  3443. argv : A pointer to each of the command line arguments (an array of pchar
  3444. pointers)
  3445. \subsection{CPU specific hooks}
  3446. \label{subsec:mylabel9}
  3447. The following routines must absolutely be implemented for each processor, as
  3448. they are dependent on the processor:
  3449. \subsubsection{FPC{\_}SETJMP}
  3450. \label{subsubsec:mylabel30}
  3451. \begin{function}{SetJmp}
  3452. \Declaration
  3453. function SetJmp (Var S : Jmp{\_}Buf) : Longint;
  3454. \Description
  3455. A call to SetJmp(), saves the calling environment in its \textsf{s} argument
  3456. for later use by \textsf{longjmp()}. Called by the code generator in
  3457. exception handling code. The return value should be zero.
  3458. \Notes
  3459. This routine should save / restore all used registers (except the
  3460. accumulator which should be cleared).
  3461. \end{function}
  3462. \subsubsection{FPC{\_}LONGJMP}
  3463. \label{subsubsec:mylabel31}
  3464. \subsubsection{function SPtr()}
  3465. \label{subsubsec:function}
  3466. \subsubsection{function Get{\_}Caller{\_}Frame(framebp:longint):longint;}
  3467. \label{subsubsec:mylabel32}
  3468. \subsubsection{function Get{\_}Caller{\_}Addr(framebp:longint):longint;}
  3469. \label{subsubsec:mylabel33}
  3470. \subsubsection{function Get{\_}Frame:longint;}
  3471. \label{subsubsec:mylabel34}
  3472. \subsubsection{function Trunc()}
  3473. \label{subsubsec:mylabel35}
  3474. \subsection{String related}
  3475. \label{subsec:string}
  3476. \subsubsection{FPC{\_}SHORTSTR{\_}COPY}
  3477. \label{subsubsec:mylabel36}
  3478. \begin{procedurel}{Int{\_}StrCopy}{intstrcopy}
  3479. \Declaration
  3480. Procedure Int{\_}StrCopy(len:longint;sstr,dstr:pointer);
  3481. \Description
  3482. This routine copies the string pointed to by the address in sstr, to the
  3483. string pointed in the destination. The old string is overwritten, and the
  3484. source string will be truncated to make it fit in destination if the length
  3485. of the source is greater then destination string len (the len parameter).
  3486. \Parameters
  3487. len = maximum length to copy (the destination string length) \par
  3488. sstr = pointer to source shortstring \par
  3489. dstr = point to destination shortstring
  3490. \Notes
  3491. Called by code generator when a string is assigned to another string.
  3492. \end{procedurel}
  3493. \subsubsection{FPC{\_}SHORTSTR{\_}COMPARE}
  3494. \label{subsubsec:mylabel37}
  3495. \begin{functionl}{Int{\_}StrCmp}{intstrcmp}
  3496. \Declaration
  3497. Function Int{\_}StrCmp(dstr,sstr:pointer) : longint;
  3498. \Description
  3499. The routine compares two shortstrings, and returns 0 if both are equal, 1 if
  3500. \textsf{dest} is greater then \textsf{src}, otherwise it returns --1.
  3501. \Notes
  3502. Both pointers must point to shortstrings. Length checking must be performed
  3503. in the routine.
  3504. \end{functionl}
  3505. \subsubsection{FPC{\_}SHORTSTR{\_}CONCAT}
  3506. \label{subsubsec:mylabel38}
  3507. \begin{procedurel}{Int{\_}StrConcat}{intstrconcat}
  3508. \Declaration
  3509. Procedure Int{\_}StrConcat(src,dest:pointer);
  3510. \Description
  3511. This routine appends the string pointed to by \textsf{src} to the end of the
  3512. string pointed to by \textsf{dest}.
  3513. \Parameters
  3514. src = pointer to shortstring to append to dest \par
  3515. dest = pointer to shortstring to receive appended string
  3516. \Notes
  3517. Both pointers must point to shortstrings. In the case where the src string
  3518. length does not fit in dest, it is truncated.
  3519. \Algorithm
  3520. \begin{lstlisting}{}
  3521. if src =nil or dest = nil then
  3522. exit routine;
  3523. if (src string length + dest string length) > 255 then
  3524. number of bytes to copy = 255 -- dest string length
  3525. else
  3526. number of bytes to copy = src string length;
  3527. copy the string data (except the length byte)
  3528. dest string length = dest string length + number of bytes to copied
  3529. \end{lstlisting}
  3530. \end{procedurel}
  3531. \subsubsection{FPC{\_}ANSISTR{\_}CONCAT}
  3532. \label{subsubsec:mylabel39}
  3533. \begin{procedurel}{AnsiStr{\_}Concat}{ansistrconcat}
  3534. \Declaration
  3535. Procedure AnsiStr{\_}Concat(s1,s2:Pointer;var s3:Pointer);
  3536. \Description
  3537. This routine appends \textsf{s1}+\textsf{s2} and stores the result at the
  3538. address pointed to by \textsf{s3}.
  3539. \Notes
  3540. All pointers must point to ansistrings.
  3541. \end{procedurel}
  3542. \subsubsection{FPC{\_}ANSISTR{\_}COMPARE}
  3543. \label{subsubsec:mylabel40}
  3544. \begin{functionl}{AnsiStr{\_}Compare}{ansistrcompare}
  3545. \Declaration
  3546. Function AnsiStr{\_}Compare(s1,s2 : Pointer): Longint;
  3547. \Description
  3548. The routine compares two ansistrings, and returns 0 if both are equal, 1 if
  3549. \textsf{s1} is greater then \textsf{s2}, otherwise it returns --1.
  3550. \Parameters
  3551. Both pointers must point to ansistrings.
  3552. \end{functionl}
  3553. \subsubsection{FPC{\_}ANSISTR{\_}INCR{\_}REF }
  3554. \label{subsubsec:mylabel41}
  3555. \begin{procedurel}{AnsiStr{\_}Incr{\_}Ref}{ansistrincrref}
  3556. \Declaration
  3557. procedure AnsiStr{\_}Incr{\_}Ref (var s : Pointer);
  3558. \Description
  3559. This routine simply increments the ANSI string reference count, which is
  3560. used for garbage collection of ANSI strings.
  3561. \Parameters
  3562. s = pointer to the ansi string (including the header structure)
  3563. \end{procedurel}
  3564. \subsubsection{FPC{\_}ANSISTR{\_}DECR{\_}REF }
  3565. \label{subsubsec:mylabel42}
  3566. \begin{procedurel}{AnsiStr{\_}Decr{\_}Ref}{ansistrdecrref}
  3567. \Declaration
  3568. procedure AnsiStr{\_}Decr{\_}Ref (Var S : Pointer);
  3569. \Parameters
  3570. s = pointer to the ansi string (including the header structure)
  3571. \Algorithm
  3572. Decreases the internal reference count of this non constant ansistring; If
  3573. the reference count is zero, the string is deallocated from the
  3574. heap.
  3575. \end{procedurel}
  3576. \subsubsection{FPC{\_}ANSISTR{\_}ASSIGN }
  3577. \label{subsubsec:mylabel43}
  3578. \begin{functionl}{AnsiStr{\_}Assign}{ansistrassign}
  3579. \Declaration
  3580. Procedure AnsiStr{\_}Assign (var s1 : Pointer;s2 : Pointer);
  3581. \Parameters
  3582. s1 = address of ANSI string to be assigned to \par
  3583. s2 = address of ANSI string which will be assigned
  3584. \Algorithm
  3585. Assigns S2 to S1 (S1:=S2), also by the time decreasing the reference count
  3586. to S1 (it is no longer used by this variable).
  3587. \end{functionl}
  3588. \subsubsection{FPC{\_}PCHAR{\_}TO{\_}SHORTSTR}
  3589. \label{subsubsec:mylabel44}
  3590. \begin{function}{StrPas}
  3591. \Declaration
  3592. Function StrPas(p:pChar):ShortString;
  3593. \Description
  3594. Copies and converts a null-terminated string (pchar) to a shortstring with
  3595. length checking.
  3596. \Parameters
  3597. p = pointer to null terminated string to copy
  3598. \Notes
  3599. Length checking is performed. Verifies also p=nil, and if so sets the
  3600. shortstring length to zero. Called by the type conversion generated code of
  3601. code generator.
  3602. \Algorithm
  3603. \begin{lstlisting}{}
  3604. if p=nil then
  3605. string length =0
  3606. else
  3607. string length =string length(p)
  3608. if string length>255 then
  3609. string length = 255
  3610. if string length>0 then
  3611. Copy all characters of pchar array to string (except length byte)
  3612. \end{lstlisting}
  3613. \end{function}
  3614. \subsubsection{FPC{\_}SHORTSTR{\_}TO{\_}ANSISTR}
  3615. \label{subsubsec:mylabel45}
  3616. \begin{functionl}{FPC{\_}ShortStr{\_}To{\_}AnsiStr}{fpcshortstrtoansistr}
  3617. \Notes
  3618. Called by the type conversion generated code of code generator.
  3619. \end{functionl}
  3620. \subsubsection{FPC{\_}STR{\_}TO{\_}CHARARRAY}
  3621. \label{subsubsec:mylabel46}
  3622. \begin{procedurel}{Str{\_}To{\_}CharArray}{strtochararray}
  3623. \Declaration
  3624. procedure Str{\_}To{\_}CharArray(StrTyp, ArraySize: Longint; src,dest: pChar);
  3625. \Description
  3626. Converts a string to a character array (currently supports both shortstring and ansistring types). Length checking is performed, and copies up to \textsf{arraysize} elements to dest.
  3627. \Parameters
  3628. strtyp = Indicates the conversion type to do (0 = shortstring, 1 =
  3629. ansistring, 2 = longstring, 3 = widestring) \\
  3630. arraysize = size of the destination array \par
  3631. src = pointer to source string \par
  3632. dest = pointer to character array
  3633. \Notes
  3634. Called by the type conversion generated code of code generator when
  3635. converting a string to an array of char. If the size of the string is less
  3636. then the size of the array, the rest of the array is filled with zeros.
  3637. \end{procedurel}
  3638. \subsubsection{FPC{\_}CHARARRAY{\_}TO{\_}SHORTSTR}
  3639. \label{subsubsec:mylabel47}
  3640. \begin{function}{StrCharArray}
  3641. \Declaration
  3642. Function StrCharArray(p:pChar; l : Longint):ShortString;
  3643. \Description
  3644. Copies a character array to a shortstring with length checking (upto 255
  3645. characters are copied)
  3646. \Parameters
  3647. p = Character array pointer \par
  3648. l = size of the array
  3649. \Notes
  3650. Called by the type conversion generated code of code generator when
  3651. converting an array of char to a shortstring.
  3652. \Algorithm
  3653. \begin{lstlisting}{}
  3654. if size of array >= 256 then
  3655. length of string =255
  3656. else
  3657. if size of array < 0 then
  3658. length of string = 0
  3659. else
  3660. length of string = size of array
  3661. Copy all characters from array to shortstring
  3662. \end{lstlisting}
  3663. \end{function}
  3664. \subsubsection{FPC{\_}CHARARRAY{\_}TO{\_}ANSISTR}
  3665. \label{subsubsec:mylabel48}
  3666. \begin{functionl}{Fpc{\_}Chararray{\_}To{\_}AnsiStr}{chararraytoansistr}
  3667. \Notes
  3668. Called by the type conversion generated code of code generator when converting an array of char to an ansistring.
  3669. \end{functionl}
  3670. \subsubsection{FPC{\_}CHAR{\_}TO{\_}ANSISTR}
  3671. \label{subsubsec:mylabel49}
  3672. \begin{functionl}{Fpc{\_}Char{\_}To{\_}AnsiStr}{fpcchartoansistr}
  3673. \Notes
  3674. Called by the type conversion generated code of code generator when
  3675. converting a char to an ansistring.
  3676. \end{functionl}
  3677. \subsubsection{FPC{\_}PCHAR{\_}TO{\_}ANSISTR}
  3678. \label{subsubsec:mylabel50}
  3679. \begin{functionl}{Fpc{\_}pChar{\_}To{\_}AnsiStr}{fpcpchartoansistr}
  3680. \Notes
  3681. Called by the type conversion generated code of code generator when
  3682. converting a pchar to an ansistring.
  3683. \end{functionl}
  3684. % maybe not necessary anymore (since the amount of tables decreased
  3685. % by "macrofying" the procedure definitions)
  3686. \ifpdf
  3687. \clearpage
  3688. \fi
  3689. \subsection{Compiler runtime checking}
  3690. \label{subsec:compiler}
  3691. \subsubsection{FPC{\_}STACKCHECK}
  3692. \label{subsubsec:mylabel51}
  3693. \begin{procedurel}{Int{\_}StackCheck}{intstackcheck}
  3694. \Declaration
  3695. procedure int{\_}stackcheck (stack{\_}size:longint;
  3696. \Description
  3697. This routine is used to check if there will be a stack overflow when trying
  3698. to allocate stack space from the operating system. The routine must preserve
  3699. all registers. In the case the stack limit is reached, the routine calls the
  3700. appropriate error handler.
  3701. \Parameters
  3702. stack{\_}size = The amount of stack we wish to allocate
  3703. \Notes
  3704. Inserted in the entry code of a routine in the {\{}{\$}S+{\}} state by the code generator
  3705. \Algorithm
  3706. \begin{lstlisting}{}
  3707. if ((StackPointer -- stack{\_}size) < System.StackLimit) then
  3708. Throw a Runtime error with error code 202 (stack overflow)
  3709. \end{lstlisting}
  3710. \end{procedurel}
  3711. \clearpage
  3712. \subsubsection{FPC{\_}RANGEERROR}
  3713. \label{subsubsec:mylabel52}
  3714. \begin{procedurel}{Int{\_}RangeError}{intrangerror}
  3715. \Declaration
  3716. procedure Int{\_}RangeError;
  3717. \Description
  3718. This routine is called when a range check error is detected when executing
  3719. the compiled code. This usually simply calls the default error handler, with
  3720. the correct runtime error code to produce.
  3721. \Parameters
  3722. Inserted in code generator when a Runtime error 201 {\{}{\$}R+{\}} should be
  3723. generated
  3724. \end{procedurel}
  3725. \subsubsection{FPC{\_}BOUNDCHECK}
  3726. \label{subsubsec:mylabel53}
  3727. \begin{procedurel}{Int{\_}BoundCheck}{intboundcheck}
  3728. \Declaration
  3729. procedure Int{\_}BoundCheck(l : Longint; Range : Pointer);
  3730. \Description
  3731. This routine is called at runtime in {\$}R+ mode to check if accessing
  3732. indexes in a string or array is out of bounds. In this case, the default
  3733. error handler is called, with the correct runtime error code to produce.
  3734. \Parameters
  3735. l = Index we need to check \par
  3736. range = pointer to a structure containing the minimum and maximum allowed
  3737. indexes (points to two 32-bit signed values which are the limits of the
  3738. array to verify).
  3739. \Notes
  3740. Inserted in the generated code after assignments, and array indexing to
  3741. verify if the result of operands is within range (in the {\{}{\$}R+{\}}
  3742. state)
  3743. \end{procedurel}
  3744. \subsubsection{FPC{\_}OVERFLOW}
  3745. \label{subsubsec:mylabel54}
  3746. \begin{procedurel}{Int{\_}OverFlow}{intoverflow}
  3747. \Declaration
  3748. procedure Int{\_}OverFlow;
  3749. \Description
  3750. This routine is called when an overflow is detected when executing the
  3751. compiled code. This usually simply calls the default error handler, with the
  3752. correct runtime error code to produce.
  3753. \Parameters
  3754. Inserted in code generator when a Runtime error 215 {\{}{\$}Q+{\}} should be
  3755. generated.
  3756. \end{procedurel}
  3757. \subsubsection{FPC{\_}CHECK{\_}OBJECT}
  3758. \label{subsubsec:mylabel55}
  3759. \begin{procedurel}{Int{\_}Check{\_}Object}{intcheckobject}
  3760. \Declaration
  3761. procedure Int{\_}Check{\_}Object(vmt : Pointer);
  3762. \Description
  3763. This routine is called at runtime in the {\$}R+ state each time a virtual
  3764. method is called. It verifies that the object constructor has been called
  3765. first to build the VMT of the object, otherwise it throws an Runtime error 210.
  3766. \Parameters
  3767. vmt = Current value of the SELF register
  3768. \Notes
  3769. Call inserted by the code generator before calling the virtual method. This
  3770. routine should save / restore all used registers.
  3771. \Algorithm
  3772. \begin{lstlisting}{}
  3773. if vmt = nil or size of method table =0 then
  3774. Throw a Runtime error with error code 210 (object not initialized)
  3775. \end{lstlisting}
  3776. \end{procedurel}
  3777. \subsubsection{FPC{\_}CHECK{\_}OBJECT{\_}EXT}
  3778. \label{subsubsec:mylabel56}
  3779. \begin{procedurel}{Int{\_}Check{\_}Object{\_}Ext}{intcheckobjectext}
  3780. \Declaration
  3781. procedure Int{\_}Check{\_}Object{\_}Ext(vmt, expvmt : pointer);
  3782. \Description
  3783. This routine is called at runtime when extended object checking is enabled (on the command line) and a virtual method is called. It verifies that the object constructor has been called first to build the VMT of the object, otherwise it throws an Runtime error 210, and furthermore it check that the object is actually a descendant of the parent object, otherwise it returns a Runtime error 220.
  3784. \Parameters
  3785. vmt = Current value of the SELF register \par
  3786. expvmt = Pointer to TRUE object definition
  3787. \Notes
  3788. Call inserted by the code generator before calling the virtual method. \par
  3789. This routine should save / restore all used registers.
  3790. \Algorithm
  3791. \begin{lstlisting}{}
  3792. if vmt = nil or size of method table =0 then
  3793. Throw a Runtime error with error code 210 (object not initialized)
  3794. Repeat
  3795. If SELF (VMT) <> VMT Address (expvmt) Then
  3796. Get Parent VMT Address
  3797. Else
  3798. Exit;
  3799. until no more ent;
  3800. Throw a Runtime error with error code 220 (Incorrect object reference)
  3801. \end{lstlisting}
  3802. \end{procedurel}
  3803. \subsubsection{FPC{\_}IO{\_}CHECK}
  3804. \label{subsubsec:mylabel57}
  3805. \begin{procedurel}{Int{\_}IOCheck}{intiocheck}
  3806. \Declaration
  3807. procedure Int{\_}IOCheck(addr : longint);
  3808. \Description
  3809. This routine is called after an I/O operation to verify the success of the
  3810. operation when the code is compiled in the {\$}I+ state.
  3811. \Parameters
  3812. addr = currently unused
  3813. \Algorithm
  3814. Check last I/O was successful, if not call error handler.
  3815. \end{procedurel}
  3816. \subsubsection{FPC{\_}HANDLEERROR}
  3817. \label{subsubsec:mylabel58}
  3818. \begin{procedure}{HandleError}
  3819. \Declaration
  3820. procedure HandleError (Errno : longint);
  3821. \Description
  3822. This routine should be called to generate a runtime error either from one of
  3823. the system unit routines or the code generator.
  3824. \Parameters
  3825. Errno = Runtime error to generate
  3826. \Notes
  3827. This routine calls the appropriate existing error handler with the specified
  3828. error code.
  3829. \Algorithm
  3830. \end{procedure}
  3831. \subsubsection{FPC{\_}ASSERT}
  3832. \label{subsubsec:mylabel59}
  3833. \begin{procedurel}{Int{\_}Assert}{intassert}
  3834. \Declaration
  3835. procedure Int{\_}Assert(Const Msg,FName:Shortstring;LineNo,ErrorAddr:Longint);
  3836. \Description
  3837. This routine is called by the code generator in an assert statement. When
  3838. the assertion fails, this routine is called.
  3839. \Parameters
  3840. msg = string to print \par
  3841. Fname = Current filename of source \par
  3842. LineNo = Current line number of source \par
  3843. ErrorAddr = Address of assertion failure
  3844. \end{procedurel}
  3845. \subsection{Exception handling}
  3846. \label{subsec:exception}
  3847. \subsubsection{FPC{\_}RAISEEXCEPTION}
  3848. \label{subsubsec:mylabel60}
  3849. \begin{function}{RaiseExcept}
  3850. \Declaration
  3851. function RaiseExcept (Obj : Tobject; AnAddr,AFrame : Pointer) : Tobject;
  3852. \Description
  3853. Called by the code generator in the raise statement to raise an exception.
  3854. \Parameters
  3855. Obj = Instance of class exception handler \par
  3856. AnAddr = Address of exception \par
  3857. Aframe = Exception frame address
  3858. \Notes
  3859. REGISTERS NOT SAVED???????????
  3860. \end{function}
  3861. \subsubsection{FPC{\_}PUSHEXCEPTADDR}
  3862. \label{subsubsec:mylabel61}
  3863. \begin{function}{PushExceptAddr}
  3864. \Declaration
  3865. function PushExceptAddr (Ft: Longint): PJmp{\_}buf ;
  3866. \Description
  3867. This routine should be called to save the current caller context to be used
  3868. for exception handling, usually called in the context where ANSI strings are
  3869. used (they can raise exceptions), or in a try..finally or on statements to
  3870. save the current context.
  3871. \Parameters
  3872. Ft = Indicates the frame type on the stack (1= Exception frame or 2=Finalize
  3873. frame)
  3874. \Algorithm
  3875. Adds this item to the linked list of stack frame context information saved.
  3876. Allocates a buffer for the jump statement and returns it.
  3877. \end{function}
  3878. \subsubsection{FPC{\_}RERAISE}
  3879. \label{subsubsec:mylabel62}
  3880. \begin{procedure}{ReRaise}
  3881. \Declaration
  3882. procedure ReRaise;
  3883. \Notes
  3884. REGISTERS NOT SAVED???????????
  3885. \end{procedure}
  3886. \subsubsection{FPC{\_}POPOBJECTSTACK}
  3887. \label{subsubsec:mylabel63}
  3888. \begin{function}{PopObjectStack}
  3889. \Declaration
  3890. function PopObjectStack : TObject;
  3891. \Description
  3892. This is called by the code generator when an exception occurs, it is used to
  3893. retrieve the exception handler object from the context information.
  3894. \Notes
  3895. REGISTERS NOT SAVED???????????
  3896. \end{function}
  3897. \subsubsection{FPC{\_}POPSECONDOBJECTSTACK}
  3898. \label{subsubsec:mylabel64}
  3899. \begin{function}{PopSecondObjectStack}
  3900. \Declaration
  3901. function PopSecondObjectStack : TObject;
  3902. \Description
  3903. This is called by the code generator when a double exception occurs, it is
  3904. used to retrieve the second exception handler object from the context
  3905. information.
  3906. \Notes
  3907. REGISTERS NOT SAVED???????????
  3908. \end{function}
  3909. \subsubsection{FPC{\_}DESTROYEXCEPTION}
  3910. \label{subsubsec:mylabel65}
  3911. \begin{procedure}{DestroyException}
  3912. \Declaration
  3913. Procedure DestroyException(o : TObject);
  3914. \Description
  3915. This routine is called by the code generator after the exception handling
  3916. code is complete to destroy the exception object.
  3917. \Parameters
  3918. o = Exception handler object reference
  3919. \Notes
  3920. REGISTERS NOT SAVED?????????????
  3921. \end{procedure}
  3922. \subsubsection{FPC{\_}POPADDRSTACK}
  3923. \label{subsubsec:mylabel66}
  3924. \begin{procedure}{PopAddrStack}
  3925. \Declaration
  3926. procedure PopAddrStack;
  3927. \Description
  3928. Called by the code generator in the finally part of a try statement to
  3929. restore the stackframe and dispose of all the saved context information.
  3930. \Notes
  3931. REGISTERS NOT SAVED??????????
  3932. \end{procedure}
  3933. \subsubsection{FPC{\_}CATCHES}
  3934. \label{subsubsec:mylabel67}
  3935. \begin{function}{Catches}
  3936. \Declaration
  3937. function Catches(Objtype : TExceptObjectClass) : TObject;
  3938. \Description
  3939. This routine is called by the code generator to get the exception handler
  3940. object. ?????????????????
  3941. \Parameters
  3942. ObjType = The exception type class
  3943. \Notes
  3944. REGISTERS NOT SAVED??????????
  3945. \end{function}
  3946. \subsubsection{FPC{\_}GETRESOURCESTRING}
  3947. \label{subsubsec:mylabel68}
  3948. \begin{function}{GetResourceString}
  3949. \Declaration
  3950. function GetResourceString(Const TheTable: TResourceStringTable;Index : longint) : AnsiString;
  3951. \Description
  3952. Called by code generator when a reference to a resource string is made. This
  3953. routine loads the correct string from the resource string section and
  3954. returns the found string (or `' if not found).
  3955. \Parameters
  3956. TheTable = pointer to the resource string table \par
  3957. Index = Index in the resource string table.
  3958. \end{function}
  3959. \subsection{Runtime type information}
  3960. \label{subsec:runtime}
  3961. \subsubsection{FPC{\_}DO{\_}IS}
  3962. \label{subsubsec:mylabel69}
  3963. \begin{functionl}{Int{\_}Do{\_}Is}{intdois}
  3964. \Declaration
  3965. Function Int{\_}Do{\_}Is(AClass : TClass;AObject : TObject) : Boolean;
  3966. \Description
  3967. If \textsf{aclass} is of type \textsf{aobject}, returns TRUE otherwise
  3968. returns FALSE.
  3969. \Parameters
  3970. aclass = class type reference \par
  3971. aobject = Object instance to compare against
  3972. \Notes
  3973. This is called by the code generator when the \textsf{is} operator is used.
  3974. \Algorithm
  3975. \end{functionl}
  3976. \subsubsection{FPC{\_}DO{\_}AS}
  3977. \label{subsubsec:mylabel70}
  3978. \begin{procedurel}{Int{\_}Do{\_}As}{intdoas}
  3979. \Declaration
  3980. Procedure Int{\_}Do{\_}As(AClass : TClass;AObject : TObject)
  3981. \Description
  3982. Typecasts \textsf{aclass} as \textsf{aobject}, with dynamic type checking.
  3983. If the object is not from the correct type class, a runtime error 219 is
  3984. generated. Called by the code generator for the \textsf{as} statement.
  3985. \Parameters
  3986. aclass = Class to typecast to \par
  3987. aobject = Object to typecast
  3988. \end{procedurel}
  3989. \subsubsection{FPC{\_}INITIALIZE }
  3990. \label{subsubsec:mylabel71}
  3991. \begin{procedure}{Initialize}
  3992. \Declaration
  3993. Procedure Initialize (Data,TypeInfo : Pointer);
  3994. \Description
  3995. \Parameters
  3996. data = pointer to the data to initialize \par
  3997. typeinfo = pointer to the type information for this data
  3998. \Notes
  3999. This routine should save / restore all used registers.
  4000. \Algorithm
  4001. Initializes the class data for runtime typed values
  4002. \end{procedure}
  4003. \subsubsection{FPC{\_}FINALIZE}
  4004. \label{subsubsec:mylabel72}
  4005. \begin{procedure}{Finalize}
  4006. \Declaration
  4007. procedure Finalize (Data,TypeInfo: Pointer);
  4008. \Description
  4009. Called by code generator if and only if the reference to finalize <> nil.
  4010. \Parameters
  4011. data = point to the data to finalize \par
  4012. typeinfo = Pointer to the type information of this data
  4013. \Notes
  4014. This routine should save / restore all used registers. Finalizes and frees
  4015. the heap class data for runtime typed values (decrements the reference
  4016. count)
  4017. \end{procedure}
  4018. \subsubsection{FPC{\_}ADDREF}
  4019. \label{subsubsec:mylabel73}
  4020. \begin{procedure}{AddRef}
  4021. \Declaration
  4022. Procedure AddRef (Data,TypeInfo : Pointer);
  4023. \Description
  4024. Called by the code generator for class parameters (property support) of type
  4025. const or value in parameters, to increment the reference count of ANSI
  4026. strings.
  4027. \Notes
  4028. This routine should save / restore all used registers. This routine can be
  4029. called recursively with a very deep nesting level, an assembler
  4030. implementation in suggested.
  4031. \end{procedure}
  4032. \subsubsection{FPC{\_}DECREF}
  4033. \label{subsubsec:mylabel74}
  4034. \begin{procedure}{DecRef}
  4035. \Declaration
  4036. Procedure DecRef (Data, TypeInfo : Pointer);
  4037. \Description
  4038. Called by the code generator for class parameters (property support) of type
  4039. const or value parameters, to decrement the reference count. of ANSI
  4040. strings.
  4041. \Parameters
  4042. \Notes
  4043. This routine should save / restore all used registers. This routine can be
  4044. called recursively with a very deep nesting level, an assembler
  4045. implementation in suggested.
  4046. \end{procedure}
  4047. \subsection{Memory related}
  4048. \label{subsec:memory}
  4049. \clearpage
  4050. \subsubsection{FPC{\_}GETMEM}
  4051. \label{subsubsec:mylabel75}
  4052. \begin{procedure}{GetMem}
  4053. \Declaration
  4054. procedure GetMem(Var p:Pointer;Size:Longint);
  4055. \end{procedure}
  4056. \subsubsection{FPC{\_}FREEMEM}
  4057. \label{subsubsec:mylabel76}
  4058. \begin{procedure}{FreeMem}
  4059. \Declaration
  4060. Procedure FreeMem(Var P:Pointer;Size:Longint);
  4061. \end{procedure}
  4062. \subsubsection{FPC{\_}CHECKPOINTER}
  4063. \label{subsubsec:mylabel77}
  4064. \begin{function}{CheckPointer}
  4065. \Declaration
  4066. Procedure CheckPointer(p : Pointer);
  4067. \Description
  4068. Called by the code generator when a pointer is referenced in heap debug
  4069. mode. Verifies that the pointer actually points in the heap area.
  4070. \Parameters
  4071. p = pointer to check
  4072. \Notes
  4073. This routine should save /restore all used registers.
  4074. \end{function}
  4075. \subsubsection{FPC{\_}DO{\_}EXIT}
  4076. \label{subsubsec:mylabel78}
  4077. \begin{procedurel}{Do{\_}Exit}{doexit}
  4078. \Declaration
  4079. procedure Do{\_}Exit;
  4080. \Description
  4081. Called by code generator at the end of the program entry point.
  4082. \Notes
  4083. Called to terminate the program
  4084. \Algorithm
  4085. Call all unit exit handlers. \par
  4086. Finalize all units which have a finalization section \par
  4087. Print runtime error in case of error\par
  4088. Call OS-dependant system{\_}exit routine
  4089. \end{procedurel}
  4090. \subsubsection{FPC{\_}ABSTRACTERROR}
  4091. \label{subsubsec:mylabel79}
  4092. \begin{function}{AbstractError}
  4093. \Declaration
  4094. procedure AbstractError;
  4095. \Description
  4096. The code generator allocates a VMT entry equal to this routine address when
  4097. a method of a class is declared as being abstract. This routine simply calls
  4098. the default error handler.
  4099. \Algorithm
  4100. Throw a Runtime error with error code 211 (Abstract call)
  4101. \end{function}
  4102. \subsubsection{FPC{\_}INITIALIZEUNITS}
  4103. \label{subsubsec:mylabel80}
  4104. \begin{function}{InitializeUnits}
  4105. \Declaration
  4106. \Description
  4107. Called by the code generator in the main program, this is only available if
  4108. an \textsf{initialization} section exists in one of the units used by the
  4109. program.
  4110. \end{function}
  4111. \subsubsection{FPC{\_}NEW{\_}CLASS (assembler)}
  4112. \label{subsubsec:mylabel81}
  4113. \begin{procedurel}{int{\_}new{\_}class}{intnewclass}
  4114. \Description
  4115. This routine will call the TObject.InitInstance() routine to
  4116. instantiate a class (Delphi-styled class) and allocate the memory for all
  4117. fields of the class.
  4118. On entry the self{\_}register should be valid, and should point either to
  4119. nil, for a non-initialized class, or to the current instance of the class.
  4120. The first parameter on the top of the stack should be a pointer to the VMT
  4121. table for this class(????).
  4122. \end{procedurel}
  4123. \subsubsection{FPC{\_}HELP{\_}DESTRUCTOR}
  4124. \label{subsubsec:mylabel82}
  4125. Could be implemented in ASM directly with register parameter passing.
  4126. \begin{procedurel}{Int{\_}Help{\_}Destructor}{inthelpdestructor}
  4127. \Declaration
  4128. Procedure Int{\_}Help{\_}Destructor(Var {\_}Self : Pointer; Vmt : Pointer; Vmt{\_}Pos : Cardinal);
  4129. \Description
  4130. Frees the memory allocated for the object fields, and if the object had a
  4131. VMT field, sets it to nil.
  4132. \Parameters
  4133. self = pointer to the object field image in memory \par
  4134. vmt = pointer to the the actual vmt table (used to get the size of the object) \par
  4135. vmt{\_}pos = offset in the object field image to the vmt pointer field
  4136. \Notes
  4137. This routine should / save restore all used registers.
  4138. \Algorithm
  4139. \begin{lstlisting}{}
  4140. if self = nil then
  4141. exit
  4142. set VMT field in object field image ,if present, to nil
  4143. Free the allocated heap memory for the field objects
  4144. set Self = nil
  4145. \end{lstlisting}
  4146. \end{procedurel}
  4147. \subsubsection{FPC{\_}HELP{\_}CONSTRUCTOR}
  4148. \label{subsubsec:mylabel83}
  4149. Could be implemented in ASM directly with register parameter passing.
  4150. \begin{functionl}{Int{\_}Help{\_}Constructor}{inthelpconstructor}
  4151. \Declaration
  4152. function Int{\_}Help{\_}Constructor(Var {\_}self : Pointer; Var VMT : Pointer; Vmt{\_}Pos : Cardinal):Pointer;
  4153. \Description
  4154. Allocates the memory for an object's field, and fills the object fields with
  4155. zeros. Returns the newly allocated self{\_}pointer
  4156. \Parameters
  4157. self = pointer to the object field image in memory \par
  4158. vmt = pointer to the the actual vmt table (used to get the size of the object) \par
  4159. vmt{\_}pos = offset in the object field image to the vmt pointer field
  4160. \Notes
  4161. The self{\_}pointer register should be set appropriately by the code
  4162. generator to the allocated memory (self parameter)
  4163. \Algorithm
  4164. Self = Allocate Memory block for object fields \par
  4165. Fill the object field image with zeros\par
  4166. Set the VMT field in allocated object to VMT pointer
  4167. \end{functionl}
  4168. \subsubsection{FPC{\_}HELP{\_}FAIL{\_}CLASS}
  4169. \label{subsubsec:mylabel84}
  4170. \begin{functionl}{Help{\_}Fail{\_}Class}{inthelpfileclass}
  4171. \Description
  4172. Inserted by code generator after constructor call. If the constructor failed
  4173. to allocate the memory for its fields, this routine will be called.
  4174. \end{functionl}
  4175. \subsubsection{FPC{\_}HELP{\_}FAIL}
  4176. \label{subsubsec:mylabel85}
  4177. \begin{functionl}{Help{\_}Fail}{HelpFail}
  4178. \Description
  4179. Inserted by code generator after constructor call. If the constructor failed
  4180. to allocate the memory for its fields, this routine will be called.
  4181. \end{functionl}
  4182. \subsection{Set handling}
  4183. \label{subsec:mylabel10}
  4184. \subsubsection{FPC{\_}SET{\_}COMP{\_}SETS}
  4185. \label{subsubsec:mylabel86}
  4186. \begin{functionl}{Do{\_}Comp{\_}Sets}{docompsets}
  4187. \Declaration
  4188. function Do{\_}Comp{\_}Sets(Set1,Set2 : Pointer): Boolean;
  4189. \Description
  4190. This routine compares if set1 and set2 are exactly equal and returns 1 if
  4191. so, otherwise it returns false.
  4192. \Parameters
  4193. set1 = Pointer to 32 byte set to compare \par
  4194. set2 = Pointer to 32 byte set to compare
  4195. \Notes
  4196. Both pointers must point to normal sets.
  4197. \end{functionl}
  4198. \subsubsection{FPC{\_}SET{\_}CONTAINS{\_}SET}
  4199. \label{subsubsec:mylabel87}
  4200. \begin{procedurel}{Do{\_}Contains{\_}Sets}{docontainssets}
  4201. \Declaration
  4202. Procedure Do{\_}Contains{\_}Sets(Set1,Set2 : Pointer): Boolean;
  4203. \Description
  4204. Returns 1 if set2 contains set1 (That is all elements of set2 are in set1).
  4205. \Parameters
  4206. set1 = Pointer to 32 byte set to verify \par
  4207. set2 = Pointer to 32 byte set to verify
  4208. \Notes
  4209. Both pointers must point to normal sets.
  4210. \end{procedurel}
  4211. \subsubsection{FPC{\_}SET{\_}CREATE{\_}ELEMENT}
  4212. \label{subsubsec:mylabel88}
  4213. \begin{procedurel}{Do{\_}Create{\_}Element}{docreateelement}
  4214. \Declaration
  4215. procedure Do{\_}Create{\_}Element(p : Pointer; b : Byte);
  4216. \Description
  4217. Create a new normal set in the area pointed to by \textsf{p} and add the
  4218. element value \textsf{b} in that set.
  4219. \Parameters
  4220. p = pointer to area where the 32 byte set will be created \par
  4221. b = bit value within that set which must be set
  4222. \Notes
  4223. This works on normal sets only.
  4224. \Algorithm
  4225. Zero the area pointed to by p \par
  4226. Set the bit number b to 1
  4227. \end{procedurel}
  4228. \subsubsection{FPC{\_}SET{\_}SET{\_}RANGE}
  4229. \label{subsubsec:mylabel89}
  4230. \begin{procedurel}{Do{\_}Set{\_}Range}{dosetrange}
  4231. \Declaration
  4232. Procedure Do{\_}Set{\_}Range(P : Pointer;l,h : Byte);
  4233. \Description
  4234. Sets the bit values within the \textsf{l} and \textsf{h }bit ranges in the
  4235. normal set pointed to by \textsf{p}
  4236. \Parameters
  4237. p = pointer to area where the 32 bytes of the set will be updated \par
  4238. l = low bit number value to set \par
  4239. h = high bit number value to set
  4240. \Notes
  4241. This works on normal sets only.
  4242. \Algorithm
  4243. Set all bit numbers from l to h in set p
  4244. \end{procedurel}
  4245. \subsubsection{FPC{\_}SET{\_}SET{\_}BYTE}
  4246. \label{subsubsec:mylabel90}
  4247. \begin{procedurel}{Do{\_}Set{\_}Byte}{dosetbyte}
  4248. \Declaration
  4249. procedure Do{\_}Set{\_}Byte(P : Pointer;B : byte);
  4250. \Description
  4251. Add the element \textsf{b} in the normal set pointed to by \textsf{p}
  4252. \Parameters
  4253. p = pointer to 32 byte set \par
  4254. b = bit number to set
  4255. \Notes
  4256. This works on normal sets only. The intel 80386 version of the compiler does
  4257. not save the used registers, therefore, in that case, it must be done in the
  4258. routine itself.
  4259. \Algorithm
  4260. Set bit number b in p
  4261. \end{procedurel}
  4262. \subsubsection{FPC{\_}SET{\_}SUB{\_}SETS}
  4263. \label{subsubsec:mylabel91}
  4264. \begin{procedurel}{Do{\_}Sub{\_}Sets}{dosubsets}
  4265. \Declaration
  4266. Procedure Do{\_}Sub{\_}Sets(Set1,Set2,Dest:Pointer);
  4267. \Description
  4268. Calculate the difference between \textsf{set1} and \textsf{set2}, setting
  4269. the result in \textsf{dest}.
  4270. \Parameters
  4271. set1 = pointer to 32 byte set \par
  4272. set2 = pointer to 32 byte set \par
  4273. dest = pointer to 32 byte set which will receive the result
  4274. \Notes
  4275. This works on normal sets only.
  4276. \Algorithm
  4277. \begin{lstlisting}{}
  4278. For each bit in the set do
  4279. dest bit = set1 bit AND NOT set2 bit
  4280. \end{lstlisting}
  4281. \end{procedurel}
  4282. \subsubsection{FPC{\_}SET{\_}MUL{\_}SETS}
  4283. \label{subsubsec:mylabel92}
  4284. \begin{procedurel}{Do{\_}Mul{\_}Sets}{domulsets}
  4285. \Declaration
  4286. procedure Do{\_}Mul{\_}Sets(Set1,Set2,Dest:Pointer);
  4287. \Description
  4288. Calculate the multiplication between \textsf{set1} and \textsf{set2},
  4289. setting the result in \textsf{dest}.
  4290. \Parameters
  4291. set1 = pointer to 32 byte set \par
  4292. set2 = pointer to 32 byte set \par
  4293. dest = pointer to 32 byte set which will receive the result
  4294. \Notes
  4295. This works on normal sets only.
  4296. \Algorithm
  4297. \begin{lstlisting}{}
  4298. For each bit in the set do
  4299. dest bit = set1 bit AND set2 bit
  4300. \end{lstlisting}
  4301. \end{procedurel}
  4302. \subsubsection{FPC{\_}SET{\_}SYMDIF{\_}SETS}
  4303. \label{subsubsec:mylabel93}
  4304. \begin{procedurel}{Do{\_}Symdif{\_}Sets}{dosymdifssets}
  4305. \Declaration
  4306. Procedure Do{\_}Symdif{\_}Sets(Set1,Set2,Dest:Pointer);
  4307. \Description
  4308. Calculate the symmetric between \textsf{set1} and \textsf{set2}, setting the
  4309. result in \textsf{dest}.
  4310. \Parameters
  4311. set1 = pointer to 32 byte set \par
  4312. set2 = pointer to 32 byte set \par
  4313. dest = pointer to 32 byte set which will receive the result
  4314. \Notes
  4315. This works on normal sets only.
  4316. \Algorithm
  4317. \begin{lstlisting}{}
  4318. For each bit in the set do
  4319. dest bit = set1 bit XOR set2 bit
  4320. \end{lstlisting}
  4321. \end{procedurel}
  4322. \subsubsection{FPC{\_}SET{\_}ADD{\_}SETS}
  4323. \label{subsubsec:mylabel94}
  4324. \begin{procedurel}{Do{\_}Add{\_}Sets}{doaddsets}
  4325. \Declaration
  4326. procedure Do{\_}Add{\_}Sets(Set1,Set2,Dest : Pointer);
  4327. \Description
  4328. Calculate the addition between \textsf{set1} and \textsf{set2}, setting the
  4329. result in \textsf{dest}.
  4330. \Parameters
  4331. set1 = pointer to 32 byte set \par
  4332. set2 = pointer to 32 byte set \par
  4333. dest = pointer to 32 byte set which will receive the result
  4334. \Notes
  4335. This works on normal sets only.
  4336. \Algorithm
  4337. \begin{lstlisting}{}
  4338. For each bit in the set do
  4339. dest bit = set1 bit OR set2 bit
  4340. \end{lstlisting}
  4341. \end{procedurel}
  4342. \subsubsection{FPC{\_}SET{\_}LOAD{\_}SMALL}
  4343. \label{subsubsec:mylabel95}
  4344. \begin{procedurel}{Do{\_}Load{\_}Small}{doloadsmall}
  4345. \Declaration
  4346. Procedure Do{\_}Load{\_}Small(P : Pointer;L:Longint);
  4347. \Description
  4348. Load a small set into a 32-byte normal set.
  4349. \Parameters
  4350. p = pointer to 32 byte set \par
  4351. l = value of the small set
  4352. \Notes
  4353. Called by code generator (type conversion) from small set to large set.
  4354. Apart from the first 32 bits of the 32 byte set, other bits are not
  4355. modified.
  4356. \Algorithm
  4357. \begin{lstlisting}{}
  4358. For n = bit 0 to bit 31 of l do
  4359. p bit n = l bit n
  4360. \end{lstlisting}
  4361. \end{procedurel}
  4362. \subsubsection{FPC{\_}SET{\_}UNSET{\_}BYTE}
  4363. \label{subsubsec:mylabel96}
  4364. \begin{procedurel}{Do{\_}Unset{\_}Byte}{dounsetbyte}
  4365. \Declaration
  4366. Procedure Do{\_}Unset{\_}Byte(P : Pointer;B : Byte);
  4367. \Description
  4368. Called by code generator to exclude element b from a big 32-byte set pointed
  4369. to by p.
  4370. \Parameters
  4371. p = pointer to 32 byte set \par b = element number to exclude
  4372. \Notes
  4373. The intel 80386 version of the compiler does not save the used registers,
  4374. therefore, in that case, it must be done in the routine itself.
  4375. \Algorithm
  4376. Clear bit number b in p
  4377. \end{procedurel}
  4378. \subsubsection{FPC{\_}SET{\_}IN{\_}BYTE}
  4379. \label{subsubsec:mylabel97}
  4380. \begin{functionl}{Do{\_}In{\_}Byte}{doinbyte}
  4381. \Declaration
  4382. Function Do{\_}In{\_}Byte(P : Pointer;B : Byte):boolean;
  4383. \Description
  4384. Called by code generator to verify the existence of an element in a set.
  4385. Returns TRUE if b is in the set pointed to by p, otherwise returns FALSE.
  4386. \Parameters
  4387. p = pointer to 32 byte set \par b = element number to verify
  4388. \Notes
  4389. This routine should save / restore all used registers.
  4390. \Algorithm
  4391. Clear bit number b in p
  4392. \end{functionl}
  4393. \subsection{Optional internal routines}
  4394. \label{subsec:optional}
  4395. These routines are dependant on the target architecture. They are present in
  4396. software if the hardware does not support these features.
  4397. They could be implemented in assembler directly with register parameter
  4398. passing.
  4399. \subsubsection{FPC{\_}MUL{\_}INT64}
  4400. \label{subsubsec:mylabel98}
  4401. \begin{function}{MulInt64}
  4402. \Declaration
  4403. function MulInt64(f1,f2 : Int64;CheckOverflow : LongBool) : Int64;
  4404. \Description
  4405. Called by the code generator to multiply two int64 values, when the hardware
  4406. does not support this type of operation. The value returned is the result of
  4407. the multiplication.
  4408. \Parameters
  4409. f1 = first operand \par
  4410. f2 = second operand \par
  4411. checkoverflow = TRUE if overflow checking should be done
  4412. \end{function}
  4413. \subsubsection{FPC{\_}DIV{\_}INT64}
  4414. \label{subsubsec:mylabel99}
  4415. \begin{function}{DivInt64}
  4416. \Declaration
  4417. function DivInt64(n,z : Int64) : Int64;
  4418. \Description
  4419. Called by the code generator to get the division two int64 values, when the
  4420. hardware does not support this type of operation. The value returned is the
  4421. result of the division.
  4422. \Parameters
  4423. n =numerator \par
  4424. z = denominator
  4425. \end{function}
  4426. \subsubsection{FPC{\_}MOD{\_}INT64}
  4427. \label{subsubsec:mylabel100}
  4428. \begin{function}{ModInt64}
  4429. \Declaration
  4430. function ModInt64(n,z : Int64) : Int64;
  4431. \Description
  4432. Called by the code generator to get the modulo two int64 values, when the
  4433. architecture does not support this type of operation. The value returned is
  4434. the result of the modulo.
  4435. \Parameters
  4436. n = numerator \par
  4437. z = denominator
  4438. \end{function}
  4439. \subsubsection{FPC{\_}SHL{\_}INT64}
  4440. \label{subsubsec:mylabel101}
  4441. \begin{function}{ShlInt64}
  4442. \Declaration
  4443. Function ShlInt64(Cnt : Longint; Low, High: Longint): Int64;
  4444. \Description
  4445. Called by the code generator to shift left a 64-bit integer by the specified
  4446. amount cnt, when this is not directly supported by the hardware. Returns the
  4447. shifted value.
  4448. \Parameters
  4449. low,high = value to shift (low / high 32-bit value) \par
  4450. cnt = shift count
  4451. \end{function}
  4452. \subsubsection{FPC{\_}SHR{\_}INT64}
  4453. \label{subsubsec:mylabel102}
  4454. \begin{function}{ShrInt64}
  4455. \Declaration
  4456. function ShrInt64(Cnt : Longint; Low, High: Longint): Int64;
  4457. \Description
  4458. Called by the code generator to shift left a 64-bit integer by the specified
  4459. amount cnt, when this is not directly supported by the hardware. Returns the
  4460. shifted value.
  4461. \Parameters
  4462. low,high = value to shift (low/high 32-bit values) \par
  4463. cnt = shift count
  4464. \end{function}
  4465. \subsubsection{FPC{\_}MUL{\_}LONGINT}
  4466. \label{subsubsec:mylabel103}
  4467. \begin{function}{MulLong}
  4468. \Declaration
  4469. Function MulLong: Longint;
  4470. \Description
  4471. Called by the code generator to multiply two longint values, when the hardware does not support this type of operation. The value returned is the result of the multiplication.
  4472. \Parameters
  4473. Parameters are passed in registers.
  4474. \Notes
  4475. This routine should save / restore all used registers.
  4476. \end{function}
  4477. \subsubsection{FPC{\_}REM{\_}LONGINT}
  4478. \label{subsubsec:mylabel104}
  4479. \begin{function}{RemLong}
  4480. \Declaration
  4481. Function RemLong: Longint;
  4482. \Description
  4483. Called by the code generator to get the modulo two longint values, when the
  4484. hardware does not support this type of operation. The value returned is the
  4485. result of the modulo.
  4486. \Parameters
  4487. Parameters are passed in registers.
  4488. \Notes
  4489. This routine should save / restore all used registers.
  4490. \end{function}
  4491. \subsubsection{FPC{\_}DIV{\_}LONGINT}
  4492. \label{subsubsec:mylabel105}
  4493. \begin{function}{DivLong}
  4494. \Declaration
  4495. Function DivLong: Longint;
  4496. \Description
  4497. Called by the code generator to get the division two longint values, when
  4498. the hardware does not support this type of operation. The value returned is
  4499. the result of the division.
  4500. \Parameters
  4501. Parameters are passed in registers.
  4502. \Notes
  4503. This routine should save / restore all used registers.
  4504. \end{function}
  4505. \subsubsection{FPC{\_}MUL{\_}LONGINT}
  4506. \label{subsubsec:mylabel106}
  4507. \begin{function}{MulCardinal}
  4508. \Declaration
  4509. Function MulCardinal: Cardinal;
  4510. \Description
  4511. Called by the code generator to multiply two cardinal values, when the
  4512. hardware does not support this type of operation. The value returned is the
  4513. result of the multiplication.
  4514. \Parameters
  4515. Parameters are passed in registers.
  4516. \Notes
  4517. This routine should save / restore all used registers.
  4518. \end{function}
  4519. \subsubsection{FPC{\_}REM{\_}CARDINAL}
  4520. \label{subsubsec:mylabel107}
  4521. \begin{function}{RemCardinal}
  4522. \Declaration
  4523. Function RemCardinal : Cardinal;
  4524. \Description
  4525. Called by the code generator to get the modulo two cardinal values, when the
  4526. hardware does not support this type of operation. The value returned is the
  4527. result of the modulo.
  4528. \Parameters
  4529. Parameters are passed in registers.
  4530. \Notes
  4531. This routine should save / restore all used registers.
  4532. \end{function}
  4533. \subsubsection{FPC{\_}DIV{\_}CARDINAL}
  4534. \label{subsubsec:mylabel108}
  4535. \begin{function}{DivCardinal}
  4536. \Declaration
  4537. Function DivCardinal: Cardinal;
  4538. \Description
  4539. Called by the code generator to get the division two cardinal values, when
  4540. the hardware does not support this type of operation. The value returned is
  4541. the result of the division.
  4542. \Parameters
  4543. Parameters are passed in registers.
  4544. \Notes
  4545. This routine should save / restore all used registers.
  4546. \end{function}
  4547. \subsubsection{FPC{\_}LONG{\_}TO{\_}SINGLE}
  4548. \label{subsubsec:mylabel109}
  4549. \begin{function}{LongSingle}
  4550. \Declaration
  4551. Function LongSingle: Single;
  4552. \Description
  4553. Called by the code generator to convert a longint to a single IEEE floating
  4554. point value.
  4555. \Parameters
  4556. Parameters are passed in registers
  4557. \Notes
  4558. This routine should save / restore all used registers.
  4559. \end{function}
  4560. FPC{\_}ADD{\_}SINGLE
  4561. FPC{\_}SUB{\_}SINGLE
  4562. FPC{\_}MUL{\_}SINGLE
  4563. FPC{\_}REM{\_}SINGLE
  4564. FPC{\_}DIV{\_}SINGLE
  4565. FPC{\_}CMP{\_}SINGLE
  4566. FPC{\_}SINGLE{\_}TO{\_}LONGINT
  4567. \section{Optimizing your code}
  4568. \label{sec:optimizing}
  4569. \subsection{Simple types}
  4570. \label{subsec:simple}
  4571. Use the most simple types, when defining and declaring variables, they
  4572. require less overhead. Classes, and complex string types (ansi strings and
  4573. wide strings) posess runtime type information, as well as more overhead for
  4574. operating on them then simple types such as shortstring and simple ordinal
  4575. types.
  4576. \subsection{constant duplicate merging}
  4577. \label{subsec:constant}
  4578. When duplicates of constant strings, sets or floating point values are found
  4579. in the code, they are replaced by only once instance of the same string, set
  4580. or floating point constant which reduces the size of the final executable.
  4581. \subsection{inline routines}
  4582. \label{subsec:inline}
  4583. The following routines of the system unit are directly inlined by the
  4584. compiler, and generate more efficient code:
  4585. \begin{longtable}{|l|p{7cm}|}
  4586. \hline
  4587. Prototype& Definition and notes \\
  4588. \hline
  4589. \endhead
  4590. \hline
  4591. \endfoot
  4592. \textsf{function pi : extended;}& \\
  4593. \textsf{function abs(d : extended) : extended;}& \\
  4594. \textsf{function sqr(d : extended) : extended;}& \\
  4595. \textsf{function sqrt(d : extended) : extended;}& \\
  4596. \textsf{function arctan(d : extended) : extended;}& \\
  4597. \textsf{function ln(d : extended) : extended;}& \\
  4598. \textsf{function sin(d : extended) : extended;}& \\
  4599. \textsf{function cos(d : extended) : extended;}& \\
  4600. \textsf{function ord(X): longint;}&
  4601. Changes node type to be type compatible \\
  4602. \textsf{function lo(X) : byte or word;}&
  4603. Generates 2-3 instruction sequence inline \\
  4604. \textsf{function hi(X) : byte or word;}&
  4605. Generates 2-3 instruction sequence inline \\
  4606. \textsf{function chr(b : byte) : Char;}&
  4607. Changes node type to be type compatible \\
  4608. \textsf{function Length(s : string) : byte;}&
  4609. Generate 2-3 instruction sequence \\
  4610. \textsf{function Length(c : char) : byte;}&
  4611. Generates 1 instruction sequence (appx.) \\
  4612. \textsf{procedure Reset(var f : TypedFile);}&
  4613. Calls FPC{\_}RESET{\_}TYPED \\
  4614. \textsf{procedure rewrite(var f : TypedFile);}&
  4615. Calls FPC{\_}REWRITE{\_}TYPED \\
  4616. \textsf{procedure settextbuf(var F : Text; var Buf);}&
  4617. Calls SetTextBuf of runtime library \\
  4618. \textsf{procedure writen;}&
  4619. Calls FPC{\_}WRITE{\_}XXXX routines \\
  4620. \textsf{procedure writeln;}&
  4621. Calls FPC{\_}WRITE{\_}XXXX routines \\
  4622. \textsf{procedure read;}&
  4623. Calls FPC{\_}READ{\_}XXXX routines \\
  4624. \textsf{procedure readln;}&
  4625. Calls FPC{\_}READ{\_}XXXX routines \\
  4626. \textsf{procedure concat;}&
  4627. Generates a TREE NODES of type addn \\
  4628. \textsf{function assigned(var p): boolean;}&
  4629. Generates 1-2 instruction sequence inline \\
  4630. \textsf{procedure str(X :[Width [:Decimals]]; var S);}& \\
  4631. \textsf{}& \\
  4632. \textsf{function sizeof(X): longint;}&
  4633. Generates 2-3 instruction sequence inline \\
  4634. \textsf{function typeof(X): pointer;}&
  4635. Generates 2-3 instruction sequence inline \\
  4636. \textsf{procedure val(S;var V; var Code: integer);}& \\
  4637. \textsf{function seg(X): longint;}& \\
  4638. \textsf{function High(X)}&
  4639. Generates a TREE NODE of type ordconstn \\
  4640. \textsf{function Low(X)}&
  4641. Generates a TREE NODE of type ordconstn \\
  4642. \textsf{function pred(x)}&
  4643. Generates 2-3 instruction sequence inline \\
  4644. \textsf{function succ(X)}&
  4645. Generates 2-3 instruction sequence inline \\
  4646. \textsf{procedure inc(var X [ ; N: longint]);}&
  4647. Generate 2-3 instruction sequence inline \\
  4648. \textsf{procedure dec(var X [; N:longint]);}&
  4649. Generate 2-3 instruction sequence inline \\
  4650. \textsf{procedure include(var s: set of T; I: T);}&
  4651. In the case of a small set : Generates 2-3 instruction sequence inline,
  4652. otherwise calls FPC{\_}SET{\_}SET{\_}BYTE \\
  4653. \textsf{procedure exclude(var S : set of T; I: T);}&
  4654. In the case of a small set : Generates 2-3 instruction sequence inline,
  4655. otherwise calls FPC{\_}SET{\_}UNSET{\_}BYTE \\
  4656. \textsf{procedure assert(expr : Boolean [; const msg: string]);}&
  4657. Calls the internal routine FPC{\_}ASSERT in the case where the assert fails.\\
  4658. \textsf{function addr(X): pointer;}&
  4659. Generates a TREE NODE of type addrn \\
  4660. \textsf{function typeInfo(typeIdent): pointer;}&
  4661. Generates 1 instruction sequence inline \\
  4662. \end{longtable}
  4663. \subsection{temporary memory allocation reuse}
  4664. \label{subsec:mylabel11}
  4665. When routines are very complex , they may require temporary allocated space
  4666. on the stack to store intermediate results. The temporary memory space can
  4667. be reused for several different operations if other space is required on the
  4668. stack.
  4669. \section{Appendix A}
  4670. \label{sec:appendix}
  4671. This appendix describes the temporary defines when compiling software under
  4672. the compiler:
  4673. The following defines are defined in FreePascal for v1.0.x, but they will be
  4674. removed in future versions, they are used for debugging purposes only:
  4675. \begin{itemize}
  4676. \item INT64
  4677. \item HASRESOURCESTRINGS
  4678. \item NEWVMTOFFSET
  4679. \item HASINTERNMATH
  4680. \item SYSTEMVARREC
  4681. \item INCLUDEOK
  4682. \item NEWMM
  4683. \item HASWIDECHAR
  4684. \item INT64FUNCRESOK
  4685. \item CORRECTFLDCW
  4686. \item ENHANCEDRAISE
  4687. \item PACKENUMFIXED
  4688. \end{itemize}
  4689. NOTE: Currently, the only possible stack alignment are either 2 or 4 if the
  4690. target operating system pushes parameters on the stack directly in assembler
  4691. (because for example if pushing a long value on the stack while the required
  4692. stack alignment is 8 will give out wrong access to data in the actual
  4693. routine -- the offset will be wrong).
  4694. \printindex
  4695. \end{document}