comparch.tex 170 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411
  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 \seefig{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 \seefig{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\; shl \; 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{5.5cm}|}
  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.2cm}|}
  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{8cm}|}
  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 : Eord;} & \\
  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,}& Search path for obtaining module source code \\
  1999. &\textsf{LocalObjectSearchPath,}& \\
  2000. &\textsf{LocalIncludeSearchPath,}& Search path for includes for this module \\
  2001. &\textsf{LocalLibrarySearchPath:TSearchPathList;}& \\
  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. Picture \seefig{fig9} 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{fig: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{fig: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
  2398. \textsf{FpuRegisterLow : TRegister;}&
  2399. Indicates in what registers the operand are located (for emulation
  2400. support - these are general purpose registers)
  2401. \end{longtable}
  2402. \subsubsection{LOC{\_}REGISTER}
  2403. \label{subsubsec:mylabel20}
  2404. This fields indicates that the operand is located in a CPU register. It is
  2405. possible to allocate more then one register, if trying to access 64-bit
  2406. values on 32-bit wide register machines.
  2407. \begin{longtable}{|l|p{10cm}|}
  2408. \hline
  2409. Field & Description \\
  2410. \hline
  2411. \endhead
  2412. \hline
  2413. \endfoot
  2414. \textsf{Register : TRegister}&
  2415. Indicates in what register the operand is located. \\
  2416. \textsf{RegisterHigh : TRegister;}&
  2417. High 32-bit of 64-bit virtual register (on 32-bit machines) \\
  2418. \textsf{RegisterLow : TRegister;}&
  2419. Low 32-bit of 64-bit virtual register (on 32-bit machines)
  2420. \end{longtable}
  2421. \subsubsection{LOC{\_}MEM, LOC{\_}REFERENCE}
  2422. \label{subsubsec:mylabel21}
  2423. This either indicates an operand in memory, or a constant integer numeric
  2424. value. The fields for this type of operand is as follows:
  2425. \begin{longtable}{|l|p{10cm}|}
  2426. \hline
  2427. Field & Description \\
  2428. \hline
  2429. \endhead
  2430. \hline
  2431. \endfoot
  2432. \textsf{Reference : TReference;}&
  2433. Information on the location in memory
  2434. \end{longtable}
  2435. References are the basic building blocks of the code generator, every load
  2436. and store in memory is done via a reference. A reference type can either
  2437. point to a symbolic name, an assembler expression (base register + index
  2438. register + offset)*scale factor, as well as simply giving information on a
  2439. numeric value.
  2440. The treference consists of the following:
  2441. \begin{tabular*}{6.5in}{|l@{\extracolsep{\fill}}lp{6,5cm}|}
  2442. \hline
  2443. \textsf{TYPE} & & \\
  2444. \xspace \textsf{pReference} &= \^{} \textbf{TReference};& \\
  2445. \xspace \textsf{TReference} &= \textbf{packed Record} & \\
  2446. &\textsf{Is{\_}Immediate : Boolean;}&
  2447. Indicates that this location points to a memory location, but to a
  2448. constant value (TRUE), which is located in the offset field. \\
  2449. &\textsf{Segment : TRegister;}& (cpu-specific) \\
  2450. &\textsf{Base : TRegister;}&
  2451. Base address register for assembler expression \\
  2452. &\textsf{Index : TRegister;}&
  2453. Index register for assembler expression \\
  2454. &\textsf{ScaleFactor : Byte;}&
  2455. Multiplication factor for assembler expression (this field is
  2456. cpu-specific) \\
  2457. &\textsf{Offset : Longint;}&
  2458. Either an offset from base assembler address expression to add (if
  2459. Is{\_}Constant = FALSE) otherwise the numeric value of the operand \\
  2460. &\textsf{Symbol : pAsmSymbol;}&
  2461. Pointer to the symbol name string of the reference in case where it is
  2462. a symbolic reference \\
  2463. &\textsf{OffsetFixup : Longint;}& \\
  2464. &\textsf{Options : TRefOptions;}& \\
  2465. &\textsf{END;}& \\
  2466. \hline
  2467. \end{tabular*}
  2468. \subsubsection{LOC{\_}JUMP}
  2469. \label{subsubsec:mylabel22}
  2470. There are no fields associated with this location, it simply indicates that
  2471. it is a boolean comparison which must be done to verify the succeeding
  2472. operations. (i.e the processor zero flag is valid and gives information on
  2473. the result of the last operation).
  2474. \subsubsection{LOC{\_}FLAGS}
  2475. \label{subsubsec:mylabel23}
  2476. The operand is in the flags register. From this operand, the conditional
  2477. jumps can be done. This is processor dependant, but normally the flags for
  2478. all different comparisons should be present.
  2479. \begin{longtable}{|l|p{10cm}|}
  2480. \hline
  2481. Field & Description \\
  2482. \hline
  2483. \endhead
  2484. \hline
  2485. \endfoot
  2486. \textsf{ResFlags : TResFlags;}&
  2487. This indicates the flag which must be verified for the actual jump
  2488. operation. \textsf{tresflags }is an enumeration of all possible
  2489. conditional flags which can be set by the processor. \\
  2490. \end{longtable}
  2491. \subsubsection{LOC{\_}CREGISTER}
  2492. \label{subsubsec:mylabel24}
  2493. This is a read-only register allocated somewhere else in the code generator.
  2494. It is used mainly for optimization purposes. It has the same fields as
  2495. LOC{\_}REGISTER, except that the registers associated with this location can
  2496. only be read from, and should never be modified directly.
  2497. \begin{longtable}{|l|p{10cm}|}
  2498. \hline
  2499. Field & Description \\
  2500. \hline
  2501. \endhead
  2502. \hline
  2503. \endfoot
  2504. \textsf{Register : TRegister}&
  2505. Indicates in what register the operand is located. \\
  2506. \textsf{RegisterHigh : TRegister;}&
  2507. High 32-bit of 64-bit virtual register (on 32-bit machines) \\
  2508. \textsf{RegisterLow : TRegister;}&
  2509. Low 32-bit of 64-bit virtual register (on 32-bit machines) \\
  2510. \end{longtable}
  2511. \subsubsection{LOCATION PUBLIC INTERFACE}
  2512. \label{subsubsec:location}
  2513. \begin{procedurel}{Del{\_}Location}{dellocation}
  2514. \Declaration
  2515. procedur Del{\_}Location(const L : TLocation);
  2516. \Description
  2517. If the location points to a LOC{\_}REGISTER or LOC{\_}CREGISTER, it frees up
  2518. the allocated register(s) associated with this location. If the location
  2519. points to LOC{\_}REFERENCE or LOC{\_}MEM, it frees up the the allocated base
  2520. and index registers associated with this node.
  2521. \end{procedurel}
  2522. \begin{procedurel}{Clear{\_}Location}{clearlocation}
  2523. \Declaration
  2524. procedure Clear{\_}location(var Loc : TLocation);
  2525. \Description
  2526. Sets the location to point to a LOC{\_}INVALID type.
  2527. \end{procedurel}
  2528. \begin{procedurel}{Set{\_}Location}{setlocation}
  2529. \Declaration
  2530. procedure Set{\_}Location(var Destloc,Sourceloc : TLocation);
  2531. \Description
  2532. The destination location now points to the destination location (now copy is
  2533. made, a simple pointer assignment)
  2534. \end{procedurel}
  2535. \begin{procedurel}{Swap{\_}Location}{swaplocation}
  2536. \Declaration
  2537. Procedure Swap{\_}Location(var Destloc,Sourceloc : TLocation);
  2538. \Description
  2539. Swap both location pointers.
  2540. \end{procedurel}
  2541. \subsection{Registers (cpubase.pas)}
  2542. \label{subsec:registers}
  2543. The code generator defines several types of registers which are categorized
  2544. by classes. All (except for the scratch register class) of these register
  2545. classes are allocated / freed on the fly, when the code is generated in the
  2546. code generator: The registers are defined in a special enumeration called
  2547. tregister. This enumeration contains all possible register defines for the
  2548. target architecture, and a possible definition could be as follows :
  2549. % FIXME this should be changed to something more TeXish
  2550. \textsf{tregister = (}
  2551. \textsf{{\{} general purpose registers {\}} }
  2552. \textsf{R{\_}NO,R{\_}D0,R{\_}D1,R{\_}D2,R{\_}D3,R{\_}D4,R{\_}D5,R{\_}D6,R{\_}D7,}
  2553. \textsf{{\{} address registers {\}}}
  2554. \textsf{R{\_}A0,R{\_}A1,R{\_}A2,R{\_}A3,R{\_}A4,R{\_}A5,R{\_}A6,R{\_}SP,}
  2555. \textsf{{\{} PUSH/PULL- quick and dirty hack {\}}}
  2556. \textsf{R{\_}SPPUSH,R{\_}SPPULL,}
  2557. \textsf{{\{} misc. and floating point registers {\}}}
  2558. \textsf{R{\_}CCR,R{\_}FP0,R{\_}FP1,R{\_}FP2,R{\_}FP3,R{\_}FP4,R{\_}FP5,R{\_}FP6,}
  2559. \textsf{R{\_}FP7,R{\_}FPCR,R{\_}SR,R{\_}SSP,R{\_}DFC,R{\_}SFC,R{\_}VBR,R{\_}FPSR,}
  2560. \textsf{{\{} other - not used {\}}}
  2561. \textsf{R{\_}DEFAULT{\_}SEG}
  2562. \textsf{);}
  2563. \subsubsection{integer registers}
  2564. \label{subsubsec:integer}
  2565. \textsf{intregs: array[1..maxintregs] of tregister;}
  2566. General purpose registers which can contain any data, usually integer
  2567. values. These can also be used, when no floating point coprocessor is
  2568. present, to hold values for floating point operations.
  2569. \subsubsection{address registers}
  2570. \label{subsubsec:address}
  2571. \textsf{addrregs: array[1..maxaddrregs] of tregister;}
  2572. Registers which are used to construct assembler address expressions, usually
  2573. the address registers are used as the base registers in these assembler
  2574. expressions.
  2575. \subsubsection{fpu registers}
  2576. \label{subsubsec:mylabel25}
  2577. \textsf{FpuRegs: array[1..MaxFpuRegs] of TRegister;}
  2578. Hardware floating point registers. These registers must at least be able to
  2579. load and store IEEE DOUBLE floating point values, otherwise they cannot be
  2580. considered as FPU registers. Not available on systems with no floating point
  2581. coprocessor.
  2582. \subsubsection{scratch registers}
  2583. \label{subsubsec:scratch}
  2584. \textsf{Scratch{\_}Regs: array[1..MaxScratchRegs] of TRegister;}
  2585. These registers are used as scratch, and can be used in assembler statement
  2586. in the pascal code, without being saved. They will always be valid across
  2587. routine calls. These registers are sometimes temporarily allocated inside
  2588. code generator nodes, and then immediately freed (always inside the same
  2589. routine).
  2590. \subsection{Special registers (cpubase.pas)}
  2591. \label{subsec:special}
  2592. The code generator has special uses for certain types of registers. These
  2593. special registers are of course CPU dependant, but as an indication, the
  2594. following sections explains the uses of these special registers and their
  2595. defines.
  2596. \subsubsection{Stack{\_}Pointer}
  2597. \label{subsubsec:stack}
  2598. \textsf{Const Stack{\_}Pointer = R{\_}A7}
  2599. This represents the stack pointer, an address register pointing to the
  2600. allocated stack area.
  2601. \subsubsection{Frame{\_}Pointer}
  2602. \label{subsubsec:frame}
  2603. \textsf{Const Frame{\_}Pointer = R{\_}A6}
  2604. This represents the frame register which is used to access values in the
  2605. stack. This is usually also an address register.
  2606. \subsubsection{Self{\_}Pointer}
  2607. \label{subsubsec:mylabel26}
  2608. \textsf{Const Self{\_}Pointer = R{\_}A5}
  2609. This represents the self register, which represents a pointer to the current
  2610. instance of a class or object.
  2611. \subsubsection{accumulator}
  2612. \label{subsubsec:accumulatorents}
  2613. \textsf{Const Accumulator = R{\_}D0}
  2614. The accumulator is used (except in the i386) as a scratch register, and also
  2615. for return value in functions (in the case where they are 32-bit or less).
  2616. In the case it is a 64-bit value (and the target processor only supports
  2617. 32-bit registers) , the result of the routine is stored in the accumulator
  2618. for the low 32-bit value, and in the scratch register
  2619. (\textsf{scratch{\_}register}) for the high 32-bit value.
  2620. \subsubsection{scratch register}
  2621. \label{subsubsec:mylabel27}
  2622. \textsf{const scratch{\_}reg = R{\_}D1}
  2623. This register is used in special circumstances by the code generator. It is
  2624. simply a define to one of the registers in the \textsf{scratch{\_}regs
  2625. }array.
  2626. \subsection{Instructions}
  2627. \label{subsec:instructionsr}
  2628. \subsection{Reference subsystem}
  2629. \label{subsec:reference}
  2630. \subsubsection{Architecture}
  2631. \label{subsubsec:architecturebsysteme}
  2632. As described before in the locations section, one of the possible locations
  2633. for an operand is a memory location, which is described in a special
  2634. structure \textsf{treference} (described earlier). This subsection describes
  2635. the interface available by the code generator for allocation and freeing
  2636. reference locations.
  2637. \subsubsection{Code generator interface}
  2638. \label{subsubsec:mylabel28}
  2639. \lstinline!Function NewReference(Const R : TReference) : pReference;!
  2640. \begin{procedure}{DisposeReference}
  2641. \Declaration
  2642. Procedure DisposeReference(Var R : pReference);
  2643. \Description
  2644. Disposes of the reference \textsf{R} and sets r to \textsf{NIL}
  2645. \Notes
  2646. Does not verify if \textsf{R} is assigned first.
  2647. \end{procedure}
  2648. \begin{function}{NewReference}
  2649. \Declaration
  2650. Function NewReference(Const R : TReference) : pReference;
  2651. \Description
  2652. Allocates in the heap a copy of the reference \textsf{r} and returns that
  2653. allocated pointer.
  2654. \end{function}
  2655. \begin{functionl}{Del{\_}Reference}{delreference}
  2656. \Declaration
  2657. Procedure Del{\_}Reference(Const Ref : tReference);
  2658. \Description
  2659. Free up all address registers allocated in this reference for the index and
  2660. base (if required).
  2661. \Notes
  2662. Does not free the reference symbol if it exists.
  2663. \end{functionl}
  2664. \begin{functionl}{New{\_}Reference}{resetreference}
  2665. \Declaration
  2666. Function New{\_}Reference(Base : TRegister;Offset : Longint) : PReference;
  2667. \Description
  2668. Allocates a reference pointer, clears all the fields to zero, and sets the
  2669. offset to the offset field and the base to the base fields of the newly
  2670. allocated reference. Returns this newly allocated reference.
  2671. \end{functionl}
  2672. \begin{procedurel}{Reset{\_}Reference}{resetreference}
  2673. \Declaration
  2674. Procedure Reset{\_}Reference(Var Ref : TReference);
  2675. \Description
  2676. Clears all fields of the reference.
  2677. \end{procedurel}
  2678. \subsection{The register allocator subsystem}
  2679. \label{subsec:mylabel7}
  2680. \subsubsection{Architecture}
  2681. \label{subsubsec:architecture}
  2682. This system allocates and deallocates registers, from a pool of free
  2683. registers. Each time the code generator requires a register for generating
  2684. assembler instructions, it either calls the register allocator subsystem to
  2685. get a free register or directly uses the scratch registers (which are never
  2686. allocated in a pool except in the optimization phases of the compiler).
  2687. The code generator when no longer referencing the register should deallocate
  2688. it so it can be used once again.
  2689. \subsubsection{Code generator interface (tgen.pas)}
  2690. \label{subsubsec:mylabel29}
  2691. The following interface routines are used by the code generator to allocate
  2692. and deallocate registers from the different register pools available to code
  2693. generator.
  2694. \paragraph{General purpose registers}
  2695. \begin{function}{GetRegister32}
  2696. \Declaration
  2697. Function GetRegister32 : TRegister;
  2698. \Description
  2699. Allocates and returns a general purpose (integer) register which can be used
  2700. in the code generator. The register, when no longer used should be
  2701. deallocated with ungetregister32() or ungetregister()
  2702. \Notes
  2703. On non 32-bit machines, this routine should return the normal register for
  2704. this machine (eg : 64-bit machines will alloate and return a 64-bit
  2705. register).
  2706. \end{function}
  2707. \begin{procedure}{GetRegisterPair}
  2708. \Declaration
  2709. Procedure GetRegisterPair(Var Low, High : TRegister);
  2710. \Description
  2711. Returns a register pair to be used by the code generator when accessing
  2712. 64-bit values on 32-bit wide register machines.
  2713. \Notes
  2714. On machines which support 64-bit registers naturally, this routine should
  2715. never be used, it is intended for 32-bit machines only.par Some machines
  2716. support 64-bit integer operations using register 32-bit pairs in hardware,
  2717. but the allocated registers must be specific, this routine is here to
  2718. support these architectures.
  2719. \end{procedure}
  2720. \begin{procedure}{UngetRegister32}
  2721. \Declaration
  2722. Procedure UnGetRegister32(R : TRegister);
  2723. \Description
  2724. Deallocates a general purpose register which was previously allocated with
  2725. \seef{GetRegister32}().
  2726. \end{procedure}
  2727. \paragraph{Floating point registers}
  2728. \begin{function}{GetFloatRegister}
  2729. \Declaration
  2730. Function GetFloatRegister : TRegister;
  2731. \Description
  2732. Allocates and returns a floating point register which can be used in the
  2733. code generator. The register, when no longer used should be deallocated with
  2734. ungetregister(). The register returned is a true floating point register (if
  2735. supported).
  2736. \Notes
  2737. This routine should only be used when floating point hardware is present in
  2738. the system. For emulation of floating point, the general purpose register
  2739. allocator / deallocator routines should be used instead.
  2740. \end{function}
  2741. \begin{function}{IsFloatsRegister}
  2742. \Declaration
  2743. Function IsFloatsRegister(R : TRegister): Boolean;
  2744. \Description
  2745. Returns TRUE if the register r is actually a floating point register,
  2746. otherwise returns FALSE. This is used when the location is LOC{\_}FPU on
  2747. machines which do not support true floating point registers.
  2748. \end{function}
  2749. \paragraph{Address registers}
  2750. \begin{function}{GetAdressReg}
  2751. \Declaration
  2752. Function GetAddressReg : TRegister;
  2753. \Description
  2754. Allocates and returns an address register which can be used for address
  2755. related opcodes in the code generator. The register, when no longer used
  2756. should be deallocated with ungetregister()
  2757. \Notes
  2758. If there is no distinction between address registers, and general purpose
  2759. register in the architecture, this routine may simply call and return the
  2760. getregister32() result.
  2761. \end{function}
  2762. \begin{function}{IsAddressRegister}
  2763. \Declaration
  2764. Function IsAddressRegister(r : TRegister): Boolean;
  2765. \Description
  2766. Returns TRUE if the register r is actually an address register, otherwise
  2767. returns FALSE.
  2768. \Notes
  2769. If there is no distinction between address registers, and general purpose
  2770. register in the architecture, this routine may simply verify if this is a
  2771. general purpose register and return TRUE in that case.
  2772. \end{function}
  2773. \paragraph{Generic}
  2774. \begin{procedure}{UngetRegister}
  2775. \Declaration
  2776. Procedure UngetRegister(r : TRegister);
  2777. \Description
  2778. Deallocates any register which was previously allocated with any of the
  2779. allocation register routines.
  2780. \end{procedure}
  2781. \begin{function}{SaveUsedRegisters}
  2782. \Declaration
  2783. Procedure SaveUsedRegisters(Var Saved : TSaved; ToSave: TRegisterset);
  2784. \Description
  2785. Saves in a temporary location all specified registers. On stack based
  2786. machines the registers are saved on the stack, otherwise they are saved in a
  2787. temporary memory location. The registers which were saved are stored in the
  2788. \textsf{saved} variable. The constant \textsf{ALL{\_}REGISTERS} passed to
  2789. the \textsf{tosave} parameter indicates to save all used registers.
  2790. \end{function}
  2791. \begin{function}{RestoreUsedRegisters}
  2792. \Declaration
  2793. procedure restoreusedregisters(Saved : TSaved);
  2794. \Description
  2795. Restores all saved registers from the stack (or a temporary memory
  2796. location). Free any temporary memory space allocated, if necessary.
  2797. \end{function}
  2798. \paragraph{Debugging}
  2799. \begin{function}{GetExplicitRegister32}
  2800. \Declaration
  2801. Function GetExplicitRegister32(R : TRegister): TRegister;
  2802. \Description
  2803. This routine allocates specifically the specified register \textsf{r} and
  2804. returns that register. The register to allocate can only be one of the
  2805. scratch registers.
  2806. \Notes
  2807. This routine is used for debugging purposes only. It should be used in
  2808. conjunctions with UnGetRegister32() to explicitly allocate and deallocate a
  2809. scratch register.
  2810. \end{function}
  2811. \subsection{Temporary memory allocator subsystem}
  2812. \label{subsec:temporary}
  2813. \subsubsection{Architecture}
  2814. \label{subsubsec:architecturemory}
  2815. Sometimes it is necessary to reserve temporary memory locations on the stack
  2816. to store intermediate results of statements. This is done by the temporary
  2817. management module.
  2818. Since entry and exit code for routines are added after the code for the
  2819. statements in the routine have been generated, temporary memory allocation
  2820. can be used `on the fly' in the case where temporary memory values are
  2821. required in the code generation phase of the routines being compiled. After
  2822. usage, the temporary memory space should be freed, so it can be reused if
  2823. necessary.
  2824. The temporary memory allocation is a linked list of entries containing
  2825. information where to access the data via a negative offset from the
  2826. Frame{\_}Pointer register. The linked list is only valid when compiling and
  2827. generating the code for the procedure bodies; it is reset and cleared each
  2828. time a new routine is compiled. There are currently three different types of
  2829. memory spaces in use : volatile (\textsf{tt{\_}Normal}) which can be
  2830. allocated and freed any time in the procedure body, ansistring, which is
  2831. currently the same as volatile, except it only stored references to
  2832. ansistring's, and persistent (\textsf{tt{\_}Persistent}) which are memory
  2833. blocks which are reserved throughout the routine duration; persistent
  2834. allocated space can never be reused in a procedure body, unless explicitly
  2835. released.
  2836. The temporary memory allocator guarantees to allocate memory space on the
  2837. stack at least on a 16-bit alignment boundary. The exact alignment depends
  2838. on the operating system required alignment.
  2839. \subsubsection{Temporary memory allocator interface (temp{\_}gen.pas)}
  2840. \label{subsubsec:temporary}
  2841. \paragraph{volatile / ansistring memory}
  2842. \begin{function}{GetTempOfSize}
  2843. \Declaration
  2844. Function GetTempOfSize(Size : Longint) : Longint;
  2845. \Description
  2846. Allocates at least \textsf{size} bytes of temporary volatile memory on the
  2847. stack. The return value is the negative offset from the frame pointer where
  2848. this memory was allocated.
  2849. \Notes
  2850. The return offset always has the required alignment for the target system,
  2851. and can be used as an offset from the Frame{\_}Pointer to access the
  2852. temporary space.
  2853. \end{function}
  2854. \begin{procedure}{GetTempOfSizeReference}
  2855. \Declaration
  2856. Procedure GetTempOfSizeReference(L : Longint;Var Ref : TReference);
  2857. \Description
  2858. This routine is used to assign and allocate extra temporary volatile memory
  2859. space on the stack from a reference. \textsf{l} is the size of the
  2860. persistent memory space to allocate, while \textsf{Ref} is a reference entry
  2861. which will be set to the correct offset from the Frame{\_}Pointer register
  2862. base. The \textsf{Offset} and \textsf{Base} fields of \textsf{Ref} will be
  2863. set appropriately in this routine, and can be considered valid on exit of
  2864. this routine.
  2865. \Notes
  2866. The return offset always has the required alignment for the target system.
  2867. \end{procedure}
  2868. \begin{procedure}{UnGetIfTemp}
  2869. \Declaration
  2870. Procedure UnGetIfTemp(Const Ref : TReference);
  2871. \Description
  2872. Frees a reference \textsf{Ref} which was allocated in the volatile temporary
  2873. memory space.
  2874. \Notes
  2875. The freed space can later be reallocated and reused.
  2876. \end{procedure}
  2877. \begin{procedure}{GetTempAnsiStringReference}
  2878. \Declaration
  2879. Procedure GetTempAnsiStringReference(Var Ref : TReference);
  2880. \Description
  2881. Allocates \textsf{Ref} on the volatile memory space and sets the
  2882. \textsf{Base} to the Frame{\_}Pointer register and \textsf{Offset} to the
  2883. correct offset to access this allocated memory space.
  2884. \Notes
  2885. The return offset always has the required alignment for the target system.
  2886. \end{procedure}
  2887. \paragraph{persistent memory}
  2888. \begin{function}{GetTempOfSizePersistant}
  2889. \Declaration
  2890. Function GetTempOfSizePersistant(Size : Longint) :Longint;
  2891. \Description
  2892. Allocates persistent storage space on the stack. return value is the
  2893. negative offset from the frame pointer where this memory was allocated.
  2894. \Notes
  2895. The return offset always has the required alignment for the target system.
  2896. \end{function}
  2897. \begin{function}{UngetPersistantTemp}
  2898. \Declaration
  2899. Procedure UnGetPersistantTemp(Pos : Longint);
  2900. \Description
  2901. Frees space allocated as being persistent. This persistent space can then
  2902. later be used and reallocated. \textsf{Pos} is the offset relative to the
  2903. Frame{\_}Pointer of the persistent memory block to free.
  2904. \end{function}
  2905. \paragraph{utility routines}
  2906. \begin{procedure}{ResetTempGen}
  2907. \Declaration
  2908. Procedure ResetTempGen;
  2909. \Description
  2910. Clear and free the complete linked list of temporary memory locations. The
  2911. list is set to nil.
  2912. \Notes
  2913. This routine is called each time a routine has been fully compiled.
  2914. \end{procedure}
  2915. \begin{procedure}{SetFirstTemp}
  2916. \Declaration
  2917. Procedure SetFirstTemp(L : Longint);
  2918. \Description
  2919. This routine sets the start of the temporary local area (this value is a
  2920. negative offset from the Frame{\_}Pointer, which is located after the local
  2921. variables). Usually the start offset is the size of the local variables,
  2922. modified by any alignment requirements.
  2923. \Notes
  2924. This routine is called once before compiling a routine, it indicates the
  2925. start address where to allocate temporary memory space.
  2926. \end{procedure}
  2927. \begin{function}{GetFirstTempSize}
  2928. \Declaration
  2929. Function GetFirstTempSize : Longint;
  2930. \Description
  2931. Returns the total number of bytes allocated for local and temporary
  2932. allocated stack space. This value is aligned according to the target system
  2933. alignment requirements, even if the actual size is not aligned.
  2934. \Notes
  2935. This routine is used by the code generator to get the total number of bytes
  2936. to allocate locally (i.e the stackframe size) in the entry and exit code of
  2937. the routine being compiled.
  2938. \end{function}
  2939. \begin{function}{NormalTempToPersistant}
  2940. \Declaration
  2941. Procedure NormalTempToPersistant(Pos : Longint);
  2942. \Description
  2943. Searches the list of currently temporary memory allocated for the one with
  2944. the offset \textsf{Pos}, and if found converts this temporary memory space
  2945. as persistent (can never be freed and reallocated).
  2946. \end{function}
  2947. \begin{function}{PersistantTempToNormal}
  2948. \Declaration
  2949. Procedure PersistantTempToNormal(Pos : Longint);
  2950. \Description
  2951. Searches the list of currently allocated persistent memory space as the
  2952. specified address \textsf{Pos}, and if found converts this memory space to
  2953. normal volatile memory space which can be freed and reused.
  2954. \end{function}
  2955. \begin{function}{IsTemp}
  2956. \Declaration
  2957. Function IsTemp(const Ref : TReference): Boolean;
  2958. \Description
  2959. Returns TRUE if the reference \textsf{ref }is allocated in temporary
  2960. volatile memory space, otherwise returns FALSE.
  2961. \end{function}
  2962. \subsection{Assembler generation}
  2963. \label{subsec:mylabel8}
  2964. \subsubsection{Architecture}
  2965. \label{subsubsec:architectureneration}
  2966. The different architectures on the market today only support certain types
  2967. of operands as assembler instructions. The typical format of an assembler
  2968. instruction has the following format:
  2969. \begin{center}
  2970. \textsf{OPCODE [opr1,opr2[,opr3][\ldots ]]}
  2971. \end{center}
  2972. The opcode field is a mnemonic for a specific assembler instruction, such as
  2973. \textsf{MOV} on the 80x86, or \textsf{ADDX} on the 680x0. Furthermore, in
  2974. most cases, this mnemonic is followed by zero to three operands which can be
  2975. of the following types:
  2976. Possible Operand Types
  2977. \begin{itemize}
  2978. \item a LABEL or SYMBOL (to code or data)
  2979. \item a REGISTER (one of the predefined hardware registers)
  2980. \item a CONSTANT (an immediate value)
  2981. \item a MEMORY EXPRESSION (indirect addressing through offsets, symbols, and
  2982. address registers)
  2983. \end{itemize}
  2984. In the compiler, this concept of different operand types has been directly
  2985. defined for easier generation of assembler output. All opcodes generated by
  2986. the code generator are stored in a linked list of opcodes which contain
  2987. information on the operand types, The opcode and the size (which is
  2988. important to determine on what size the operand must be operated on) are
  2989. stored in that linked list.
  2990. The possible operand sizes for the code generator are as follows (a
  2991. enumeration of type \textsf{topsize}):
  2992. \begin{longtable}{|l|p{10cm}|}
  2993. \hline
  2994. Operand size enum (\textsf{topsize}) & Description \\
  2995. \hline
  2996. \endhead
  2997. \hline
  2998. \endfoot
  2999. \textsf{S{\_}B}& 8-bit integer operand \\
  3000. \textsf{S{\_}W}& 16-bit integer operand \\
  3001. \textsf{S{\_}L}& 32-bit integer operand \\
  3002. \textsf{S{\_}Q}& 64-bit integer operand \\
  3003. \textsf{S{\_}FS}& 32-bit IEEE 754 Single floating point operand \\
  3004. \textsf{S{\_}FL}& 64-bit IEEE 754 Double floating point operand \\
  3005. \textsf{S{\_}FX}& Extended point floating point operand (cpu-specific) \\
  3006. \textsf{S{\_}CPU}& A constant equal to one of the previous sizes (natural size of operands) \\
  3007. \end{longtable}
  3008. The possible operand types for the code generator are as follows (other
  3009. might be added as required by the target architecture):
  3010. \begin{longtable}{|l|p{10cm}|}
  3011. \hline
  3012. Operand type (\textsf{TOpType}) & Description \\
  3013. \hline
  3014. \endhead
  3015. \hline
  3016. \endfoot
  3017. \textsf{top{\_}None}& No operand \\
  3018. \textsf{top{\_}Reg}& Operand is a register \\
  3019. \textsf{top{\_}Ref}& Operand is a reference (\textsf{treference} type) \\
  3020. \textsf{top{\_}Symbol}& Operand is a symbol (reference or label) \\
  3021. \end{longtable}
  3022. The architecture specific opcodes are done in an enumeration of type
  3023. \textsf{tasmop}. An example of an enumeration for some of the opcodes of the
  3024. PowerPC 32-bit architecture is as follows:
  3025. \begin{lstlisting}{}
  3026. Type TAsmOp = (a_Add, a_Add_, a_Addo, a_Addo_, a_Addc, a_Addc_, a_Addco,
  3027. a_Addco_,a_Adde, a_Adde_, a_Addeo, a_Addeo_, a_Addi,
  3028. a_Addic, a_Addic_, a_Addis \ldots
  3029. \end{lstlisting}
  3030. \subsubsection{Generic instruction generation interface}
  3031. \label{subsubsec:generic}
  3032. To independently generate code for different architectures, wrappers for the
  3033. most used instructions in the code generator have been created which are
  3034. totally independent of the target system.
  3035. \paragraph{Load / store instructions}
  3036. \begin{procedurel}{Emit\_Load\_Loc\_Reg}{EmitLoadLocReg}
  3037. \Declaration
  3038. Procedure Emit{\_}Load{\_}Loc{\_}Reg(Src:TLocation;Srcdef:pDef; DstDef : pDef; Dst : TRegister);
  3039. \Description
  3040. Loads an operand from the source location in \textsf{Src} into the
  3041. destination register \textsf{Dst} taking into account the source definition
  3042. and destination definition (sign-extension, zero extension depending on the
  3043. sign and size of the operands).
  3044. \Notes
  3045. The source location can only be in LOC{\_}REGISTER, LOC{\_}CREGISTER,
  3046. LOC{\_}MEM or LOC{\_}REFERENCE otherwise an internal error will occur. This
  3047. generic opcode does not work on floating point values, only integer values.
  3048. \end{procedurel}
  3049. \begin{procedure}{FloatLoad}
  3050. \Declaration
  3051. Procedure FloatLoad(t : tFloatType;Ref : TReference; Var Location:TLocation);
  3052. \Description
  3053. This routine is to be called each time a location must be set to LOC{\_}FPU
  3054. and a value loaded into a FPU register
  3055. \Notes
  3056. The routine sets up the register field of LOC{\_}FPU correctly. The source
  3057. location can only be : LOC{\_}MEM or LOC{\_}REFERENCE. The destination
  3058. location is set to LOC{\_}FPU.
  3059. \end{procedure}
  3060. \begin{function}{FloatStore}
  3061. \Declaration
  3062. Procedure FloatStore(t : TFloatType;Var Location:TLocation; Ref:TReference);
  3063. \Description
  3064. This routine is to be called when a value located in LOC{\_}FPU must be
  3065. stored into memory.
  3066. \Notes
  3067. The destination must be LOC{\_}REFERENCE or LOC{\_}MEM. This routine frees
  3068. the LOC{\_}FPU location \\
  3069. \end{function}
  3070. \begin{functionl}{emit{\_}mov{\_}ref{\_}reg64}{emitmovrefreg64}
  3071. \Declaration
  3072. Procedure Emit{\_}Mov{\_}Ref{\_}Reg64(r : TReference;rl,rh : TRegister);
  3073. \Description
  3074. This routine moves a 64-bit integer value stored in memory location
  3075. \textsf{r} into the low 32-bit register \textsf{rl} and the high 32-bit
  3076. register \textsf{rh}.
  3077. \end{functionl}
  3078. \paragraph{Load address}
  3079. \begin{functionl}{Emit{\_}Lea{\_}Loc{\_}Ref}{emitlealocref}
  3080. \Declaration
  3081. Procedure Emit{\_}Lea{\_}Loc{\_}Ref(Const t:TLocation;Const Ref:TReference; FreeTemp:Boolean);
  3082. \Description
  3083. Loads the address of the location \textsf{loc }and stores the result into
  3084. \textsf{Ref}
  3085. \Notes
  3086. The store address \textsf{ref }should point to an allocated area at least
  3087. \textsf{sizeof(pointer)} bytes, otherwise unexpected code might be
  3088. generated.
  3089. \end{functionl}
  3090. \begin{functionl}{Emit{\_}Lea{\_}Loc{\_}Reg}{Emitlealocreg}
  3091. \Declaration
  3092. Procedure Emit{\_}Lea{\_}Loc{\_}Reg(const t:TLocation;Reg:TRegister;Freetemp:Boolean);
  3093. \Description
  3094. Loads the address of the location \textsf{loc }and stores the result into
  3095. ther target register \textsf{reg}
  3096. \end{functionl}
  3097. \paragraph{Label instructions}
  3098. \begin{procedure}{GetLabel}
  3099. \Declaration
  3100. Procedure GetLabel(Var l : pAsmLabel);
  3101. \Description
  3102. Returns a label associated with code. This label can then be used with the
  3103. instructions output by the code generator using the instruction generation
  3104. templates which require labels as parameters. The label itself can be
  3105. emitted to the assembler source by calling the \seep{EmitLab} routine.
  3106. \end{procedure}
  3107. \begin{procedure}{EmitLab}
  3108. \Declaration
  3109. Procedure EmitLab(Var l : pAsmLabel);
  3110. \Description
  3111. Output the label \textsf{l} to the assembler instruction stream.
  3112. \Notes
  3113. The label should have been previously allocated with \textsf{GetLabel}, The
  3114. output label will be of the form label: in the instruction stream. This
  3115. label is usually a jump target.
  3116. \end{procedure}
  3117. \begin{procedure}{EmitLabeled}
  3118. \Declaration
  3119. Procedure EmitLabeled(op : TAsmOp; Var l : pAsmLabel);
  3120. \Description
  3121. Output the opcode \textsf{op} with the operand \textsf{l}
  3122. which is a previously allocated label.
  3123. \Notes
  3124. This routine is used to output jump instructions such as : jmp label, jne
  3125. label. The label should have been previously allocated with a call to
  3126. \textsf{GetLabel}
  3127. \end{procedure}
  3128. \paragraph{Other instructions}
  3129. \begin{function}{EmitCall}
  3130. \Declaration
  3131. Procedure EmitCall(Const Routine:String);
  3132. \Description
  3133. Emit a call instruction to an internal routine
  3134. \Parameters
  3135. Routine = The name of the routine to call.
  3136. \end{function}
  3137. \begin{procedure}{ConcatCopy}
  3138. \Declaration
  3139. procedure ConcatCopy(Source,Dest : TReference;Size : Longint;DelSource : Boolean; loadref:boolean);
  3140. \Description
  3141. This routine copies \textsf{Size} data from the \textsf{Source} reference to
  3142. the destination \textsf{Dest} reference. \\
  3143. \Parameters
  3144. Source = Source reference to copy from \par
  3145. Dest = Depending on the value of loadref, either indicates a location where a pointer to the data to copy is
  3146. Stored, or this reference directly the address to copy to. \par
  3147. Size = Number of bytes to copy \par
  3148. DelSource = TRUE if the source reference should be freed in this routine \par
  3149. LoadRef = TRUE if the source reference contains a pointer to the address we
  3150. wish to copy to, otherwise the reference itself is the destination
  3151. location to copy to.
  3152. \end{procedure}
  3153. \begin{procedurel}{Emit{\_}Flag2Reg}{emitflag2reg}
  3154. \Declaration
  3155. Procedure Emit{\_}Flag2Reg(Flag:TResflags;HRegister:TRegister);
  3156. \Description
  3157. Sets the value of the register to 1 if the condition code flag in
  3158. \textsf{Flag} is TRUE, otherwise sets the register to zero.
  3159. \Notes
  3160. The operand should be zero extended to the natural register size for the
  3161. target architecture.
  3162. \end{procedurel}
  3163. \subsubsection{Instruction generation interface}
  3164. \label{subsubsec:instruction}
  3165. \section{The assembler output}
  3166. \label{sec:mylabel8}
  3167. All code is generated via special linked lists of instructions. The base of
  3168. this is a special object, an abstract assembler which implements all
  3169. directives which are usually implemented in the different assemblers
  3170. available on the market . When the code generator and parser generates the
  3171. final output, it is generated as a linked list for each of the sections
  3172. available for the output assembler. Each entry in the linked list is either
  3173. an instruction, or one of the abstract directives for the assembler.
  3174. \begin{figure}
  3175. \ifpdf
  3176. \includegraphics{arch11.pdf}
  3177. %\epsfig{file=arch11.png,width=\textwidth}
  3178. \else
  3179. \includegraphics[width=5.67in,height=2.17in]{arch11.eps}
  3180. \fi
  3181. \label{fig:fig11}
  3182. \caption{Assembler generation organisation}
  3183. \end{figure}
  3184. % FIXME
  3185. % If I don't do this, the assembler node table has a problem.
  3186. % untested for more recent versions (with less floats due to longtable)
  3187. \clearpage
  3188. The different possible sections which are output are as follows:
  3189. \begin{center}
  3190. Section lists for the assembler output
  3191. \end{center}
  3192. \begin{longtable}{|l|p{10cm}|}
  3193. \hline
  3194. Internal section name & Description \\
  3195. \hline
  3196. \endhead
  3197. \hline
  3198. \endfoot
  3199. ExparAsmList & temporary list \\
  3200. DataSegment & initialized variables \\
  3201. CodeSegment & instructions and general code directives \\
  3202. DebugList & debugging information \\
  3203. WithDebugList & ??????????????? \\
  3204. Consts & read only constants \\
  3205. ImportSection & imported symbols \\
  3206. ExportSection & exported symbols \\
  3207. ResourceSection & Resource data \\
  3208. RttiList & runtime type information data \\
  3209. ResourceStringList& resource string data
  3210. \end{longtable}
  3211. The following directives for the abstract assembler currently exist:
  3212. Abstract assembler node types:
  3213. \begin{longtable}{|l|p{10cm}|}
  3214. \hline
  3215. Node entry Type & Description \\
  3216. \hline
  3217. \endhead
  3218. \hline
  3219. \endfoot
  3220. Ait{\_}None&
  3221. This entry in the linked list is invalid (this should
  3222. normally never occur) \\
  3223. Ait{\_}Direct&
  3224. Direct output to the resulting assembler file (as string) \\
  3225. Ait{\_}String&
  3226. Shortstring with a predefined length \\
  3227. Ait{\_}Label&
  3228. Numbered assembler label used for jumps \\
  3229. Ait{\_}Comment&
  3230. Assembler output comment \\
  3231. Ait{\_}Instruction&
  3232. Processor specific instruction \\
  3233. Ait{\_}DataBlock&
  3234. Unitialized data block (BSS) \\
  3235. Ait{\_}Symbol&
  3236. Entry represents a symbol (exported, imported, or other public
  3237. symbol type) \newline
  3238. Possible symbol types : NONE, EXTERNAL, LOCAL and GLOBAL \newline
  3239. eg : A symbol followed by an Ait{\_}const{\_}32bit \\
  3240. Ait{\_}Symbol{\_}End &
  3241. Symbol end (for example the end of a routine) \\
  3242. Ait{\_}Const{\_}32bit&
  3243. Initialized 32-bit constant (without a symbol) \\
  3244. Ait{\_}Const{\_}16bit&
  3245. Initialized 16-bit constant (without a symbol) \\
  3246. Ait{\_}Const{\_}8bit&
  3247. Initialized 8-bit constant (without a symbol) \\
  3248. Ait{\_}Const{\_}symbol & ???????????? \\
  3249. Ait{\_}Real{\_}80bit (x86)&
  3250. Initialized 80-bit floating point constant (without symbol) \\
  3251. Ait{\_}Real{\_}64bit&
  3252. Initialized Double IEEE floating point constant (without symbol) \\
  3253. Ait{\_}Real{\_}32bit&
  3254. Initialized Single IEEE floating point constant (without symbol) \\
  3255. Ait{\_}Comp{\_}64bit (x86)&
  3256. Initialized 64-bit floating point integer (without symbol) \\
  3257. Ait{\_}Align&
  3258. Alignment directive \\
  3259. Ait{\_}Section&
  3260. Section directive \\
  3261. Ait{\_}const{\_}rva (Win32)& \\
  3262. Ait{\_}Stabn &
  3263. stabs debugging information (numerical value) \\
  3264. Ait{\_}Stabs &
  3265. stabs debugging information (string) \\
  3266. Ait{\_}Force{\_}Line&
  3267. stabs debugging line information \\
  3268. Ait{\_}Stab{\_}Function{\_}Name&
  3269. stabs debug information routine name \\
  3270. Ait{\_}Cut&
  3271. Cut in the assembler files (used for smartlinking) \\
  3272. Ait{\_}RegAlloc&
  3273. Debugging information for the register allocator \\
  3274. Ait{\_}Marker & ???????????? \\
  3275. Ait{\_}Frame (Alpha)& \\
  3276. Ait{\_}Ent (Alpha)& \\
  3277. Ait{\_}Labeled{\_}Instruction (m68k)& \\
  3278. Ait{\_}Dummy & Unused - should never appear
  3279. \end{longtable}
  3280. \section{The Runtime library}
  3281. \label{sec:mylabel9}
  3282. This section describes the requirements of the internal routines which MUST
  3283. be implemented for all relevant platforms to port the system unit to a new
  3284. architecture or operating system.
  3285. The following defines are available when compiling the runtime library:
  3286. \begin{longtable}{|l|p{10cm}|}
  3287. \hline
  3288. Define Name & Description \\
  3289. \hline
  3290. \endhead
  3291. \hline
  3292. \endfoot
  3293. i386 & Intel 80x86 family of processors (and compatibles) \\
  3294. m68k & Motorola 680x0 family of processors (excludes coldfire) \\
  3295. alpha & Alpha 21x64 family of processors \\
  3296. powerpc & Motorola / IBM 32-bit family of processors \\
  3297. sparc & SPARC v7 compatible processors
  3298. \end{longtable}
  3299. \begin{longtable}{|l|p{10cm}|}
  3300. \hline
  3301. Define name & Description \\
  3302. \hline
  3303. \endhead
  3304. \hline
  3305. \endfoot
  3306. RTLLITE&
  3307. Removes some extraneous routine from compilation (system unit
  3308. is minimal). Mvdv: Afaik the status of this is unknown \\
  3309. DEFAULT{\_}EXTENDED&
  3310. The runtime library routines dealing with fixed point values have the
  3311. \textsf{extended} type instead of the \textsf{real} type. \\
  3312. SUPPORT{\_}SINGLE&
  3313. The compiler supports the \textsf{single} floating point precision type \\
  3314. SUPPORT{\_}DOUBLE&
  3315. The compiler supports the \textsf{double }floating point precision type \\
  3316. SUPPORT{\_}EXTENDED&
  3317. The compiler supports the \textsf{extended }floating point
  3318. precision type \\
  3319. SUPPORT{\_}FIXED&
  3320. The compiler supports the \textsf{fixed} floating point precision type \\
  3321. HASWIDECHAR&
  3322. The compiler supported the \textsf{widechar} character type \\
  3323. INT64&
  3324. The compiler supports 64-bit integer operations \\
  3325. MAC{\_}LINEBREAK&
  3326. Text I/O uses Mac styled line break ({\#}13) instead of {\#}13{\#}10 \\
  3327. SHORT{\_}LINEBREAK&
  3328. Text I/O uses UNIX styled line breaks ({\#}10) instead of {\#}13{\#}10 \\
  3329. EOF{\_}CTRLZ&
  3330. A Ctrl-Z character in a text file is an EOF marker (UNIX mostly) \\
  3331. \end{longtable}
  3332. The following defines are used for fexpand definitions:
  3333. % FIXME Seem to miss a *nix symlink expand behaviour define.
  3334. \begin{longtable}{|l|p{10cm}|}
  3335. \hline
  3336. Define name & Description \\
  3337. \hline
  3338. \endhead
  3339. \hline
  3340. \endfoot
  3341. FPC{\_}EXPAND{\_}DRIVES&
  3342. Different devices with different names (as drives) are
  3343. supported \par (like DOS, Netware, etc\ldots ) \\
  3344. FPC{\_}EXPAND{\_}UNC&
  3345. Universal Naming convention support i.e \par $\backslash \backslash
  3346. < $server-name>$\backslash $<share-name>$\backslash $<directory/filename> \\
  3347. UNIX&
  3348. Unix style file names \\
  3349. FPC{\_}EXPAND{\_}VOLUMES&
  3350. Volume names (i.e. drive descriptions longer than 1
  3351. character) are supported. \\
  3352. FPC{\_}EXPAND{\_}TILDE&
  3353. Replaces the $\sim $ character, with the `HOME' directory
  3354. (mostly on UNIX platforms) \\
  3355. \end{longtable}
  3356. The following defines some debugging routines for the runtime library:
  3357. \begin{longtable}{|l|p{10cm}|}
  3358. \hline
  3359. Define Name & Description \\
  3360. \hline
  3361. \endhead
  3362. \hline
  3363. \endfoot
  3364. DEFINE NAME & Description \\
  3365. ANSISTRDEBUG & Add Debug routines for ansi string support \\
  3366. EXCDEBUG & Add Debug routines for exception debugging \\
  3367. LOGGING & Log the operations to a file \\
  3368. \end{longtable}
  3369. \subsection{Operating system hooks}
  3370. \label{subsec:operating}
  3371. This section contains information on all routines which should be hooked and
  3372. implemented to be able to compile and use the system unit for a new
  3373. operating system:
  3374. \begin{functionl}{System{\_}Exit}{systemexit}
  3375. \Declaration
  3376. Procedure System{\_}Exit;
  3377. \Description
  3378. This routine is internally called by the system unit when the application
  3379. exits.
  3380. \Notes
  3381. This routine should actually exit the application. It should exit with the
  3382. error code specified in the \textsf{ExitCode} variable.
  3383. \Algorithm
  3384. Exit application with ExitCode value.
  3385. \end{functionl}
  3386. \begin{function}{ParamCount}
  3387. \Declaration
  3388. Function ParamCount : Longint;
  3389. \end{function}
  3390. \begin{procedure}{Randomize}
  3391. \Declaration
  3392. Procedure Randomize;
  3393. \Description
  3394. This routine initializes the built-in random generator with a random value.
  3395. \Notes
  3396. This routine is used by random
  3397. \Algorithm
  3398. Randseed := pseudo random 32-bit value
  3399. \end{procedure}
  3400. \begin{function}{GetHeapStart}
  3401. \Declaration
  3402. Function GetHeapStart : Pointer;
  3403. \Description
  3404. This routine should return a pointer to the start of the heap area.
  3405. \Notes
  3406. GetHeapStart := address of start of heap.
  3407. \end{function}
  3408. \begin{function}{GetHeapSize}
  3409. \Declaration
  3410. Function GetHeapSize : Longint;
  3411. \Description
  3412. This routine should return the total heap size in bytes
  3413. \Parameters
  3414. \Algorithm
  3415. GetHeapSize := total size of the initial heap area.
  3416. \end{function}
  3417. \begin{function}{sbrk}
  3418. \Declaration
  3419. Function SBrk(Size : Longint): Longint;
  3420. \Description
  3421. \end{function}
  3422. \begin{procedurel}{Do{\_}Close}{doclose}
  3423. \Declaration
  3424. Procedure Do{\_}Close(Handle : Longint);
  3425. \Description
  3426. This closes the file specified of the specified handle number.
  3427. \Parameters
  3428. handle = file handle of file to close
  3429. \Notes
  3430. This routine should close the specified file.
  3431. \end{procedurel}
  3432. \begin{functionl}{Do{\_}Erase}{doerase}
  3433. \Declaration
  3434. Procedure Do{\_}Erase(p: pChar);
  3435. \Description
  3436. This erases the file specifed by p.
  3437. \Parameters
  3438. p = name of the file to erase
  3439. \Notes
  3440. \end{functionl}
  3441. The following variables should also be defined for each new operating
  3442. system, they are used by external units:
  3443. \noindent
  3444. ArgC : The number of command line arguments of the program
  3445. \noindent
  3446. ArgV : A pointer to each of the command line arguments (an array of pchar
  3447. pointers)
  3448. \subsection{CPU specific hooks}
  3449. \label{subsec:mylabel9}
  3450. The following routines must absolutely be implemented for each processor, as
  3451. they are dependent on the processor:
  3452. \subsubsection{FPC{\_}SETJMP}
  3453. \label{subsubsec:mylabel30}
  3454. \begin{function}{SetJmp}
  3455. \Declaration
  3456. Function SetJmp (Var S : Jmp{\_}Buf) : Longint;
  3457. \Description
  3458. A call to SetJmp(), saves the calling environment in its \textsf{s} argument
  3459. for later use by \textsf{longjmp()}. Called by the code generator in
  3460. exception handling code. The return value should be zero.
  3461. \Notes
  3462. This routine should save / restore all used registers (except the
  3463. accumulator which should be cleared).
  3464. \end{function}
  3465. \subsubsection{FPC{\_}LONGJMP}
  3466. \label{subsubsec:mylabel31}
  3467. \subsubsection{function SPtr()}
  3468. \label{subsubsec:function}
  3469. \subsubsection{function Get{\_}Caller{\_}Frame(framebp:longint):longint;}
  3470. \label{subsubsec:mylabel32}
  3471. \subsubsection{function Get{\_}Caller{\_}Addr(framebp:longint):longint;}
  3472. \label{subsubsec:mylabel33}
  3473. \subsubsection{function Get{\_}Frame:longint;}
  3474. \label{subsubsec:mylabel34}
  3475. \subsubsection{function Trunc()}
  3476. \label{subsubsec:mylabel35}
  3477. \subsection{String related}
  3478. \label{subsec:string}
  3479. \subsubsection{FPC{\_}SHORTSTR{\_}COPY}
  3480. \label{subsubsec:mylabel36}
  3481. \begin{procedurel}{Int{\_}StrCopy}{intstrcopy}
  3482. \Declaration
  3483. Procedure Int{\_}StrCopy(Len:Longint;SStr,DStr:pointer);
  3484. \Description
  3485. This routine copies the string pointed to by the address in sstr, to the
  3486. string pointed in the destination. The old string is overwritten, and the
  3487. source string will be truncated to make it fit in destination if the length
  3488. of the source is greater then destination string len (the len parameter).
  3489. \Parameters
  3490. Len = maximum length to copy (the destination string length) \par
  3491. SStr = pointer to source shortstring \par
  3492. DStr = point to destination shortstring
  3493. \Notes
  3494. Called by code generator when a string is assigned to another string.
  3495. \end{procedurel}
  3496. \subsubsection{FPC{\_}SHORTSTR{\_}COMPARE}
  3497. \label{subsubsec:mylabel37}
  3498. \begin{functionl}{Int{\_}StrCmp}{intstrcmp}
  3499. \Declaration
  3500. Function Int{\_}StrCmp(DStr,SStr:Pointer) : Longint;
  3501. \Description
  3502. The routine compares two shortstrings, and returns 0 if both are equal, 1 if
  3503. \textsf{DStr} is greater then \textsf{SSrc}, otherwise it returns --1.
  3504. \Notes
  3505. Both pointers must point to shortstrings. Length checking must be performed
  3506. in the routine.
  3507. \end{functionl}
  3508. \subsubsection{FPC{\_}SHORTSTR{\_}CONCAT}
  3509. \label{subsubsec:mylabel38}
  3510. \begin{procedurel}{Int{\_}StrConcat}{intstrconcat}
  3511. \Declaration
  3512. Procedure Int{\_}StrConcat(Src,Dest:Pointer);
  3513. \Description
  3514. This routine appends the string pointed to by \textsf{Src} to the end of the
  3515. string pointed to by \textsf{Dest}.
  3516. \Parameters
  3517. Src = pointer to shortstring to append to dest \par
  3518. Dest = pointer to shortstring to receive appended string
  3519. \Notes
  3520. Both pointers must point to shortstrings. In the case where the src string
  3521. length does not fit in dest, it is truncated.
  3522. \Algorithm
  3523. \begin{lstlisting}{}
  3524. if src =nil or dest = nil then
  3525. exit routine;
  3526. if (src string length + dest string length) > 255 then
  3527. number of bytes to copy = 255 -- dest string length
  3528. else
  3529. number of bytes to copy = src string length;
  3530. copy the string data (except the length byte)
  3531. dest string length = dest string length + number of bytes to copied
  3532. \end{lstlisting}
  3533. \end{procedurel}
  3534. \subsubsection{FPC{\_}ANSISTR{\_}CONCAT}
  3535. \label{subsubsec:mylabel39}
  3536. \begin{procedurel}{AnsiStr{\_}Concat}{ansistrconcat}
  3537. \Declaration
  3538. Procedure AnsiStr{\_}Concat(s1,s2:Pointer;var s3:Pointer);
  3539. \Description
  3540. This routine appends \textsf{s1}+\textsf{s2} and stores the result at the
  3541. address pointed to by \textsf{s3}.
  3542. \Notes
  3543. All pointers must point to ansistrings.
  3544. \end{procedurel}
  3545. \subsubsection{FPC{\_}ANSISTR{\_}COMPARE}
  3546. \label{subsubsec:mylabel40}
  3547. \begin{functionl}{AnsiStr{\_}Compare}{ansistrcompare}
  3548. \Declaration
  3549. Function AnsiStr{\_}Compare(s1,s2 : Pointer): Longint;
  3550. \Description
  3551. The routine compares two ansistrings, and returns 0 if both are equal, 1 if
  3552. \textsf{s1} is greater then \textsf{s2}, otherwise it returns --1.
  3553. \Parameters
  3554. Both pointers must point to ansistrings.
  3555. \end{functionl}
  3556. \subsubsection{FPC{\_}ANSISTR{\_}INCR{\_}REF }
  3557. \label{subsubsec:mylabel41}
  3558. \begin{procedurel}{AnsiStr{\_}Incr{\_}Ref}{ansistrincrref}
  3559. \Declaration
  3560. procedure AnsiStr{\_}Incr{\_}Ref (var s : Pointer);
  3561. \Description
  3562. This routine simply increments the ANSI string reference count, which is
  3563. used for garbage collection of ANSI strings.
  3564. \Parameters
  3565. s = pointer to the ansi string (including the header structure)
  3566. \end{procedurel}
  3567. \subsubsection{FPC{\_}ANSISTR{\_}DECR{\_}REF }
  3568. \label{subsubsec:mylabel42}
  3569. \begin{procedurel}{AnsiStr{\_}Decr{\_}Ref}{ansistrdecrref}
  3570. \Declaration
  3571. procedure AnsiStr{\_}Decr{\_}Ref (Var S : Pointer);
  3572. \Parameters
  3573. s = pointer to the ansi string (including the header structure)
  3574. \Algorithm
  3575. Decreases the internal reference count of this non constant ansistring; If
  3576. the reference count is zero, the string is deallocated from the
  3577. heap.
  3578. \end{procedurel}
  3579. \subsubsection{FPC{\_}ANSISTR{\_}ASSIGN }
  3580. \label{subsubsec:mylabel43}
  3581. \begin{functionl}{AnsiStr{\_}Assign}{ansistrassign}
  3582. \Declaration
  3583. Procedure AnsiStr{\_}Assign (var s1 : Pointer;s2 : Pointer);
  3584. \Parameters
  3585. s1 = address of ANSI string to be assigned to \par
  3586. s2 = address of ANSI string which will be assigned
  3587. \Algorithm
  3588. Assigns S2 to S1 (S1:=S2), also by the time decreasing the reference count
  3589. to S1 (it is no longer used by this variable).
  3590. \end{functionl}
  3591. \subsubsection{FPC{\_}PCHAR{\_}TO{\_}SHORTSTR}
  3592. \label{subsubsec:mylabel44}
  3593. \begin{function}{StrPas}
  3594. \Declaration
  3595. Function StrPas(p:pChar):ShortString;
  3596. \Description
  3597. Copies and converts a null-terminated string (pchar) to a shortstring with
  3598. length checking.
  3599. \Parameters
  3600. p = pointer to null terminated string to copy
  3601. \Notes
  3602. Length checking is performed. Verifies also p=nil, and if so sets the
  3603. shortstring length to zero. Called by the type conversion generated code of
  3604. code generator.
  3605. \Algorithm
  3606. \begin{lstlisting}{}
  3607. if p=nil then
  3608. string length =0
  3609. else
  3610. string length =string length(p)
  3611. if string length>255 then
  3612. string length = 255
  3613. if string length>0 then
  3614. Copy all characters of pchar array to string (except length byte)
  3615. \end{lstlisting}
  3616. \end{function}
  3617. \subsubsection{FPC{\_}SHORTSTR{\_}TO{\_}ANSISTR}
  3618. \label{subsubsec:mylabel45}
  3619. \begin{functionl}{FPC{\_}ShortStr{\_}To{\_}AnsiStr}{fpcshortstrtoansistr}
  3620. \Notes
  3621. Called by the type conversion generated code of code generator.
  3622. \end{functionl}
  3623. \subsubsection{FPC{\_}STR{\_}TO{\_}CHARARRAY}
  3624. \label{subsubsec:mylabel46}
  3625. \begin{procedurel}{Str{\_}To{\_}CharArray}{strtochararray}
  3626. \Declaration
  3627. procedure Str{\_}To{\_}CharArray(StrTyp, ArraySize: Longint; src,dest: pChar);
  3628. \Description
  3629. 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.
  3630. \Parameters
  3631. strtyp = Indicates the conversion type to do (0 = shortstring, 1 =
  3632. ansistring, 2 = longstring, 3 = widestring) \\
  3633. arraysize = size of the destination array \par
  3634. src = pointer to source string \par
  3635. dest = pointer to character array
  3636. \Notes
  3637. Called by the type conversion generated code of code generator when
  3638. converting a string to an array of char. If the size of the string is less
  3639. then the size of the array, the rest of the array is filled with zeros.
  3640. \end{procedurel}
  3641. \subsubsection{FPC{\_}CHARARRAY{\_}TO{\_}SHORTSTR}
  3642. \label{subsubsec:mylabel47}
  3643. \begin{function}{StrCharArray}
  3644. \Declaration
  3645. Function StrCharArray(p:pChar; l : Longint):ShortString;
  3646. \Description
  3647. Copies a character array to a shortstring with length checking (upto 255
  3648. characters are copied)
  3649. \Parameters
  3650. p = Character array pointer \par
  3651. l = size of the array
  3652. \Notes
  3653. Called by the type conversion generated code of code generator when
  3654. converting an array of char to a shortstring.
  3655. \Algorithm
  3656. \begin{lstlisting}{}
  3657. if size of array >= 256 then
  3658. length of string =255
  3659. else
  3660. if size of array < 0 then
  3661. length of string = 0
  3662. else
  3663. length of string = size of array
  3664. Copy all characters from array to shortstring
  3665. \end{lstlisting}
  3666. \end{function}
  3667. \subsubsection{FPC{\_}CHARARRAY{\_}TO{\_}ANSISTR}
  3668. \label{subsubsec:mylabel48}
  3669. \begin{functionl}{Fpc{\_}Chararray{\_}To{\_}AnsiStr}{chararraytoansistr}
  3670. \Notes
  3671. Called by the type conversion generated code of code generator when converting an array of char to an ansistring.
  3672. \end{functionl}
  3673. \subsubsection{FPC{\_}CHAR{\_}TO{\_}ANSISTR}
  3674. \label{subsubsec:mylabel49}
  3675. \begin{functionl}{Fpc{\_}Char{\_}To{\_}AnsiStr}{fpcchartoansistr}
  3676. \Notes
  3677. Called by the type conversion generated code of code generator when
  3678. converting a char to an ansistring.
  3679. \end{functionl}
  3680. \subsubsection{FPC{\_}PCHAR{\_}TO{\_}ANSISTR}
  3681. \label{subsubsec:mylabel50}
  3682. \begin{functionl}{Fpc{\_}pChar{\_}To{\_}AnsiStr}{fpcpchartoansistr}
  3683. \Notes
  3684. Called by the type conversion generated code of code generator when
  3685. converting a pchar to an ansistring.
  3686. \end{functionl}
  3687. % maybe not necessary anymore (since the amount of tables decreased
  3688. % by "macrofying" the procedure definitions)
  3689. \ifpdf
  3690. \clearpage
  3691. \fi
  3692. \subsection{Compiler runtime checking}
  3693. \label{subsec:compiler}
  3694. \subsubsection{FPC{\_}STACKCHECK}
  3695. \label{subsubsec:mylabel51}
  3696. \begin{procedurel}{Int{\_}StackCheck}{intstackcheck}
  3697. \Declaration
  3698. procedure int{\_}stackcheck (stack{\_}size:longint;
  3699. \Description
  3700. This routine is used to check if there will be a stack overflow when trying
  3701. to allocate stack space from the operating system. The routine must preserve
  3702. all registers. In the case the stack limit is reached, the routine calls the
  3703. appropriate error handler.
  3704. \Parameters
  3705. stack{\_}size = The amount of stack we wish to allocate
  3706. \Notes
  3707. Inserted in the entry code of a routine in the {\{}{\$}S+{\}} state by the code generator
  3708. \Algorithm
  3709. \begin{lstlisting}{}
  3710. if ((StackPointer -- stack{\_}size) < System.StackLimit) then
  3711. Throw a Runtime error with error code 202 (stack overflow)
  3712. \end{lstlisting}
  3713. \end{procedurel}
  3714. \clearpage
  3715. \subsubsection{FPC{\_}RANGEERROR}
  3716. \label{subsubsec:mylabel52}
  3717. \begin{procedurel}{Int{\_}RangeError}{intrangerror}
  3718. \Declaration
  3719. procedure Int{\_}RangeError;
  3720. \Description
  3721. This routine is called when a range check error is detected when executing
  3722. the compiled code. This usually simply calls the default error handler, with
  3723. the correct runtime error code to produce.
  3724. \Parameters
  3725. Inserted in code generator when a Runtime error 201 {\{}{\$}R+{\}} should be
  3726. generated
  3727. \end{procedurel}
  3728. \subsubsection{FPC{\_}BOUNDCHECK}
  3729. \label{subsubsec:mylabel53}
  3730. \begin{procedurel}{Int{\_}BoundCheck}{intboundcheck}
  3731. \Declaration
  3732. procedure Int{\_}BoundCheck(l : Longint; Range : Pointer);
  3733. \Description
  3734. This routine is called at runtime in {\$}R+ mode to check if accessing
  3735. indexes in a string or array is out of bounds. In this case, the default
  3736. error handler is called, with the correct runtime error code to produce.
  3737. \Parameters
  3738. l = Index we need to check \par
  3739. range = pointer to a structure containing the minimum and maximum allowed
  3740. indexes (points to two 32-bit signed values which are the limits of the
  3741. array to verify).
  3742. \Notes
  3743. Inserted in the generated code after assignments, and array indexing to
  3744. verify if the result of operands is within range (in the {\{}{\$}R+{\}}
  3745. state)
  3746. \end{procedurel}
  3747. \subsubsection{FPC{\_}OVERFLOW}
  3748. \label{subsubsec:mylabel54}
  3749. \begin{procedurel}{Int{\_}OverFlow}{intoverflow}
  3750. \Declaration
  3751. procedure Int{\_}OverFlow;
  3752. \Description
  3753. This routine is called when an overflow is detected when executing the
  3754. compiled code. This usually simply calls the default error handler, with the
  3755. correct runtime error code to produce.
  3756. \Parameters
  3757. Inserted in code generator when a Runtime error 215 {\{}{\$}Q+{\}} should be
  3758. generated.
  3759. \end{procedurel}
  3760. \subsubsection{FPC{\_}CHECK{\_}OBJECT}
  3761. \label{subsubsec:mylabel55}
  3762. \begin{procedurel}{Int{\_}Check{\_}Object}{intcheckobject}
  3763. \Declaration
  3764. procedure Int{\_}Check{\_}Object(vmt : Pointer);
  3765. \Description
  3766. This routine is called at runtime in the {\$}R+ state each time a virtual
  3767. method is called. It verifies that the object constructor has been called
  3768. first to build the VMT of the object, otherwise it throws an Runtime error 210.
  3769. \Parameters
  3770. vmt = Current value of the SELF register
  3771. \Notes
  3772. Call inserted by the code generator before calling the virtual method. This
  3773. routine should save / restore all used registers.
  3774. \Algorithm
  3775. \begin{lstlisting}{}
  3776. if vmt = nil or size of method table =0 then
  3777. Throw a Runtime error with error code 210 (object not initialized)
  3778. \end{lstlisting}
  3779. \end{procedurel}
  3780. \subsubsection{FPC{\_}CHECK{\_}OBJECT{\_}EXT}
  3781. \label{subsubsec:mylabel56}
  3782. \begin{procedurel}{Int{\_}Check{\_}Object{\_}Ext}{intcheckobjectext}
  3783. \Declaration
  3784. procedure Int{\_}Check{\_}Object{\_}Ext(vmt, expvmt : pointer);
  3785. \Description
  3786. 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.
  3787. \Parameters
  3788. vmt = Current value of the SELF register \par
  3789. expvmt = Pointer to TRUE object definition
  3790. \Notes
  3791. Call inserted by the code generator before calling the virtual method. \par
  3792. This routine should save / restore all used registers.
  3793. \Algorithm
  3794. \begin{lstlisting}{}
  3795. if vmt = nil or size of method table =0 then
  3796. Throw a Runtime error with error code 210 (object not initialized)
  3797. Repeat
  3798. If SELF (VMT) <> VMT Address (expvmt) Then
  3799. Get Parent VMT Address
  3800. Else
  3801. Exit;
  3802. until no more ent;
  3803. Throw a Runtime error with error code 220 (Incorrect object reference)
  3804. \end{lstlisting}
  3805. \end{procedurel}
  3806. \subsubsection{FPC{\_}IO{\_}CHECK}
  3807. \label{subsubsec:mylabel57}
  3808. \begin{procedurel}{Int{\_}IOCheck}{intiocheck}
  3809. \Declaration
  3810. procedure Int{\_}IOCheck(addr : longint);
  3811. \Description
  3812. This routine is called after an I/O operation to verify the success of the
  3813. operation when the code is compiled in the {\$}I+ state.
  3814. \Parameters
  3815. addr = currently unused
  3816. \Algorithm
  3817. Check last I/O was successful, if not call error handler.
  3818. \end{procedurel}
  3819. \subsubsection{FPC{\_}HANDLEERROR}
  3820. \label{subsubsec:mylabel58}
  3821. \begin{procedure}{HandleError}
  3822. \Declaration
  3823. procedure HandleError (Errno : longint);
  3824. \Description
  3825. This routine should be called to generate a runtime error either from one of
  3826. the system unit routines or the code generator.
  3827. \Parameters
  3828. Errno = Runtime error to generate
  3829. \Notes
  3830. This routine calls the appropriate existing error handler with the specified
  3831. error code.
  3832. \Algorithm
  3833. \end{procedure}
  3834. \subsubsection{FPC{\_}ASSERT}
  3835. \label{subsubsec:mylabel59}
  3836. \begin{procedurel}{Int{\_}Assert}{intassert}
  3837. \Declaration
  3838. procedure Int{\_}Assert(Const Msg,FName:Shortstring;LineNo,ErrorAddr:Longint);
  3839. \Description
  3840. This routine is called by the code generator in an assert statement. When
  3841. the assertion fails, this routine is called.
  3842. \Parameters
  3843. msg = string to print \par
  3844. Fname = Current filename of source \par
  3845. LineNo = Current line number of source \par
  3846. ErrorAddr = Address of assertion failure
  3847. \end{procedurel}
  3848. \subsection{Exception handling}
  3849. \label{subsec:exception}
  3850. \subsubsection{FPC{\_}RAISEEXCEPTION}
  3851. \label{subsubsec:mylabel60}
  3852. \begin{function}{RaiseExcept}
  3853. \Declaration
  3854. function RaiseExcept (Obj : Tobject; AnAddr,AFrame : Pointer) : Tobject;
  3855. \Description
  3856. Called by the code generator in the raise statement to raise an exception.
  3857. \Parameters
  3858. Obj = Instance of class exception handler \par
  3859. AnAddr = Address of exception \par
  3860. Aframe = Exception frame address
  3861. \Notes
  3862. REGISTERS NOT SAVED???????????
  3863. \end{function}
  3864. \subsubsection{FPC{\_}PUSHEXCEPTADDR}
  3865. \label{subsubsec:mylabel61}
  3866. \begin{function}{PushExceptAddr}
  3867. \Declaration
  3868. function PushExceptAddr (Ft: Longint): PJmp{\_}buf ;
  3869. \Description
  3870. This routine should be called to save the current caller context to be used
  3871. for exception handling, usually called in the context where ANSI strings are
  3872. used (they can raise exceptions), or in a try..finally or on statements to
  3873. save the current context.
  3874. \Parameters
  3875. Ft = Indicates the frame type on the stack (1= Exception frame or 2=Finalize
  3876. frame)
  3877. \Algorithm
  3878. Adds this item to the linked list of stack frame context information saved.
  3879. Allocates a buffer for the jump statement and returns it.
  3880. \end{function}
  3881. \subsubsection{FPC{\_}RERAISE}
  3882. \label{subsubsec:mylabel62}
  3883. \begin{procedure}{ReRaise}
  3884. \Declaration
  3885. procedure ReRaise;
  3886. \Notes
  3887. REGISTERS NOT SAVED???????????
  3888. \end{procedure}
  3889. \subsubsection{FPC{\_}POPOBJECTSTACK}
  3890. \label{subsubsec:mylabel63}
  3891. \begin{function}{PopObjectStack}
  3892. \Declaration
  3893. function PopObjectStack : TObject;
  3894. \Description
  3895. This is called by the code generator when an exception occurs, it is used to
  3896. retrieve the exception handler object from the context information.
  3897. \Notes
  3898. REGISTERS NOT SAVED???????????
  3899. \end{function}
  3900. \subsubsection{FPC{\_}POPSECONDOBJECTSTACK}
  3901. \label{subsubsec:mylabel64}
  3902. \begin{function}{PopSecondObjectStack}
  3903. \Declaration
  3904. function PopSecondObjectStack : TObject;
  3905. \Description
  3906. This is called by the code generator when a double exception occurs, it is
  3907. used to retrieve the second exception handler object from the context
  3908. information.
  3909. \Notes
  3910. REGISTERS NOT SAVED???????????
  3911. \end{function}
  3912. \subsubsection{FPC{\_}DESTROYEXCEPTION}
  3913. \label{subsubsec:mylabel65}
  3914. \begin{procedure}{DestroyException}
  3915. \Declaration
  3916. Procedure DestroyException(o : TObject);
  3917. \Description
  3918. This routine is called by the code generator after the exception handling
  3919. code is complete to destroy the exception object.
  3920. \Parameters
  3921. o = Exception handler object reference
  3922. \Notes
  3923. REGISTERS NOT SAVED?????????????
  3924. \end{procedure}
  3925. \subsubsection{FPC{\_}POPADDRSTACK}
  3926. \label{subsubsec:mylabel66}
  3927. \begin{procedure}{PopAddrStack}
  3928. \Declaration
  3929. procedure PopAddrStack;
  3930. \Description
  3931. Called by the code generator in the finally part of a try statement to
  3932. restore the stackframe and dispose of all the saved context information.
  3933. \Notes
  3934. REGISTERS NOT SAVED??????????
  3935. \end{procedure}
  3936. \subsubsection{FPC{\_}CATCHES}
  3937. \label{subsubsec:mylabel67}
  3938. \begin{function}{Catches}
  3939. \Declaration
  3940. function Catches(Objtype : TExceptObjectClass) : TObject;
  3941. \Description
  3942. This routine is called by the code generator to get the exception handler
  3943. object. ?????????????????
  3944. \Parameters
  3945. ObjType = The exception type class
  3946. \Notes
  3947. REGISTERS NOT SAVED??????????
  3948. \end{function}
  3949. \subsubsection{FPC{\_}GETRESOURCESTRING}
  3950. \label{subsubsec:mylabel68}
  3951. \begin{function}{GetResourceString}
  3952. \Declaration
  3953. function GetResourceString(Const TheTable: TResourceStringTable;Index : longint) : AnsiString;
  3954. \Description
  3955. Called by code generator when a reference to a resource string is made. This
  3956. routine loads the correct string from the resource string section and
  3957. returns the found string (or `' if not found).
  3958. \Parameters
  3959. TheTable = pointer to the resource string table \par
  3960. Index = Index in the resource string table.
  3961. \end{function}
  3962. \subsection{Runtime type information}
  3963. \label{subsec:runtime}
  3964. \subsubsection{FPC{\_}DO{\_}IS}
  3965. \label{subsubsec:mylabel69}
  3966. \begin{functionl}{Int{\_}Do{\_}Is}{intdois}
  3967. \Declaration
  3968. Function Int{\_}Do{\_}Is(AClass : TClass;AObject : TObject) : Boolean;
  3969. \Description
  3970. If \textsf{aclass} is of type \textsf{aobject}, returns TRUE otherwise
  3971. returns FALSE.
  3972. \Parameters
  3973. aclass = class type reference \par
  3974. aobject = Object instance to compare against
  3975. \Notes
  3976. This is called by the code generator when the \textsf{is} operator is used.
  3977. \Algorithm
  3978. \end{functionl}
  3979. \subsubsection{FPC{\_}DO{\_}AS}
  3980. \label{subsubsec:mylabel70}
  3981. \begin{procedurel}{Int{\_}Do{\_}As}{intdoas}
  3982. \Declaration
  3983. Procedure Int{\_}Do{\_}As(AClass : TClass;AObject : TObject)
  3984. \Description
  3985. Typecasts \textsf{aclass} as \textsf{aobject}, with dynamic type checking.
  3986. If the object is not from the correct type class, a runtime error 219 is
  3987. generated. Called by the code generator for the \textsf{as} statement.
  3988. \Parameters
  3989. aclass = Class to typecast to \par
  3990. aobject = Object to typecast
  3991. \end{procedurel}
  3992. \subsubsection{FPC{\_}INITIALIZE }
  3993. \label{subsubsec:mylabel71}
  3994. \begin{procedure}{Initialize}
  3995. \Declaration
  3996. Procedure Initialize (Data,TypeInfo : Pointer);
  3997. \Description
  3998. \Parameters
  3999. data = pointer to the data to initialize \par
  4000. typeinfo = pointer to the type information for this data
  4001. \Notes
  4002. This routine should save / restore all used registers.
  4003. \Algorithm
  4004. Initializes the class data for runtime typed values
  4005. \end{procedure}
  4006. \subsubsection{FPC{\_}FINALIZE}
  4007. \label{subsubsec:mylabel72}
  4008. \begin{procedure}{Finalize}
  4009. \Declaration
  4010. procedure Finalize (Data,TypeInfo: Pointer);
  4011. \Description
  4012. Called by code generator if and only if the reference to finalize <> nil.
  4013. \Parameters
  4014. data = point to the data to finalize \par
  4015. typeinfo = Pointer to the type information of this data
  4016. \Notes
  4017. This routine should save / restore all used registers. Finalizes and frees
  4018. the heap class data for runtime typed values (decrements the reference
  4019. count)
  4020. \end{procedure}
  4021. \subsubsection{FPC{\_}ADDREF}
  4022. \label{subsubsec:mylabel73}
  4023. \begin{procedure}{AddRef}
  4024. \Declaration
  4025. Procedure AddRef (Data,TypeInfo : Pointer);
  4026. \Description
  4027. Called by the code generator for class parameters (property support) of type
  4028. const or value in parameters, to increment the reference count of ANSI
  4029. strings.
  4030. \Notes
  4031. This routine should save / restore all used registers. This routine can be
  4032. called recursively with a very deep nesting level, an assembler
  4033. implementation in suggested.
  4034. \end{procedure}
  4035. \subsubsection{FPC{\_}DECREF}
  4036. \label{subsubsec:mylabel74}
  4037. \begin{procedure}{DecRef}
  4038. \Declaration
  4039. Procedure DecRef (Data, TypeInfo : Pointer);
  4040. \Description
  4041. Called by the code generator for class parameters (property support) of type
  4042. const or value parameters, to decrement the reference count. of ANSI
  4043. strings.
  4044. \Parameters
  4045. \Notes
  4046. This routine should save / restore all used registers. This routine can be
  4047. called recursively with a very deep nesting level, an assembler
  4048. implementation in suggested.
  4049. \end{procedure}
  4050. \subsection{Memory related}
  4051. \label{subsec:memory}
  4052. \clearpage
  4053. \subsubsection{FPC{\_}GETMEM}
  4054. \label{subsubsec:mylabel75}
  4055. \begin{procedure}{GetMem}
  4056. \Declaration
  4057. procedure GetMem(Var p:Pointer;Size:Longint);
  4058. \end{procedure}
  4059. \subsubsection{FPC{\_}FREEMEM}
  4060. \label{subsubsec:mylabel76}
  4061. \begin{procedure}{FreeMem}
  4062. \Declaration
  4063. Procedure FreeMem(Var P:Pointer;Size:Longint);
  4064. \end{procedure}
  4065. \subsubsection{FPC{\_}CHECKPOINTER}
  4066. \label{subsubsec:mylabel77}
  4067. \begin{function}{CheckPointer}
  4068. \Declaration
  4069. Procedure CheckPointer(p : Pointer);
  4070. \Description
  4071. Called by the code generator when a pointer is referenced in heap debug
  4072. mode. Verifies that the pointer actually points in the heap area.
  4073. \Parameters
  4074. p = pointer to check
  4075. \Notes
  4076. This routine should save /restore all used registers.
  4077. \end{function}
  4078. \subsubsection{FPC{\_}DO{\_}EXIT}
  4079. \label{subsubsec:mylabel78}
  4080. \begin{procedurel}{Do{\_}Exit}{doexit}
  4081. \Declaration
  4082. procedure Do{\_}Exit;
  4083. \Description
  4084. Called by code generator at the end of the program entry point.
  4085. \Notes
  4086. Called to terminate the program
  4087. \Algorithm
  4088. Call all unit exit handlers. \par
  4089. Finalize all units which have a finalization section \par
  4090. Print runtime error in case of error\par
  4091. Call OS-dependant system{\_}exit routine
  4092. \end{procedurel}
  4093. \subsubsection{FPC{\_}ABSTRACTERROR}
  4094. \label{subsubsec:mylabel79}
  4095. \begin{function}{AbstractError}
  4096. \Declaration
  4097. procedure AbstractError;
  4098. \Description
  4099. The code generator allocates a VMT entry equal to this routine address when
  4100. a method of a class is declared as being abstract. This routine simply calls
  4101. the default error handler.
  4102. \Algorithm
  4103. Throw a Runtime error with error code 211 (Abstract call)
  4104. \end{function}
  4105. \subsubsection{FPC{\_}INITIALIZEUNITS}
  4106. \label{subsubsec:mylabel80}
  4107. \begin{function}{InitializeUnits}
  4108. \Declaration
  4109. \Description
  4110. Called by the code generator in the main program, this is only available if
  4111. an \textsf{initialization} section exists in one of the units used by the
  4112. program.
  4113. \end{function}
  4114. \subsubsection{FPC{\_}NEW{\_}CLASS (assembler)}
  4115. \label{subsubsec:mylabel81}
  4116. \begin{procedurel}{int{\_}new{\_}class}{intnewclass}
  4117. \Description
  4118. This routine will call the TObject.InitInstance() routine to
  4119. instantiate a class (Delphi-styled class) and allocate the memory for all
  4120. fields of the class.
  4121. On entry the self{\_}register should be valid, and should point either to
  4122. nil, for a non-initialized class, or to the current instance of the class.
  4123. The first parameter on the top of the stack should be a pointer to the VMT
  4124. table for this class(????).
  4125. \end{procedurel}
  4126. \subsubsection{FPC{\_}HELP{\_}DESTRUCTOR}
  4127. \label{subsubsec:mylabel82}
  4128. Could be implemented in ASM directly with register parameter passing.
  4129. \begin{procedurel}{Int{\_}Help{\_}Destructor}{inthelpdestructor}
  4130. \Declaration
  4131. Procedure Int{\_}Help{\_}Destructor(Var {\_}Self : Pointer; Vmt : Pointer; Vmt{\_}Pos : Cardinal);
  4132. \Description
  4133. Frees the memory allocated for the object fields, and if the object had a
  4134. VMT field, sets it to nil.
  4135. \Parameters
  4136. self = pointer to the object field image in memory \par
  4137. vmt = pointer to the the actual vmt table (used to get the size of the object) \par
  4138. vmt{\_}pos = offset in the object field image to the vmt pointer field
  4139. \Notes
  4140. This routine should / save restore all used registers.
  4141. \Algorithm
  4142. \begin{lstlisting}{}
  4143. if self = nil then
  4144. exit
  4145. set VMT field in object field image ,if present, to nil
  4146. Free the allocated heap memory for the field objects
  4147. set Self = nil
  4148. \end{lstlisting}
  4149. \end{procedurel}
  4150. \subsubsection{FPC{\_}HELP{\_}CONSTRUCTOR}
  4151. \label{subsubsec:mylabel83}
  4152. Could be implemented in ASM directly with register parameter passing.
  4153. \begin{functionl}{Int{\_}Help{\_}Constructor}{inthelpconstructor}
  4154. \Declaration
  4155. function Int{\_}Help{\_}Constructor(Var {\_}self : Pointer; Var VMT : Pointer; Vmt{\_}Pos : Cardinal):Pointer;
  4156. \Description
  4157. Allocates the memory for an object's field, and fills the object fields with
  4158. zeros. Returns the newly allocated self{\_}pointer
  4159. \Parameters
  4160. self = pointer to the object field image in memory \par
  4161. vmt = pointer to the the actual vmt table (used to get the size of the object) \par
  4162. vmt{\_}pos = offset in the object field image to the vmt pointer field
  4163. \Notes
  4164. The self{\_}pointer register should be set appropriately by the code
  4165. generator to the allocated memory (self parameter)
  4166. \Algorithm
  4167. Self = Allocate Memory block for object fields \par
  4168. Fill the object field image with zeros\par
  4169. Set the VMT field in allocated object to VMT pointer
  4170. \end{functionl}
  4171. \subsubsection{FPC{\_}HELP{\_}FAIL{\_}CLASS}
  4172. \label{subsubsec:mylabel84}
  4173. \begin{functionl}{Help{\_}Fail{\_}Class}{inthelpfileclass}
  4174. \Description
  4175. Inserted by code generator after constructor call. If the constructor failed
  4176. to allocate the memory for its fields, this routine will be called.
  4177. \end{functionl}
  4178. \subsubsection{FPC{\_}HELP{\_}FAIL}
  4179. \label{subsubsec:mylabel85}
  4180. \begin{functionl}{Help{\_}Fail}{HelpFail}
  4181. \Description
  4182. Inserted by code generator after constructor call. If the constructor failed
  4183. to allocate the memory for its fields, this routine will be called.
  4184. \end{functionl}
  4185. \subsection{Set handling}
  4186. \label{subsec:mylabel10}
  4187. \subsubsection{FPC{\_}SET{\_}COMP{\_}SETS}
  4188. \label{subsubsec:mylabel86}
  4189. \begin{functionl}{Do{\_}Comp{\_}Sets}{docompsets}
  4190. \Declaration
  4191. function Do{\_}Comp{\_}Sets(Set1,Set2 : Pointer): Boolean;
  4192. \Description
  4193. This routine compares if set1 and set2 are exactly equal and returns 1 if
  4194. so, otherwise it returns false.
  4195. \Parameters
  4196. set1 = Pointer to 32 byte set to compare \par
  4197. set2 = Pointer to 32 byte set to compare
  4198. \Notes
  4199. Both pointers must point to normal sets.
  4200. \end{functionl}
  4201. \subsubsection{FPC{\_}SET{\_}CONTAINS{\_}SET}
  4202. \label{subsubsec:mylabel87}
  4203. \begin{procedurel}{Do{\_}Contains{\_}Sets}{docontainssets}
  4204. \Declaration
  4205. Procedure Do{\_}Contains{\_}Sets(Set1,Set2 : Pointer): Boolean;
  4206. \Description
  4207. Returns 1 if set2 contains set1 (That is all elements of set2 are in set1).
  4208. \Parameters
  4209. set1 = Pointer to 32 byte set to verify \par
  4210. set2 = Pointer to 32 byte set to verify
  4211. \Notes
  4212. Both pointers must point to normal sets.
  4213. \end{procedurel}
  4214. \subsubsection{FPC{\_}SET{\_}CREATE{\_}ELEMENT}
  4215. \label{subsubsec:mylabel88}
  4216. \begin{procedurel}{Do{\_}Create{\_}Element}{docreateelement}
  4217. \Declaration
  4218. procedure Do{\_}Create{\_}Element(p : Pointer; b : Byte);
  4219. \Description
  4220. Create a new normal set in the area pointed to by \textsf{p} and add the
  4221. element value \textsf{b} in that set.
  4222. \Parameters
  4223. p = pointer to area where the 32 byte set will be created \par
  4224. b = bit value within that set which must be set
  4225. \Notes
  4226. This works on normal sets only.
  4227. \Algorithm
  4228. Zero the area pointed to by p \par
  4229. Set the bit number b to 1
  4230. \end{procedurel}
  4231. \subsubsection{FPC{\_}SET{\_}SET{\_}RANGE}
  4232. \label{subsubsec:mylabel89}
  4233. \begin{procedurel}{Do{\_}Set{\_}Range}{dosetrange}
  4234. \Declaration
  4235. Procedure Do{\_}Set{\_}Range(P : Pointer;l,h : Byte);
  4236. \Description
  4237. Sets the bit values within the \textsf{l} and \textsf{h }bit ranges in the
  4238. normal set pointed to by \textsf{p}
  4239. \Parameters
  4240. p = pointer to area where the 32 bytes of the set will be updated \par
  4241. l = low bit number value to set \par
  4242. h = high bit number value to set
  4243. \Notes
  4244. This works on normal sets only.
  4245. \Algorithm
  4246. Set all bit numbers from l to h in set p
  4247. \end{procedurel}
  4248. \subsubsection{FPC{\_}SET{\_}SET{\_}BYTE}
  4249. \label{subsubsec:mylabel90}
  4250. \begin{procedurel}{Do{\_}Set{\_}Byte}{dosetbyte}
  4251. \Declaration
  4252. procedure Do{\_}Set{\_}Byte(P : Pointer;B : byte);
  4253. \Description
  4254. Add the element \textsf{b} in the normal set pointed to by \textsf{p}
  4255. \Parameters
  4256. p = pointer to 32 byte set \par
  4257. b = bit number to set
  4258. \Notes
  4259. This works on normal sets only. The intel 80386 version of the compiler does
  4260. not save the used registers, therefore, in that case, it must be done in the
  4261. routine itself.
  4262. \Algorithm
  4263. Set bit number b in p
  4264. \end{procedurel}
  4265. \subsubsection{FPC{\_}SET{\_}SUB{\_}SETS}
  4266. \label{subsubsec:mylabel91}
  4267. \begin{procedurel}{Do{\_}Sub{\_}Sets}{dosubsets}
  4268. \Declaration
  4269. Procedure Do{\_}Sub{\_}Sets(Set1,Set2,Dest:Pointer);
  4270. \Description
  4271. Calculate the difference between \textsf{set1} and \textsf{set2}, setting
  4272. the result in \textsf{dest}.
  4273. \Parameters
  4274. set1 = pointer to 32 byte set \par
  4275. set2 = pointer to 32 byte set \par
  4276. dest = pointer to 32 byte set which will receive the result
  4277. \Notes
  4278. This works on normal sets only.
  4279. \Algorithm
  4280. \begin{lstlisting}{}
  4281. For each bit in the set do
  4282. dest bit = set1 bit AND NOT set2 bit
  4283. \end{lstlisting}
  4284. \end{procedurel}
  4285. \subsubsection{FPC{\_}SET{\_}MUL{\_}SETS}
  4286. \label{subsubsec:mylabel92}
  4287. \begin{procedurel}{Do{\_}Mul{\_}Sets}{domulsets}
  4288. \Declaration
  4289. procedure Do{\_}Mul{\_}Sets(Set1,Set2,Dest:Pointer);
  4290. \Description
  4291. Calculate the multiplication between \textsf{set1} and \textsf{set2},
  4292. setting the result in \textsf{dest}.
  4293. \Parameters
  4294. set1 = pointer to 32 byte set \par
  4295. set2 = pointer to 32 byte set \par
  4296. dest = pointer to 32 byte set which will receive the result
  4297. \Notes
  4298. This works on normal sets only.
  4299. \Algorithm
  4300. \begin{lstlisting}{}
  4301. For each bit in the set do
  4302. dest bit = set1 bit AND set2 bit
  4303. \end{lstlisting}
  4304. \end{procedurel}
  4305. \subsubsection{FPC{\_}SET{\_}SYMDIF{\_}SETS}
  4306. \label{subsubsec:mylabel93}
  4307. \begin{procedurel}{Do{\_}Symdif{\_}Sets}{dosymdifssets}
  4308. \Declaration
  4309. Procedure Do{\_}Symdif{\_}Sets(Set1,Set2,Dest:Pointer);
  4310. \Description
  4311. Calculate the symmetric between \textsf{set1} and \textsf{set2}, setting the
  4312. result in \textsf{dest}.
  4313. \Parameters
  4314. set1 = pointer to 32 byte set \par
  4315. set2 = pointer to 32 byte set \par
  4316. dest = pointer to 32 byte set which will receive the result
  4317. \Notes
  4318. This works on normal sets only.
  4319. \Algorithm
  4320. \begin{lstlisting}{}
  4321. For each bit in the set do
  4322. dest bit = set1 bit XOR set2 bit
  4323. \end{lstlisting}
  4324. \end{procedurel}
  4325. \subsubsection{FPC{\_}SET{\_}ADD{\_}SETS}
  4326. \label{subsubsec:mylabel94}
  4327. \begin{procedurel}{Do{\_}Add{\_}Sets}{doaddsets}
  4328. \Declaration
  4329. procedure Do{\_}Add{\_}Sets(Set1,Set2,Dest : Pointer);
  4330. \Description
  4331. Calculate the addition between \textsf{set1} and \textsf{set2}, setting the
  4332. result in \textsf{dest}.
  4333. \Parameters
  4334. set1 = pointer to 32 byte set \par
  4335. set2 = pointer to 32 byte set \par
  4336. dest = pointer to 32 byte set which will receive the result
  4337. \Notes
  4338. This works on normal sets only.
  4339. \Algorithm
  4340. \begin{lstlisting}{}
  4341. For each bit in the set do
  4342. dest bit = set1 bit OR set2 bit
  4343. \end{lstlisting}
  4344. \end{procedurel}
  4345. \subsubsection{FPC{\_}SET{\_}LOAD{\_}SMALL}
  4346. \label{subsubsec:mylabel95}
  4347. \begin{procedurel}{Do{\_}Load{\_}Small}{doloadsmall}
  4348. \Declaration
  4349. Procedure Do{\_}Load{\_}Small(P : Pointer;L:Longint);
  4350. \Description
  4351. Load a small set into a 32-byte normal set.
  4352. \Parameters
  4353. p = pointer to 32 byte set \par
  4354. l = value of the small set
  4355. \Notes
  4356. Called by code generator (type conversion) from small set to large set.
  4357. Apart from the first 32 bits of the 32 byte set, other bits are not
  4358. modified.
  4359. \Algorithm
  4360. \begin{lstlisting}{}
  4361. For n = bit 0 to bit 31 of l do
  4362. p bit n = l bit n
  4363. \end{lstlisting}
  4364. \end{procedurel}
  4365. \subsubsection{FPC{\_}SET{\_}UNSET{\_}BYTE}
  4366. \label{subsubsec:mylabel96}
  4367. \begin{procedurel}{Do{\_}Unset{\_}Byte}{dounsetbyte}
  4368. \Declaration
  4369. Procedure Do{\_}Unset{\_}Byte(P : Pointer;B : Byte);
  4370. \Description
  4371. Called by code generator to exclude element b from a big 32-byte set pointed
  4372. to by p.
  4373. \Parameters
  4374. p = pointer to 32 byte set \par b = element number to exclude
  4375. \Notes
  4376. The intel 80386 version of the compiler does not save the used registers,
  4377. therefore, in that case, it must be done in the routine itself.
  4378. \Algorithm
  4379. Clear bit number b in p
  4380. \end{procedurel}
  4381. \subsubsection{FPC{\_}SET{\_}IN{\_}BYTE}
  4382. \label{subsubsec:mylabel97}
  4383. \begin{functionl}{Do{\_}In{\_}Byte}{doinbyte}
  4384. \Declaration
  4385. Function Do{\_}In{\_}Byte(P : Pointer;B : Byte):boolean;
  4386. \Description
  4387. Called by code generator to verify the existence of an element in a set.
  4388. Returns TRUE if b is in the set pointed to by p, otherwise returns FALSE.
  4389. \Parameters
  4390. p = pointer to 32 byte set \par b = element number to verify
  4391. \Notes
  4392. This routine should save / restore all used registers.
  4393. \Algorithm
  4394. Clear bit number b in p
  4395. \end{functionl}
  4396. \subsection{Optional internal routines}
  4397. \label{subsec:optional}
  4398. These routines are dependant on the target architecture. They are present in
  4399. software if the hardware does not support these features.
  4400. They could be implemented in assembler directly with register parameter
  4401. passing.
  4402. \subsubsection{FPC{\_}MUL{\_}INT64}
  4403. \label{subsubsec:mylabel98}
  4404. \begin{function}{MulInt64}
  4405. \Declaration
  4406. function MulInt64(f1,f2 : Int64;CheckOverflow : LongBool) : Int64;
  4407. \Description
  4408. Called by the code generator to multiply two int64 values, when the hardware
  4409. does not support this type of operation. The value returned is the result of
  4410. the multiplication.
  4411. \Parameters
  4412. f1 = first operand \par
  4413. f2 = second operand \par
  4414. checkoverflow = TRUE if overflow checking should be done
  4415. \end{function}
  4416. \subsubsection{FPC{\_}DIV{\_}INT64}
  4417. \label{subsubsec:mylabel99}
  4418. \begin{function}{DivInt64}
  4419. \Declaration
  4420. function DivInt64(n,z : Int64) : Int64;
  4421. \Description
  4422. Called by the code generator to get the division two int64 values, when the
  4423. hardware does not support this type of operation. The value returned is the
  4424. result of the division.
  4425. \Parameters
  4426. n =numerator \par
  4427. z = denominator
  4428. \end{function}
  4429. \subsubsection{FPC{\_}MOD{\_}INT64}
  4430. \label{subsubsec:mylabel100}
  4431. \begin{function}{ModInt64}
  4432. \Declaration
  4433. function ModInt64(n,z : Int64) : Int64;
  4434. \Description
  4435. Called by the code generator to get the modulo two int64 values, when the
  4436. architecture does not support this type of operation. The value returned is
  4437. the result of the modulo.
  4438. \Parameters
  4439. n = numerator \par
  4440. z = denominator
  4441. \end{function}
  4442. \subsubsection{FPC{\_}SHL{\_}INT64}
  4443. \label{subsubsec:mylabel101}
  4444. \begin{function}{ShlInt64}
  4445. \Declaration
  4446. Function ShlInt64(Cnt : Longint; Low, High: Longint): Int64;
  4447. \Description
  4448. Called by the code generator to shift left a 64-bit integer by the specified
  4449. amount cnt, when this is not directly supported by the hardware. Returns the
  4450. shifted value.
  4451. \Parameters
  4452. low,high = value to shift (low / high 32-bit value) \par
  4453. cnt = shift count
  4454. \end{function}
  4455. \subsubsection{FPC{\_}SHR{\_}INT64}
  4456. \label{subsubsec:mylabel102}
  4457. \begin{function}{ShrInt64}
  4458. \Declaration
  4459. function ShrInt64(Cnt : Longint; Low, High: Longint): Int64;
  4460. \Description
  4461. Called by the code generator to shift left a 64-bit integer by the specified
  4462. amount cnt, when this is not directly supported by the hardware. Returns the
  4463. shifted value.
  4464. \Parameters
  4465. low,high = value to shift (low/high 32-bit values) \par
  4466. cnt = shift count
  4467. \end{function}
  4468. \subsubsection{FPC{\_}MUL{\_}LONGINT}
  4469. \label{subsubsec:mylabel103}
  4470. \begin{function}{MulLong}
  4471. \Declaration
  4472. Function MulLong: Longint;
  4473. \Description
  4474. 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.
  4475. \Parameters
  4476. Parameters are passed in registers.
  4477. \Notes
  4478. This routine should save / restore all used registers.
  4479. \end{function}
  4480. \subsubsection{FPC{\_}REM{\_}LONGINT}
  4481. \label{subsubsec:mylabel104}
  4482. \begin{function}{RemLong}
  4483. \Declaration
  4484. Function RemLong: Longint;
  4485. \Description
  4486. Called by the code generator to get the modulo two longint values, when the
  4487. hardware does not support this type of operation. The value returned is the
  4488. result of the modulo.
  4489. \Parameters
  4490. Parameters are passed in registers.
  4491. \Notes
  4492. This routine should save / restore all used registers.
  4493. \end{function}
  4494. \subsubsection{FPC{\_}DIV{\_}LONGINT}
  4495. \label{subsubsec:mylabel105}
  4496. \begin{function}{DivLong}
  4497. \Declaration
  4498. Function DivLong: Longint;
  4499. \Description
  4500. Called by the code generator to get the division two longint values, when
  4501. the hardware does not support this type of operation. The value returned is
  4502. the result of the division.
  4503. \Parameters
  4504. Parameters are passed in registers.
  4505. \Notes
  4506. This routine should save / restore all used registers.
  4507. \end{function}
  4508. \subsubsection{FPC{\_}MUL{\_}LONGINT}
  4509. \label{subsubsec:mylabel106}
  4510. \begin{function}{MulCardinal}
  4511. \Declaration
  4512. Function MulCardinal: Cardinal;
  4513. \Description
  4514. Called by the code generator to multiply two cardinal values, when the
  4515. hardware does not support this type of operation. The value returned is the
  4516. result of the multiplication.
  4517. \Parameters
  4518. Parameters are passed in registers.
  4519. \Notes
  4520. This routine should save / restore all used registers.
  4521. \end{function}
  4522. \subsubsection{FPC{\_}REM{\_}CARDINAL}
  4523. \label{subsubsec:mylabel107}
  4524. \begin{function}{RemCardinal}
  4525. \Declaration
  4526. Function RemCardinal : Cardinal;
  4527. \Description
  4528. Called by the code generator to get the modulo two cardinal values, when the
  4529. hardware does not support this type of operation. The value returned is the
  4530. result of the modulo.
  4531. \Parameters
  4532. Parameters are passed in registers.
  4533. \Notes
  4534. This routine should save / restore all used registers.
  4535. \end{function}
  4536. \subsubsection{FPC{\_}DIV{\_}CARDINAL}
  4537. \label{subsubsec:mylabel108}
  4538. \begin{function}{DivCardinal}
  4539. \Declaration
  4540. Function DivCardinal: Cardinal;
  4541. \Description
  4542. Called by the code generator to get the division two cardinal values, when
  4543. the hardware does not support this type of operation. The value returned is
  4544. the result of the division.
  4545. \Parameters
  4546. Parameters are passed in registers.
  4547. \Notes
  4548. This routine should save / restore all used registers.
  4549. \end{function}
  4550. \subsubsection{FPC{\_}LONG{\_}TO{\_}SINGLE}
  4551. \label{subsubsec:mylabel109}
  4552. \begin{function}{LongSingle}
  4553. \Declaration
  4554. Function LongSingle: Single;
  4555. \Description
  4556. Called by the code generator to convert a longint to a single IEEE floating
  4557. point value.
  4558. \Parameters
  4559. Parameters are passed in registers
  4560. \Notes
  4561. This routine should save / restore all used registers.
  4562. \end{function}
  4563. FPC{\_}ADD{\_}SINGLE
  4564. FPC{\_}SUB{\_}SINGLE
  4565. FPC{\_}MUL{\_}SINGLE
  4566. FPC{\_}REM{\_}SINGLE
  4567. FPC{\_}DIV{\_}SINGLE
  4568. FPC{\_}CMP{\_}SINGLE
  4569. FPC{\_}SINGLE{\_}TO{\_}LONGINT
  4570. \section{Optimizing your code}
  4571. \label{sec:optimizing}
  4572. \subsection{Simple types}
  4573. \label{subsec:simple}
  4574. Use the most simple types, when defining and declaring variables, they
  4575. require less overhead. Classes, and complex string types (ansi strings and
  4576. wide strings) posess runtime type information, as well as more overhead for
  4577. operating on them then simple types such as shortstring and simple ordinal
  4578. types.
  4579. \subsection{constant duplicate merging}
  4580. \label{subsec:constant}
  4581. When duplicates of constant strings, sets or floating point values are found
  4582. in the code, they are replaced by only once instance of the same string, set
  4583. or floating point constant which reduces the size of the final executable.
  4584. \subsection{inline routines}
  4585. \label{subsec:inline}
  4586. The following routines of the system unit are directly inlined by the
  4587. compiler, and generate more efficient code:
  4588. \begin{longtable}{|l|p{7cm}|}
  4589. \hline
  4590. Prototype& Definition and notes \\
  4591. \hline
  4592. \endhead
  4593. \hline
  4594. \endfoot
  4595. \textsf{function pi : extended;}& \\
  4596. \textsf{function abs(d : extended) : extended;}& \\
  4597. \textsf{function sqr(d : extended) : extended;}& \\
  4598. \textsf{function sqrt(d : extended) : extended;}& \\
  4599. \textsf{function arctan(d : extended) : extended;}& \\
  4600. \textsf{function ln(d : extended) : extended;}& \\
  4601. \textsf{function sin(d : extended) : extended;}& \\
  4602. \textsf{function cos(d : extended) : extended;}& \\
  4603. \textsf{function ord(X): longint;}&
  4604. Changes node type to be type compatible \\
  4605. \textsf{function lo(X) : byte or word;}&
  4606. Generates 2-3 instruction sequence inline \\
  4607. \textsf{function hi(X) : byte or word;}&
  4608. Generates 2-3 instruction sequence inline \\
  4609. \textsf{function chr(b : byte) : Char;}&
  4610. Changes node type to be type compatible \\
  4611. \textsf{function Length(s : string) : byte;}&
  4612. Generate 2-3 instruction sequence \\
  4613. \textsf{function Length(c : char) : byte;}&
  4614. Generates 1 instruction sequence (appx.) \\
  4615. \textsf{procedure Reset(var f : TypedFile);}&
  4616. Calls FPC{\_}RESET{\_}TYPED \\
  4617. \textsf{procedure rewrite(var f : TypedFile);}&
  4618. Calls FPC{\_}REWRITE{\_}TYPED \\
  4619. \textsf{procedure settextbuf(var F : Text; var Buf);}&
  4620. Calls SetTextBuf of runtime library \\
  4621. \textsf{procedure writen;}&
  4622. Calls FPC{\_}WRITE{\_}XXXX routines \\
  4623. \textsf{procedure writeln;}&
  4624. Calls FPC{\_}WRITE{\_}XXXX routines \\
  4625. \textsf{procedure read;}&
  4626. Calls FPC{\_}READ{\_}XXXX routines \\
  4627. \textsf{procedure readln;}&
  4628. Calls FPC{\_}READ{\_}XXXX routines \\
  4629. \textsf{procedure concat;}&
  4630. Generates a TREE NODES of type addn \\
  4631. \textsf{function assigned(var p): boolean;}&
  4632. Generates 1-2 instruction sequence inline \\
  4633. \textsf{procedure str(X :[Width [:Decimals]]; var S);}& \\
  4634. \textsf{}& \\
  4635. \textsf{function sizeof(X): longint;}&
  4636. Generates 2-3 instruction sequence inline \\
  4637. \textsf{function typeof(X): pointer;}&
  4638. Generates 2-3 instruction sequence inline \\
  4639. \textsf{procedure val(S;var V; var Code: integer);}& \\
  4640. \textsf{function seg(X): longint;}& \\
  4641. \textsf{function High(X)}&
  4642. Generates a TREE NODE of type ordconstn \\
  4643. \textsf{function Low(X)}&
  4644. Generates a TREE NODE of type ordconstn \\
  4645. \textsf{function pred(x)}&
  4646. Generates 2-3 instruction sequence inline \\
  4647. \textsf{function succ(X)}&
  4648. Generates 2-3 instruction sequence inline \\
  4649. \textsf{procedure inc(var X [ ; N: longint]);}&
  4650. Generate 2-3 instruction sequence inline \\
  4651. \textsf{procedure dec(var X [; N:longint]);}&
  4652. Generate 2-3 instruction sequence inline \\
  4653. \textsf{procedure include(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{\_}SET{\_}BYTE \\
  4656. \textsf{procedure exclude(var S : set of T; I: T);}&
  4657. In the case of a small set : Generates 2-3 instruction sequence inline,
  4658. otherwise calls FPC{\_}SET{\_}UNSET{\_}BYTE \\
  4659. \textsf{procedure assert(expr : Boolean [; const msg: string]);}&
  4660. Calls the internal routine FPC{\_}ASSERT in the case where the assert fails.\\
  4661. \textsf{function addr(X): pointer;}&
  4662. Generates a TREE NODE of type addrn \\
  4663. \textsf{function typeInfo(typeIdent): pointer;}&
  4664. Generates 1 instruction sequence inline \\
  4665. \end{longtable}
  4666. \subsection{temporary memory allocation reuse}
  4667. \label{subsec:mylabel11}
  4668. When routines are very complex , they may require temporary allocated space
  4669. on the stack to store intermediate results. The temporary memory space can
  4670. be reused for several different operations if other space is required on the
  4671. stack.
  4672. \section{Appendix A}
  4673. \label{sec:appendix}
  4674. This appendix describes the temporary defines when compiling software under
  4675. the compiler:
  4676. The following defines are defined in FreePascal for v1.0.x, but they will be
  4677. removed in future versions, they are used for debugging purposes only:
  4678. \begin{itemize}
  4679. \item INT64
  4680. \item HASRESOURCESTRINGS
  4681. \item NEWVMTOFFSET
  4682. \item HASINTERNMATH
  4683. \item SYSTEMVARREC
  4684. \item INCLUDEOK
  4685. \item NEWMM
  4686. \item HASWIDECHAR
  4687. \item INT64FUNCRESOK
  4688. \item CORRECTFLDCW
  4689. \item ENHANCEDRAISE
  4690. \item PACKENUMFIXED
  4691. \end{itemize}
  4692. NOTE: Currently, the only possible stack alignment are either 2 or 4 if the
  4693. target operating system pushes parameters on the stack directly in assembler
  4694. (because for example if pushing a long value on the stack while the required
  4695. stack alignment is 8 will give out wrong access to data in the actual
  4696. routine -- the offset will be wrong).
  4697. \printindex
  4698. \end{document}