comparch.tex 171 KB

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