FoxIntermediateBackend.Mod 558 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221
  1. MODULE FoxIntermediateBackend; (** AUTHOR ""; PURPOSE ""; *)
  2. IMPORT Basic := FoxBasic, SyntaxTree := FoxSyntaxTree, SemanticChecker := FoxSemanticChecker, Backend := FoxBackend, Global := FoxGlobal,
  3. Scanner := FoxScanner, IntermediateCode := FoxIntermediateCode, Sections := FoxSections, BinaryCode := FoxBinaryCode, Printout := FoxPrintout,
  4. SYSTEM, Diagnostics, Strings, Options, Streams, Compiler, Formats := FoxFormats, SymbolFileFormat := FoxTextualSymbolFile, D := Debugging,
  5. FingerPrinter := FoxFingerPrinter, StringPool;
  6. CONST
  7. (* operand modes *)
  8. ModeUndefined = 0;
  9. ModeReference = 1;
  10. ModeValue = 2;
  11. (* heap data offsets *)
  12. ArrayDimTable = 3; (* dimension table in dyn arrays *)
  13. (* math array offsets *)
  14. MathPtrOffset=0;
  15. MathAdrOffset=1;
  16. MathFlagsOffset=2;
  17. MathDimOffset=3;
  18. MathElementSizeOffset=4;
  19. MathLenOffset=5;
  20. MathIncrOffset=6;
  21. SysDataArrayOffset* = 0; (* array offset in system bl ock, for 32 byte alignment *)
  22. ArrDataArrayOffset*= 16*8; (* 16 bytes array offset in array block, to be compatible with the GC scheme of POINTER TO ARRAY OF ... *)
  23. TensorFlag* = 0; (* flag indicating a tensor array *)
  24. RangeFlag* = 1; (* flag indicating a range, e.g. an array derived from A[..,..] *)
  25. StackFlag* = 2; (* flag indicates temporary result *)
  26. StaticFlag* = 1; (* flag indicating a static array, may not be reallocated *)
  27. (** compiler generated traps *)
  28. WithTrap* = 1; (* generated when a WITH statement fails *)
  29. CaseTrap* = 2; (* generated when a case statement without else block fails *)
  30. ReturnTrap* = 3;
  31. TypeEqualTrap* = 5;
  32. TypeCheckTrap* = 6;
  33. IndexCheckTrap* = 7; (* generated when index is out of bounds or range is invalid *)
  34. AssertTrap* = 8; (* generated when an assert fails *)
  35. ArraySizeTrap* = 9;
  36. ArrayFormTrap*=10; (* indicates that array cannot be (re-)allocated since shape, type or size does not match *)
  37. SetElementTrap*=11; (* indicates that a set element is out of MIN(SET)...MAX(SET) *)
  38. NegativeDivisorTrap*=12;
  39. NoReturnTrap*=16; (* indicates that a procedure marked no return did return *)
  40. NilPointerTrap*=17; (* indicates that a nil pointer was being dereferenced *)
  41. Trace = FALSE;
  42. TraceRegisterUsageCount=TRUE;
  43. ArrayAlignment = 8*8; (* first array element of ArrayBlock and first data element of SystemBlock must be aligned to 0 MOD ArrayAlignment *)
  44. (** system call numbers *)
  45. NumberSystemCalls* = 12;
  46. SysNewRec* = 0; SysNewArr* = 1; SysNewSys* = 2; SysCaseTable* = 3; SysProcAddr* = 4;
  47. SysLock* = 5; SysUnlock* = 6; SysStart* = 7; SysAwait* = 8; SysInterfaceLookup* = 9;
  48. SysRegisterInterface* = 10; SysGetProcedure* = 11;
  49. DefaultRuntimeModuleName ="Runtime";
  50. DefaultTraceModuleName ="KernelLog";
  51. ChannelModuleName = "Channels";
  52. NonPointer = -1; (* special pointer values *)
  53. NoType = 0; (* special type info values *)
  54. LhsIsPointer = 0; (* for the operator kind *)
  55. RhsIsPointer = 1;
  56. (* priority values, lower means higher priority *)
  57. EntryPriority=-4;
  58. FirstPriority=-3;
  59. InitPriority=-2;
  60. ExitPriority=-1;
  61. BasePointerTypeSize = 5;
  62. BaseArrayTypeSize = BasePointerTypeSize + 3;
  63. LengthOffset = BasePointerTypeSize + 0;
  64. DataOffset = BasePointerTypeSize + 1;
  65. DescriptorOffset = BasePointerTypeSize + 2;
  66. BaseRecordTypeSize = BasePointerTypeSize + 2;
  67. ActionOffset = BasePointerTypeSize + 0;
  68. MonitorOffset = BasePointerTypeSize + 1;
  69. BaseObjectTypeSize = BaseRecordTypeSize;
  70. ActionTypeSize = 3;
  71. MonitorTypeSize = 7;
  72. ProcessorOffset = BaseObjectTypeSize + 1;
  73. StackLimitOffset* = BaseObjectTypeSize + 3;
  74. QuantumOffset = BaseObjectTypeSize + 4;
  75. (* flags for optimizations with small matricies and vectors (Alexey Morozov) *)
  76. SmallMatrixFlag = 3; (* flag for identification of a small matrix *)
  77. SmallVectorFlag = 3; (* flag for identification of a small vector *)
  78. Size2Flag = 4; (* size = 2 *)
  79. Size3Flag = 5; (* size = 3 *)
  80. Size4Flag = 6; (* size = 4 *)
  81. Size5Flag = 7; (* size = 5 *)
  82. Size6Flag = 8; (* size = 6 *)
  83. Size7Flag = 9; (* size = 7 *)
  84. Size8Flag = 10; (* size = 8 *)
  85. ReflectionSupport = TRUE;
  86. (* Solution for identifying procedure descriptors on the stack and for being able to differentiate "old school" stack frames from the underlying operating system stack frames:
  87. push a procedure desriptor plus one to where the BP pointer would be located. The misalignment of the procedure descriptor makes it possible to identify that it is not
  88. a base pointer but a procedure descriptor. The base pointer itself is in such cases located at BP + address size.
  89. *)
  90. TYPE
  91. Position=SyntaxTree.Position;
  92. SupportedInstructionProcedure* = PROCEDURE {DELEGATE} (CONST instr: IntermediateCode.Instruction; VAR moduleName,procedureName: ARRAY OF CHAR): BOOLEAN;
  93. SupportedImmediateProcedure* = PROCEDURE {DELEGATE} (CONST op: IntermediateCode.Operand): BOOLEAN;
  94. Operand = RECORD
  95. mode: SHORTINT;
  96. op: IntermediateCode.Operand;
  97. tag: IntermediateCode.Operand;
  98. extra: IntermediateCode.Operand; (* stores the step size of an array range *)
  99. dimOffset: LONGINT;
  100. END;
  101. Fixup= POINTER TO RECORD
  102. pc: LONGINT;
  103. nextFixup: Fixup;
  104. END;
  105. WriteBackCall = POINTER TO RECORD
  106. call: SyntaxTree.ProcedureCallDesignator;
  107. next: WriteBackCall;
  108. END;
  109. Label= OBJECT
  110. VAR
  111. fixups: Fixup;
  112. section: IntermediateCode.Section;
  113. pc: LONGINT;
  114. PROCEDURE &InitLabel(section: IntermediateCode.Section);
  115. BEGIN
  116. SELF.section := section; pc := -1;
  117. END InitLabel;
  118. PROCEDURE Resolve(pc: LONGINT);
  119. VAR at: LONGINT;
  120. BEGIN
  121. SELF.pc := pc;
  122. WHILE(fixups # NIL) DO
  123. at := fixups.pc;
  124. section.PatchAddress(at,pc);
  125. fixups := fixups.nextFixup;
  126. END;
  127. END Resolve;
  128. PROCEDURE AddFixup(at: LONGINT);
  129. VAR fixup: Fixup;
  130. BEGIN
  131. ASSERT(pc=-1);
  132. NEW(fixup); fixup.pc := at; fixup.nextFixup := fixups; fixups := fixup;
  133. END AddFixup;
  134. END Label;
  135. ConditionalBranch = PROCEDURE {DELEGATE}(label: Label; op1,op2: IntermediateCode.Operand);
  136. DeclarationVisitor =OBJECT(SyntaxTree.Visitor)
  137. VAR
  138. backend: IntermediateBackend;
  139. implementationVisitor: ImplementationVisitor;
  140. meta: MetaDataGenerator;
  141. system: Global.System;
  142. currentScope: SyntaxTree.Scope;
  143. module: Sections.Module;
  144. moduleSelf: SyntaxTree.Variable;
  145. dump: BOOLEAN;
  146. forceModuleBody: BOOLEAN;
  147. addressType: IntermediateCode.Type;
  148. PROCEDURE & Init(system: Global.System; implementationVisitor: ImplementationVisitor; backend: IntermediateBackend; forceModuleBody, dump: BOOLEAN);
  149. BEGIN
  150. currentScope := NIL; module := NIL; moduleSelf := NIL;
  151. SELF.system := system; SELF.implementationVisitor := implementationVisitor;
  152. SELF.dump := dump;
  153. SELF.backend := backend;
  154. SELF.forceModuleBody := forceModuleBody;
  155. addressType := IntermediateCode.GetType(system,system.addressType)
  156. END Init;
  157. PROCEDURE Error(position: Position; CONST s: ARRAY OF CHAR);
  158. BEGIN
  159. backend.Error(module.module.sourceName, position, Diagnostics.Invalid, s);
  160. END Error;
  161. PROCEDURE Type(x: SyntaxTree.Type);
  162. BEGIN
  163. x.Accept(SELF);
  164. END Type;
  165. (** types **)
  166. PROCEDURE VisitBasicType(x: SyntaxTree.BasicType);
  167. BEGIN (* no code emission *) END VisitBasicType;
  168. PROCEDURE VisitCharacterType(x: SyntaxTree.CharacterType);
  169. BEGIN (* no code emission *) END VisitCharacterType;
  170. PROCEDURE VisitIntegerType(x: SyntaxTree.IntegerType);
  171. BEGIN (* no code emission *) END VisitIntegerType;
  172. PROCEDURE VisitFloatType(x: SyntaxTree.FloatType);
  173. BEGIN (* no code emission *) END VisitFloatType;
  174. PROCEDURE VisitComplexType(x: SyntaxTree.ComplexType);
  175. BEGIN (* no code emission *) END VisitComplexType;
  176. PROCEDURE VisitQualifiedType(x: SyntaxTree.QualifiedType);
  177. VAR type: SyntaxTree.Type;
  178. BEGIN (* no further traversal to x.resolved necessary since type descriptor and code will be inserted at "original" position ? *)
  179. type := x.resolved;
  180. IF (type.typeDeclaration # NIL) & (type.typeDeclaration.scope.ownerModule # module.module) THEN
  181. meta.CheckTypeDeclaration(type);
  182. END;
  183. END VisitQualifiedType;
  184. PROCEDURE VisitStringType(x: SyntaxTree.StringType);
  185. BEGIN (* no code emission *) END VisitStringType;
  186. PROCEDURE VisitArrayRangeType(x: SyntaxTree.RangeType);
  187. BEGIN (* no code emission *)
  188. END VisitArrayRangeType;
  189. PROCEDURE VisitArrayType(x: SyntaxTree.ArrayType);
  190. BEGIN (* no code emission *) END VisitArrayType;
  191. PROCEDURE VisitPortType(x: SyntaxTree.PortType);
  192. BEGIN (* no code emission *) END VisitPortType;
  193. PROCEDURE VisitMathArrayType(x: SyntaxTree.MathArrayType);
  194. BEGIN
  195. meta.CheckTypeDeclaration(x);
  196. END VisitMathArrayType;
  197. PROCEDURE VisitPointerType(x: SyntaxTree.PointerType);
  198. BEGIN
  199. meta.CheckTypeDeclaration(x);
  200. (* base type must not be visited => will be done via record type declaration, otherwise is done twice ! *)
  201. END VisitPointerType;
  202. PROCEDURE VisitRecordType(x: SyntaxTree.RecordType);
  203. VAR name: ARRAY 256 OF CHAR; td: SyntaxTree.TypeDeclaration;
  204. BEGIN (* no code emission *)
  205. meta.CheckTypeDeclaration(x);
  206. IF (x.recordScope.ownerModule = module.module) & (x.isObject) THEN
  207. IF x.pointerType.typeDeclaration # NIL THEN
  208. td := x.pointerType.typeDeclaration
  209. ELSE
  210. td := x.typeDeclaration
  211. END;
  212. Global.GetSymbolName(td,name);
  213. (* code section for object *)
  214. END;
  215. Scope(x.recordScope);
  216. END VisitRecordType;
  217. PROCEDURE HasFlag(modifiers: SyntaxTree.Modifier; CONST name: ARRAY OF CHAR): BOOLEAN;
  218. VAR this: SyntaxTree.Modifier; id: SyntaxTree.Identifier;
  219. BEGIN
  220. this := modifiers; id := SyntaxTree.NewIdentifier(name);
  221. WHILE (this # NIL) & (this.identifier# id) DO
  222. this := this.nextModifier;
  223. END;
  224. RETURN this # NIL
  225. END HasFlag;
  226. PROCEDURE VisitCellType(x: SyntaxTree.CellType);
  227. VAR port,adr: LONGINT; symbol: IntermediateCode.Section; op: IntermediateCode.Operand; capabilities: SET;
  228. BEGIN
  229. IF backend.cellsAreObjects THEN meta.CheckTypeDeclaration(x) END;
  230. capabilities := {};
  231. IF HasFlag(x.modifiers, Global.StringFloatingPoint) THEN INCL(capabilities, Global.FloatingPointCapability) END;
  232. IF HasFlag(x.modifiers, Global.StringVector) THEN INCL(capabilities, Global.VectorCapability) END;
  233. backend.SetCapabilities(capabilities);
  234. IF ~implementationVisitor.checker.SkipImplementation(x) THEN
  235. Scope(x.cellScope);
  236. END;
  237. END VisitCellType;
  238. PROCEDURE VisitProcedureType(x: SyntaxTree.ProcedureType);
  239. BEGIN (* no code emission *) END VisitProcedureType;
  240. PROCEDURE VisitEnumerationType(x: SyntaxTree.EnumerationType);
  241. BEGIN (* no code emission, exported enumeration type values should be included in symbol file *)
  242. END VisitEnumerationType;
  243. (* symbols *)
  244. PROCEDURE VisitProcedure(x: SyntaxTree.Procedure);
  245. BEGIN
  246. Procedure(x);
  247. END VisitProcedure;
  248. PROCEDURE VisitOperator(x: SyntaxTree.Operator);
  249. BEGIN
  250. Procedure(x);
  251. END VisitOperator;
  252. PROCEDURE VisitVariable(x: SyntaxTree.Variable);
  253. VAR name: Basic.SegmentedName; irv: IntermediateCode.Section; align, dim, i: LONGINT;
  254. size: LONGINT; lastUpdated: LONGINT; imm: IntermediateCode.Operand;
  255. PROCEDURE TypeNeedsInitialization(type: SyntaxTree.Type): BOOLEAN;
  256. BEGIN
  257. type := type.resolved;
  258. IF type IS SyntaxTree.RecordType THEN
  259. IF ScopeNeedsInitialization(type(SyntaxTree.RecordType).recordScope) THEN RETURN TRUE END;
  260. ELSIF (type IS SyntaxTree.ArrayType) THEN
  261. IF type(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  262. IF TypeNeedsInitialization(type(SyntaxTree.ArrayType).arrayBase) THEN RETURN TRUE END;
  263. END;
  264. ELSIF type IS SyntaxTree.MathArrayType THEN
  265. WITH type: SyntaxTree.MathArrayType DO
  266. IF type.form = SyntaxTree.Open THEN
  267. RETURN TRUE
  268. ELSIF type.form = SyntaxTree.Static THEN
  269. IF TypeNeedsInitialization(type.arrayBase) THEN RETURN TRUE END;
  270. END;
  271. END;
  272. END;
  273. RETURN FALSE
  274. END TypeNeedsInitialization;
  275. PROCEDURE ScopeNeedsInitialization(scope: SyntaxTree.Scope): BOOLEAN;
  276. VAR variable: SyntaxTree.Variable;
  277. BEGIN
  278. variable := scope.firstVariable;
  279. WHILE variable # NIL DO
  280. IF TypeNeedsInitialization(variable.type) THEN RETURN TRUE END;
  281. IF variable.initializer # NIL THEN RETURN TRUE END;
  282. variable := variable.nextVariable;
  283. END;
  284. RETURN FALSE
  285. END ScopeNeedsInitialization;
  286. PROCEDURE SingleInitialize(CONST op: IntermediateCode.Operand; offset: LONGINT);
  287. BEGIN
  288. size := offset - lastUpdated;
  289. IF size > 0 THEN
  290. irv.Emit(Reserve(x.position,size));
  291. END;
  292. irv.Emit(Data(x.position, op));
  293. lastUpdated := offset + ToMemoryUnits(system, op.type.sizeInBits);
  294. END SingleInitialize;
  295. PROCEDURE Initialize(type: SyntaxTree.Type; initializer: SyntaxTree.Expression; offset: LONGINT);
  296. VAR op: Operand; baseType: SyntaxTree.Type; variable: SyntaxTree.Variable; i: LONGINT; size: SIZE;
  297. BEGIN
  298. IF type = NIL THEN RETURN ELSE type := type.resolved END;
  299. WITH type: SyntaxTree.RecordType DO
  300. baseType := type.baseType;
  301. IF baseType # NIL THEN
  302. baseType := baseType.resolved;
  303. IF baseType IS SyntaxTree.PointerType THEN
  304. baseType := baseType(SyntaxTree.PointerType).pointerBase
  305. END;
  306. Initialize(baseType,NIL, offset);
  307. END;
  308. variable := type.recordScope.firstVariable;
  309. WHILE variable # NIL DO
  310. Initialize(variable.type, variable.initializer, offset+ToMemoryUnits(system,variable.offsetInBits));
  311. variable := variable.nextVariable
  312. END;
  313. | type: SyntaxTree.ArrayType DO
  314. IF type.form = SyntaxTree.Static THEN
  315. baseType := type.arrayBase;
  316. IF TypeNeedsInitialization(baseType) THEN
  317. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  318. FOR i := 0 TO type.staticLength-1 DO
  319. Initialize(baseType,NIL,offset+i*size);
  320. END;
  321. END;
  322. END;
  323. | type: SyntaxTree.MathArrayType DO
  324. IF type.form = SyntaxTree.Open THEN
  325. dim := DynamicDim(type);
  326. baseType := SemanticChecker.ArrayBase(type,dim);
  327. imm := IntermediateCode.Immediate(addressType,dim);
  328. SingleInitialize(imm, offset + ToMemoryUnits(system, addressType.sizeInBits)* MathDimOffset);
  329. IF baseType = NIL THEN size := 0 ELSE size := system.AlignedSizeOf(baseType) END;
  330. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,size));
  331. SingleInitialize(imm, offset + ToMemoryUnits(system, addressType.sizeInBits)* MathElementSizeOffset);
  332. (* flags remain empty (=0) for open array *)
  333. ELSIF type.form = SyntaxTree.Static THEN
  334. baseType := type.arrayBase;
  335. IF TypeNeedsInitialization(baseType) THEN
  336. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  337. ASSERT(type.staticLength < 1024*1024*1024);
  338. FOR i := 0 TO type.staticLength-1 DO
  339. Initialize(baseType,NIL,offset+i*size);
  340. END;
  341. END;
  342. END;
  343. ELSE
  344. IF initializer # NIL THEN
  345. implementationVisitor.Evaluate(initializer, op);
  346. SingleInitialize(op.op, offset);
  347. END;
  348. END;
  349. END Initialize;
  350. BEGIN
  351. IF x.externalName # NIL THEN RETURN END;
  352. IF (currentScope IS SyntaxTree.ModuleScope) OR (currentScope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN
  353. (* code section for variable *)
  354. Global.GetSymbolSegmentedName(x,name);
  355. irv := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,x,dump);
  356. irv.SetExported(IsExported(x));
  357. irv.SetOffset(ToMemoryUnits(system,x.offsetInBits));
  358. IF (currentScope IS SyntaxTree.CellScope) & IsSemiDynamicArray(x.type) THEN
  359. irv.Emit(Reserve(x.position, ToMemoryUnits(system, system.addressSize)));
  360. Basic.SuffixSegmentedName (name, Basic.MakeString ("@len"));
  361. irv := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,NIL,dump);
  362. FOR i := 0 TO DynamicDim(x.type)-1 DO
  363. irv.Emit(Reserve(x.position, ToMemoryUnits(system, system.addressSize)));
  364. END;
  365. ELSE
  366. lastUpdated:= 0;
  367. IF implementationVisitor.newObjectFile & ((x.initializer # NIL) OR TypeNeedsInitialization(x.type)) THEN
  368. Initialize(x.type, x.initializer, 0);
  369. END;
  370. size := ToMemoryUnits(system,system.SizeOf(x.type)) - lastUpdated;
  371. IF size > 0 THEN
  372. irv.Emit(Reserve(x.position,size));
  373. END;
  374. IF ~x.fixed THEN
  375. align := CommonAlignment(x.alignment, ToMemoryUnits(system, system.AlignmentOf(system.variableAlignment, x.type)));
  376. ELSE
  377. align := x.alignment;
  378. END;
  379. irv.SetPositionOrAlignment(x.fixed, align);
  380. meta.CheckTypeDeclaration(x.type);
  381. END;
  382. ELSIF currentScope IS SyntaxTree.RecordScope THEN
  383. ELSIF currentScope IS SyntaxTree.ProcedureScope THEN
  384. END;
  385. (* do not call Type(x.type) here as this must already performed in the type declaration section ! *)
  386. END VisitVariable;
  387. PROCEDURE VisitProperty(x: SyntaxTree.Property);
  388. BEGIN
  389. VisitVariable(x)
  390. END VisitProperty;
  391. PROCEDURE VisitParameter(x: SyntaxTree.Parameter);
  392. VAR name: Basic.SegmentedName; irv: IntermediateCode.Section; align, i, dim: LONGINT;
  393. size: LONGINT; lastUpdated: LONGINT; imm: IntermediateCode.Operand;
  394. BEGIN
  395. ASSERT(currentScope IS SyntaxTree.CellScope);
  396. Global.GetSymbolSegmentedName(x,name);
  397. irv := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,x,dump);
  398. irv.SetExported(IsExported(x));
  399. irv.SetOffset(ToMemoryUnits(system,x.offsetInBits));
  400. IF (currentScope IS SyntaxTree.CellScope) & IsSemiDynamicArray(x.type) THEN
  401. irv.Emit(Reserve(x.position, ToMemoryUnits(system, system.addressSize)));
  402. Basic.SuffixSegmentedName (name, Basic.MakeString ("@len"));
  403. irv := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,NIL,dump);
  404. FOR i := 0 TO DynamicDim(x.type)-1 DO
  405. irv.Emit(Reserve(x.position, ToMemoryUnits(system, system.addressSize)));
  406. END;
  407. ELSE
  408. lastUpdated:= 0;
  409. size := ToMemoryUnits(system,system.SizeOf(x.type)) - lastUpdated;
  410. IF size > 0 THEN
  411. irv.Emit(Reserve(x.position,size));
  412. END;
  413. IF ~x.fixed THEN
  414. align := CommonAlignment(x.alignment, ToMemoryUnits(system, system.AlignmentOf(system.variableAlignment, x.type)));
  415. ELSE
  416. align := x.alignment;
  417. END;
  418. irv.SetPositionOrAlignment(x.fixed, align);
  419. meta.CheckTypeDeclaration(x.type);
  420. END;
  421. END VisitParameter;
  422. PROCEDURE VisitTypeDeclaration(x: SyntaxTree.TypeDeclaration);
  423. BEGIN
  424. Type(x.declaredType); (* => code in objects *)
  425. IF ~(x.declaredType IS SyntaxTree.QualifiedType) & (x.declaredType.resolved IS SyntaxTree.PointerType) THEN
  426. Type(x.declaredType.resolved(SyntaxTree.PointerType).pointerBase);
  427. END;
  428. END VisitTypeDeclaration;
  429. PROCEDURE VisitConstant(x: SyntaxTree.Constant);
  430. BEGIN
  431. IF (SyntaxTree.Public * x.access # {}) THEN
  432. implementationVisitor.VisitConstant(x);
  433. END;
  434. END VisitConstant;
  435. PROCEDURE Scope(x: SyntaxTree.Scope);
  436. VAR procedure: SyntaxTree.Procedure;
  437. constant: SyntaxTree.Constant;
  438. variable: SyntaxTree.Variable;
  439. prevScope: SyntaxTree.Scope; typeDeclaration: SyntaxTree.TypeDeclaration;
  440. cell: SyntaxTree.CellType;
  441. parameter: SyntaxTree.Parameter;
  442. property: SyntaxTree.Property;
  443. BEGIN
  444. prevScope := currentScope;
  445. currentScope := x;
  446. (* constants treated in implementation visitor *)
  447. WITH x: SyntaxTree.CellScope DO
  448. cell := x.ownerCell;
  449. parameter := cell.firstParameter;
  450. WHILE parameter # NIL DO
  451. VisitParameter(parameter);
  452. parameter := parameter.nextParameter;
  453. END;
  454. property := cell.firstProperty;
  455. WHILE property # NIL DO
  456. VisitProperty(property);
  457. property := property.nextProperty;
  458. END;
  459. ELSE
  460. END;
  461. typeDeclaration := x.firstTypeDeclaration;
  462. WHILE typeDeclaration # NIL DO
  463. VisitTypeDeclaration(typeDeclaration);
  464. typeDeclaration := typeDeclaration.nextTypeDeclaration;
  465. END;
  466. variable := x.firstVariable;
  467. WHILE variable # NIL DO
  468. VisitVariable(variable);
  469. variable := variable.nextVariable;
  470. END;
  471. procedure := x.firstProcedure;
  472. WHILE procedure # NIL DO
  473. VisitProcedure(procedure);
  474. procedure := procedure.nextProcedure;
  475. END;
  476. constant := x.firstConstant;
  477. WHILE constant # NIL DO
  478. VisitConstant(constant);
  479. constant := constant.nextConstant;
  480. END;
  481. currentScope := prevScope;
  482. END Scope;
  483. PROCEDURE Parameters(first: SyntaxTree.Parameter);
  484. VAR parameter: SyntaxTree.Parameter;
  485. BEGIN
  486. parameter := first;
  487. WHILE parameter # NIL DO
  488. VisitParameter(parameter);
  489. parameter := parameter.nextParameter;
  490. END;
  491. END Parameters;
  492. PROCEDURE Procedure(x: SyntaxTree.Procedure);
  493. VAR scope: SyntaxTree.ProcedureScope;
  494. prevScope: SyntaxTree.Scope;
  495. inline, finalizer: BOOLEAN;
  496. procedureType: SyntaxTree.ProcedureType;
  497. pc: LONGINT;
  498. stackSize: LONGINT;
  499. name,baseObject: Basic.SegmentedName; ir: IntermediateCode.Section;
  500. null,size,src,dest,fp,res: IntermediateCode.Operand;
  501. cc: LONGINT;
  502. cellType: SyntaxTree.CellType;
  503. registerNumber: LONGINT;
  504. registerParameter: Backend.Registers;
  505. registerParameters: LONGINT;
  506. registerClass: IntermediateCode.RegisterClass;
  507. type: IntermediateCode.Type;
  508. formalParameter: SyntaxTree.Parameter;
  509. recordType: SyntaxTree.RecordType;
  510. isModuleBody: BOOLEAN;
  511. parametersSize: LONGINT;
  512. PROCEDURE Signature;
  513. VAR parameter: SyntaxTree.Parameter; procedureType: SyntaxTree.ProcedureType; returnType : SyntaxTree.Type;
  514. BEGIN
  515. procedureType := x.type(SyntaxTree.ProcedureType);
  516. returnType := procedureType.returnType;
  517. IF returnType # NIL THEN
  518. meta.CheckTypeDeclaration(returnType)
  519. END;
  520. parameter := procedureType.firstParameter;
  521. WHILE parameter # NIL DO
  522. meta.CheckTypeDeclaration(parameter.type); (* we have to re-export a type, i.e. it has to be present in the list of symbols *)
  523. parameter := parameter.nextParameter;
  524. END;
  525. END Signature;
  526. PROCEDURE CheckIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  527. VAR result: BOOLEAN;
  528. BEGIN
  529. result := FALSE;
  530. IF x = SyntaxTree.invalidExpression THEN
  531. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  532. result := TRUE;
  533. value := x.resolved(SyntaxTree.IntegerValue).value;
  534. ELSE
  535. Error(x.position,"expression is not an integer constant");
  536. END;
  537. RETURN result;
  538. END CheckIntegerValue;
  539. PROCEDURE HasValue(modifiers: SyntaxTree.Modifier; CONST name: ARRAY OF CHAR; VAR value: LONGINT): BOOLEAN;
  540. VAR this: SyntaxTree.Modifier; id: SyntaxTree.Identifier;
  541. BEGIN
  542. this := modifiers; id := SyntaxTree.NewIdentifier(name);
  543. WHILE (this # NIL) & (this.identifier # id) DO
  544. this := this.nextModifier;
  545. END;
  546. IF this # NIL THEN
  547. IF this.expression = NIL THEN
  548. Error(this.position,"expected expression value");
  549. ELSIF CheckIntegerValue(this.expression,value) THEN
  550. END;
  551. RETURN TRUE
  552. ELSE RETURN FALSE
  553. END;
  554. END HasValue;
  555. CONST DefaultDataMemorySize=512;
  556. BEGIN
  557. IF x.externalName # NIL THEN RETURN END;
  558. (*
  559. IF Trace & (dump # NIL) THEN dump.String("DeclarationVisitor:Procedure"); dump.Ln END;
  560. *)
  561. (* code section for this procedure *)
  562. scope := x.procedureScope;
  563. prevScope := currentScope;
  564. currentScope := scope;
  565. procedureType := x.type(SyntaxTree.ProcedureType);
  566. isModuleBody := x = module.module.moduleScope.bodyProcedure;
  567. implementationVisitor.temporaries.Init;
  568. implementationVisitor.usedRegisters := NIL;
  569. implementationVisitor.registerUsageCount.Init;
  570. implementationVisitor.GetCodeSectionNameForSymbol(x, name);
  571. IF (scope.body # NIL) & (x.isInline) THEN
  572. inline := TRUE;
  573. ir := implementationVisitor.NewSection(module.allSections, Sections.InlineCodeSection, name,x,dump);
  574. ir.SetExported(IsExported(x));
  575. ELSIF (x.scope # NIL) & (x.scope IS SyntaxTree.CellScope) & (x.scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  576. OR (x.scope # NIL) & (x.scope IS SyntaxTree.ModuleScope) & (x.scope(SyntaxTree.ModuleScope).ownerModule.isCellNet) THEN
  577. IF backend.cellsAreObjects THEN
  578. ir := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name, x, dump);
  579. ir.SetExported(IsExported(x));
  580. ELSE
  581. RETURN; (* cellnet cannot be compiled for final static hardware *)
  582. END;
  583. ELSIF x = module.module.moduleScope.bodyProcedure THEN
  584. inline := FALSE;
  585. AddBodyCallStub(x);
  586. ir := implementationVisitor.NewSection(module.allSections, Sections.BodyCodeSection, name,x,dump);
  587. ir.SetExported(IsExported(x));
  588. ELSIF (scope.outerScope IS SyntaxTree.CellScope) & (x = scope.outerScope(SyntaxTree.CellScope).bodyProcedure) THEN
  589. inline := FALSE;
  590. cellType := scope.outerScope(SyntaxTree.CellScope).ownerCell;
  591. IF ~HasValue(cellType.modifiers,Global.StringDataMemorySize,stackSize) THEN stackSize := DefaultDataMemorySize END;
  592. AddBodyCallStub(x);
  593. AddStackAllocation(x,stackSize);
  594. ir := implementationVisitor.NewSection(module.allSections,Sections.BodyCodeSection, name,x,dump);
  595. ir.SetExported(IsExported(x));
  596. ELSIF (scope.outerScope IS SyntaxTree.CellScope) & (x.isConstructor) THEN
  597. inline := FALSE;
  598. Parameters(procedureType.firstParameter);
  599. ir := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name,x,dump);
  600. ir.SetExported(IsExported(x));
  601. ELSE
  602. inline := FALSE;
  603. IF x.isEntry OR x.isExit THEN
  604. ir := implementationVisitor.NewSection(module.allSections, Sections.InitCodeSection, name,x,dump);
  605. ir.SetExported(TRUE);
  606. IF x.isEntry THEN ir.SetPriority(EntryPriority) ELSE ir.SetPriority(ExitPriority) END;
  607. ELSE
  608. ir := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name,x,dump);
  609. ir.SetExported(IsExported(x) OR SemanticChecker.InMethodTable(x));
  610. END;
  611. END;
  612. cc := procedureType.callingConvention;
  613. IF cc = SyntaxTree.WinAPICallingConvention THEN
  614. parametersSize := ProcedureParametersSize(backend.system,x);
  615. ELSE
  616. parametersSize := 0;
  617. END;
  618. IF scope.body # NIL THEN
  619. IF implementationVisitor.emitLabels THEN ir.Emit(LabelInstruction(scope.body.position)) END;
  620. registerNumber := 0;
  621. IF ~inline THEN
  622. IF scope.lastVariable = NIL THEN
  623. stackSize := 0
  624. ELSE
  625. stackSize := scope.lastVariable.offsetInBits;
  626. IF stackSize <0 THEN stackSize := -stackSize END;
  627. Basic.Align(stackSize,system.AlignmentOf(system.parameterAlignment,system.byteType)); (* round up to parameter alignment *)
  628. END;
  629. (*
  630. ir.Emit(Nop(position)); (* placeholder for stack frame check *)
  631. ir.Emit(Nop(position)); (* placeholder for stack frame check (2) *)
  632. *)
  633. IF ~procedureType.noPAF THEN (* no procedure activation frame ! *)
  634. implementationVisitor.EmitEnter(ir,x.position,x,cc,ToMemoryUnits(system,stackSize),registerNumber);
  635. END;
  636. pc := ir.pc-1;
  637. (*
  638. ir.Emit(Nop(position)); (* placeholder for fill *)
  639. *)
  640. IF procedureType.callingConvention # SyntaxTree.OberonCallingConvention THEN
  641. registerParameter := backend.GetParameterRegisters(procedureType.callingConvention);
  642. IF registerParameter = NIL THEN registerParameters := 0
  643. ELSE registerParameters := LEN(registerParameter)
  644. END;
  645. formalParameter := procedureType.lastParameter;
  646. WHILE (formalParameter # NIL) & (registerNumber < registerParameters) DO
  647. IF ~PassInRegister(formalParameter) THEN
  648. Error(formalParameter.position,"Calling convention error: cannot be passed as register");
  649. ELSE
  650. IntermediateCode.InitRegisterClass(registerClass, IntermediateCode.Parameter, registerParameter[registerNumber]);
  651. type := GetType(system, formalParameter.type);
  652. src := IntermediateCode.Register(type, registerClass, implementationVisitor.AcquireRegister(type, registerClass));
  653. IntermediateCode.InitMemory(dest,GetType(system,formalParameter.type),implementationVisitor.fp,ToMemoryUnits(system,formalParameter.offsetInBits));
  654. ir.Emit(Mov(Basic.invalidPosition,dest, src));
  655. implementationVisitor.ReleaseIntermediateOperand(src);
  656. INC(registerNumber);
  657. formalParameter := formalParameter.prevParameter;
  658. END;
  659. END;
  660. END;
  661. END;
  662. implementationVisitor.tagsAvailable := procedureType.callingConvention = SyntaxTree.OberonCallingConvention;
  663. implementationVisitor.Body(scope.body,currentScope,ir,isModuleBody);
  664. IF ~inline & ~(procedureType.noPAF) & ~x.isEntry & ~x.isExit THEN
  665. IF scope.lastVariable # NIL THEN
  666. stackSize := scope.lastVariable.offsetInBits;
  667. IF stackSize <0 THEN stackSize := -stackSize END;
  668. Basic.Align(stackSize,system.AlignmentOf(system.parameterAlignment,system.byteType)); (* round up to parameter alignment *)
  669. END;
  670. END;
  671. IF ~inline & ~(procedureType.noPAF) & ~x.isEntry & ~x.isExit THEN
  672. IF ToMemoryUnits(system,stackSize) > 4*1024-256 THEN (* stack frame potentially larger than page size *) (*! get page size from backend *)
  673. (*! unnecessary with new implementation of ENTER -- should potentially be called by backend
  674. IF implementationVisitor.GetRuntimeProcedure(implementationVisitor.runtimeModuleName,"EnsureAllocatedStack",procedure,TRUE) THEN
  675. size := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,stackSize+256));
  676. ir.EmitAt(pc,Push(size));
  677. implementationVisitor.StaticCallOperand(result,procedure);
  678. ir.EmitAt(pc+1,Call(result.op,ProcedureParametersSize(system,procedure)));
  679. END;
  680. *)
  681. END;
  682. ir.EmitAt(pc(*+2*),implementationVisitor.Enter(x.position,cc,ToMemoryUnits(system,stackSize))); (*!!*)
  683. IF stackSize > 0 THEN
  684. IF (stackSize MOD system.addressSize = 0) THEN
  685. null := IntermediateCode.Immediate(addressType,0);
  686. fp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.FP);
  687. IntermediateCode.AddOffset(fp,ToMemoryUnits(system,-system.addressSize));
  688. size := IntermediateCode.Immediate(addressType,stackSize DIV system.addressSize);
  689. ELSE
  690. null := IntermediateCode.Immediate(int8,0);
  691. fp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.FP);
  692. IntermediateCode.AddOffset(fp,ToMemoryUnits(system,-null.type.sizeInBits));
  693. size := IntermediateCode.Immediate(addressType,stackSize DIV null.type.sizeInBits);
  694. END;
  695. (*! should potentially be called by backend -- enter might initialize
  696. ir.EmitAt(pc+3,Fill(fp,null,size,TRUE));
  697. *)
  698. END;
  699. IF procedureType.callingConvention = SyntaxTree.WinAPICallingConvention THEN
  700. parametersSize := ProcedureParametersSize(backend.system,x);
  701. ELSE
  702. parametersSize := 0;
  703. END;
  704. IF (procedureType.returnType = NIL) OR (scope.body.code # NIL) THEN
  705. finalizer := FALSE;
  706. IF backend.cooperative & x.isFinalizer THEN
  707. recordType := x.scope(SyntaxTree.RecordScope).ownerRecord;
  708. Basic.ToSegmentedName("BaseTypes.Object", baseObject);
  709. GetRecordTypeName(recordType,name);
  710. finalizer := (name # baseObject) & (recordType.baseType = NIL);
  711. END;
  712. implementationVisitor.SetLabel(implementationVisitor.exitLabel);
  713. IF backend.cooperative THEN
  714. IF HasPointers (scope) THEN implementationVisitor.ResetVariables(scope); END;
  715. IF implementationVisitor.profile & ~isModuleBody THEN
  716. implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE)
  717. END;
  718. END;
  719. implementationVisitor.EmitLeave(ir, x.position,x,cc);
  720. IF finalizer THEN
  721. IF backend.hasLinkRegister THEN
  722. ir.Emit(Pop(Basic.invalidPosition, implementationVisitor.lr));
  723. END;
  724. Basic.ToSegmentedName("BaseTypes.Object.Finalize", name);
  725. IntermediateCode.InitAddress(dest, addressType, name , 0, 0);
  726. ir.Emit(Br(x.position,dest));
  727. ELSE
  728. ir.Emit(Exit(x.position,procedureType.pcOffset,cc, parametersSize));
  729. END;
  730. ELSE
  731. IF ~scope.body.isUnchecked & ~backend.noRuntimeChecks THEN
  732. implementationVisitor.EmitTrap(x.position,ReturnTrap);
  733. END;
  734. implementationVisitor.SetLabel(implementationVisitor.exitLabel);
  735. IF backend.cooperative THEN
  736. IF HasPointers (scope) THEN
  737. IF ~SemanticChecker.ReturnedAsParameter(procedureType.returnType) THEN
  738. res := implementationVisitor.NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  739. ir.Emit(Result(x.position, res));
  740. ir.Emit(Push(x.position, res));
  741. implementationVisitor.ResetVariables(scope);
  742. IF implementationVisitor.profile & ~isModuleBody THEN implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE) END;
  743. ir.Emit(Pop(x.position, res));
  744. ir.Emit(Return(x.position, res));
  745. ELSE
  746. implementationVisitor.ResetVariables(scope);
  747. IF implementationVisitor.profile & ~isModuleBody THEN implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE) END;
  748. END;
  749. ELSIF implementationVisitor.profile & ~isModuleBody THEN
  750. IF ~SemanticChecker.ReturnedAsParameter(procedureType.returnType) THEN
  751. res := implementationVisitor.NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  752. ir.Emit(Result(x.position, res));
  753. ir.Emit(Push(x.position, res));
  754. implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE);
  755. ir.Emit(Pop(x.position, res));
  756. ir.Emit(Return(x.position, res));
  757. ELSE
  758. implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE);
  759. END;
  760. END;
  761. implementationVisitor.EmitLeave(ir,x.position,x,cc);
  762. ir.Emit(Exit(x.position,procedureType.pcOffset,cc, parametersSize));
  763. ELSE
  764. ir.Emit(Nop(x.position));
  765. IF scope.body.isUnchecked OR backend.noRuntimeChecks THEN (* return from procedure in any case *)
  766. implementationVisitor.EmitLeave(ir,x.position,x,cc);
  767. ir.Emit(Exit(x.position,procedureType.pcOffset,cc, parametersSize));
  768. END;
  769. END;
  770. END
  771. END;
  772. ELSE (* force body for procedures *)
  773. implementationVisitor.EmitEnter(ir, x.position,x,cc,0,0);
  774. implementationVisitor.Body(scope.body,currentScope,ir,x = module.module.moduleScope.bodyProcedure);
  775. (*IF implementationVisitor.usedRegisters # NIL THEN D.TraceBack END;*)
  776. implementationVisitor.EmitLeave(ir,x.position,x,cc);
  777. ir.Emit(Exit(x.position,procedureType.pcOffset,cc, parametersSize));
  778. END;
  779. Scope(scope);
  780. Signature;
  781. IF (x IS SyntaxTree.Operator) & x(SyntaxTree.Operator).isDynamic THEN implementationVisitor.RegisterDynamicOperator(x(SyntaxTree.Operator)) END;
  782. currentScope := prevScope;
  783. END Procedure;
  784. PROCEDURE AddBodyCallStub(bodyProcedure: SyntaxTree.Procedure); (* code that is only necessary for static linkers *)
  785. VAR procedure: SyntaxTree.Procedure; procedureScope: SyntaxTree.ProcedureScope; name: Basic.SegmentedName;
  786. ir: IntermediateCode.Section; op: IntermediateCode.Operand;
  787. BEGIN
  788. ASSERT (bodyProcedure # NIL);
  789. procedureScope := SyntaxTree.NewProcedureScope(bodyProcedure.scope);
  790. procedure := SyntaxTree.NewProcedure(Basic.invalidPosition,SyntaxTree.NewIdentifier("@BodyStub"), procedureScope);
  791. procedure.SetScope(bodyProcedure.scope);
  792. procedure.SetType(SyntaxTree.NewProcedureType(Basic.invalidPosition,bodyProcedure.scope));
  793. procedure.SetAccess(SyntaxTree.Hidden);
  794. Global.GetSymbolSegmentedName (procedure,name);
  795. ir := implementationVisitor.NewSection(module.allSections, Sections.InitCodeSection, name,procedure,dump);
  796. ir.SetExported(TRUE);
  797. ir.SetPriority(InitPriority);
  798. Global.GetSymbolSegmentedName (bodyProcedure,name);
  799. IF (backend.newObjectFile OR backend.cooperative) & ~meta.simple THEN
  800. implementationVisitor.currentScope := module.module.moduleScope;
  801. implementationVisitor.section := ir;
  802. implementationVisitor.PushSelfPointer();
  803. implementationVisitor.CallThis(bodyProcedure.position,"Modules","Register",1);
  804. ELSIF backend.preregisterStatic THEN
  805. implementationVisitor.currentScope := module.module.moduleScope;
  806. implementationVisitor.section := ir;
  807. implementationVisitor.PushSelfPointer();
  808. implementationVisitor.CallThis(bodyProcedure.position,"Modules","Preregister",1);
  809. ELSE
  810. IntermediateCode.InitAddress(op, addressType, name, implementationVisitor.GetFingerprint(bodyProcedure), 0);
  811. ir.Emit(Call(bodyProcedure.position,op, 0));
  812. END;
  813. END AddBodyCallStub;
  814. PROCEDURE AddStackAllocation(symbol: SyntaxTree.Symbol; initStack: LONGINT); (* code that is only necessary for static linkers *)
  815. VAR name: Basic.SegmentedName;
  816. ir: IntermediateCode.Section; op: IntermediateCode.Operand;
  817. BEGIN
  818. Global.GetSymbolSegmentedName (symbol,name);
  819. Basic.RemoveSuffix(name);
  820. Basic.SuffixSegmentedName(name, Basic.MakeString("@StackAllocation"));
  821. ir := implementationVisitor.NewSection(module.allSections,Sections.InitCodeSection,name,NIL,dump);
  822. ir.SetExported(TRUE);
  823. ir.SetPriority(FirstPriority);
  824. IntermediateCode.InitImmediate(op,addressType,initStack);
  825. ir.Emit(Mov(Basic.invalidPosition,implementationVisitor.sp,op));
  826. END AddStackAllocation;
  827. (** entry function to visit a complete module *)
  828. PROCEDURE Module(x: SyntaxTree.Module; module: Sections.Module);
  829. VAR
  830. ir: IntermediateCode.Section; op: IntermediateCode.Operand; name: Basic.SegmentedName; idstr: SyntaxTree.IdentifierString;
  831. hasDynamicOperatorDeclarations: BOOLEAN;
  832. operator: SyntaxTree.Operator;
  833. import: SyntaxTree.Import;
  834. PROCEDURE TypeNeedsInitialization(type: SyntaxTree.Type): BOOLEAN;
  835. BEGIN
  836. type := type.resolved;
  837. IF type IS SyntaxTree.RecordType THEN
  838. IF ScopeNeedsInitialization(type(SyntaxTree.RecordType).recordScope) THEN RETURN TRUE END;
  839. ELSIF (type IS SyntaxTree.ArrayType) THEN
  840. IF type(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  841. IF TypeNeedsInitialization(type(SyntaxTree.ArrayType).arrayBase) THEN RETURN TRUE END;
  842. END;
  843. ELSIF type IS SyntaxTree.MathArrayType THEN
  844. WITH type: SyntaxTree.MathArrayType DO
  845. IF type.form = SyntaxTree.Open THEN
  846. RETURN TRUE
  847. ELSIF type.form = SyntaxTree.Static THEN
  848. IF TypeNeedsInitialization(type.arrayBase) THEN RETURN TRUE END;
  849. END;
  850. END;
  851. END;
  852. RETURN FALSE
  853. END TypeNeedsInitialization;
  854. PROCEDURE ScopeNeedsInitialization(scope: SyntaxTree.Scope): BOOLEAN;
  855. VAR variable: SyntaxTree.Variable;
  856. BEGIN
  857. variable := scope.firstVariable;
  858. WHILE variable # NIL DO
  859. IF TypeNeedsInitialization(variable.type) THEN RETURN TRUE END;
  860. IF variable.initializer # NIL THEN RETURN TRUE END;
  861. variable := variable.nextVariable;
  862. END;
  863. RETURN FALSE
  864. END ScopeNeedsInitialization;
  865. BEGIN
  866. ASSERT(x # NIL); ASSERT(module # NIL);
  867. SELF.module := module;
  868. (* add import names to the generated Sections.Module *)
  869. import := x.moduleScope.firstImport;
  870. WHILE import # NIL DO
  871. import.module.GetName(idstr);
  872. module.imports.AddName(idstr);
  873. import := import.nextImport
  874. END;
  875. IF ~implementationVisitor.newObjectFile & ~meta.simple THEN
  876. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@moduleSelf"));
  877. moduleSelf := SyntaxTree.NewVariable(Basic.invalidPosition,SyntaxTree.NewIdentifier("@moduleSelf"));
  878. moduleSelf.SetType(system.anyType);
  879. moduleSelf.SetScope(x.moduleScope);
  880. moduleSelf.SetUntraced(TRUE);
  881. ir := implementationVisitor.NewSection(module.allSections, Sections.ConstSection, name,moduleSelf,dump); ir.SetOffset(0);
  882. ir.SetExported(TRUE);
  883. IntermediateCode.InitImmediate(op,addressType,0);
  884. ir.Emit(Data(Basic.invalidPosition,op));
  885. END;
  886. implementationVisitor.module := module;
  887. implementationVisitor.moduleScope := x.moduleScope;
  888. implementationVisitor.moduleSelf := moduleSelf;
  889. implementationVisitor.canBeLoaded := TRUE;
  890. meta.SetModule(module);
  891. IF (forceModuleBody OR implementationVisitor.newObjectFile & ~meta.simple OR ScopeNeedsInitialization(x.moduleScope)) THEN
  892. EnsureBodyProcedure(x.moduleScope); (* currently needed in Oberon, remove ? *)
  893. END;
  894. IF backend.profile THEN
  895. EnsureBodyProcedure(x.moduleScope);
  896. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@ModuleId"));
  897. implementationVisitor.profileId := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,NIL,dump);
  898. implementationVisitor.profileId.Emit(Reserve(Basic.invalidPosition,ToMemoryUnits(system,system.SizeOf(system.longintType))));
  899. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@InitProfiler"));
  900. implementationVisitor.profileInit := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump);
  901. implementationVisitor.EmitEnter(implementationVisitor.profileInit,Basic.invalidPosition,NIL,0,0,0);
  902. Global.GetModuleName(module.module,idstr);
  903. implementationVisitor.ProfilerAddModule(idstr);
  904. implementationVisitor.numberProcedures := 0;
  905. END;
  906. implementationVisitor.profile := backend.profile;
  907. (* check if there is at least one dynamic operator locally defined *)
  908. hasDynamicOperatorDeclarations := FALSE;
  909. operator := x.moduleScope.firstOperator;
  910. WHILE operator # NIL DO
  911. IF operator.isDynamic THEN hasDynamicOperatorDeclarations := TRUE END;
  912. operator := operator.nextOperator
  913. END;
  914. (* add operator initialization code section *)
  915. IF hasDynamicOperatorDeclarations THEN
  916. EnsureBodyProcedure(x.moduleScope);
  917. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@OperatorInitialization"));
  918. implementationVisitor.operatorInitializationCodeSection := implementationVisitor.NewSection(module.allSections, Sections.CodeSection,name, NIL, dump);
  919. implementationVisitor.EmitEnter(implementationVisitor.operatorInitializationCodeSection,Basic.invalidPosition,NIL,0,0,0);
  920. END;
  921. Scope(x.moduleScope);
  922. IF hasDynamicOperatorDeclarations THEN
  923. implementationVisitor.EmitLeave(implementationVisitor.operatorInitializationCodeSection,Basic.invalidPosition,NIL,0);
  924. implementationVisitor.operatorInitializationCodeSection.Emit(Exit(Basic.invalidPosition,0,0,0));
  925. END;
  926. IF backend.profile THEN
  927. implementationVisitor.ProfilerPatchInit;
  928. END;
  929. END Module;
  930. END DeclarationVisitor;
  931. UsedArray*=POINTER TO ARRAY OF RECORD count: LONGINT; map: LONGINT; type: IntermediateCode.Type; class: IntermediateCode.RegisterClass END;
  932. RegisterUsageCount*=OBJECT
  933. VAR used: UsedArray; count: LONGINT;
  934. PROCEDURE &Init;
  935. VAR i: LONGINT;
  936. BEGIN
  937. count := 0;
  938. IF used = NIL THEN NEW(used,64); END;
  939. FOR i := 0 TO LEN(used)-1 DO used[i].count := 0; used[i].map := i END;
  940. END Init;
  941. PROCEDURE Grow;
  942. VAR new: UsedArray; size,i: LONGINT;
  943. BEGIN
  944. size := LEN(used)*2;
  945. NEW(new,size);
  946. FOR i := 0 TO LEN(used)-1 DO
  947. new[i].count := used[i].count;
  948. new[i].type := used[i].type;
  949. new[i].map := used[i].map
  950. END;
  951. FOR i := LEN(used) TO LEN(new)-1 DO new[i].count := 0 END;
  952. used := new
  953. END Grow;
  954. PROCEDURE Next(type: IntermediateCode.Type; class: IntermediateCode.RegisterClass): LONGINT;
  955. BEGIN
  956. INC(count);
  957. IF count = LEN(used) THEN Grow END;
  958. used[count].type := type;
  959. used[count].class := class;
  960. used[count].map := count;
  961. RETURN count;
  962. END Next;
  963. PROCEDURE IncUse(register: LONGINT);
  964. BEGIN
  965. INC(used[register].count);
  966. (*
  967. IF (register = 1) & (count > 30) THEN
  968. D.TraceBack;
  969. END;
  970. *)
  971. END IncUse;
  972. PROCEDURE DecUse(register: LONGINT);
  973. BEGIN
  974. DEC(used[register].count);
  975. END DecUse;
  976. PROCEDURE Map(register: LONGINT): LONGINT;
  977. VAR map : LONGINT;
  978. BEGIN
  979. IF register > 0 THEN
  980. map := used[register].map;
  981. WHILE register # map DO register := map; map := used[register].map END;
  982. END;
  983. RETURN register
  984. END Map;
  985. PROCEDURE Use(register: LONGINT): LONGINT;
  986. BEGIN
  987. IF register< LEN(used) THEN
  988. RETURN used[register].count
  989. ELSE
  990. RETURN 0
  991. END
  992. END Use;
  993. END RegisterUsageCount;
  994. RegisterEntry = POINTER TO RECORD
  995. prev,next: RegisterEntry;
  996. register: LONGINT;
  997. registerClass: IntermediateCode.RegisterClass;
  998. type: IntermediateCode.Type;
  999. END;
  1000. VariableUse= ARRAY 32 OF SET; (* upper bound of 1024 temporary variables in a procedure .. should be enough for all times *)
  1001. Variables = OBJECT (Basic.List)
  1002. VAR
  1003. inUse: VariableUse;
  1004. registerIndex: LONGINT;
  1005. PROCEDURE & Init;
  1006. VAR i: LONGINT;
  1007. BEGIN
  1008. InitList(16);
  1009. FOR i := 0 TO LEN(inUse)-1 DO inUse[i] := {} END;
  1010. registerIndex := 1024;
  1011. END Init;
  1012. PROCEDURE GetUsage(VAR use: VariableUse);
  1013. BEGIN
  1014. use := inUse;
  1015. END GetUsage;
  1016. PROCEDURE SetUsage(CONST use: VariableUse);
  1017. BEGIN
  1018. inUse := use;
  1019. END SetUsage;
  1020. PROCEDURE GetVariable(i: LONGINT): SyntaxTree.Variable;
  1021. VAR any: ANY;
  1022. BEGIN
  1023. any := Get(i);;
  1024. IF any = NIL THEN RETURN NIL ELSE RETURN any(SyntaxTree.Variable) END;
  1025. END GetVariable;
  1026. PROCEDURE Occupy(pos: LONGINT);
  1027. BEGIN
  1028. INCL(inUse[pos DIV 32], pos MOD 32);
  1029. END Occupy;
  1030. PROCEDURE AddVariable(v: SyntaxTree.Variable);
  1031. BEGIN
  1032. Occupy(Length());
  1033. Add(v);
  1034. END AddVariable;
  1035. PROCEDURE GetFreeVariable(type: SyntaxTree.Type; VAR pos: LONGINT): SyntaxTree.Variable;
  1036. VAR var : SyntaxTree.Variable;
  1037. BEGIN
  1038. FOR pos := 0 TO Length()-1 DO
  1039. IF ~((pos MOD 32) IN inUse[pos DIV 32]) THEN
  1040. var := GetVariable(pos);
  1041. IF type.SameType(var.type) THEN
  1042. Occupy(pos); RETURN var
  1043. END;
  1044. END;
  1045. END;
  1046. pos := Length();
  1047. RETURN NIL
  1048. END GetFreeVariable;
  1049. END Variables;
  1050. SymbolMap = POINTER TO RECORD
  1051. this: SyntaxTree.Symbol; to, tag: SyntaxTree.Expression; next: SymbolMap;
  1052. isAddress: BOOLEAN;
  1053. END;
  1054. SymbolMapper = OBJECT
  1055. VAR
  1056. first: SymbolMap;
  1057. PROCEDURE & Init;
  1058. BEGIN
  1059. first := NIL;
  1060. END Init;
  1061. PROCEDURE Add(this: SyntaxTree.Symbol; to, tag: SyntaxTree.Expression; isAddress: BOOLEAN);
  1062. VAR new: SymbolMap;
  1063. BEGIN
  1064. NEW(new); new.this := this; new.to := to; new.tag := tag; new.isAddress := isAddress;
  1065. new.next := first; first := new;
  1066. END Add;
  1067. PROCEDURE Get(this: SyntaxTree.Symbol): SymbolMap;
  1068. VAR s: SymbolMap;
  1069. BEGIN
  1070. s := first;
  1071. WHILE (s # NIL) & (s.this # this) DO
  1072. s := s.next
  1073. END;
  1074. RETURN s
  1075. END Get;
  1076. END SymbolMapper;
  1077. ImplementationVisitor =OBJECT(SyntaxTree.Visitor)
  1078. VAR
  1079. system: Global.System;
  1080. section: IntermediateCode.Section;
  1081. module: Sections.Module;
  1082. moduleScope : SyntaxTree.ModuleScope; (* shortcut for module.module.moduleScope *)
  1083. awaitProcCounter, labelId, constId, caseId: LONGINT;
  1084. hiddenPointerType: SyntaxTree.RecordType; (* used as hidden pointer, for example for ARRAY OF ANY *)
  1085. delegatePointerType: SyntaxTree.RecordType; (* used for delegates, for example in ARRAY OF PROCEDURE{DELEGATE} *)
  1086. checker: SemanticChecker.Checker;
  1087. backend: IntermediateBackend;
  1088. meta: MetaDataGenerator;
  1089. position: Position;
  1090. moduleSelf: SyntaxTree.Variable;
  1091. (* variables for hand over of variables / temporary state *)
  1092. currentScope: SyntaxTree.Scope;
  1093. constantDeclaration : SyntaxTree.Symbol;
  1094. result: Operand; (* result of the most recent expression / statement *)
  1095. destination: IntermediateCode.Operand;
  1096. arrayDestinationTag: IntermediateCode.Operand;
  1097. arrayDestinationDimension:LONGINT;
  1098. currentLoop: Label; (* variable to hand over loop exit jump list *)
  1099. conditional: BOOLEAN;
  1100. trueLabel, falseLabel, exitLabel: Label;
  1101. locked: BOOLEAN;
  1102. (*
  1103. usedRegisters: Registers;
  1104. *)
  1105. registerUsageCount: RegisterUsageCount;
  1106. usedRegisters: RegisterEntry;
  1107. (* useful operands and types *)
  1108. nil,one,fp,sp,ap,lr,true,false: IntermediateCode.Operand;
  1109. bool,addressType,setType, sizeType, byteType: IntermediateCode.Type;
  1110. commentPrintout: Printout.Printer;
  1111. dump: Streams.Writer;
  1112. tagsAvailable : BOOLEAN;
  1113. supportedInstruction: SupportedInstructionProcedure;
  1114. supportedImmediate: SupportedImmediateProcedure;
  1115. inData: BOOLEAN; (* to prevent indirect reference to data within data sections, cf. VisitIntegerValue *)
  1116. emitLabels: BOOLEAN;
  1117. runtimeModuleName : SyntaxTree.IdentifierString;
  1118. newObjectFile: BOOLEAN;
  1119. indexCounter: LONGINT;
  1120. profile: BOOLEAN;
  1121. profileId, profileInit: IntermediateCode.Section;
  1122. profileInitPatchPosition: LONGINT;
  1123. numberProcedures: LONGINT;
  1124. procedureResultDesignator : SyntaxTree.Designator;
  1125. operatorInitializationCodeSection: IntermediateCode.Section;
  1126. fingerPrinter: FingerPrinter.FingerPrinter;
  1127. temporaries: Variables;
  1128. canBeLoaded : BOOLEAN;
  1129. currentIsInline: BOOLEAN;
  1130. currentMapper: SymbolMapper;
  1131. currentInlineExit: Label;
  1132. moduleBodySection: IntermediateCode.Section;
  1133. NeedDescriptor : BOOLEAN;
  1134. cooperativeSwitches: BOOLEAN;
  1135. lastSwitchPC: LONGINT;
  1136. isUnchecked: BOOLEAN;
  1137. PROCEDURE & Init(system: Global.System; checker: SemanticChecker.Checker; supportedInstructionProcedure: SupportedInstructionProcedure; supportedImmediateProcedure: SupportedImmediateProcedure; emitLabels: BOOLEAN; CONST runtime: SyntaxTree.IdentifierString; backend: IntermediateBackend;
  1138. newObjectFile: BOOLEAN);
  1139. BEGIN
  1140. SELF.system := system;
  1141. SELF.runtimeModuleName := runtime;
  1142. currentScope := NIL;
  1143. hiddenPointerType := NIL;
  1144. delegatePointerType := NIL;
  1145. awaitProcCounter := 0;
  1146. labelId := 0; constId := 0; labelId := 0;
  1147. SELF.checker := checker;
  1148. SELF.backend := backend;
  1149. position := Basic.invalidPosition;
  1150. conditional := FALSE;
  1151. locked := FALSE;
  1152. InitOperand(result,ModeUndefined);
  1153. addressType := IntermediateCode.GetType(system,system.addressType);
  1154. setType := IntermediateCode.GetType(system,system.setType);
  1155. sizeType := IntermediateCode.GetType(system, system.sizeType);
  1156. byteType := IntermediateCode.GetType(system, system.byteType);
  1157. fp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.FP);
  1158. sp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.SP);
  1159. ap := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.AP);
  1160. lr := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.LR);
  1161. nil := IntermediateCode.Immediate(addressType,0);
  1162. one := IntermediateCode.Immediate(addressType,1);
  1163. IntermediateCode.InitOperand(destination);
  1164. tagsAvailable := TRUE;
  1165. supportedInstruction := supportedInstructionProcedure;
  1166. supportedImmediate := supportedImmediateProcedure;
  1167. inData := FALSE;
  1168. SELF.emitLabels := emitLabels;
  1169. IntermediateCode.InitOperand(arrayDestinationTag);
  1170. bool := IntermediateCode.GetType(system,system.booleanType);
  1171. IntermediateCode.InitImmediate(false,bool,0);
  1172. IntermediateCode.InitImmediate(true,bool,1);
  1173. SELF.newObjectFile := newObjectFile;
  1174. indexCounter := 0;
  1175. NEW(registerUsageCount);
  1176. usedRegisters := NIL;
  1177. procedureResultDesignator := NIL;
  1178. NEW(fingerPrinter);
  1179. NEW(temporaries);
  1180. currentIsInline := FALSE;
  1181. NeedDescriptor := FALSE;
  1182. isUnchecked := backend.noRuntimeChecks;
  1183. END Init;
  1184. TYPE Context = RECORD
  1185. section: IntermediateCode.Section;
  1186. registerUsageCount: RegisterUsageCount;
  1187. usedRegisters: RegisterEntry;
  1188. END;
  1189. PROCEDURE SwitchContext(new: IntermediateCode.Section): Context;
  1190. VAR context: Context;
  1191. BEGIN
  1192. context.section := section;
  1193. context.registerUsageCount := registerUsageCount;
  1194. context.usedRegisters := usedRegisters;
  1195. section := new;
  1196. NEW(registerUsageCount);
  1197. usedRegisters := NIL;
  1198. RETURN context;
  1199. END SwitchContext;
  1200. PROCEDURE ReturnToContext(context: Context);
  1201. BEGIN
  1202. section := context.section;
  1203. registerUsageCount := context.registerUsageCount;
  1204. usedRegisters := context.usedRegisters;
  1205. END ReturnToContext;
  1206. PROCEDURE NewSection(list: Sections.SectionList; type: SHORTINT; CONST name: Basic.SegmentedName; syntaxTreeSymbol: SyntaxTree.Symbol; dump: BOOLEAN): IntermediateCode.Section;
  1207. VAR fp: SyntaxTree.FingerPrint; section: IntermediateCode.Section;
  1208. BEGIN
  1209. IF (syntaxTreeSymbol # NIL) & ~((syntaxTreeSymbol IS SyntaxTree.Procedure) & (syntaxTreeSymbol(SyntaxTree.Procedure).isInline)) THEN
  1210. fp := fingerPrinter.SymbolFP(syntaxTreeSymbol)
  1211. END;
  1212. section := IntermediateCode.NewSection(list, type, name, syntaxTreeSymbol, dump);
  1213. section.SetExported(IsExported(syntaxTreeSymbol));
  1214. RETURN section
  1215. END NewSection;
  1216. PROCEDURE AcquireRegister(CONST type: IntermediateCode.Type; class: IntermediateCode.RegisterClass): LONGINT;
  1217. VAR new: LONGINT;
  1218. BEGIN
  1219. new := registerUsageCount.Next(type,class);
  1220. UseRegister(new);
  1221. RETURN new
  1222. END AcquireRegister;
  1223. PROCEDURE GetFingerprintString(symbol: SyntaxTree.Symbol; VAR string: ARRAY OF CHAR);
  1224. VAR
  1225. fingerPrint: SyntaxTree.FingerPrint;
  1226. fingerPrintString: ARRAY 32 OF CHAR;
  1227. BEGIN
  1228. fingerPrint := fingerPrinter.SymbolFP(symbol);
  1229. string := "[";
  1230. Strings.IntToHexStr(fingerPrint.shallow, 8, fingerPrintString);
  1231. Strings.Append(string, fingerPrintString);
  1232. Strings.Append(string, "]");
  1233. END GetFingerprintString;
  1234. (** get the name for the code section that represens a certain symbol
  1235. (essentially the same as Global.GetSymbolName, apart from operators) **)
  1236. PROCEDURE GetCodeSectionNameForSymbol(symbol: SyntaxTree.Symbol; VAR name: Basic.SegmentedName);
  1237. VAR string: ARRAY 32 OF CHAR;
  1238. BEGIN
  1239. Global.GetSymbolSegmentedName(symbol, name);
  1240. (* if the symbol is an operator, then append the fingerprint to the name *)
  1241. IF symbol IS SyntaxTree.Operator THEN
  1242. GetFingerprintString(symbol, string);
  1243. Basic.AppendToSegmentedName(name,string);
  1244. END
  1245. END GetCodeSectionNameForSymbol;
  1246. (** get the name for the code section that represens a certain symbol
  1247. (essentially the same as Global.GetSymbolName, apart from operators) **)
  1248. PROCEDURE GetCodeSectionNameForSymbolInScope(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope; VAR name: ARRAY OF CHAR);
  1249. VAR string: ARRAY 32 OF CHAR;
  1250. BEGIN
  1251. Global.GetSymbolNameInScope(symbol, scope, name);
  1252. (* if the symbol is an operator, then append the fingerprint to the name *)
  1253. IF symbol IS SyntaxTree.Operator THEN
  1254. GetFingerprintString(symbol, string);
  1255. Strings.Append(name, string);
  1256. END
  1257. END GetCodeSectionNameForSymbolInScope;
  1258. PROCEDURE TraceEnter(CONST s: ARRAY OF CHAR);
  1259. BEGIN
  1260. IF dump # NIL THEN
  1261. dump.String("enter "); dump.String(s); dump.Ln;
  1262. END;
  1263. END TraceEnter;
  1264. PROCEDURE TraceExit(CONST s: ARRAY OF CHAR);
  1265. BEGIN
  1266. IF dump # NIL THEN
  1267. dump.String("exit "); dump.String(s); dump.Ln;
  1268. END;
  1269. END TraceExit;
  1270. PROCEDURE Emit(instruction: IntermediateCode.Instruction);
  1271. VAR moduleName, procedureName: SyntaxTree.IdentifierString;
  1272. PROCEDURE CheckRegister(VAR op: IntermediateCode.Operand);
  1273. VAR i: LONGINT;
  1274. BEGIN
  1275. IF op.register >0 THEN IntermediateCode.SetRegister(op,registerUsageCount.Map(op.register)) END;
  1276. IF op.rule # NIL THEN
  1277. FOR i := 0 TO LEN(op.rule)-1 DO
  1278. CheckRegister(op.rule[i])
  1279. END;
  1280. END;
  1281. END CheckRegister;
  1282. BEGIN
  1283. CheckRegister(instruction.op1);
  1284. CheckRegister(instruction.op2);
  1285. CheckRegister(instruction.op3);
  1286. IF supportedInstruction(instruction,moduleName,procedureName) THEN section.Emit(instruction)
  1287. ELSE section.Emit(instruction);
  1288. EnsureSymbol(moduleName,procedureName); (* remainder for binary object file *)
  1289. END;
  1290. END Emit;
  1291. PROCEDURE EmitTrap (position: Position; trapNo: LONGINT);
  1292. BEGIN
  1293. IF backend.cooperative THEN
  1294. Emit(Push(position,IntermediateCode.Immediate(sizeType,trapNo)));
  1295. CallThis(position,DefaultRuntimeModuleName,"Trap",1);
  1296. ELSE
  1297. Emit(Trap(position,trapNo));
  1298. END;
  1299. END EmitTrap;
  1300. PROCEDURE EmitEnter (section: IntermediateCode.Section; position: Position; procedure: SyntaxTree.Procedure; callconv: LONGINT; varSize: LONGINT; numRegs: LONGINT);
  1301. VAR name: Basic.SegmentedName;
  1302. VAR op1, op2, reg: IntermediateCode.Operand;
  1303. VAR call, nocall: Label;
  1304. VAR parametersSize: LONGINT;
  1305. VAR prevSection: IntermediateCode.Section;
  1306. VAR prevDump: Streams.Writer;
  1307. VAR body: SyntaxTree.Body;
  1308. VAR procedureType: SyntaxTree.ProcedureType;
  1309. BEGIN
  1310. IF procedure # NIL THEN
  1311. procedureType := procedure.type(SyntaxTree.ProcedureType);
  1312. ELSE procedureType := NIL;
  1313. END;
  1314. ASSERT((procedure = NIL) OR ~procedureType.noPAF);
  1315. prevSection := SELF.section;
  1316. SELF.section := section;
  1317. prevDump := dump;
  1318. dump := section.comments;
  1319. IF callconv # SyntaxTree.InterruptCallingConvention THEN
  1320. IF backend.hasLinkRegister THEN
  1321. Emit(Push(Basic.invalidPosition, lr));
  1322. END;
  1323. Emit(Push(Basic.invalidPosition,fp));
  1324. END;
  1325. IF backend.cooperative & (callconv = SyntaxTree.OberonCallingConvention) THEN
  1326. IF (procedure # NIL) & (HasPointers (procedure.procedureScope) OR HasVariableParameters (procedure.procedureScope) OR IsNested (procedure)) THEN
  1327. GetCodeSectionNameForSymbol(procedure, name);
  1328. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Descriptor"));
  1329. ELSE
  1330. Basic.ToSegmentedName ("BaseTypes.StackFrame",name);
  1331. END;
  1332. IntermediateCode.InitAddress(op1, addressType, name , 0, 0);
  1333. Emit(Push(Basic.invalidPosition,op1));
  1334. Emit(Mov(Basic.invalidPosition,fp, sp));
  1335. body := procedure.procedureScope.body;
  1336. IF (body # NIL) & (body.code = NIL) & ~procedure.procedureScope.body.isUnchecked THEN
  1337. NEW(call, section);
  1338. NEW(nocall, section);
  1339. reg := NewRegisterOperand(addressType);
  1340. IntermediateCode.InitImmediate(op1,addressType, varSize);
  1341. Emit(Sub(Basic.invalidPosition,reg, sp, op1));
  1342. BrltL(call, sp, reg);
  1343. IntermediateCode.InitMemory(op2, addressType,ap,ToMemoryUnits(system,system.addressSize*10));
  1344. BrgeL(nocall, sp, op2);
  1345. call.Resolve(section.pc);
  1346. parametersSize := ProcedureParametersSize(backend.system,procedure);
  1347. IntermediateCode.InitImmediate(op2,addressType, parametersSize);
  1348. Emit(Push(Basic.invalidPosition, op2));
  1349. Emit(Push(Basic.invalidPosition, reg));
  1350. ReleaseIntermediateOperand(reg);
  1351. CallThis(position, "Activities","ExpandStack",2);
  1352. Emit(Result(Basic.invalidPosition, sp));
  1353. nocall.Resolve(section.pc);
  1354. END;
  1355. ELSIF callconv # SyntaxTree.InterruptCallingConvention THEN
  1356. IF procedure # NIL THEN
  1357. body := procedure.procedureScope.body;
  1358. ELSE
  1359. body := NIL;
  1360. END;
  1361. IF backend.preciseGC & (body # NIL) & (body.code = NIL) THEN
  1362. Emit(Push(Basic.invalidPosition, one)) ;
  1363. procedureType.SetParametersOffset(1);
  1364. ASSERT(system.GenerateParameterOffsets(procedure, procedure.level > 0));
  1365. END;
  1366. Emit(Mov(Basic.invalidPosition, fp, sp));
  1367. END;
  1368. Emit(Enter(Basic.invalidPosition, callconv, varSize));
  1369. SELF.section := prevSection;
  1370. dump := prevDump;
  1371. END EmitEnter;
  1372. PROCEDURE Enter(position: Position; callconv: LONGINT; varSize: LONGINT): IntermediateCode.Instruction;
  1373. VAR op1,op2: IntermediateCode.Operand;
  1374. VAR instruction: IntermediateCode.Instruction;
  1375. BEGIN
  1376. IntermediateCode.InitNumber(op1,callconv);
  1377. IntermediateCode.InitNumber(op2,varSize);
  1378. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.enter,op1,op2,emptyOperand);
  1379. RETURN instruction
  1380. END Enter;
  1381. PROCEDURE Leave(position: Position; callconv: LONGINT): IntermediateCode.Instruction;
  1382. VAR op1: IntermediateCode.Operand;
  1383. VAR instruction: IntermediateCode.Instruction;
  1384. BEGIN
  1385. IntermediateCode.InitNumber(op1,callconv);
  1386. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.leave,op1,emptyOperand,emptyOperand);
  1387. RETURN instruction
  1388. END Leave;
  1389. PROCEDURE EmitLeave(section: IntermediateCode.Section; position: Basic.Position; procedure: SyntaxTree.Procedure; callconv: LONGINT);
  1390. VAR prevSection: IntermediateCode.Section;
  1391. VAR op2, size: IntermediateCode.Operand;
  1392. VAR body: SyntaxTree.Body;
  1393. BEGIN
  1394. prevSection := SELF.section;
  1395. SELF.section := section;
  1396. Emit(Leave(position, callconv));
  1397. IF procedure # NIL THEN
  1398. body := procedure.procedureScope.body;
  1399. ELSE
  1400. body := NIL;
  1401. END;
  1402. IF callconv # SyntaxTree.InterruptCallingConvention THEN
  1403. IF backend.cooperative OR backend.preciseGC & (body # NIL) & (body.code = NIL) THEN
  1404. IntermediateCode.InitImmediate(op2,addressType, ToMemoryUnits(system, system.addressSize));
  1405. Emit(Add(position, sp, fp, op2));
  1406. ELSE
  1407. Emit(Mov(position, sp, fp));
  1408. END;
  1409. Emit(Pop(position, fp));
  1410. END;
  1411. SELF.section := prevSection;
  1412. END EmitLeave;
  1413. PROCEDURE Symbol(x: SyntaxTree.Symbol; VAR op: Operand);
  1414. VAR m: SymbolMap;
  1415. BEGIN
  1416. position := x.position;
  1417. IF currentIsInline THEN
  1418. m := currentMapper.Get(x);
  1419. IF m # NIL THEN
  1420. (*
  1421. Printout.Info("mapping from", x);
  1422. Printout.Info("mapping to ", m.to);
  1423. *)
  1424. m.to.Accept(SELF);
  1425. op := result;
  1426. IF m.tag # NIL THEN
  1427. ReleaseIntermediateOperand(result.tag);
  1428. m.tag.Accept(SELF);
  1429. op.tag := result.op;
  1430. ReleaseIntermediateOperand(result.tag);
  1431. END;
  1432. RETURN
  1433. END;
  1434. END;
  1435. x.Accept(SELF);
  1436. op := result;
  1437. END Symbol;
  1438. PROCEDURE Expression(x: SyntaxTree.Expression);
  1439. BEGIN
  1440. position := x.position;
  1441. constantDeclaration := NIL;
  1442. IF (x IS SyntaxTree.SymbolDesignator) & (x(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Constant) THEN
  1443. constantDeclaration := x(SyntaxTree.SymbolDesignator).symbol;
  1444. END;
  1445. IF x.resolved # NIL THEN
  1446. x.resolved.Accept(SELF)
  1447. ELSE
  1448. x.Accept(SELF)
  1449. END;
  1450. (* check this, was commented out in ActiveCells3 *)
  1451. IF (x IS SyntaxTree.Designator) & (x(SyntaxTree.Designator).modifiers # NIL) & ~backend.cellsAreObjects THEN
  1452. Error(x.position, "unsupported modifier");
  1453. END;
  1454. END Expression;
  1455. (*
  1456. PROCEDURE ResetUsedTemporaries(previous: VariableUse);
  1457. VAR current: VariableUse; set: SET; i,j: LONGINT; variable: SyntaxTree.Variable; op: Operand; tmp: IntermediateCode.Operand;
  1458. BEGIN
  1459. temporaries.GetUsage(current);
  1460. FOR i := 0 TO LEN(current)-1 DO
  1461. set := current[i] - previous[i];
  1462. IF set # {} THEN
  1463. FOR j := 0 TO MAX(SET)-1 DO
  1464. IF j IN set THEN
  1465. variable := temporaries.GetVariable(i*MAX(SET)+j);
  1466. IF (variable.type.resolved IS SyntaxTree.MathArrayType) & (variable.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  1467. Symbol(variable, op);
  1468. MakeMemory(tmp,op.op,addressType,0);
  1469. ReleaseOperand(op);
  1470. Emit(Mov(position,tmp, nil ) );
  1471. ReleaseIntermediateOperand(tmp);
  1472. END;
  1473. END;
  1474. END;
  1475. END;
  1476. END;
  1477. END ResetUsedTemporaries;
  1478. *)
  1479. PROCEDURE Statement(x: SyntaxTree.Statement);
  1480. VAR use: VariableUse;
  1481. BEGIN
  1482. temporaries.GetUsage(use);
  1483. position := x.position;
  1484. IF emitLabels THEN Emit(LabelInstruction(x.position)) END;
  1485. IF commentPrintout # NIL THEN
  1486. commentPrintout.Statement(x);
  1487. dump.Ln;
  1488. (*dump.Update;*)
  1489. END;
  1490. x.Accept(SELF);
  1491. (*
  1492. CheckRegistersFree();
  1493. *)
  1494. (*ResetUsedTemporaries(use);*)
  1495. temporaries.SetUsage(use);
  1496. END Statement;
  1497. (* dereference op. If op is already a memory operand then use auxiliary register to dereference
  1498. result will be registered as a new use of operand, op is not released (op must be released by caller)
  1499. *)
  1500. PROCEDURE MakeMemory(VAR res: IntermediateCode.Operand; op: IntermediateCode.Operand; type: IntermediateCode.Type; offset: LONGINT);
  1501. BEGIN
  1502. ASSERT(op.mode # IntermediateCode.Undefined);
  1503. IF op.mode = IntermediateCode.ModeMemory THEN
  1504. ReuseCopy(res,op);
  1505. ELSE
  1506. res := op;
  1507. UseIntermediateOperand(res);
  1508. END;
  1509. IntermediateCode.AddOffset(res,offset);
  1510. IntermediateCode.MakeMemory(res,type);
  1511. END MakeMemory;
  1512. PROCEDURE ToMemory(VAR res: IntermediateCode.Operand; type: IntermediateCode.Type; offset: LONGINT);
  1513. VAR mem: IntermediateCode.Operand;
  1514. BEGIN
  1515. MakeMemory(mem,res,type,offset);
  1516. ReleaseIntermediateOperand(res);
  1517. res := mem;
  1518. END ToMemory;
  1519. PROCEDURE LoadValue(VAR operand: Operand; type: SyntaxTree.Type);
  1520. VAR mem: IntermediateCode.Operand;
  1521. firstOp, lastOp, stepOp: IntermediateCode.Operand;
  1522. componentType: SyntaxTree.Type;
  1523. BEGIN
  1524. type := type.resolved;
  1525. IF operand.mode = ModeReference THEN
  1526. IF type IS SyntaxTree.RangeType THEN
  1527. MakeMemory(firstOp, operand.op, IntermediateCode.GetType(system, system.longintType), 0);
  1528. MakeMemory(lastOp, operand.op, IntermediateCode.GetType(system, system.longintType), ToMemoryUnits(system, system.SizeOf(system.longintType)));
  1529. MakeMemory(stepOp, operand.op, IntermediateCode.GetType(system, system.longintType), 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)));
  1530. ReleaseOperand(operand);
  1531. operand.op := firstOp;
  1532. operand.tag := lastOp;
  1533. operand.extra := stepOp;
  1534. ELSIF type IS SyntaxTree.ComplexType THEN
  1535. componentType := type(SyntaxTree.ComplexType).componentType;
  1536. ASSERT((componentType.SameType(system.realType)) OR (componentType.SameType(system.longrealType)));
  1537. MakeMemory(firstOp, operand.op, IntermediateCode.GetType(system, componentType), 0);
  1538. MakeMemory(lastOp, operand.op, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  1539. ReleaseOperand(operand);
  1540. operand.op := firstOp;
  1541. operand.tag := lastOp
  1542. ELSE
  1543. MakeMemory(mem,operand.op,IntermediateCode.GetType(system,type),0);
  1544. ReleaseIntermediateOperand(operand.op);
  1545. operand.op := mem;
  1546. END;
  1547. operand.mode := ModeValue;
  1548. END;
  1549. ASSERT(operand.mode = ModeValue);
  1550. END LoadValue;
  1551. PROCEDURE Evaluate(x: SyntaxTree.Expression; VAR op: Operand);
  1552. VAR prevConditional: BOOLEAN;
  1553. BEGIN
  1554. prevConditional := conditional;
  1555. conditional := FALSE;
  1556. InitOperand(result, ModeUndefined);
  1557. Expression(x);
  1558. op := result;
  1559. LoadValue(op,x.type.resolved);
  1560. conditional := prevConditional;
  1561. END Evaluate;
  1562. PROCEDURE Designate(x: SyntaxTree.Expression; VAR op: Operand);
  1563. VAR prevConditional: BOOLEAN;
  1564. BEGIN
  1565. prevConditional := conditional;
  1566. conditional := FALSE;
  1567. InitOperand(result,ModeUndefined);
  1568. Expression(x);
  1569. op := result;
  1570. (*
  1571. ASSERT((op.mode = ModeReference) OR (x.type.resolved IS SyntaxTree.NilType)); (* special case: winapi NIL parameter on references *)
  1572. *)
  1573. conditional := prevConditional;
  1574. END Designate;
  1575. PROCEDURE Condition(x: SyntaxTree.Expression; trueL,falseL: Label);
  1576. VAR prevTrue, prevFalse: Label; prevConditional: BOOLEAN;
  1577. BEGIN
  1578. ASSERT(trueL # NIL); ASSERT(falseL # NIL);
  1579. prevTrue := trueLabel; prevFalse := falseLabel; prevConditional := conditional;
  1580. conditional := TRUE;
  1581. trueLabel := trueL; falseLabel := falseL;
  1582. Expression(x);
  1583. trueL := trueLabel; falseL := falseLabel;
  1584. trueLabel := prevTrue;falseLabel := prevFalse;conditional := prevConditional;
  1585. END Condition;
  1586. PROCEDURE NewRegisterOperand(type: IntermediateCode.Type): IntermediateCode.Operand;
  1587. VAR op: IntermediateCode.Operand; reg: LONGINT;
  1588. BEGIN
  1589. reg := AcquireRegister(type,IntermediateCode.GeneralPurposeRegister);
  1590. IntermediateCode.InitRegister(op, type, IntermediateCode.GeneralPurposeRegister,reg);
  1591. RETURN op
  1592. END NewRegisterOperand;
  1593. PROCEDURE UnuseRegister(register: LONGINT);
  1594. BEGIN
  1595. IF (register > 0) THEN
  1596. register := registerUsageCount.Map(register);
  1597. registerUsageCount.DecUse(register);
  1598. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1599. dump.String("unuse register "); dump.Int(register,1); dump.String(": ");dump.Int(registerUsageCount.Use(register),1); dump.Ln; dump.Update;
  1600. END;
  1601. IF registerUsageCount.Use(register)=0 THEN
  1602. IF ~RemoveRegisterEntry(usedRegisters,register) THEN
  1603. Warning(position, "register cannot be removed");
  1604. END;
  1605. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1606. dump.String("remove register from usedRegisters"); dump.Ln; dump.Update;
  1607. END;
  1608. ELSIF registerUsageCount.Use(register)<0 THEN
  1609. Warning(position, "register removed too often");
  1610. IF dump # NIL THEN
  1611. dump.String("register removed too often"); dump.Ln; dump.Update;
  1612. END;
  1613. END;
  1614. END;
  1615. END UnuseRegister;
  1616. PROCEDURE UseRegister(register: LONGINT);
  1617. BEGIN
  1618. IF (register > 0) THEN
  1619. register := registerUsageCount.Map(register);
  1620. registerUsageCount.IncUse(register);
  1621. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1622. dump.String("use register "); dump.Int(register,1); dump.String(": ");dump.Int(registerUsageCount.Use(register),1); dump.Ln; dump.Update;
  1623. END;
  1624. IF registerUsageCount.Use(register)=1 THEN
  1625. AddRegisterEntry(usedRegisters,register, registerUsageCount.used[register].class, registerUsageCount.used[register].type);
  1626. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1627. dump.String("add register to usedRegisters"); dump.Ln; dump.Update;
  1628. END;
  1629. END;
  1630. END;
  1631. END UseRegister;
  1632. PROCEDURE ReleaseIntermediateOperand(CONST op: IntermediateCode.Operand);
  1633. BEGIN
  1634. UnuseRegister(op.register)
  1635. END ReleaseIntermediateOperand;
  1636. PROCEDURE UseIntermediateOperand(CONST op: IntermediateCode.Operand);
  1637. BEGIN
  1638. UseRegister(op.register)
  1639. END UseIntermediateOperand;
  1640. PROCEDURE ReleaseOperand(CONST op: Operand);
  1641. BEGIN
  1642. UnuseRegister(op.op.register);
  1643. UnuseRegister(op.tag.register);
  1644. UnuseRegister(op.extra.register);
  1645. END ReleaseOperand;
  1646. (* save registers marked in array "markedRegisters" to the stack
  1647. remove entries from array "markedRegisters" and save to array "saved" (=> recursion possible)
  1648. *)
  1649. PROCEDURE SaveRegisters();
  1650. VAR op: IntermediateCode.Operand; entry: RegisterEntry; type: IntermediateCode.Type;
  1651. BEGIN
  1652. entry := usedRegisters;
  1653. WHILE entry # NIL DO
  1654. type := registerUsageCount.used[entry.register].type;
  1655. IntermediateCode.InitRegister(op,entry.type,entry.registerClass, entry.register);
  1656. Emit(Push(position,op));
  1657. entry := entry.next;
  1658. END;
  1659. END SaveRegisters;
  1660. PROCEDURE ReleaseUsedRegisters(VAR saved: RegisterEntry);
  1661. BEGIN
  1662. saved := usedRegisters;
  1663. usedRegisters := NIL;
  1664. END ReleaseUsedRegisters;
  1665. (** remove parameter registers from used queue *)
  1666. PROCEDURE ReleaseParameterRegisters;
  1667. VAR entry,prev,next: RegisterEntry;
  1668. BEGIN
  1669. entry := usedRegisters; prev := NIL; usedRegisters := NIL;
  1670. WHILE entry # NIL DO
  1671. next := entry.next;
  1672. IF entry.registerClass.class = IntermediateCode.Parameter THEN
  1673. registerUsageCount.DecUse(entry.register);
  1674. ASSERT(registerUsageCount.Use(entry.register)=0);
  1675. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1676. dump.String("unuse register "); dump.Int(entry.register,1); dump.Ln; dump.Update;
  1677. END;
  1678. ELSIF prev = NIL THEN
  1679. usedRegisters := entry; entry.prev := NIL; entry.next := NIL; prev := entry;
  1680. ELSE
  1681. prev.next := entry; entry.prev := prev; entry.next := NIL; prev:= entry;
  1682. END;
  1683. entry := next;
  1684. END;
  1685. END ReleaseParameterRegisters;
  1686. (* restore registers from array saved and re-enter into array markedRegisters (recursion possible) *)
  1687. PROCEDURE RestoreRegisters(CONST saved: RegisterEntry);
  1688. VAR op: IntermediateCode.Operand; entry,prev: RegisterEntry; type: IntermediateCode.Type; class: IntermediateCode.RegisterClass;
  1689. BEGIN
  1690. entry := saved;
  1691. WHILE (entry # NIL) DO prev := entry; entry := entry.next END;
  1692. entry := prev;
  1693. WHILE entry # NIL DO
  1694. prev := entry.prev;
  1695. type := entry.type;
  1696. class := entry.registerClass;
  1697. IntermediateCode.InitRegister(op,type,class,entry.register);
  1698. (*
  1699. new := registerUsageCount.Next(type,class);
  1700. registerUsageCount.Remap(entry.register,new);
  1701. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1702. dump.String("remap register "); dump.Int(entry.register,1);
  1703. dump.String("to "); dump.Int(new,1);
  1704. dump.String("with count "); dump.Int(registerUsageCount.Use(new),1); dump.Ln; dump.Update;
  1705. END;
  1706. entry.register := new;
  1707. *)
  1708. Emit(Pop(position,op));
  1709. AddRegisterEntry(usedRegisters,entry.register,entry.registerClass, entry.type);
  1710. entry := prev;
  1711. END;
  1712. (*
  1713. usedRegisters := saved;
  1714. *)
  1715. END RestoreRegisters;
  1716. PROCEDURE CheckRegistersFree;
  1717. VAR r: RegisterEntry; warning: ARRAY 128 OF CHAR; i: LONGINT;
  1718. BEGIN
  1719. IF usedRegisters # NIL THEN
  1720. r := usedRegisters;
  1721. WHILE r # NIL DO
  1722. warning := "register ";
  1723. Strings.AppendInt(warning, r.register);
  1724. Strings.Append(warning, " not released.");
  1725. Warning(position,warning);
  1726. r := r .next;
  1727. END;
  1728. END;
  1729. FOR i := 0 TO registerUsageCount.count-1 DO
  1730. IF registerUsageCount.used[i].count < 0 THEN
  1731. warning := "register ";
  1732. Strings.AppendInt(warning, i);
  1733. Strings.Append(warning, " unused too often.");
  1734. Warning(position,warning);
  1735. ELSIF registerUsageCount.used[i].count > 0 THEN (* should always coincide with cases above *)
  1736. warning := "register ";
  1737. Strings.AppendInt(warning, i);
  1738. Strings.Append(warning, " not unused often enough.");
  1739. Warning(position,warning);
  1740. END;
  1741. END;
  1742. END CheckRegistersFree;
  1743. (* Reuse2: reuse src1 or src2 for ongoing computation if src1 or src2, respectively, is a register.
  1744. Otherwise allocate a new register.
  1745. Does NOT necessarily keep the content of src1 or src2 in result! *)
  1746. PROCEDURE Reuse2(VAR result: IntermediateCode.Operand; src1,src2: IntermediateCode.Operand);
  1747. BEGIN
  1748. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1749. UseIntermediateOperand(result);
  1750. ELSIF ReusableRegister(src2) THEN IntermediateCode.InitRegister(result,src2.type,src2.registerClass, src2.register);
  1751. UseIntermediateOperand(result);
  1752. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass,AcquireRegister(src1.type, src1.registerClass));
  1753. END;
  1754. END Reuse2;
  1755. (* Reuse2a: reuse src1 or src2 for ongoing computation if src1 or src2, respectively, is a register.
  1756. Otherwise check if an alternative destination is available. If so, then take the alternative (which is not necessarily a register).
  1757. If not then allocate a new register.
  1758. Does NOT necessarily keep the content of src1 or src2 in result!
  1759. *)
  1760. PROCEDURE Reuse2a(VAR result: IntermediateCode.Operand; src1,src2: IntermediateCode.Operand; VAR alternative: IntermediateCode.Operand);
  1761. BEGIN
  1762. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1763. UseIntermediateOperand(result);
  1764. ELSIF ReusableRegister(src2) THEN IntermediateCode.InitRegister(result,src2.type,src2.registerClass, src2.register);
  1765. UseIntermediateOperand(result);
  1766. ELSIF alternative.mode # IntermediateCode.Undefined THEN
  1767. result := alternative; alternative := emptyOperand;
  1768. UseIntermediateOperand(result);
  1769. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1770. END;
  1771. END Reuse2a;
  1772. (* like reuse2 but only one source *)
  1773. PROCEDURE Reuse1(VAR result: IntermediateCode.Operand; src1: IntermediateCode.Operand);
  1774. BEGIN
  1775. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1776. UseIntermediateOperand(result);
  1777. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1778. END;
  1779. END Reuse1;
  1780. (* like reuse2a but only one source *)
  1781. PROCEDURE Reuse1a(VAR result: IntermediateCode.Operand; src1: IntermediateCode.Operand; VAR alternative: IntermediateCode.Operand);
  1782. BEGIN
  1783. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1784. UseIntermediateOperand(result);
  1785. ELSIF alternative.mode # IntermediateCode.Undefined THEN result := alternative; alternative := emptyOperand;
  1786. UseIntermediateOperand(result);
  1787. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1788. END;
  1789. END Reuse1a;
  1790. (* like reuse1 but guarantees that content of src1 is in result *)
  1791. PROCEDURE ReuseCopy(VAR result: IntermediateCode.Operand; src1: IntermediateCode.Operand);
  1792. BEGIN
  1793. IF ReusableRegister(src1) THEN
  1794. IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1795. ASSERT((src1.mode = IntermediateCode.ModeRegister) & (src1.offset = 0));
  1796. UseIntermediateOperand(result);
  1797. ELSE
  1798. IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1799. Emit(Mov(position,result,src1));
  1800. END
  1801. END ReuseCopy;
  1802. PROCEDURE TransferToRegister(VAR result: IntermediateCode.Operand; src: IntermediateCode.Operand);
  1803. BEGIN
  1804. IF ReusableRegister(src) THEN
  1805. IntermediateCode.InitRegister(result,src.type,src.registerClass, src.register);
  1806. ELSE
  1807. IntermediateCode.InitRegister(result,src.type,src.registerClass, AcquireRegister(src.type, src.registerClass));
  1808. Emit(Mov(position,result,src));
  1809. ReleaseIntermediateOperand(src);
  1810. END
  1811. END TransferToRegister;
  1812. (** labels and branches **)
  1813. PROCEDURE NewLabel(): Label;
  1814. VAR label: Label;
  1815. BEGIN
  1816. NEW(label,section); RETURN label;
  1817. END NewLabel;
  1818. PROCEDURE SetLabel(label: Label);
  1819. BEGIN label.Resolve(section.pc);
  1820. END SetLabel;
  1821. PROCEDURE LabelOperand(label: Label): IntermediateCode.Operand;
  1822. BEGIN
  1823. ASSERT(label # NIL);
  1824. IF label.pc < 0 THEN (* label not yet set *)
  1825. label.AddFixup(section.pc);
  1826. END;
  1827. RETURN IntermediateCode.Address(addressType,label.section.name,GetFingerprint(label.section.symbol), label.pc);
  1828. END LabelOperand;
  1829. PROCEDURE BrL(label: Label);
  1830. BEGIN
  1831. Emit(Br(position,LabelOperand(label)));
  1832. END BrL;
  1833. PROCEDURE BrgeL(label: Label; left,right: IntermediateCode.Operand);
  1834. BEGIN
  1835. Emit(Brge(position,LabelOperand(label),left,right));
  1836. END BrgeL;
  1837. PROCEDURE BrltL(label: Label; left,right: IntermediateCode.Operand);
  1838. BEGIN
  1839. Emit(Brlt(position,LabelOperand(label),left,right));
  1840. END BrltL;
  1841. PROCEDURE BreqL(label: Label; left,right: IntermediateCode.Operand);
  1842. BEGIN
  1843. Emit(Breq(position,LabelOperand(label),left,right));
  1844. END BreqL;
  1845. PROCEDURE BrneL(label: Label; left,right: IntermediateCode.Operand);
  1846. BEGIN
  1847. Emit(Brne(position,LabelOperand(label),left,right));
  1848. END BrneL;
  1849. PROCEDURE Convert(VAR operand: IntermediateCode.Operand; type: IntermediateCode.Type);
  1850. VAR new: IntermediateCode.Operand;
  1851. BEGIN
  1852. IF Trace THEN TraceEnter("Convert") END;
  1853. IF IntermediateCode.TypeEquals(type,operand.type) THEN (* nothing to be done *)
  1854. ELSIF (operand.mode = IntermediateCode.ModeRegister) THEN
  1855. IF (type.sizeInBits = operand.type.sizeInBits) & (type.form IN IntermediateCode.Integer) & (operand.type.form IN IntermediateCode.Integer)
  1856. & (operand.offset = 0)
  1857. THEN
  1858. IntermediateCode.InitRegister(new,type,IntermediateCode.GeneralPurposeRegister,operand.register);
  1859. Emit(Conv(position,new,operand));
  1860. ELSE
  1861. IntermediateCode.InitRegister(new,type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(type,IntermediateCode.GeneralPurposeRegister));
  1862. Emit(Conv(position,new,operand));
  1863. ReleaseIntermediateOperand(operand);
  1864. END;
  1865. operand := new;
  1866. ELSIF (operand.mode = IntermediateCode.ModeImmediate) & (operand.symbol.name = "") & (operand.type.sizeInBits <= type.sizeInBits) & (operand.type.form IN IntermediateCode.Integer) & (type.form IN IntermediateCode.Integer) THEN
  1867. IntermediateCode.InitImmediate(operand,type,operand.intValue);
  1868. ELSE
  1869. IntermediateCode.InitRegister(new,type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(type,IntermediateCode.GeneralPurposeRegister));
  1870. Emit(Conv(position,new,operand));
  1871. ReleaseIntermediateOperand(operand);
  1872. operand := new;
  1873. END;
  1874. IF Trace THEN TraceExit("Convert") END;
  1875. END Convert;
  1876. PROCEDURE TrapC(br: ConditionalBranch; left,right:IntermediateCode.Operand; trapNo: LONGINT);
  1877. VAR exit: Label;
  1878. BEGIN
  1879. Assert((left.mode # IntermediateCode.ModeImmediate) OR (right.mode # IntermediateCode.ModeImmediate),"trap emission with two immediates");
  1880. exit := NewLabel();
  1881. br(exit,left,right);
  1882. EmitTrap(position,trapNo);
  1883. SetLabel(exit);
  1884. END TrapC;
  1885. (** expressions *)
  1886. (** emit necessary runtime check for set elements **)
  1887. PROCEDURE CheckSetElement(o: IntermediateCode.Operand);
  1888. VAR max: IntermediateCode.Operand;
  1889. BEGIN
  1890. IF isUnchecked THEN RETURN END;
  1891. IF o.mode # IntermediateCode.ModeImmediate THEN (* otherwise it's the job of the checker *)
  1892. IntermediateCode.InitImmediate(max, setType, setType.sizeInBits (* number of bits in set *) -1);
  1893. TrapC(BrgeL, max, o, SetElementTrap);
  1894. END;
  1895. END CheckSetElement;
  1896. (** the set that a range represents **)
  1897. PROCEDURE SetFromRange(x: SyntaxTree.RangeExpression): IntermediateCode.Operand;
  1898. VAR
  1899. operand: Operand;
  1900. resultingSet, temp, size, allBits, noBits, one: IntermediateCode.Operand;
  1901. BEGIN
  1902. ASSERT((x.first # NIL) & (x.last # NIL)); (* ensured by the checker *)
  1903. allBits := IntermediateCode.Immediate(setType, -1); (* bit mask 111...11111 *)
  1904. noBits := IntermediateCode.Immediate(setType, 0); (* bit mask 0...0 *)
  1905. one := IntermediateCode.Immediate(setType, 1);
  1906. Evaluate(x, operand);
  1907. Convert(operand.op, setType);
  1908. Convert(operand.tag, setType);
  1909. CheckSetElement(operand.op);
  1910. CheckSetElement(operand.tag);
  1911. (* create mask for lower bound
  1912. i.e. shift 11111111 to the left by the value of the lower bound
  1913. *)
  1914. Reuse1(temp, operand.op);
  1915. Emit(Shl(position,temp, allBits, operand.op));
  1916. ReleaseIntermediateOperand(operand.op);
  1917. operand.op := temp;
  1918. (* create mask for upper bound
  1919. i.e. shift 11111111 to the right by the difference between the
  1920. upper bound and the maximum number of set elements
  1921. *)
  1922. IF (operand.tag.mode = IntermediateCode.ModeImmediate) & (operand.tag.symbol.name = "") THEN
  1923. IntermediateCode.InitImmediate(operand.tag, operand.tag.type, operand.op.type.sizeInBits - 1- operand.tag.intValue);
  1924. Reuse1(temp, operand.tag);
  1925. ELSE
  1926. Reuse1(temp, operand.tag);
  1927. IntermediateCode.InitImmediate(size, operand.tag.type, operand.op.type.sizeInBits - 1);
  1928. Emit(Sub(position,temp, size, operand.tag));
  1929. END;
  1930. Emit(Shr(position,temp, allBits, operand.tag));
  1931. ReleaseIntermediateOperand(operand.tag);
  1932. operand.tag := temp;
  1933. Reuse2(resultingSet, operand.op, operand.tag);
  1934. (* intersect the two masks *)
  1935. Emit(And(position,resultingSet, operand.op, operand.tag));
  1936. ReleaseOperand(operand);
  1937. RETURN resultingSet
  1938. END SetFromRange;
  1939. PROCEDURE VisitSet(x: SyntaxTree.Set);
  1940. VAR
  1941. res, operand: Operand;
  1942. temp, one, noBits, dest: IntermediateCode.Operand;
  1943. expression: SyntaxTree.Expression;
  1944. i: LONGINT;
  1945. BEGIN
  1946. IF Trace THEN TraceEnter("VisitSet") END;
  1947. dest := destination;
  1948. destination := emptyOperand;
  1949. noBits := IntermediateCode.Immediate(setType, 0);
  1950. one := IntermediateCode.Immediate(setType, 1);
  1951. (* start off with the empty set *)
  1952. InitOperand(res, ModeValue);
  1953. IntermediateCode.InitRegister(res.op, setType, IntermediateCode.GeneralPurposeRegister, AcquireRegister(setType, IntermediateCode.GeneralPurposeRegister));
  1954. Emit(Mov(position,res.op, noBits));
  1955. FOR i := 0 TO x.elements.Length() - 1 DO
  1956. expression := x.elements.GetExpression(i);
  1957. IF expression IS SyntaxTree.RangeExpression THEN
  1958. (* range of set elements *)
  1959. temp := SetFromRange(expression(SyntaxTree.RangeExpression));
  1960. ASSERT(IntermediateCode.TypeEquals(setType, temp.type));
  1961. Emit(Or(position,res.op, res.op, temp)); (* unify subset with current set *)
  1962. ReleaseIntermediateOperand(temp)
  1963. ELSE
  1964. (* singelton element *)
  1965. Evaluate(expression, operand);
  1966. Convert(operand.op, setType);
  1967. CheckSetElement(operand.op);
  1968. (* create subset containing single element *)
  1969. Reuse1(temp, operand.op);
  1970. Emit(Shl(position,temp, one, operand.op));
  1971. ReleaseOperand(operand);
  1972. Emit(Or(position,res.op, res.op, temp)); (* unify subset with current set *)
  1973. ReleaseIntermediateOperand(temp);
  1974. END
  1975. END;
  1976. result := res;
  1977. destination := dest;
  1978. IF Trace THEN TraceExit("VisitSet") END;
  1979. END VisitSet;
  1980. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1981. VAR variable: SyntaxTree.Variable; index: SyntaxTree.IndexDesignator; dim: LONGINT;
  1982. designator: SyntaxTree.Designator; i: LONGINT; element: SyntaxTree.IntegerValue;
  1983. PROCEDURE RecursiveAssignment(x: SyntaxTree.MathArrayExpression; dim: LONGINT);
  1984. VAR numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1985. element: SyntaxTree.IntegerValue;
  1986. BEGIN
  1987. numberElements := x.elements.Length();
  1988. expression := index.parameters.GetExpression(dim);
  1989. element := expression(SyntaxTree.IntegerValue);
  1990. FOR i := 0 TO numberElements-1 DO
  1991. expression := x.elements.GetExpression(i);
  1992. element.SetValue(i);
  1993. IF expression IS SyntaxTree.MathArrayExpression THEN
  1994. RecursiveAssignment(expression(SyntaxTree.MathArrayExpression),dim+1);
  1995. ELSE
  1996. Assign(index,expression);
  1997. END;
  1998. END;
  1999. END RecursiveAssignment;
  2000. BEGIN
  2001. variable := GetTemporaryVariable(x.type, FALSE);
  2002. designator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,variable);
  2003. designator.SetType(variable.type);
  2004. dim := SemanticChecker.Dimension(x.type,{SyntaxTree.Static});
  2005. index := SyntaxTree.NewIndexDesignator(x.position,designator);
  2006. FOR i := 0 TO dim-1 DO
  2007. element := SyntaxTree.NewIntegerValue(x.position,0);
  2008. element.SetType(system.longintType);
  2009. index.parameters.AddExpression(element);
  2010. END;
  2011. index.SetType(SemanticChecker.ArrayBase(x.type,dim));
  2012. RecursiveAssignment(x,0);
  2013. Expression(designator);
  2014. END VisitMathArrayExpression;
  2015. PROCEDURE VisitUnaryExpression(x: SyntaxTree.UnaryExpression);
  2016. VAR type,t0: SyntaxTree.Type; operand: Operand; dest: IntermediateCode.Operand;
  2017. BEGIN
  2018. IF Trace THEN TraceEnter("VisitUnaryExpression") END;
  2019. dest := destination; destination := emptyOperand;
  2020. IF x.operator = Scanner.Not THEN
  2021. IF conditional THEN
  2022. Condition(x.left,falseLabel,trueLabel)
  2023. ELSE
  2024. Evaluate(x.left,operand);
  2025. InitOperand(result,ModeValue);
  2026. Reuse1a(result.op,operand.op,dest);
  2027. Emit(Xor(position,result.op,operand.op,true));
  2028. ReleaseOperand(operand);
  2029. END;
  2030. ELSIF x.operator = Scanner.Minus THEN
  2031. Evaluate(x.left,operand);
  2032. InitOperand(result,ModeValue);
  2033. Reuse1a(result.op,operand.op,dest);
  2034. type := x.left.type.resolved;
  2035. IF type IS SyntaxTree.SetType THEN
  2036. Emit(Not(position,result.op,operand.op));
  2037. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2038. Reuse1(result.tag,operand.tag);
  2039. Emit(Neg(position,result.op,operand.op)); (* real part *)
  2040. Emit(Neg(position,result.tag,operand.tag)) (* imaginary part *)
  2041. ELSIF (type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.SizeType) OR (type IS SyntaxTree.AddressType) THEN
  2042. Emit(Neg(position,result.op,operand.op));
  2043. ELSE HALT(200)
  2044. END;
  2045. ReleaseOperand(operand);
  2046. ELSIF x.operator = Scanner.Address THEN
  2047. Designate(x.left,operand);
  2048. operand.mode := ModeValue;
  2049. t0 := x.left.type.resolved;
  2050. IF (t0 IS SyntaxTree.MathArrayType) & (t0(SyntaxTree.MathArrayType).form = SyntaxTree.Open) THEN
  2051. ReleaseIntermediateOperand(operand.op);
  2052. operand.op := operand.tag;
  2053. IntermediateCode.InitOperand(operand.tag);
  2054. END;
  2055. Convert(operand.op,IntermediateCode.GetType(system,x.type));
  2056. result := operand;
  2057. ELSE HALT(100)
  2058. END;
  2059. destination := dest;
  2060. IF Trace THEN TraceExit("VisitUnaryExpression") END;
  2061. END VisitUnaryExpression;
  2062. (* test if e is of type type, side effect: result of evaluation of e stays in the operand *)
  2063. PROCEDURE TypeTest(tag: IntermediateCode.Operand; type: SyntaxTree.Type; trueL,falseL: Label);
  2064. VAR left,right: IntermediateCode.Operand; level,offset: LONGINT; repeatL: Label; originalType: SyntaxTree.Type;
  2065. BEGIN
  2066. type := type.resolved;
  2067. originalType := type;
  2068. IF type IS SyntaxTree.PointerType THEN
  2069. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  2070. END;
  2071. IF type IS SyntaxTree.ObjectType THEN
  2072. BrL(trueL);
  2073. ELSE
  2074. ASSERT(type IS SyntaxTree.RecordType);
  2075. (*
  2076. IntermediateCode.MakeMemory(tag,addressType); (*! already done during generation *)
  2077. *)
  2078. ReuseCopy(left,tag);
  2079. right := TypeDescriptorAdr(type);
  2080. IF ~newObjectFile THEN
  2081. IntermediateCode.MakeMemory(right,addressType);
  2082. END;
  2083. IF backend.cooperative THEN
  2084. repeatL := NewLabel();
  2085. IF (originalType IS SyntaxTree.PointerType) & ~type(SyntaxTree.RecordType).isObject THEN
  2086. Emit(Mov(position,left,IntermediateCode.Memory(addressType,left,ToMemoryUnits(system,addressType.sizeInBits))));
  2087. END;
  2088. SetLabel(repeatL);
  2089. BreqL(trueL,left,right);
  2090. Emit(Mov(position,left,IntermediateCode.Memory(addressType,left,0)));
  2091. BrneL(repeatL,left,nil);
  2092. ELSIF meta.simple THEN
  2093. level := type(SyntaxTree.RecordType).Level();
  2094. (* get type desc tag of level relative to base tag *)
  2095. offset := (meta.BaseTypesTableOffset + level) * addressType.sizeInBits;
  2096. IntermediateCode.AddOffset(left,ToMemoryUnits(system,offset));
  2097. IntermediateCode.MakeMemory(left,addressType);
  2098. BreqL(trueL,left,right);
  2099. ELSE
  2100. level := type(SyntaxTree.RecordType).Level();
  2101. (* get type desc tag of level relative to base tag *)
  2102. offset := (meta.BaseTypesTableOffset - level) * addressType.sizeInBits;
  2103. IntermediateCode.AddOffset(left,ToMemoryUnits(system,offset));
  2104. IntermediateCode.MakeMemory(left,addressType);
  2105. BreqL(trueL,left,right);
  2106. END;
  2107. ReleaseIntermediateOperand(left); ReleaseIntermediateOperand(right);
  2108. BrL(falseL);
  2109. END;
  2110. END TypeTest;
  2111. PROCEDURE Error(position: Position; CONST s: ARRAY OF CHAR);
  2112. BEGIN
  2113. backend.Error(module.module.sourceName,position,Diagnostics.Invalid,s);
  2114. IF dump # NIL THEN
  2115. dump.String(s); dump.Ln;
  2116. END;
  2117. END Error;
  2118. PROCEDURE Warning(position: Position; CONST s: ARRAY OF CHAR);
  2119. BEGIN
  2120. Basic.Warning(backend.diagnostics, module.module.sourceName,position, s);
  2121. IF dump # NIL THEN
  2122. dump.String(s); dump.Ln; dump.Update;
  2123. END;
  2124. END Warning;
  2125. PROCEDURE CreateTraceModuleMethod(mod: SyntaxTree.Module);
  2126. VAR name: Basic.SectionName; pooledName: Basic.SegmentedName; previousSection: IntermediateCode.Section;
  2127. VAR variable: SyntaxTree.Variable; register,op: IntermediateCode.Operand;
  2128. operand:Operand;
  2129. BEGIN
  2130. previousSection := section;
  2131. Global.GetModuleName(mod,name);
  2132. Strings.Append(name,".@Trace");
  2133. Basic.ToSegmentedName(name, pooledName);
  2134. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, pooledName,NIL,TRUE);
  2135. IF dump # NIL THEN dump := section.comments END;
  2136. IF backend.hasLinkRegister THEN
  2137. Emit(Push(Basic.invalidPosition, lr));
  2138. END;
  2139. variable := mod.moduleScope.firstVariable;
  2140. WHILE variable # NIL DO
  2141. IF ~variable.untraced & variable.type.resolved.hasPointers THEN
  2142. Symbol(variable, operand);
  2143. register := operand.op;
  2144. CallTraceMethod(register, variable.type);
  2145. ReleaseIntermediateOperand(register);
  2146. END;
  2147. variable := variable.nextVariable;
  2148. END;
  2149. IF backend.hasLinkRegister THEN
  2150. Emit(Pop(Basic.invalidPosition, lr));
  2151. END;
  2152. Basic.ToSegmentedName ("Modules.Module.@Trace",pooledName);
  2153. IntermediateCode.InitAddress(op, addressType, pooledName , 0, 0);
  2154. Emit(Br(position,op));
  2155. INC(statCoopTraceModule, section.pc);
  2156. section := previousSection;
  2157. IF dump # NIL THEN dump := section.comments END;
  2158. END CreateTraceModuleMethod;
  2159. PROCEDURE CallAssignPointer(CONST dst (* address *) , src (* value *): IntermediateCode.Operand);
  2160. BEGIN
  2161. Emit (Push(position, dst));
  2162. Emit (Push(position, src));
  2163. CallThis(position,"GarbageCollector","Assign", 2);
  2164. END CallAssignPointer;
  2165. PROCEDURE CallAssignMethod(CONST dst (* address *) , src (* address *) : IntermediateCode.Operand; type: SyntaxTree.Type);
  2166. VAR name: Basic.SegmentedName; size: LONGINT; base: SyntaxTree.Type; op: IntermediateCode.Operand;
  2167. BEGIN
  2168. IF SemanticChecker.IsPointerType (type) THEN
  2169. CallAssignPointer(dst, IntermediateCode.Memory (addressType,src,0));
  2170. ELSIF type.IsRecordType() THEN
  2171. Emit (Push(position,dst));
  2172. Emit (Push(position,src));
  2173. GetRecordTypeName (type.resolved(SyntaxTree.RecordType),name);
  2174. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2175. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2176. Emit(Call(position,op, ToMemoryUnits(system, 2*system.addressSize)));
  2177. ELSIF IsStaticArray(type) THEN
  2178. size := StaticArrayNumElements(type);
  2179. base := StaticArrayBaseType(type);
  2180. IF base.IsRecordType() THEN
  2181. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2182. Emit (Push(position, dst));
  2183. Emit (Push(position, src));
  2184. GetRecordTypeName (base.resolved(SyntaxTree.RecordType),name);
  2185. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2186. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2187. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2188. Emit(Call(position,op,ToMemoryUnits(system, 3*system.addressSize)));
  2189. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  2190. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2191. Emit (Push(position, dst));
  2192. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2193. Emit (Push(position, src));
  2194. CallThis(position,"GarbageCollector","AssignDelegateArray", 4);
  2195. ELSE
  2196. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2197. Emit (Push(position, dst));
  2198. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2199. Emit (Push(position, src));
  2200. CallThis(position,"GarbageCollector","AssignPointerArray", 4);
  2201. ASSERT(StaticArrayBaseType(type).IsPointer());
  2202. END;
  2203. ELSIF type.resolved IS SyntaxTree.ProcedureType THEN
  2204. ASSERT(type.resolved(SyntaxTree.ProcedureType).isDelegate);
  2205. Emit (Push(position, dst));
  2206. Emit (Push(position, src));
  2207. CallThis(position,"GarbageCollector","AssignDelegate", 2);
  2208. ELSE HALT(100); (* missing ? *)
  2209. END;
  2210. END CallAssignMethod;
  2211. PROCEDURE CreateAssignProcedure (recordType: SyntaxTree.RecordType);
  2212. VAR name: Basic.SegmentedName;
  2213. VAR variable: SyntaxTree.Variable; src, dst, op, ofs: IntermediateCode.Operand; recordBase: SyntaxTree.RecordType;
  2214. parameter1, parameter2, parameter0: IntermediateCode.Operand; label: Label;
  2215. context: Context;
  2216. BEGIN
  2217. parameter0 (* len *) := IntermediateCode.Memory(sizeType,sp,ToMemoryUnits(system,3*addressType.sizeInBits));
  2218. parameter1 (* dest *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,2*addressType.sizeInBits));
  2219. parameter2 (* src *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,1*addressType.sizeInBits));
  2220. GetRecordTypeName (recordType,name);
  2221. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2222. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE));
  2223. IF dump # NIL THEN dump := section.comments END;
  2224. IF backend.hasLinkRegister THEN
  2225. Emit(Push(Basic.invalidPosition, lr));
  2226. END;
  2227. variable := recordType.recordScope.firstVariable;
  2228. WHILE variable # NIL DO
  2229. IF variable.NeedsTrace() THEN
  2230. dst := NewRegisterOperand (addressType);
  2231. src := NewRegisterOperand (addressType);
  2232. Emit (Mov(position, dst, parameter1));
  2233. Emit (Mov(position, src, parameter2));
  2234. IntermediateCode.AddOffset(dst,ToMemoryUnits(system,variable.offsetInBits));
  2235. IntermediateCode.AddOffset(src,ToMemoryUnits(system,variable.offsetInBits));
  2236. CallAssignMethod(dst, src, variable.type);
  2237. ReleaseIntermediateOperand(src);
  2238. ReleaseIntermediateOperand(dst);
  2239. END;
  2240. variable := variable.nextVariable;
  2241. END;
  2242. recordBase := recordType.GetBaseRecord();
  2243. IF (recordBase # NIL) & recordBase.NeedsTrace() THEN
  2244. IF backend.hasLinkRegister THEN
  2245. Emit(Pop(Basic.invalidPosition, lr));
  2246. END;
  2247. GetRecordTypeName (recordBase,name);
  2248. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2249. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2250. Emit(Br(position,op));
  2251. ELSE
  2252. Emit(Exit(position,0,0, 0));
  2253. END;
  2254. IF ~recordType.isObject THEN
  2255. GetRecordTypeName (recordType,name);
  2256. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2257. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2258. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL);
  2259. NEW(registerUsageCount);
  2260. usedRegisters := NIL;
  2261. dst := NewRegisterOperand (addressType);
  2262. src := NewRegisterOperand (addressType);
  2263. ofs := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(recordType)));
  2264. Emit(Mov(position, dst, parameter1));
  2265. Emit(Mov(position, src, parameter2));
  2266. label := NewLabel();
  2267. SetLabel(label);
  2268. Emit(Push(position, dst));
  2269. Emit(Push(position, src));
  2270. GetRecordTypeName (recordType,name);
  2271. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2272. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2273. Emit(Call(position, op, 0));
  2274. Emit(Pop(position, src));
  2275. Emit(Pop(position, dst));
  2276. Emit(Add(position, dst, dst, ofs));
  2277. Emit(Add(position, src, src, ofs));
  2278. Emit(Sub(position, parameter0, parameter0, IntermediateCode.Immediate(sizeType, 1)));
  2279. BrneL(label, parameter0, IntermediateCode.Immediate(sizeType, 0));
  2280. Emit(Exit(position,0,0, 0));
  2281. END;
  2282. INC(statCoopAssignProcedure, section.pc);
  2283. ReturnToContext(context);
  2284. IF dump # NIL THEN dump := section.comments END;
  2285. END CreateAssignProcedure;
  2286. PROCEDURE CallTraceMethod(CONST register: IntermediateCode.Operand; type: SyntaxTree.Type);
  2287. VAR name: Basic.SegmentedName; op: IntermediateCode.Operand; size: LONGINT; base: SyntaxTree.Type; skip: Label;
  2288. BEGIN
  2289. IF IsUnsafePointer (type) THEN
  2290. skip := NewLabel();
  2291. IntermediateCode.InitRegister(op, addressType, IntermediateCode.GeneralPurposeRegister, register.register);
  2292. Emit (Mov (position, op, IntermediateCode.Memory (addressType,register,0)));
  2293. BreqL (skip, op, nil);
  2294. CallTraceMethod (op,type.resolved(SyntaxTree.PointerType).pointerBase);
  2295. SetLabel (skip);
  2296. ELSIF SemanticChecker.IsPointerType (type) THEN
  2297. Emit (Push(position, IntermediateCode.Memory (addressType,register,0)));
  2298. CallThis(position,"GarbageCollector","Mark", 1);
  2299. ELSIF type.IsRecordType() THEN
  2300. Emit (Push(position,register));
  2301. GetRecordTypeName (type.resolved(SyntaxTree.RecordType),name);
  2302. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2303. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2304. Emit(Call(position,op, ToMemoryUnits(system, system.addressSize)));
  2305. ELSIF IsStaticArray(type) THEN
  2306. size := StaticArrayNumElements(type);
  2307. base := StaticArrayBaseType(type);
  2308. IF base.IsRecordType() THEN
  2309. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2310. Emit (Push(position, register));
  2311. GetRecordTypeName (base.resolved(SyntaxTree.RecordType), name);
  2312. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2313. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2314. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2315. Emit(Call(position, op, ToMemoryUnits(system, system.addressSize*2)));
  2316. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  2317. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2318. Emit (Push(position, register));
  2319. CallThis(position,"GarbageCollector","MarkDelegateArray", 2);
  2320. ELSE
  2321. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2322. Emit (Push(position, register));
  2323. CallThis(position,"GarbageCollector","MarkPointerArray", 2);
  2324. ASSERT(base.IsPointer());
  2325. END;
  2326. ELSIF type.resolved IS SyntaxTree.ProcedureType THEN
  2327. Emit (Push(position, IntermediateCode.Memory (addressType,register,ToMemoryUnits(system,addressType.sizeInBits))));
  2328. CallThis(position,"GarbageCollector","Mark", 1);
  2329. ELSE HALT(100); (* missing ? *)
  2330. END;
  2331. END CallTraceMethod;
  2332. PROCEDURE CreateTraceMethod (recordType: SyntaxTree.RecordType);
  2333. VAR name: Basic.SegmentedName; previousSection: IntermediateCode.Section;
  2334. VAR variable: SyntaxTree.Variable; register,op,ofs: IntermediateCode.Operand; recordBase: SyntaxTree.RecordType;
  2335. parameter0, parameter1: IntermediateCode.Operand; label: Label; context: Context;
  2336. BEGIN
  2337. previousSection := section;
  2338. parameter0 (* size *) := IntermediateCode.Memory(sizeType,sp,ToMemoryUnits(system,2*addressType.sizeInBits));
  2339. parameter1 (* address *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,1*addressType.sizeInBits));
  2340. GetRecordTypeName (recordType,name);
  2341. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2342. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL));
  2343. IF dump # NIL THEN dump := section.comments END;
  2344. IF backend.hasLinkRegister THEN
  2345. Emit(Push(Basic.invalidPosition, lr));
  2346. END;
  2347. variable := recordType.recordScope.firstVariable;
  2348. WHILE variable # NIL DO
  2349. IF variable.NeedsTrace() THEN
  2350. register := NewRegisterOperand (addressType);
  2351. Emit (Mov(position,register,parameter1));
  2352. IntermediateCode.AddOffset(register,ToMemoryUnits(system,variable.offsetInBits));
  2353. IF recordType.isObject & ((recordType.pointerType = NIL) OR ~recordType.pointerType.isPlain) THEN
  2354. IntermediateCode.AddOffset(register,BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  2355. END;
  2356. CallTraceMethod(register, variable.type);
  2357. ReleaseIntermediateOperand(register);
  2358. END;
  2359. variable := variable.nextVariable;
  2360. END;
  2361. recordBase := recordType.GetBaseRecord();
  2362. WHILE (recordBase # NIL) & ~recordBase.hasPointers DO
  2363. recordBase := recordBase.GetBaseRecord();
  2364. END;
  2365. IF recordBase # NIL THEN
  2366. IF backend.hasLinkRegister THEN
  2367. Emit(Pop(Basic.invalidPosition, lr));
  2368. END;
  2369. GetRecordTypeName (recordBase,name);
  2370. IF HasExplicitTraceMethod (recordBase) THEN
  2371. Basic.SuffixSegmentedName (name, Basic.MakeString ("Trace"));
  2372. ELSE
  2373. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2374. END;
  2375. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2376. Emit(Br(position,op));
  2377. ELSIF (recordType.pointerType # NIL) & recordType.pointerType.isPlain THEN
  2378. Emit(Exit(position,0,0,0));
  2379. ELSE
  2380. IF backend.hasLinkRegister THEN
  2381. Emit(Pop(Basic.invalidPosition, lr));
  2382. END;
  2383. IF recordType.isObject THEN
  2384. Basic.ToSegmentedName ("BaseTypes.Object",name);
  2385. ELSE
  2386. Basic.ToSegmentedName ("BaseTypes.Record",name);
  2387. END;
  2388. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2389. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2390. Emit(Br(position,op));
  2391. END;
  2392. IF ~recordType.isObject THEN
  2393. GetRecordTypeName (recordType,name);
  2394. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  2395. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2396. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL);
  2397. NEW(registerUsageCount);
  2398. usedRegisters := NIL;
  2399. IF dump # NIL THEN dump := section.comments END;
  2400. register := NewRegisterOperand (addressType);
  2401. Emit (Mov(position,register,IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,addressType.sizeInBits))));
  2402. IF (recordType.pointerType = NIL) OR ~recordType.pointerType.isPlain THEN
  2403. IntermediateCode.AddOffset(register,BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  2404. END;
  2405. Emit (Push(position,register));
  2406. GetRecordTypeName (recordType,name);
  2407. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2408. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2409. Emit(Call(position,op, ToMemoryUnits(system, system.addressSize)));
  2410. ReleaseIntermediateOperand(register);
  2411. Emit(Exit(position,0,0,0));
  2412. GetRecordTypeName (recordType,name);
  2413. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2414. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2415. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL);
  2416. NEW(registerUsageCount);
  2417. usedRegisters := NIL;
  2418. register := NewRegisterOperand (addressType);
  2419. ofs := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(recordType)));
  2420. Emit(Mov(position, register, parameter1));
  2421. label := NewLabel();
  2422. SetLabel(label);
  2423. Emit(Push(position, register));
  2424. GetRecordTypeName (recordType,name);
  2425. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2426. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2427. Emit(Call(position, op, 0));
  2428. Emit(Pop(position, register));
  2429. Emit(Add(position, register, register, ofs));
  2430. Emit(Sub(position, parameter0, parameter0, IntermediateCode.Immediate(sizeType, 1)));
  2431. BrneL(label, parameter0, IntermediateCode.Immediate(sizeType, 0));
  2432. Emit(Exit(position,0,0,0));
  2433. END;
  2434. INC(statCoopTraceMethod, section.pc);
  2435. ReturnToContext(context);
  2436. IF dump # NIL THEN dump := section.comments END;
  2437. END CreateTraceMethod;
  2438. PROCEDURE CreateResetProcedure (recordType: SyntaxTree.RecordType);
  2439. VAR name: Basic.SegmentedName;
  2440. VAR variable: SyntaxTree.Variable; dst, op, ofs: IntermediateCode.Operand; recordBase: SyntaxTree.RecordType;
  2441. parameter1, parameter0: IntermediateCode.Operand; label: Label;
  2442. context: Context;
  2443. BEGIN
  2444. IF recordType.isObject THEN RETURN END;
  2445. parameter0 (* len *) := IntermediateCode.Memory(sizeType,sp,ToMemoryUnits(system,2*addressType.sizeInBits));
  2446. parameter1 (* dest *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,1*addressType.sizeInBits));
  2447. GetRecordTypeName (recordType,name);
  2448. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2449. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL));
  2450. IF dump # NIL THEN dump := section.comments END;
  2451. IF backend.hasLinkRegister THEN
  2452. Emit(Push(Basic.invalidPosition, lr));
  2453. END;
  2454. variable := recordType.recordScope.firstVariable;
  2455. WHILE variable # NIL DO
  2456. IF variable.NeedsTrace() THEN
  2457. dst := NewRegisterOperand (addressType);
  2458. Emit (Mov(position, dst, parameter1));
  2459. IntermediateCode.AddOffset(dst,ToMemoryUnits(system,variable.offsetInBits));
  2460. IF recordType.isObject & ((recordType.pointerType = NIL) OR ~recordType.pointerType.isPlain) THEN
  2461. IntermediateCode.AddOffset(dst,BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  2462. END;
  2463. CallResetProcedure(dst, nil, variable.type);
  2464. ReleaseIntermediateOperand(dst);
  2465. END;
  2466. variable := variable.nextVariable;
  2467. END;
  2468. recordBase := recordType.GetBaseRecord();
  2469. IF (recordBase # NIL) & recordBase.NeedsTrace() THEN
  2470. IF backend.hasLinkRegister THEN
  2471. Emit(Pop(Basic.invalidPosition, lr));
  2472. END;
  2473. GetRecordTypeName (recordBase,name);
  2474. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2475. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2476. Emit(Br(position,op));
  2477. ELSE
  2478. Emit(Exit(position,0,0, 0));
  2479. END;
  2480. GetRecordTypeName (recordType,name);
  2481. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2482. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2483. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL);
  2484. NEW(registerUsageCount);
  2485. usedRegisters := NIL;
  2486. dst := NewRegisterOperand (addressType);
  2487. ofs := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(recordType)));
  2488. Emit(Mov(position, dst, parameter1));
  2489. label := NewLabel();
  2490. SetLabel(label);
  2491. Emit(Push(position, dst));
  2492. GetRecordTypeName (recordType,name);
  2493. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2494. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2495. Emit(Call(position, op, 0));
  2496. Emit(Pop(position, dst));
  2497. Emit(Add(position, dst, dst, ofs));
  2498. Emit(Sub(position, parameter0, parameter0, IntermediateCode.Immediate(sizeType, 1)));
  2499. BrneL(label, parameter0, IntermediateCode.Immediate(sizeType, 0));
  2500. Emit(Exit(position,0,0, 0));
  2501. INC(statCoopResetProcedure, section.pc);
  2502. ReturnToContext(context);
  2503. IF dump # NIL THEN dump := section.comments END;
  2504. END CreateResetProcedure;
  2505. PROCEDURE CreateResetMethod (scope: SyntaxTree.ProcedureScope);
  2506. VAR name: Basic.SegmentedName; context: Context;
  2507. BEGIN
  2508. GetCodeSectionNameForSymbol(scope.ownerProcedure, name);
  2509. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2510. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL));
  2511. IF dump # NIL THEN dump := section.comments END;
  2512. IF backend.hasLinkRegister THEN
  2513. Emit(Push(Basic.invalidPosition, lr));
  2514. END;
  2515. Emit(Push(position,fp));
  2516. Emit(Mov(position,fp, IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,addressType.sizeInBits * 2))));
  2517. ResetVariables(scope);
  2518. Emit(Pop(position,fp));
  2519. Emit(Exit(position,0,0, 0));
  2520. ReturnToContext(context);
  2521. IF dump # NIL THEN dump := section.comments END;
  2522. END CreateResetMethod;
  2523. PROCEDURE CallResetProcedure(dest, tag: IntermediateCode.Operand; type: SyntaxTree.Type);
  2524. VAR base: SyntaxTree.Type; op, size: IntermediateCode.Operand; name: Basic.SegmentedName;
  2525. BEGIN
  2526. IF SemanticChecker.IsPointerType (type) THEN
  2527. Emit (Push(position, dest));
  2528. CallThis(position,"GarbageCollector","Reset", 1);
  2529. ELSIF type.IsRecordType() THEN
  2530. Emit (Push(position, dest));
  2531. GetRecordTypeName (type.resolved(SyntaxTree.RecordType),name);
  2532. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2533. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2534. Emit(Call(position,op, ToMemoryUnits(system, system.addressSize)));
  2535. ELSIF type.resolved IS SyntaxTree.ArrayType THEN
  2536. size := GetArrayLength(type, tag);
  2537. base := ArrayBaseType(type);
  2538. IF base.IsRecordType() THEN
  2539. Emit (Push(position, size));
  2540. Emit (Push(position, dest));
  2541. GetRecordTypeName (base.resolved(SyntaxTree.RecordType),name);
  2542. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2543. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2544. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2545. Emit(Call(position,op,ToMemoryUnits(system, 2*system.addressSize)));
  2546. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  2547. Emit (Push(position, size));
  2548. Emit (Push(position, dest));
  2549. CallThis(position,"GarbageCollector","ResetDelegateArray", 2);
  2550. ELSE
  2551. Emit (Push(position, size));
  2552. Emit (Push(position, dest));
  2553. CallThis(position,"GarbageCollector","ResetArray", 2);
  2554. ASSERT(ArrayBaseType(type).IsPointer());
  2555. END;
  2556. ReleaseIntermediateOperand(size);
  2557. ELSIF type.resolved IS SyntaxTree.ProcedureType THEN
  2558. ASSERT(type.resolved(SyntaxTree.ProcedureType).isDelegate);
  2559. Emit (Push(position, dest));
  2560. CallThis(position,"GarbageCollector","ResetDelegate", 1);
  2561. ELSE HALT(100); (* missing ? *)
  2562. END;
  2563. END CallResetProcedure;
  2564. PROCEDURE ResetVariables (scope: SyntaxTree.ProcedureScope);
  2565. VAR variable: SyntaxTree.Variable; parameter: SyntaxTree.Parameter; previousScope: SyntaxTree.Scope; pc: LONGINT;
  2566. PROCEDURE Reset (symbol: SyntaxTree.Symbol);
  2567. VAR operand: Operand;
  2568. BEGIN
  2569. Symbol (symbol, operand);
  2570. CallResetProcedure(operand.op, operand.tag, symbol.type.resolved);
  2571. ReleaseOperand(operand);
  2572. END Reset;
  2573. BEGIN
  2574. previousScope := currentScope;
  2575. currentScope := scope;
  2576. pc := section.pc;
  2577. variable := scope.firstVariable;
  2578. WHILE variable # NIL DO
  2579. IF variable.NeedsTrace() THEN
  2580. Reset (variable);
  2581. END;
  2582. variable := variable.nextVariable;
  2583. END;
  2584. parameter := scope.ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  2585. WHILE parameter # NIL DO
  2586. IF parameter.NeedsTrace() & ~IsVariableParameter(parameter) THEN
  2587. Reset (parameter);
  2588. END;
  2589. parameter := parameter.nextParameter;
  2590. END;
  2591. INC(statCoopResetVariables, section.pc - pc);
  2592. currentScope := previousScope;
  2593. END ResetVariables;
  2594. PROCEDURE CreateProcedureDescriptor (procedure: SyntaxTree.Procedure);
  2595. VAR previousSection: IntermediateCode.Section; name: Basic.SegmentedName;
  2596. VAR op: IntermediateCode.Operand; context: Context;
  2597. BEGIN
  2598. previousSection := section;
  2599. GetCodeSectionNameForSymbol(procedure, name);
  2600. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Descriptor"));
  2601. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,dump # NIL));
  2602. IF dump # NIL THEN dump := section.comments END;
  2603. Basic.ToSegmentedName ("BaseTypes.StackFrame",name);
  2604. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2605. Emit(Data(position,op));
  2606. Emit(Data(position,nil));
  2607. IF HasPointers (procedure.procedureScope) THEN
  2608. GetCodeSectionNameForSymbol(procedure, name);
  2609. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2610. ELSE
  2611. Basic.ToSegmentedName ("BaseTypes.StackFrame.Reset",name);
  2612. END;
  2613. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2614. Emit(Data(position,op));
  2615. ReturnToContext(context);
  2616. IF dump # NIL THEN dump := section.comments END;
  2617. END CreateProcedureDescriptor;
  2618. PROCEDURE AddImport(CONST moduleName: ARRAY OF CHAR; VAR module: SyntaxTree.Module; force: BOOLEAN): BOOLEAN;
  2619. VAR import: SyntaxTree.Import;
  2620. s: Basic.MessageString;
  2621. selfName: SyntaxTree.IdentifierString;
  2622. BEGIN
  2623. moduleScope.ownerModule.GetName(selfName);
  2624. IF (moduleName = selfName) & (moduleScope.ownerModule.context = Global.A2Name) THEN
  2625. module := moduleScope.ownerModule
  2626. ELSE
  2627. import := moduleScope.ImportByModuleName(SyntaxTree.NewIdentifier(moduleName),SyntaxTree.NewIdentifier("A2"));
  2628. IF import = NIL THEN
  2629. import := SyntaxTree.NewImport(Basic.invalidPosition,SyntaxTree.NewIdentifier(moduleName),SyntaxTree.NewIdentifier(moduleName),FALSE);
  2630. import.SetContext(SyntaxTree.NewIdentifier("A2"));
  2631. IF ~checker.AddImport(moduleScope.ownerModule,import) THEN
  2632. s := "Module ";
  2633. Strings.Append(s,moduleName);
  2634. Strings.Append(s," cannot be imported.");
  2635. IF force THEN
  2636. Error(position,s);
  2637. ELSIF canBeLoaded THEN
  2638. Strings.Append(s, "=> no dynamic linking.");
  2639. Warning(position, s);
  2640. canBeLoaded := FALSE;
  2641. END;
  2642. RETURN FALSE
  2643. ELSE
  2644. SELF.module.imports.AddName(moduleName)
  2645. END;
  2646. ELSIF import.module = NIL THEN (* already tried *)
  2647. RETURN FALSE
  2648. END;
  2649. module := import.module;
  2650. END;
  2651. RETURN TRUE
  2652. END AddImport;
  2653. (* needed for old binary object file format*)
  2654. PROCEDURE EnsureSymbol(CONST moduleName,procedureName: SyntaxTree.IdentifierString);
  2655. VAR r: Operand; procedure: SyntaxTree.Procedure; module: SyntaxTree.Module; s: ARRAY 128 OF CHAR; fp: LONGINT;
  2656. BEGIN
  2657. IF AddImport(moduleName,module,TRUE) THEN
  2658. procedure := module.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  2659. IF procedure = NIL THEN
  2660. s := "Instruction not supported on target, emulation procedure ";
  2661. Strings.Append(s,moduleName);
  2662. Strings.Append(s,".");
  2663. Strings.Append(s,procedureName);
  2664. Strings.Append(s," not present");
  2665. Error(position,s);
  2666. ELSE
  2667. StaticCallOperand(r,procedure);
  2668. ReleaseOperand(r);
  2669. fp := GetFingerprint(procedure);
  2670. END;
  2671. END;
  2672. END EnsureSymbol;
  2673. PROCEDURE ConditionToValue(x: SyntaxTree.Expression);
  2674. VAR exit: Label; trueL,falseL: Label;
  2675. BEGIN
  2676. trueL := NewLabel();
  2677. falseL := NewLabel();
  2678. exit := NewLabel();
  2679. Condition(x,trueL,falseL);
  2680. InitOperand(result,ModeValue);
  2681. SetLabel(trueL);
  2682. IntermediateCode.InitRegister(result.op,IntermediateCode.GetType(system,x.type),IntermediateCode.GeneralPurposeRegister,AcquireRegister(IntermediateCode.GetType(system,x.type),IntermediateCode.GeneralPurposeRegister));
  2683. Emit(Mov(position,result.op,true));
  2684. BrL(exit);
  2685. SetLabel(falseL);
  2686. Emit(MovReplace(position,result.op,false));
  2687. SetLabel(exit);
  2688. END ConditionToValue;
  2689. PROCEDURE ValueToCondition(VAR op: Operand);
  2690. BEGIN
  2691. LoadValue(op,system.booleanType);
  2692. BrneL(trueLabel,op.op, false);
  2693. ReleaseOperand(op);
  2694. BrL(falseLabel);
  2695. END ValueToCondition;
  2696. PROCEDURE GetDynamicSize(type: SyntaxTree.Type; tag: IntermediateCode.Operand):IntermediateCode.Operand;
  2697. VAR size: LONGINT;
  2698. PROCEDURE GetArraySize(type: SyntaxTree.ArrayType; offset: LONGINT):IntermediateCode.Operand;
  2699. VAR baseType: SyntaxTree.Type; size: LONGINT; sizeOperand,len,res: IntermediateCode.Operand;
  2700. BEGIN
  2701. ASSERT(type.form = SyntaxTree.Open);
  2702. baseType := type.arrayBase.resolved;
  2703. IF IsOpenArray(baseType) THEN
  2704. sizeOperand := GetArraySize(baseType(SyntaxTree.ArrayType),offset+system.addressSize);
  2705. ELSE
  2706. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  2707. sizeOperand := IntermediateCode.Immediate(addressType,size);
  2708. END;
  2709. len := tag;
  2710. IntermediateCode.AddOffset(len,ToMemoryUnits(system,offset));
  2711. IntermediateCode.MakeMemory(len,addressType);
  2712. UseIntermediateOperand(len);
  2713. Reuse2(res,sizeOperand,len);
  2714. Emit(Mul(position,res,sizeOperand,len));
  2715. ReleaseIntermediateOperand(sizeOperand); ReleaseIntermediateOperand(len);
  2716. RETURN res
  2717. END GetArraySize;
  2718. BEGIN
  2719. type := type.resolved;
  2720. IF IsOpenArray(type) THEN
  2721. IF tag.mode = IntermediateCode.ModeImmediate THEN (* special rule for winapi/c arrays *)
  2722. RETURN tag
  2723. ELSE
  2724. RETURN GetArraySize(type.resolved(SyntaxTree.ArrayType),0)
  2725. END;
  2726. ELSE
  2727. size := ToMemoryUnits(system,system.SizeOf(type));
  2728. RETURN IntermediateCode.Immediate(addressType,size)
  2729. END;
  2730. END GetDynamicSize;
  2731. PROCEDURE GetArrayLength(type: SyntaxTree.Type; tag: IntermediateCode.Operand):IntermediateCode.Operand;
  2732. PROCEDURE GetLength(type: SyntaxTree.ArrayType; offset: LONGINT):IntermediateCode.Operand;
  2733. VAR baseType: SyntaxTree.Type; sizeOperand,len,res: IntermediateCode.Operand;
  2734. BEGIN
  2735. ASSERT(type.form = SyntaxTree.Open);
  2736. baseType := type.arrayBase.resolved;
  2737. IF IsOpenArray(baseType) THEN
  2738. sizeOperand := GetLength(baseType(SyntaxTree.ArrayType),offset+system.addressSize);
  2739. ELSE
  2740. sizeOperand := IntermediateCode.Immediate(addressType,StaticArrayNumElements(baseType));
  2741. END;
  2742. len := tag;
  2743. IntermediateCode.AddOffset(len,ToMemoryUnits(system,offset));
  2744. IntermediateCode.MakeMemory(len,addressType);
  2745. UseIntermediateOperand(len);
  2746. Reuse2(res,sizeOperand,len);
  2747. Emit(Mul(position,res,sizeOperand,len));
  2748. ReleaseIntermediateOperand(sizeOperand); ReleaseIntermediateOperand(len);
  2749. RETURN res
  2750. END GetLength;
  2751. BEGIN
  2752. type := type.resolved;
  2753. IF IsOpenArray(type) THEN
  2754. ASSERT(tag.mode # IntermediateCode.ModeImmediate);
  2755. RETURN GetLength(type.resolved(SyntaxTree.ArrayType),0)
  2756. ELSIF type IS SyntaxTree.StringType THEN
  2757. RETURN tag;
  2758. ELSE
  2759. RETURN IntermediateCode.Immediate(addressType,StaticArrayNumElements(type))
  2760. END;
  2761. END GetArrayLength;
  2762. PROCEDURE GetSizeFromTag(tag: IntermediateCode.Operand): IntermediateCode.Operand;
  2763. VAR result: IntermediateCode.Operand;
  2764. BEGIN
  2765. IF backend.cooperative THEN
  2766. MakeMemory(result, tag, addressType, ToMemoryUnits(system,system.addressSize));
  2767. ELSE
  2768. MakeMemory(result, tag, addressType, 0);
  2769. END;
  2770. RETURN result
  2771. END GetSizeFromTag;
  2772. PROCEDURE GetArrayOfBytesSize(e: SyntaxTree.Expression; tag: IntermediateCode.Operand): IntermediateCode.Operand;
  2773. VAR parameter: SyntaxTree.Parameter;
  2774. BEGIN
  2775. IF (e IS SyntaxTree.SymbolDesignator) & (e(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  2776. parameter := e(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter);
  2777. IF (parameter.kind IN {SyntaxTree.VarParameter,SyntaxTree.ConstParameter}) & (parameter.type.resolved IS SyntaxTree.RecordType) THEN RETURN GetSizeFromTag(tag) END;
  2778. ELSIF e IS SyntaxTree.DereferenceDesignator THEN
  2779. IF (e.type.resolved IS SyntaxTree.RecordType) THEN RETURN GetSizeFromTag(tag) END;
  2780. END;
  2781. RETURN GetDynamicSize(e.type, tag);
  2782. END GetArrayOfBytesSize;
  2783. (*
  2784. to find imported symbol. not needed ?
  2785. PROCEDURE SymbolByName(CONST moduleName, symbolName: ARRAY OF CHAR): SyntaxTree.Symbol;
  2786. VAR importedModule: SyntaxTree.Module; symbol: SyntaxTree.Symbol;
  2787. BEGIN
  2788. IF AddImport(moduleName,importedModule,FALSE) THEN
  2789. symbol := importedModule.moduleScope.FindSymbol(SyntaxTree.NewIdentifier(symbolName));
  2790. RETURN symbol
  2791. ELSE
  2792. RETURN NIL
  2793. END
  2794. END SymbolByName;
  2795. *)
  2796. PROCEDURE GetRuntimeProcedure(CONST moduleName, procedureName: ARRAY OF CHAR; VAR procedure: SyntaxTree.Procedure; force: BOOLEAN): BOOLEAN;
  2797. VAR runtimeModule: SyntaxTree.Module; s: Basic.MessageString;
  2798. BEGIN
  2799. IF AddImport(moduleName,runtimeModule,force) THEN
  2800. procedure := runtimeModule.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  2801. IF procedure = NIL THEN
  2802. s := "Procedure ";
  2803. Strings.Append(s,moduleName);
  2804. Strings.Append(s,".");
  2805. Strings.Append(s,procedureName);
  2806. Strings.Append(s," not present");
  2807. Error(position,s);
  2808. RETURN FALSE
  2809. ELSE
  2810. RETURN TRUE
  2811. END;
  2812. ELSE RETURN FALSE
  2813. END;
  2814. END GetRuntimeProcedure;
  2815. PROCEDURE GetTypeDescriptor(CONST moduleName, typeName: ARRAY OF CHAR; VAR name: Basic.SegmentedName): SyntaxTree.Symbol;
  2816. VAR importedModule: SyntaxTree.Module; source: IntermediateCode.Section; symbol: SyntaxTree.Symbol;
  2817. s: Basic.MessageString;
  2818. BEGIN
  2819. Basic.InitSegmentedName(name);
  2820. name[0] := Basic.MakeString(moduleName);
  2821. name[1] := Basic.MakeString(typeName);
  2822. name[2] := -1;
  2823. IF AddImport(moduleName,importedModule, FALSE) THEN
  2824. symbol := importedModule.moduleScope.FindTypeDeclaration(SyntaxTree.NewIdentifier(typeName));
  2825. IF symbol = NIL THEN
  2826. s := "type ";
  2827. Strings.Append(s,moduleName);
  2828. Strings.Append(s,".");
  2829. Strings.Append(s,typeName);
  2830. Strings.Append(s," not present");
  2831. Error(position,s);
  2832. END;
  2833. ELSE symbol := NIL;
  2834. END;
  2835. IF importedModule = moduleScope.ownerModule THEN
  2836. source := NewSection(module.allSections, Sections.ConstSection, name, symbol, commentPrintout # NIL);
  2837. ELSE
  2838. source := NewSection(module.importedSections, Sections.ConstSection, name, symbol, commentPrintout # NIL);
  2839. END;
  2840. RETURN symbol
  2841. END GetTypeDescriptor;
  2842. (* Call a runtime procedure. If numberParameters >= 0 then the procedure may be called without module import. Otherwise the signature has to be inferred from the import. *)
  2843. PROCEDURE CallThisChecked(position: Position; CONST moduleName, procedureName: ARRAY OF CHAR; numberParameters: LONGINT; checkNumParameters: BOOLEAN);
  2844. VAR procedure: SyntaxTree.Procedure; result: Operand; reg: IntermediateCode.Operand; source: IntermediateCode.Section;
  2845. pooledName: Basic.SegmentedName; size: LONGINT;
  2846. BEGIN
  2847. IF GetRuntimeProcedure(moduleName,procedureName,procedure,numberParameters < 0) THEN (* ready for dynamic linking *)
  2848. StaticCallOperand(result,procedure);
  2849. IF numberParameters < 0 THEN
  2850. size := ProcedureParametersSize(system,procedure);
  2851. ELSE
  2852. size := ToMemoryUnits(system,numberParameters * system.addressSize);
  2853. IF checkNumParameters & (size # ProcedureParametersSize(system,procedure)) THEN
  2854. Error(position,"runtime call parameter count mismatch");
  2855. END;
  2856. END;
  2857. Emit(Call(position, result.op, size));
  2858. ReleaseOperand(result);
  2859. ELSE (* only static linking possible *)
  2860. ASSERT(numberParameters >= 0);
  2861. Basic.InitSegmentedName(pooledName);
  2862. pooledName[0] := Basic.MakeString(moduleName);
  2863. pooledName[1] := Basic.MakeString(procedureName);
  2864. pooledName[2] := -1;
  2865. source := NewSection(module.importedSections, Sections.CodeSection, pooledName, NIL,commentPrintout # NIL);
  2866. IntermediateCode.InitAddress(reg, addressType, pooledName , 0, 0);
  2867. Emit(Call(position,reg, ToMemoryUnits(system,numberParameters * system.addressSize)));
  2868. END;
  2869. END CallThisChecked;
  2870. (* Call a runtime procedure. If numberParameters >= 0 then the procedure may be called without module import. Otherwise the signature has to be inferred from the import. *)
  2871. PROCEDURE CallThis(position: Position; CONST moduleName, procedureName: ARRAY OF CHAR; numberParameters: LONGINT);
  2872. BEGIN
  2873. CallThisChecked(position, moduleName, procedureName, numberParameters,TRUE);
  2874. END CallThis;
  2875. PROCEDURE CompareString(br: ConditionalBranch; leftExpression,rightExpression: SyntaxTree.Expression);
  2876. VAR
  2877. left,right: Operand;
  2878. leftSize, rightSize: IntermediateCode.Operand;
  2879. saved: RegisterEntry;
  2880. reg: IntermediateCode.Operand;
  2881. procedureName: SyntaxTree.IdentifierString;
  2882. BEGIN
  2883. procedureName := "CompareString";
  2884. SaveRegisters();ReleaseUsedRegisters(saved);
  2885. Designate(leftExpression,left);
  2886. leftSize := GetDynamicSize(leftExpression.type,left.tag);
  2887. Emit(Push(position,leftSize));
  2888. ReleaseIntermediateOperand(leftSize);
  2889. Emit(Push(position,left.op));
  2890. ReleaseOperand(left);
  2891. Designate(rightExpression,right);
  2892. rightSize := GetDynamicSize(rightExpression.type,right.tag);
  2893. Emit(Push(position,rightSize));
  2894. ReleaseIntermediateOperand(rightSize);
  2895. Emit(Push(position,right.op));
  2896. ReleaseOperand(right);
  2897. IF backend.cooperative THEN
  2898. CallThis(position,DefaultRuntimeModuleName,procedureName, 4);
  2899. ELSE
  2900. CallThis(position,runtimeModuleName,procedureName, 4);
  2901. END;
  2902. IntermediateCode.InitRegister(reg,int8,IntermediateCode.GeneralPurposeRegister,AcquireRegister(int8,IntermediateCode.GeneralPurposeRegister));
  2903. Emit(Result(position,reg));
  2904. (*
  2905. AcquireThisRegister(int8,IntermediateCode.Result);
  2906. *)
  2907. RestoreRegisters(saved); (* must be done before use of result, might be jumped over otherwise *)
  2908. (*
  2909. IntermediateCode.InitRegister(reg,int8,IntermediateCode.Result);
  2910. *)
  2911. br(trueLabel,reg,IntermediateCode.Immediate(int8,0));
  2912. ReleaseIntermediateOperand(reg);
  2913. BrL(falseLabel);
  2914. END CompareString;
  2915. PROCEDURE CopyString(leftExpression,rightExpression: SyntaxTree.Expression);
  2916. VAR
  2917. left,right: Operand;
  2918. leftSize, rightSize: IntermediateCode.Operand;
  2919. saved: RegisterEntry;
  2920. procedureName: SyntaxTree.IdentifierString;
  2921. BEGIN
  2922. procedureName := "CopyString";
  2923. SaveRegisters();ReleaseUsedRegisters(saved);
  2924. Designate(leftExpression,left);
  2925. leftSize := GetDynamicSize(leftExpression.type,left.tag);
  2926. Emit(Push(position,leftSize));
  2927. ReleaseIntermediateOperand(leftSize);
  2928. Emit(Push(position,left.op));
  2929. ReleaseOperand(left);
  2930. Designate(rightExpression,right);
  2931. rightSize := GetDynamicSize(rightExpression.type,right.tag);
  2932. Emit(Push(position,rightSize));
  2933. ReleaseIntermediateOperand(rightSize);
  2934. Emit(Push(position,right.op));
  2935. ReleaseOperand(right);
  2936. IF backend.cooperative THEN
  2937. CallThis(position,DefaultRuntimeModuleName,procedureName, 4);
  2938. ELSE
  2939. CallThis(position,runtimeModuleName,procedureName,4);
  2940. END;
  2941. RestoreRegisters(saved);
  2942. END CopyString;
  2943. PROCEDURE VisitBinaryExpression(x: SyntaxTree.BinaryExpression);
  2944. VAR left,right: Operand; temp: Operand; zero, one, tempReg, tempReg2: IntermediateCode.Operand;
  2945. leftType,rightType: SyntaxTree.Type;
  2946. leftExpression,rightExpression : SyntaxTree.Expression;
  2947. componentType: IntermediateCode.Type;
  2948. value: HUGEINT; exp: LONGINT;next,exit: Label; recordType: SyntaxTree.RecordType; dest: IntermediateCode.Operand;
  2949. size: LONGINT;
  2950. BEGIN
  2951. IF Trace THEN TraceEnter("VisitBinaryExpression") END;
  2952. dest := destination; destination := emptyOperand;
  2953. leftType := x.left.type.resolved;
  2954. rightType := x.right.type.resolved;
  2955. (* for "OR" and "&" the left and right expressions may not be emitted first <= shortcut evaluation *)
  2956. CASE x.operator OF
  2957. Scanner.Or:
  2958. (* shortcut evaluation of left OR right *)
  2959. IF ~conditional THEN ConditionToValue(x);
  2960. ELSE
  2961. next := NewLabel();
  2962. Condition(x.left,trueLabel,next);
  2963. SetLabel(next);
  2964. Condition(x.right,trueLabel,falseLabel);
  2965. END;
  2966. |Scanner.And:
  2967. (* shortcut evaluation of left & right *)
  2968. IF ~conditional THEN ConditionToValue(x);
  2969. ELSE
  2970. next := NewLabel();
  2971. Condition(x.left,next,falseLabel);
  2972. SetLabel(next);
  2973. Condition(x.right,trueLabel,falseLabel);
  2974. END;
  2975. |Scanner.Is:
  2976. IF ~conditional THEN ConditionToValue(x);
  2977. ELSE
  2978. (* get type desc tag *)
  2979. IF IsPointerToRecord(leftType,recordType) THEN
  2980. Evaluate(x.left,left);
  2981. Dereference(left,recordType,IsUnsafePointer(leftType))
  2982. ELSE
  2983. Designate(x.left,left);
  2984. END;
  2985. TypeTest(left.tag,x.right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved,trueLabel,falseLabel);
  2986. ReleaseOperand(left);
  2987. END;
  2988. |Scanner.Plus:
  2989. Evaluate(x.left,left);
  2990. Evaluate(x.right,right);
  2991. IF leftType IS SyntaxTree.SetType THEN
  2992. InitOperand(result,ModeValue);
  2993. Reuse2a(result.op,left.op,right.op,dest);
  2994. Emit(Or(position,result.op,left.op,right.op));
  2995. ELSIF leftType IS SyntaxTree.ComplexType THEN
  2996. InitOperand(result,ModeValue);
  2997. Reuse2a(result.op,left.op,right.op,dest); (* TODO: review this *)
  2998. Reuse2(result.tag,left.tag,right.tag);
  2999. Emit(Add(position,result.op,left.op,right.op));
  3000. Emit(Add(position,result.tag,left.tag,right.tag))
  3001. ELSE
  3002. InitOperand(result,ModeValue);
  3003. Reuse2a(result.op,left.op,right.op,dest);
  3004. Emit(Add(position,result.op,left.op,right.op));
  3005. END;
  3006. ReleaseOperand(left); ReleaseOperand(right);
  3007. |Scanner.Minus:
  3008. Evaluate(x.left,left);
  3009. Evaluate(x.right,right);
  3010. IF leftType IS SyntaxTree.SetType THEN
  3011. InitOperand(result,ModeValue);
  3012. Reuse1(result.op,right.op);
  3013. Emit(Not(position,result.op,right.op));
  3014. ReleaseOperand(right);
  3015. Emit(And(position,result.op,result.op,left.op));
  3016. ReleaseOperand(left);
  3017. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3018. InitOperand(result,ModeValue);
  3019. Reuse2a(result.op,left.op,right.op,dest); (* TODO: review this *)
  3020. Reuse2(result.tag,left.tag,right.tag);
  3021. Emit(Sub(position,result.op,left.op,right.op));
  3022. Emit(Sub(position,result.tag,left.tag,right.tag));
  3023. ReleaseOperand(left); ReleaseOperand(right)
  3024. ELSE
  3025. InitOperand(result,ModeValue);
  3026. Reuse2a(result.op,left.op,right.op,dest);
  3027. Emit(Sub(position,result.op,left.op,right.op));
  3028. ReleaseOperand(left); ReleaseOperand(right);
  3029. END;
  3030. |Scanner.Times:
  3031. Evaluate(x.left,left);
  3032. Evaluate(x.right,right);
  3033. IF leftType IS SyntaxTree.SetType THEN
  3034. InitOperand(result,ModeValue);
  3035. Reuse2a(result.op,left.op,right.op,dest);
  3036. Emit(And(position,result.op,left.op,right.op));
  3037. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3038. InitOperand(result, ModeValue);
  3039. componentType := left.op.type;
  3040. (* TODO: review this *)
  3041. (*
  3042. result.op = left.op * right.op - left.tag * right.tag
  3043. result.tag = left.tag * right.op + left.op * right.tag
  3044. *)
  3045. result.op := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  3046. Emit(Mul(position,result.op, left.op, right.op));
  3047. tempReg := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  3048. Emit(Mul(position,tempReg, left.tag, right.tag));
  3049. Emit(Sub(position,result.op, result.op, tempReg));
  3050. Reuse2(result.tag, left.tag, right.op);
  3051. Emit(Mul(position,result.tag, left.tag, right.op));
  3052. Emit(Mul(position,tempReg, left.op, right.tag));
  3053. Emit(Add(position,result.tag, result.tag, tempReg));
  3054. ReleaseIntermediateOperand(tempReg)
  3055. ELSE
  3056. InitOperand(result,ModeValue);
  3057. Reuse2a(result.op,left.op,right.op,dest);
  3058. Emit(Mul(position,result.op,left.op,right.op));
  3059. END;
  3060. ReleaseOperand(left); ReleaseOperand(right);
  3061. |Scanner.Div:
  3062. Evaluate(x.left,left);
  3063. Evaluate(x.right,right);
  3064. IF (x.type.resolved IS SyntaxTree.IntegerType) & (x.right.resolved = NIL) THEN (* divisor negative check *)
  3065. IntermediateCode.InitImmediate(zero,IntermediateCode.GetType(system,rightType),0);
  3066. IF ~isUnchecked THEN
  3067. exit := NewLabel();
  3068. BrltL(exit,zero,right.op);
  3069. EmitTrap(position,NegativeDivisorTrap);
  3070. SetLabel(exit);
  3071. END;
  3072. END;
  3073. InitOperand(result,ModeValue);
  3074. Reuse2a(result.op,left.op,right.op,dest);
  3075. Emit(Div(position,result.op,left.op,right.op));
  3076. ReleaseOperand(left); ReleaseOperand(right);
  3077. |Scanner.Mod:
  3078. Evaluate(x.left,left);
  3079. Evaluate(x.right,right);
  3080. IF (x.type.resolved IS SyntaxTree.IntegerType) & (x.right.resolved = NIL) THEN (* divisor negative check *)
  3081. IntermediateCode.InitImmediate(zero,IntermediateCode.GetType(system,rightType),0);
  3082. IF ~isUnchecked THEN
  3083. exit := NewLabel();
  3084. BrltL(exit,zero,right.op);
  3085. EmitTrap(position,NegativeDivisorTrap);
  3086. SetLabel(exit);
  3087. END;
  3088. END;
  3089. InitOperand(result,ModeValue);
  3090. Reuse2a(result.op,left.op,right.op,dest);
  3091. Emit(Mod(position,result.op,left.op,right.op));
  3092. ReleaseOperand(left); ReleaseOperand(right);
  3093. |Scanner.Slash:
  3094. Evaluate(x.left,left);
  3095. Evaluate(x.right,right);
  3096. IF leftType IS SyntaxTree.SetType THEN
  3097. InitOperand(result,ModeValue);
  3098. Reuse2a(result.op,left.op,right.op,dest);
  3099. Emit(Xor(position,result.op,left.op,right.op));
  3100. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3101. InitOperand(result,ModeValue);
  3102. componentType := left.op.type;
  3103. (* review this *)
  3104. (*
  3105. divisor = right.op * right.op + right.tag * right.tag
  3106. result.op = (left.op * right.op + left.tag * right.tag) / divisor
  3107. result.tag = (left.tag * right.op - left.op * right.tag) / divisor
  3108. *)
  3109. tempReg := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  3110. tempReg2 := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  3111. Emit(Mul(position,tempReg, right.op, right.op));
  3112. Emit(Mul(position,tempReg2, right.tag, right.tag));
  3113. Emit(Add(position,tempReg, tempReg, tempReg2));
  3114. result.op := tempReg2;
  3115. Emit(Mul(position,result.op, left.op, right.op));
  3116. tempReg2 := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister, AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  3117. Emit(Mul(position,tempReg2, left.tag, right.tag));
  3118. Emit(Add(position,result.op, result.op, tempReg2));
  3119. Emit(Div(position,result.op, result.op, tempReg));
  3120. Reuse2(result.tag, left.tag, right.op);
  3121. Emit(Mul(position,result.tag, left.tag, right.op));
  3122. Emit(Mul(position,tempReg2, left.op, right.tag));
  3123. Emit(Sub(position,result.tag, result.tag, tempReg2));
  3124. Emit(Div(position,result.tag, result.tag, tempReg));
  3125. ReleaseIntermediateOperand(tempReg);
  3126. ReleaseIntermediateOperand(tempReg2)
  3127. ELSE
  3128. InitOperand(result,ModeValue);
  3129. Reuse2a(result.op,left.op,right.op,dest);
  3130. Emit(Div(position,result.op,left.op,right.op));
  3131. END;
  3132. ReleaseOperand(left); ReleaseOperand(right);
  3133. |Scanner.Equal:
  3134. IF ~conditional THEN ConditionToValue(x);
  3135. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3136. CompareString(BreqL,x.left,x.right);
  3137. ELSE
  3138. Evaluate(x.left,left);
  3139. Evaluate(x.right,right);
  3140. IF leftType IS SyntaxTree.RangeType THEN
  3141. ASSERT(rightType IS SyntaxTree.RangeType);
  3142. BrneL(falseLabel, left.op, right.op); (* first *)
  3143. BrneL(falseLabel, left.tag, right.tag); (* last *)
  3144. BrneL(falseLabel, left.extra, right.extra); (* step *)
  3145. ReleaseOperand(left); ReleaseOperand(right);
  3146. BrL(trueLabel)
  3147. ELSIF IsDelegate(leftType) THEN (* delegate comparison *)
  3148. BrneL(falseLabel, left.op, right.op); (* first *)
  3149. BrneL(falseLabel, left.tag, right.tag); (* last *)
  3150. ReleaseOperand(left); ReleaseOperand(right);
  3151. BrL(trueLabel)
  3152. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3153. (* TODO: review this *)
  3154. BrneL(falseLabel, left.op, right.op); (* real part *)
  3155. BrneL(falseLabel, left.tag, right.tag); (* imaginary part *)
  3156. ReleaseOperand(left); ReleaseOperand(right);
  3157. BrL(trueLabel)
  3158. ELSE
  3159. BrneL(falseLabel,left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3160. ReleaseOperand(left); ReleaseOperand(right);
  3161. BrL(trueLabel);
  3162. END;
  3163. END;
  3164. |Scanner.LessEqual:
  3165. IF ~conditional THEN ConditionToValue(x);
  3166. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3167. CompareString(BrgeL,x.right,x.left);
  3168. ELSE
  3169. Evaluate(x.left,left);
  3170. Evaluate(x.right,right);
  3171. IF leftType IS SyntaxTree.SetType THEN (* left subsetequal right: left \cap right = left *)
  3172. Reuse1(temp.op,right.op);
  3173. Emit(And(position,temp.op,left.op,right.op));
  3174. ReleaseOperand(right);
  3175. BreqL(trueLabel,temp.op,left.op);
  3176. BrL(falseLabel);
  3177. ReleaseOperand(temp);ReleaseOperand(left);
  3178. ELSE
  3179. BrltL(falseLabel,right.op,left.op); (* inverse evaluation to optimize jumps for true case *)
  3180. ReleaseOperand(left); ReleaseOperand(right);
  3181. BrL(trueLabel);
  3182. END;
  3183. END;
  3184. |Scanner.Less:
  3185. IF leftType IS SyntaxTree.SetType THEN (* left < right <=> left <= right & left # right *)
  3186. leftExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.left,x.right,Scanner.LessEqual);
  3187. leftExpression.SetType(system.booleanType);
  3188. rightExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.left,x.right,Scanner.Unequal);
  3189. rightExpression.SetType(system.booleanType);
  3190. leftExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,leftExpression,rightExpression,Scanner.And);
  3191. leftExpression.SetType(system.booleanType);
  3192. Expression(leftExpression);
  3193. ELSIF ~conditional THEN ConditionToValue(x);
  3194. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3195. CompareString(BrltL,x.left,x.right);
  3196. ELSE
  3197. Evaluate(x.left,left);
  3198. Evaluate(x.right,right);
  3199. BrgeL(falseLabel,left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3200. ReleaseOperand(left); ReleaseOperand(right);
  3201. BrL(trueLabel);
  3202. END;
  3203. |Scanner.Greater:
  3204. IF leftType IS SyntaxTree.SetType THEN (* left > right <=> left >= right & left # right *)
  3205. leftExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.left,x.right,Scanner.GreaterEqual);
  3206. leftExpression.SetType(system.booleanType);
  3207. rightExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.left,x.right,Scanner.Unequal);
  3208. rightExpression.SetType(system.booleanType);
  3209. leftExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,leftExpression,rightExpression,Scanner.And);
  3210. leftExpression.SetType(system.booleanType);
  3211. Expression(leftExpression);
  3212. ELSIF ~conditional THEN ConditionToValue(x);
  3213. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3214. CompareString(BrltL,x.right,x.left);
  3215. ELSE
  3216. Evaluate(x.left,left);
  3217. Evaluate(x.right,right);
  3218. BrgeL(falseLabel, right.op,left.op); (* inverse evaluation to optimize jumps for true case *)
  3219. ReleaseOperand(left); ReleaseOperand(right);
  3220. BrL(trueLabel);
  3221. END;
  3222. |Scanner.GreaterEqual:
  3223. IF ~conditional THEN ConditionToValue(x);
  3224. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3225. CompareString(BrgeL,x.left,x.right);
  3226. ELSE
  3227. Evaluate(x.left,left);
  3228. Evaluate(x.right,right);
  3229. IF leftType IS SyntaxTree.SetType THEN (* left supsetequal right: left \cap right = right *)
  3230. Reuse1(temp.op,left.op);
  3231. Emit(And(position,temp.op,left.op,right.op));
  3232. ReleaseOperand(left);
  3233. BreqL(trueLabel, temp.op,right.op);
  3234. ReleaseOperand(temp); ReleaseOperand(right);
  3235. BrL(falseLabel);
  3236. ELSE
  3237. BrltL(falseLabel, left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3238. ReleaseOperand(left); ReleaseOperand(right);
  3239. BrL(trueLabel);
  3240. END;
  3241. END;
  3242. |Scanner.Unequal:
  3243. IF ~conditional THEN ConditionToValue(x);
  3244. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3245. CompareString(BrneL,x.left,x.right);
  3246. ELSE
  3247. Evaluate(x.left,left);
  3248. Evaluate(x.right,right);
  3249. IF leftType IS SyntaxTree.RangeType THEN
  3250. ASSERT(rightType IS SyntaxTree.RangeType);
  3251. BrneL(trueLabel, left.op, right.op); (* first *)
  3252. BrneL(trueLabel, left.tag, right.tag); (* last *)
  3253. BrneL(trueLabel, left.extra, right.extra); (* step *)
  3254. ReleaseOperand(left); ReleaseOperand(right);
  3255. BrL(falseLabel)
  3256. ELSIF IsDelegate(leftType) THEN (* delegate comparison *)
  3257. BrneL(trueLabel, left.op, right.op); (* first *)
  3258. BrneL(trueLabel, left.tag, right.tag); (* last *)
  3259. ReleaseOperand(left); ReleaseOperand(right);
  3260. BrL(falseLabel)
  3261. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3262. (* TODO: review this *)
  3263. BrneL(trueLabel, left.op, right.op); (* real part *)
  3264. BrneL(trueLabel, left.tag, right.tag); (* imaginary part *)
  3265. ReleaseOperand(left); ReleaseOperand(right);
  3266. BrL(falseLabel)
  3267. ELSE
  3268. BreqL(falseLabel,left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3269. ReleaseOperand(left); ReleaseOperand(right);
  3270. BrL(trueLabel);
  3271. END;
  3272. END;
  3273. |Scanner.In:
  3274. ASSERT(rightType.resolved IS SyntaxTree.SetType);
  3275. Evaluate(x.left,left);
  3276. Evaluate(x.right,right);
  3277. Convert(left.op,setType);
  3278. ReuseCopy(temp.op,right.op);
  3279. Emit(Shr(position,temp.op,temp.op,left.op));
  3280. ReleaseOperand(right); ReleaseOperand(left);
  3281. IntermediateCode.InitImmediate(one,setType,1);
  3282. Emit(And(position,temp.op,temp.op,one));
  3283. IF conditional THEN
  3284. IntermediateCode.InitImmediate(zero,setType,0);
  3285. BrneL(trueLabel,temp.op,zero);
  3286. ReleaseOperand(temp);
  3287. BrL(falseLabel);
  3288. ELSE
  3289. Convert(temp.op,bool);
  3290. result.mode := ModeValue;
  3291. result.op := temp.op;
  3292. result.tag := nil; (* may be left over from calls to evaluate *)
  3293. END;
  3294. ELSE
  3295. IF (x.operator = Scanner.Questionmarks) OR (x.operator = Scanner.LessLessQ) & (x.right.type.resolved IS SyntaxTree.PortType) THEN
  3296. IF x.operator = Scanner.Questionmarks THEN
  3297. leftExpression := x.left;
  3298. rightExpression := x.right;
  3299. ELSE
  3300. leftExpression := x.right;
  3301. rightExpression := x.left;
  3302. END;
  3303. Evaluate(leftExpression, left);
  3304. Emit(Push(position,left.op));
  3305. ReleaseOperand(left);
  3306. Designate(rightExpression, right);
  3307. size := ToMemoryUnits(system,system.SizeOf(x.right.type));
  3308. IF ~backend.cellsAreObjects THEN
  3309. IF size # 1 THEN Error(x.right.position,"receive not implemented for complex data types") END;
  3310. END;
  3311. Emit(Push(position,right.op));
  3312. ReleaseOperand(right);
  3313. IF backend.cellsAreObjects THEN
  3314. CallThis(position,"ActiveCellsRuntime","ReceiveNonBlocking",2);
  3315. ELSE
  3316. CallThis(position,ChannelModuleName,"ReceiveNonBlocking",2);
  3317. END;
  3318. InitOperand(result, ModeValue);
  3319. result.op := NewRegisterOperand(bool);
  3320. Emit(Result(position,result.op));
  3321. IF conditional THEN
  3322. IntermediateCode.InitImmediate(zero,setType,0);
  3323. BrneL(trueLabel,result.op,zero);
  3324. ReleaseOperand(result);
  3325. BrL(falseLabel);
  3326. END;
  3327. ELSIF (x.operator = Scanner.ExclamationMarks) OR (x.operator = Scanner.LessLessQ) & (x.left.type.resolved IS SyntaxTree.PortType) THEN
  3328. leftExpression := x.left;
  3329. rightExpression := x.right;
  3330. Evaluate(leftExpression, left);
  3331. Emit(Push(position,left.op));
  3332. ReleaseOperand(left);
  3333. Evaluate(rightExpression, right);
  3334. size := ToMemoryUnits(system,system.SizeOf(x.right.type));
  3335. IF ~backend.cellsAreObjects THEN
  3336. IF size # 1 THEN Error(x.right.position,"send not implemented for complex data types") END;
  3337. END;
  3338. Emit(Push(position,right.op));
  3339. ReleaseOperand(right);
  3340. IF backend.cellsAreObjects THEN
  3341. CallThis(position,"ActiveCellsRuntime","SendNonBlocking",2);
  3342. ELSE
  3343. CallThis(position,ChannelModuleName,"SendNonBlocking",2);
  3344. END;
  3345. InitOperand(result, ModeValue);
  3346. result.op := NewRegisterOperand(bool);
  3347. Emit(Result(position,result.op));
  3348. IF conditional THEN
  3349. IntermediateCode.InitImmediate(zero,setType,0);
  3350. BrneL(trueLabel,result.op,zero);
  3351. ReleaseOperand(result);
  3352. BrL(falseLabel);
  3353. END;
  3354. ELSE
  3355. HALT(100);
  3356. END;
  3357. END;
  3358. destination := dest;
  3359. IF Trace THEN TraceExit("VisitBinaryExpression") END;
  3360. END VisitBinaryExpression;
  3361. PROCEDURE VisitRangeExpression(x: SyntaxTree.RangeExpression);
  3362. VAR localResult, operand: Operand;
  3363. BEGIN
  3364. IF Trace THEN TraceEnter("VisitRangeExpression") END;
  3365. InitOperand(localResult, ModeValue);
  3366. ASSERT(x.first # NIL);
  3367. Evaluate(x.first, operand);
  3368. localResult.op := operand.op;
  3369. ReleaseOperand(operand);
  3370. UseIntermediateOperand(localResult.op);
  3371. ASSERT(x.last # NIL);
  3372. Evaluate(x.last, operand);
  3373. localResult.tag := operand.op;
  3374. ReleaseOperand(operand);
  3375. UseIntermediateOperand(localResult.tag);
  3376. IF x.step # NIL THEN
  3377. Evaluate(x.step, operand);
  3378. localResult.extra := operand.op;
  3379. ReleaseOperand(operand);
  3380. UseIntermediateOperand(localResult.extra);
  3381. END;
  3382. result := localResult;
  3383. IF Trace THEN TraceExit("VisitRangeExpression") END
  3384. END VisitRangeExpression;
  3385. PROCEDURE VisitTensorRangeExpression*(x: SyntaxTree.TensorRangeExpression);
  3386. BEGIN
  3387. HALT(100); (* should never be evaluated *)
  3388. END VisitTensorRangeExpression;
  3389. PROCEDURE VisitConversion(x: SyntaxTree.Conversion);
  3390. VAR old: Operand; dest: IntermediateCode.Operand; componentType: SyntaxTree.Type;
  3391. BEGIN
  3392. IF Trace THEN TraceEnter("VisitConversion") END;
  3393. ASSERT(~(x.expression.type.resolved IS SyntaxTree.RangeType));
  3394. dest := destination; destination := emptyOperand;
  3395. Evaluate(x.expression,old);
  3396. InitOperand(result,ModeValue);
  3397. result.op := old.op;
  3398. ASSERT(result.op.mode # 0);
  3399. IF x.type.resolved IS SyntaxTree.ComplexType THEN
  3400. (* convert TO a complex number *)
  3401. componentType := x.type.resolved(SyntaxTree.ComplexType).componentType;
  3402. Convert(result.op,IntermediateCode.GetType(system, componentType));
  3403. ASSERT(result.op.mode # 0);
  3404. IF x.expression.type.resolved IS SyntaxTree.ComplexType THEN
  3405. (* convert FROM a complex number TO a complex number*)
  3406. result.tag := old.tag;
  3407. ASSERT(result.tag.mode # 0);
  3408. Convert(result.tag,IntermediateCode.GetType(system, componentType));
  3409. ASSERT(result.tag.mode # 0)
  3410. ELSE
  3411. ASSERT(componentType IS SyntaxTree.FloatType); (* this excludes complex types based on integer types *)
  3412. result.tag := IntermediateCode.FloatImmediate(IntermediateCode.GetType(system, componentType), 0); (* the imaginary part is set to 0 *)
  3413. END
  3414. ELSE
  3415. Convert(result.op,IntermediateCode.GetType(system,x.type));
  3416. ASSERT(result.op.mode # 0);
  3417. result.tag := old.tag; (*! probably never used *)
  3418. END;
  3419. destination := dest;
  3420. IF Trace THEN TraceExit("VisitConversion") END;
  3421. END VisitConversion;
  3422. PROCEDURE VisitTypeDeclaration(x: SyntaxTree.TypeDeclaration);
  3423. BEGIN
  3424. IF Trace THEN TraceEnter("VisitTypeDeclaration") END;
  3425. ASSERT((x.declaredType.resolved IS SyntaxTree.EnumerationType) OR (x.declaredType.resolved IS SyntaxTree.RecordType)
  3426. OR (x.declaredType.resolved IS SyntaxTree.PointerType) & (x.declaredType.resolved(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType));
  3427. IF Trace THEN TraceExit("VisitTypeDeclaration") END;
  3428. END VisitTypeDeclaration;
  3429. (** designators (expressions) *)
  3430. PROCEDURE VisitSymbolDesignator(x: SyntaxTree.SymbolDesignator);
  3431. VAR ownerType, designatorType: SyntaxTree.RecordType;
  3432. BEGIN
  3433. IF Trace THEN TraceEnter("VisitSymbolDesignator") END;
  3434. IF x.left # NIL THEN Expression(x.left) END;
  3435. Symbol(x.symbol,result);
  3436. IF backend.cooperative & (x.symbol IS SyntaxTree.Variable) & (x.symbol.scope IS SyntaxTree.RecordScope) THEN
  3437. ASSERT ((x.left # NIL) & (x.left.type.resolved IS SyntaxTree.RecordType));
  3438. ownerType := x.symbol.scope(SyntaxTree.RecordScope).ownerRecord;
  3439. designatorType := x.left.type.resolved(SyntaxTree.RecordType);
  3440. IF ~ownerType.isObject & designatorType.isObject & ~designatorType.pointerType.isPlain THEN
  3441. IntermediateCode.AddOffset(result.op,BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  3442. END;
  3443. END;
  3444. IF Trace THEN TraceExit("VisitSymbolDesignator") END;
  3445. END VisitSymbolDesignator;
  3446. PROCEDURE BoundCheck(index,length: IntermediateCode.Operand);
  3447. BEGIN
  3448. IF isUnchecked THEN RETURN END;
  3449. IF tagsAvailable THEN
  3450. TrapC(BrltL,index,length,IndexCheckTrap);
  3451. END;
  3452. END BoundCheck;
  3453. PROCEDURE DimensionCheck(base,dim: IntermediateCode.Operand; op: ConditionalBranch );
  3454. VAR d: IntermediateCode.Operand;
  3455. BEGIN
  3456. IF isUnchecked THEN RETURN END;
  3457. MakeMemory(d,base,dim.type,ToMemoryUnits(system,MathDimOffset * addressType.sizeInBits));
  3458. TrapC(op,dim,d,ArraySizeTrap);
  3459. ReleaseIntermediateOperand(d);
  3460. END DimensionCheck;
  3461. PROCEDURE MathIndexDesignator(x: SyntaxTree.IndexDesignator);
  3462. VAR
  3463. index, range, array, sourceLength, sourceIncrement, localResult: Operand;
  3464. firstIndex, lastIndex, stepSize, summand, targetLength, targetIncrement, tmp, srcDim, destDim: IntermediateCode.Operand;
  3465. expression: SyntaxTree.Expression;
  3466. resultingType, leftType, baseType: SyntaxTree.Type;
  3467. skipLabel1: Label;
  3468. i, indexListSize, indexDim, rangeCount, indexCount, tensorRangeCount, srcDimOffset, destDimOffset, targetArrayDimensionality: LONGINT;
  3469. staticSourceLength, staticSourceIncrement, staticIndex, staticFirstIndex, staticLastIndex, staticStepSize, staticTargetLength: LONGINT;
  3470. variableOp: Operand;
  3471. variable: SyntaxTree.Variable;
  3472. PROCEDURE CountIndices(parameters: SyntaxTree.ExpressionList; VAR indexCount: LONGINT; VAR rangeCount: LONGINT; VAR tensorRangeCount: LONGINT);
  3473. VAR expression: SyntaxTree.Expression;
  3474. BEGIN
  3475. (* count the number of indices, ranges and tensorRanges in the index list *)
  3476. indexCount := 0; rangeCount := 0; tensorRangeCount := 0;
  3477. FOR i := 0 TO parameters.Length() - 1 DO
  3478. expression := parameters.GetExpression(i);
  3479. IF expression IS SyntaxTree.TensorRangeExpression THEN INC(tensorRangeCount)
  3480. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.RangeType) THEN INC(indexCount)
  3481. ELSE INC(indexCount)
  3482. END
  3483. END;
  3484. END CountIndices;
  3485. BEGIN
  3486. ASSERT(tagsAvailable);
  3487. resultingType := x.type.resolved; (* resulting type *)
  3488. leftType := x.left.type.resolved; (* type of array to be indexed over *)
  3489. InitOperand(localResult, ModeReference);
  3490. IF (resultingType IS SyntaxTree.MathArrayType) & ( (resultingType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) OR NeedDescriptor) THEN
  3491. targetArrayDimensionality := resultingType(SyntaxTree.MathArrayType).Dimensionality();
  3492. IF arrayDestinationTag.mode # IntermediateCode.Undefined THEN
  3493. (* a globally defined array destination tag is available -> use and invalidate it*)
  3494. localResult.tag := arrayDestinationTag;
  3495. IntermediateCode.InitOperand(arrayDestinationTag)
  3496. ELSE
  3497. (* otherwise, create a temporary variable and use it to store the array destination tag *)
  3498. variable := GetTemporaryVariable(GetMathArrayDescriptorType(targetArrayDimensionality), FALSE);
  3499. Symbol(variable, variableOp);
  3500. ReuseCopy(localResult.tag, variableOp.op);
  3501. ReleaseOperand(variableOp);
  3502. END
  3503. END;
  3504. indexListSize := x.parameters.Length();
  3505. CountIndices(x.parameters, indexCount, rangeCount, tensorRangeCount);
  3506. ASSERT(tensorRangeCount <= 1);
  3507. (* designate the array to be indexed over, perform tensor range check if known *)
  3508. Designate(x.left, array);
  3509. IF leftType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  3510. Dereference(array, leftType,FALSE);
  3511. IF tensorRangeCount=0 THEN
  3512. DimensionCheck(array.tag, IntermediateCode.Immediate(int32, rangeCount+indexCount), BreqL)
  3513. END
  3514. END;
  3515. (* determine source and destination dimension offsets; this depends on if the list starts with a '?' *)
  3516. IF x.parameters.GetExpression(0) IS SyntaxTree.TensorRangeExpression THEN
  3517. srcDimOffset := -indexListSize;
  3518. destDimOffset := -rangeCount
  3519. ELSE
  3520. srcDimOffset := 0;
  3521. destDimOffset := 0
  3522. END;
  3523. indexDim := 0;
  3524. (* use address of source array as basis *)
  3525. (*
  3526. ReuseCopy(localResult.op, array.op);
  3527. *)
  3528. localResult.op := array.op;
  3529. UseIntermediateOperand(localResult.op);
  3530. (* go through the index list *)
  3531. FOR i := 0 TO indexListSize - 1 DO
  3532. expression := x.parameters.GetExpression(i);
  3533. IF expression IS SyntaxTree.TensorRangeExpression THEN
  3534. (* nothing to do *)
  3535. ELSE
  3536. (* determine which dimension of source array is currently looked at *)
  3537. IF srcDimOffset < 0 THEN (* tensor expression or the form a[?,i,j] *)
  3538. (* get the memory operand pointing to array descriptor dimension *)
  3539. GetMathArrayField(tmp, array.tag, MathDimOffset);
  3540. (* make a reusable register from it *)
  3541. ReuseCopy(srcDim, tmp);
  3542. ReleaseIntermediateOperand(tmp);
  3543. AddInt(srcDim, srcDim, IntermediateCode.Immediate(addressType, i + srcDimOffset));
  3544. ELSE
  3545. srcDim := IntermediateCode.Immediate(int32, i)
  3546. END;
  3547. (* get length and increment of source array for current dimension *)
  3548. GetMathArrayLength(leftType(SyntaxTree.MathArrayType), array, srcDim, FALSE, sourceLength);
  3549. Convert(sourceLength.op, sizeType);
  3550. GetMathArrayIncrement(leftType(SyntaxTree.MathArrayType), array, srcDim, FALSE, sourceIncrement);
  3551. Convert(sourceIncrement.op, sizeType);
  3552. (* release the dim operand, if dynamic. No register reuse to decrease register pressure *)
  3553. ReleaseIntermediateOperand(srcDim);
  3554. IF SemanticChecker.IsIntegerType(expression.type.resolved) THEN
  3555. (* SINGLE INDEX *)
  3556. Evaluate(expression, index);
  3557. ReleaseIntermediateOperand(index.tag);
  3558. index.tag := emptyOperand;
  3559. Convert(index.op, sizeType);
  3560. (* lower bound check *)
  3561. IF IsIntegerImmediate(index.op, staticIndex) THEN
  3562. ASSERT(staticIndex >= 0) (* ensured by the checker *)
  3563. ELSIF isUnchecked THEN (* do nothing *)
  3564. ELSE
  3565. TrapC(BrgeL, index.op, IntermediateCode.Immediate(sizeType, 0), IndexCheckTrap)
  3566. END;
  3567. (* upper bound check *)
  3568. IF IsIntegerImmediate(index.op, staticIndex) & IsIntegerImmediate(sourceLength.op, staticSourceLength) THEN
  3569. ASSERT(staticIndex < staticSourceLength) (* ensured by checker *)
  3570. ELSIF isUnchecked THEN (* do nothing *)
  3571. ELSE
  3572. TrapC(BrltL, index.op, sourceLength.op, IndexCheckTrap)
  3573. END;
  3574. ReleaseOperand(sourceLength);
  3575. Convert(index.op, addressType);
  3576. summand := index.op;
  3577. ELSIF expression.type.resolved IS SyntaxTree.RangeType THEN
  3578. (* RANGE OF INDICES *)
  3579. Evaluate(expression, range);
  3580. firstIndex := range.op; UseIntermediateOperand(firstIndex);
  3581. lastIndex := range.tag; UseIntermediateOperand(lastIndex);
  3582. stepSize := range.extra; UseIntermediateOperand(stepSize);
  3583. ReleaseOperand(range);
  3584. Convert(firstIndex, sizeType);
  3585. Convert(lastIndex, sizeType);
  3586. Convert(stepSize, sizeType);
  3587. (* for dynamic upper bounds: add a runtime check, which repaces the upper bound with the largest valid index
  3588. if it is 'MAX(LONGINT)' *)
  3589. IF ~IsIntegerImmediate(lastIndex, staticLastIndex) THEN
  3590. TransferToRegister(lastIndex, lastIndex);
  3591. skipLabel1 := NewLabel();
  3592. BrneL(skipLabel1, lastIndex, IntermediateCode.Immediate(sizeType, MAX(LONGINT)));
  3593. Emit(MovReplace(position,lastIndex, sourceLength.op)); (* make sure that no new register is allocated *)
  3594. Emit(Sub(position,lastIndex, lastIndex, IntermediateCode.Immediate(sizeType, 1)));
  3595. SetLabel(skipLabel1)
  3596. END;
  3597. (* check if step size is valid *)
  3598. IF IsIntegerImmediate(stepSize, staticStepSize) THEN
  3599. ASSERT(staticStepSize >= 1) (* ensured by the checker *)
  3600. ELSIF isUnchecked THEN (* do nothing *)
  3601. ELSE
  3602. TrapC(BrgeL, stepSize, IntermediateCode.Immediate(sizeType, 1), IndexCheckTrap)
  3603. END;
  3604. (* check lower bound check *)
  3605. IF IsIntegerImmediate(firstIndex, staticFirstIndex) THEN
  3606. ASSERT(staticFirstIndex >= 0) (* ensured by the checker *)
  3607. ELSIF isUnchecked THEN (* do nothing *)
  3608. ELSE
  3609. TrapC(BrgeL, firstIndex, IntermediateCode.Immediate(sizeType, 0), IndexCheckTrap)
  3610. END;
  3611. (* check upper bound check *)
  3612. IF IsIntegerImmediate(lastIndex, staticLastIndex) & (staticLastIndex = MAX(LONGINT)) THEN
  3613. (* statically open range: nothing to do *)
  3614. ELSIF IsIntegerImmediate(lastIndex, staticLastIndex) & IsIntegerImmediate(sourceLength.op, staticSourceLength) THEN
  3615. ASSERT(staticLastIndex < staticSourceLength)
  3616. ELSIF isUnchecked THEN (* do nothing *)
  3617. ELSE
  3618. TrapC(BrltL, lastIndex, sourceLength.op, IndexCheckTrap)
  3619. END;
  3620. (* determine length of target array for current dimension *)
  3621. (* 1. incorporate last index: *)
  3622. IF IsIntegerImmediate(lastIndex, staticLastIndex) THEN
  3623. (* last index is static *)
  3624. IF IsIntegerImmediate(lastIndex, staticLastIndex) & (staticLastIndex = MAX(LONGINT)) THEN
  3625. targetLength := sourceLength.op
  3626. ELSE
  3627. targetLength := IntermediateCode.Immediate(sizeType, staticLastIndex + 1)
  3628. END;
  3629. UseIntermediateOperand(targetLength);
  3630. ELSE
  3631. (* targetLength := lastIndex + 1
  3632. Reuse1(targetLength, lastIndex);
  3633. *)
  3634. AddInt(targetLength, lastIndex, IntermediateCode.Immediate(sizeType, 1));
  3635. END;
  3636. ReleaseOperand(sourceLength);
  3637. ReleaseIntermediateOperand(lastIndex);
  3638. (* 2. incorporate first index: *)
  3639. IF IsIntegerImmediate(firstIndex, staticFirstIndex) & IsIntegerImmediate(targetLength, staticTargetLength) THEN
  3640. (* first index and current target length are static *)
  3641. targetLength := IntermediateCode.Immediate(sizeType, staticTargetLength - staticFirstIndex)
  3642. ELSIF IsIntegerImmediate(firstIndex, staticFirstIndex) & (staticFirstIndex = 0) THEN
  3643. (* first index = 0: nothing to do *)
  3644. ELSE
  3645. (* targetLength := targetLength - firstIndex *)
  3646. TransferToRegister(targetLength, targetLength);
  3647. Emit(Sub(position,targetLength, targetLength, firstIndex))
  3648. END;
  3649. (* clip negative lengths to 0 *)
  3650. IF IsIntegerImmediate(targetLength, staticTargetLength) THEN
  3651. IF staticTargetLength < 0 THEN
  3652. targetLength := IntermediateCode.Immediate(sizeType, 0)
  3653. END
  3654. ELSE
  3655. skipLabel1 := NewLabel();
  3656. TransferToRegister(targetLength, targetLength);
  3657. BrgeL(skipLabel1, targetLength, IntermediateCode.Immediate(sizeType, 0));
  3658. Emit(Mov(position,targetLength, IntermediateCode.Immediate(sizeType, 0)));
  3659. SetLabel(skipLabel1)
  3660. END;
  3661. (* 3. incorporate index step size: *)
  3662. IF IsIntegerImmediate(stepSize, staticStepSize) & IsIntegerImmediate(targetLength, staticTargetLength) THEN
  3663. (*step size and current target length are static *)
  3664. staticTargetLength := (staticTargetLength-1) DIV staticStepSize + 1;
  3665. targetLength := IntermediateCode.Immediate(sizeType, staticTargetLength)
  3666. ELSIF IsIntegerImmediate(stepSize, staticStepSize) & (staticStepSize = 1) THEN
  3667. (* step size = 1: nothing to do *)
  3668. ELSE
  3669. (* emit code for this:
  3670. targetLength := (targetLength-1) DIV stepSize +1;
  3671. *)
  3672. AddInt(targetLength, targetLength, IntermediateCode.Immediate(sizeType, -1));
  3673. DivInt(targetLength, targetLength, stepSize);
  3674. AddInt(targetLength, targetLength, IntermediateCode.Immediate(sizeType, 1));
  3675. END;
  3676. (* determine increment of target array for current dimension *)
  3677. IF IsIntegerImmediate(sourceIncrement.op, staticSourceIncrement) & IsIntegerImmediate(stepSize, staticStepSize) THEN
  3678. targetIncrement := IntermediateCode.Immediate(sizeType, staticSourceIncrement * staticStepSize);
  3679. ELSIF IsIntegerImmediate(stepSize, staticStepSize) & (staticStepSize = 1) THEN
  3680. (* step size = 1 *)
  3681. targetIncrement := sourceIncrement.op;
  3682. UseIntermediateOperand(targetIncrement)
  3683. ELSE
  3684. (* targetIncrement := sourceIncrement * stepSize *)
  3685. Reuse1(targetIncrement, stepSize);
  3686. ASSERT((sourceIncrement.op.mode # IntermediateCode.ModeImmediate) OR (stepSize.mode # IntermediateCode.ModeImmediate));
  3687. MulInt(targetIncrement, sourceIncrement.op, stepSize);
  3688. END;
  3689. ReleaseIntermediateOperand(stepSize);
  3690. (* write length and increment of target array to descriptor *)
  3691. IF destDimOffset < 0 THEN
  3692. (* determine which dimension of target array is currently looked at *)
  3693. GetMathArrayField(tmp, array.tag, MathDimOffset);
  3694. TransferToRegister(destDim, tmp);
  3695. AddInt(destDim, destDim, IntermediateCode.Immediate(sizeType, indexDim + destDimOffset));
  3696. PutMathArrayLenOrIncr(localResult.tag, targetLength, destDim, FALSE);
  3697. PutMathArrayLenOrIncr(localResult.tag, targetIncrement, destDim, TRUE);
  3698. ReleaseIntermediateOperand(destDim)
  3699. ELSE
  3700. PutMathArrayLength(localResult.tag, targetLength, indexDim);
  3701. PutMathArrayIncrement(localResult.tag , targetIncrement, indexDim)
  3702. END;
  3703. ReleaseIntermediateOperand(targetLength); targetLength := nil;
  3704. ReleaseIntermediateOperand(targetIncrement); targetIncrement := nil;
  3705. INC(indexDim);
  3706. Convert(firstIndex, addressType);
  3707. TransferToRegister(summand, firstIndex);
  3708. ELSE HALT(100);
  3709. END;
  3710. (*
  3711. ASSERT((summand.mode # IntermediateCode.ModeImmediate) OR (sourceIncrement.op.mode # IntermediateCode.ModeImmediate));
  3712. *)
  3713. Convert(sourceIncrement.op, addressType);
  3714. Convert(summand, addressType);
  3715. MulInt(summand, summand, sourceIncrement.op);
  3716. ReleaseIntermediateOperand(sourceIncrement.op);
  3717. AddInt(localResult.op, localResult.op, summand);
  3718. ReleaseIntermediateOperand(summand);
  3719. END
  3720. END;
  3721. result := localResult;
  3722. IF (resultingType IS SyntaxTree.RecordType) & (resultingType(SyntaxTree.RecordType).pointerType = NIL) THEN
  3723. ReleaseIntermediateOperand(result.tag);
  3724. result.tag := TypeDescriptorAdr(resultingType);
  3725. IF ~newObjectFile THEN
  3726. IntermediateCode.MakeMemory(result.tag,addressType);
  3727. END;
  3728. ELSIF IsDelegate(resultingType) THEN
  3729. ReleaseIntermediateOperand(result.tag);
  3730. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  3731. UseIntermediateOperand(result.tag);
  3732. ELSIF (resultingType IS SyntaxTree.ArrayType) & (resultingType(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  3733. ReleaseIntermediateOperand(result.tag);
  3734. IntermediateCode.InitImmediate(result.tag,addressType,resultingType(SyntaxTree.ArrayType).staticLength);
  3735. ELSIF (resultingType IS SyntaxTree.ArrayType) THEN
  3736. result.tag := array.tag; UseIntermediateOperand(result.tag); result.dimOffset := array.dimOffset+indexListSize-1;
  3737. ELSIF (resultingType IS SyntaxTree.MathArrayType) & ( (resultingType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) OR NeedDescriptor) THEN
  3738. (* finalize target array descriptor *)
  3739. ASSERT(result.tag.mode # IntermediateCode.Undefined); (* tag has been already set in the beginning *)
  3740. (* write lengths and increments of target array for remaining dimensions *)
  3741. i := indexListSize;
  3742. WHILE indexDim < targetArrayDimensionality DO
  3743. GetMathArrayLengthAt(leftType(SyntaxTree.MathArrayType),array,i,FALSE, sourceLength);
  3744. PutMathArrayLength(result.tag, sourceLength.op,indexDim);
  3745. ReleaseOperand(sourceLength);
  3746. GetMathArrayIncrementAt(leftType(SyntaxTree.MathArrayType),array,i,FALSE,sourceIncrement);
  3747. PutMathArrayIncrement(result.tag, sourceIncrement.op,indexDim);
  3748. ReleaseOperand(sourceIncrement);
  3749. INC(i); INC(indexDim);
  3750. END;
  3751. IF leftType(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  3752. tmp := nil;
  3753. ELSE
  3754. GetMathArrayField(tmp,array.tag,MathPtrOffset);
  3755. END;
  3756. PutMathArrayField(result.tag, tmp, MathPtrOffset);
  3757. ReleaseIntermediateOperand(tmp);
  3758. IF leftType(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  3759. baseType := SemanticChecker.ArrayBase(resultingType, indexDim);
  3760. tmp := IntermediateCode.Immediate(addressType, ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  3761. ELSE
  3762. GetMathArrayField(tmp,array.tag, MathElementSizeOffset);
  3763. END;
  3764. PutMathArrayField(result.tag, tmp, MathElementSizeOffset);
  3765. ReleaseIntermediateOperand(tmp);
  3766. PutMathArrayField(result.tag, result.op, MathAdrOffset);
  3767. (* write dimensionality *)
  3768. IF targetArrayDimensionality # 0 THEN
  3769. PutMathArrayField(result.tag, IntermediateCode.Immediate(addressType, targetArrayDimensionality),MathDimOffset);
  3770. END;
  3771. PutMathArrayField(result.tag, IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{RangeFlag})),MathFlagsOffset);
  3772. END;
  3773. ReleaseOperand(array);
  3774. IF conditional & (resultingType.resolved IS SyntaxTree.BooleanType) THEN
  3775. ValueToCondition(result); (*! wrong as the result of an index designator is always an address *)
  3776. END;
  3777. END MathIndexDesignator;
  3778. (* get the length of an array , trying to make use of static information *)
  3779. PROCEDURE ArrayLength(type: SyntaxTree.Type; dim: LONGINT; tag: IntermediateCode.Operand): IntermediateCode.Operand;
  3780. VAR res: IntermediateCode.Operand; size: LONGINT;
  3781. BEGIN
  3782. type := type.resolved;
  3783. IF type IS SyntaxTree.ArrayType THEN
  3784. WITH type: SyntaxTree.ArrayType DO
  3785. IF type.form = SyntaxTree.Static THEN
  3786. RETURN IntermediateCode.Immediate(addressType,type.staticLength);
  3787. (*ELSIF (type.form = SyntaxTree.SemiDynamic) & backend.cellsAreObjects THEN
  3788. Evaluate(type.length, op);
  3789. ReleaseIntermediateOperand(op.tag);
  3790. RETURN op.op;*)
  3791. ELSE
  3792. res := tag;
  3793. IntermediateCode.AddOffset(res,ToMemoryUnits(system,addressType.sizeInBits*(DynamicDim(type)-1)));
  3794. IntermediateCode.MakeMemory(res,addressType);
  3795. UseIntermediateOperand(res);
  3796. RETURN res
  3797. END
  3798. END;
  3799. ELSE
  3800. size := ToMemoryUnits(system,system.AlignedSizeOf(type));
  3801. RETURN IntermediateCode.Immediate(addressType,size);
  3802. END;
  3803. END ArrayLength;
  3804. PROCEDURE CopyInt(VAR res: IntermediateCode.Operand; x: IntermediateCode.Operand);
  3805. BEGIN
  3806. IF IsImmediate(x) THEN
  3807. IntermediateCode.InitImmediate(res,x.type,x.intValue);
  3808. ELSE
  3809. IF ~ReusableRegister(res) THEN
  3810. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3811. ELSE
  3812. UseIntermediateOperand(res);
  3813. END;
  3814. Emit(Mov(position,res,x))
  3815. END;
  3816. END CopyInt;
  3817. PROCEDURE AddInt(VAR res: IntermediateCode.Operand; x,y: IntermediateCode.Operand);
  3818. BEGIN
  3819. ReleaseIntermediateOperand(res);
  3820. IF IsImmediate(x) & IsImmediate(y) THEN
  3821. IntermediateCode.InitImmediate(res,x.type,x.intValue+y.intValue);
  3822. ELSIF IsAddress(x) & IsImmediate(y) THEN
  3823. IntermediateCode.InitAddress(res,x.type,x.symbol.name, x.symbol.fingerprint, x.symbolOffset);
  3824. IntermediateCode.AddOffset(res, LONGINT(y.intValue)+x.offset);
  3825. ELSIF IsAddress(y) & IsImmediate(x) THEN
  3826. IntermediateCode.InitAddress(res,y.type,y.symbol.name, y.symbol.fingerprint, y.symbolOffset);
  3827. IntermediateCode.AddOffset(res, LONGINT(x.intValue)+y.offset);
  3828. ELSIF IsRegister(x) & IsImmediate(y) THEN
  3829. IntermediateCode.InitRegister(res, x.type, x.registerClass, x.register);
  3830. IntermediateCode.AddOffset(res, x.offset + LONGINT(y.intValue));
  3831. UseIntermediateOperand(res);
  3832. ELSIF IsRegister(y) & IsImmediate(x) THEN
  3833. IntermediateCode.InitRegister(res, y.type, y.registerClass, y.register);
  3834. IntermediateCode.AddOffset(res, y.offset + LONGINT(x.intValue));
  3835. UseIntermediateOperand(res);
  3836. ELSE
  3837. IF ~ReusableRegister(res) THEN
  3838. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3839. ELSE
  3840. UseIntermediateOperand(res);
  3841. END;
  3842. IF IsImmediate(x) & (x.intValue = 0) THEN
  3843. Emit(Mov(position,res,y))
  3844. ELSIF IsImmediate(y) & (y.intValue=0) THEN
  3845. Emit(Mov(position,res,x))
  3846. ELSE
  3847. Emit(Add(position,res, x, y));
  3848. END;
  3849. END;
  3850. END AddInt;
  3851. PROCEDURE MulInt(VAR res: IntermediateCode.Operand; x,y: IntermediateCode.Operand);
  3852. BEGIN
  3853. ReleaseIntermediateOperand(res);
  3854. IF IsImmediate(x) & IsImmediate(y) THEN
  3855. IntermediateCode.InitImmediate(res,x.type,x.intValue*y.intValue);
  3856. ELSE
  3857. IF ~ReusableRegister(res) THEN
  3858. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3859. ELSE UseIntermediateOperand(res);
  3860. END;
  3861. IF IsImmediate(x) & (x.intValue = 1) THEN
  3862. Emit(Mov(position,res,y))
  3863. ELSIF IsImmediate(y) & (y.intValue=1) THEN
  3864. Emit(Mov(position,res,x))
  3865. ELSE
  3866. Emit(Mul(position,res, x, y));
  3867. END;
  3868. END;
  3869. END MulInt;
  3870. PROCEDURE DivInt(VAR res: IntermediateCode.Operand; x,y: IntermediateCode.Operand);
  3871. BEGIN
  3872. ReleaseIntermediateOperand(res);
  3873. IF IsImmediate(x) & IsImmediate(y) THEN
  3874. IntermediateCode.InitImmediate(res,x.type,x.intValue DIV y.intValue);
  3875. ELSE
  3876. IF ~ReusableRegister(res) THEN
  3877. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3878. ELSE UseIntermediateOperand(res);
  3879. END;
  3880. IF IsImmediate(x) & (x.intValue = 1) THEN
  3881. Emit(Mov(position,res,y))
  3882. ELSIF IsImmediate(y) & (y.intValue=1) THEN
  3883. Emit(Mov(position,res,x))
  3884. ELSE
  3885. Emit(Div(position,res, x, y));
  3886. END;
  3887. END;
  3888. END DivInt;
  3889. PROCEDURE IndexDesignator(x: SyntaxTree.IndexDesignator);
  3890. VAR length,res: IntermediateCode.Operand; type,ttype: SyntaxTree.Type; maxDim: LONGINT; array:Operand;
  3891. index: Operand; e: SyntaxTree.Expression;i: LONGINT; size: LONGINT; atype: SyntaxTree.ArrayType;
  3892. BEGIN
  3893. type := x.left.type.resolved;
  3894. IF type IS SyntaxTree.StringType THEN
  3895. atype := SyntaxTree.NewArrayType(Basic.invalidPosition, NIL, SyntaxTree.Static);
  3896. atype.SetArrayBase(type(SyntaxTree.StringType).baseType);
  3897. atype.SetLength(Global.NewIntegerValue(system,Basic.invalidPosition, type(SyntaxTree.StringType).length));
  3898. type := atype;
  3899. x.left.SetType(type);
  3900. END;
  3901. IntermediateCode.InitImmediate(res,addressType,0);
  3902. maxDim := x.parameters.Length()-1;
  3903. (*
  3904. computation rule:
  3905. a: ARRAY X,Y,Z OF Element with size S
  3906. a[i,j,k] -->
  3907. ( ( ( ( i ) * Y + j ) * Z) + k) * S
  3908. *)
  3909. FOR i := 0 TO maxDim DO
  3910. e := x.parameters.GetExpression(i);
  3911. Evaluate(e,index);
  3912. Convert(index.op,addressType);
  3913. AddInt(res, res, index.op);
  3914. IF i = 0 THEN
  3915. (*
  3916. ReuseCopy(res, index.op);
  3917. *)
  3918. Designate(x.left,array);
  3919. type := x.left.type.resolved;
  3920. IF (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) & backend.cellsAreObjects THEN
  3921. Dereference(array, type, FALSE);
  3922. END;
  3923. (*
  3924. ELSE AddInt(res, res, index.op);
  3925. *)
  3926. END;
  3927. IF (array.tag.mode # IntermediateCode.Undefined ) THEN
  3928. length := ArrayLength(type(SyntaxTree.ArrayType),array.dimOffset+i,array.tag);
  3929. IF ((length.mode # IntermediateCode.ModeImmediate) OR (index.op.mode # IntermediateCode.ModeImmediate)) & tagsAvailable THEN
  3930. BoundCheck(index.op, length);
  3931. END;
  3932. ReleaseIntermediateOperand(length);
  3933. END;
  3934. ReleaseOperand(index);
  3935. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  3936. length := ArrayLength(type,array.dimOffset+i-1,array.tag);
  3937. IF (length.mode # IntermediateCode.ModeImmediate) OR (length.intValue # 1) THEN
  3938. MulInt(res,res,length);
  3939. END;
  3940. ReleaseIntermediateOperand(length);
  3941. END;
  3942. (* remaining open dimensions -- compute address *)
  3943. i := maxDim+1;
  3944. IF type IS SyntaxTree.ArrayType THEN
  3945. ttype := type(SyntaxTree.ArrayType).arrayBase.resolved;
  3946. WHILE (ttype IS SyntaxTree.ArrayType) & (ttype(SyntaxTree.ArrayType).form # SyntaxTree.Static) DO
  3947. length := ArrayLength(ttype,array.dimOffset+i-1,array.tag);
  3948. IF (length.mode # IntermediateCode.ModeImmediate) OR (length.intValue # 1) THEN
  3949. MulInt(res,res,length);
  3950. END;
  3951. ReleaseIntermediateOperand(length);
  3952. INC(i);
  3953. ttype := ttype(SyntaxTree.ArrayType).arrayBase.resolved;
  3954. END;
  3955. END;
  3956. IF (type IS SyntaxTree.ArrayType) THEN
  3957. IF (type(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  3958. size := StaticSize(system, type);
  3959. IF size # 1 THEN
  3960. length := IntermediateCode.Immediate(addressType,size);
  3961. MulInt(res,res,length);
  3962. END;
  3963. ELSE
  3964. size := StaticSize(system, type(SyntaxTree.ArrayType).arrayBase);
  3965. IF size # 1 THEN
  3966. length := IntermediateCode.Immediate(addressType,size);
  3967. MulInt(res,res,length);
  3968. END;
  3969. END;
  3970. END;
  3971. AddInt(res,res,array.op);
  3972. InitOperand(result,ModeReference);
  3973. result.op := res;
  3974. IF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType = NIL) THEN
  3975. ReleaseIntermediateOperand(result.tag);
  3976. result.tag := TypeDescriptorAdr(type);
  3977. IF ~newObjectFile THEN
  3978. IntermediateCode.MakeMemory(result.tag,addressType);
  3979. END
  3980. ELSIF IsDelegate(type) THEN
  3981. ReleaseIntermediateOperand(result.tag);
  3982. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  3983. UseIntermediateOperand(result.tag);
  3984. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  3985. ReleaseIntermediateOperand(result.tag);
  3986. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  3987. ELSIF (type IS SyntaxTree.ArrayType) THEN
  3988. result.tag := array.tag; UseIntermediateOperand(result.tag); result.dimOffset := array.dimOffset+maxDim;
  3989. END;
  3990. ReleaseOperand(array);
  3991. IF (conditional) & (type.resolved IS SyntaxTree.BooleanType) THEN
  3992. ValueToCondition(result); (*! wrong as the result of an index designator is always an address *)
  3993. END;
  3994. END IndexDesignator;
  3995. PROCEDURE VisitIndexDesignator(x: SyntaxTree.IndexDesignator);
  3996. VAR type: SyntaxTree.Type; dest: IntermediateCode.Operand;
  3997. BEGIN
  3998. IF Trace THEN TraceEnter("VisitIndexDesignator") END;
  3999. dest := destination; destination := emptyOperand;
  4000. type := x.left.type.resolved;
  4001. IF type IS SyntaxTree.MathArrayType THEN
  4002. MathIndexDesignator(x);
  4003. ELSE ASSERT((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.StringType));
  4004. IndexDesignator(x);
  4005. END;
  4006. destination := dest;
  4007. IF Trace THEN TraceExit("VisitIndexDesignator") END;
  4008. END VisitIndexDesignator;
  4009. PROCEDURE PrepareTensorDescriptor(expression: SyntaxTree.IndexDesignator): SyntaxTree.Variable;
  4010. VAR variable: SyntaxTree.Variable; srcOperand,destOperand,procOp: Operand;
  4011. moduleName, procedureName: SyntaxTree.IdentifierString; arrayBase: SyntaxTree.Module; saved: RegisterEntry; s: Basic.MessageString;
  4012. procedure: SyntaxTree.Procedure;
  4013. parameters: SyntaxTree.ExpressionList; e: SyntaxTree.Expression;
  4014. prefixIndices, prefixRanges, suffixIndices, suffixRanges,i : LONGINT; tensorFound: BOOLEAN;
  4015. BEGIN
  4016. variable := GetTemporaryVariable(expression.left.type, FALSE);
  4017. parameters := expression.parameters;
  4018. moduleName := "FoxArrayBase";
  4019. procedureName := "CopyDescriptor";
  4020. IF AddImport(moduleName,arrayBase,TRUE) THEN
  4021. SaveRegisters();ReleaseUsedRegisters(saved);
  4022. procedure := arrayBase.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  4023. IF procedure = NIL THEN
  4024. s := "procedure ";
  4025. Strings.Append(s,moduleName);
  4026. Strings.Append(s,".");
  4027. Strings.Append(s,procedureName);
  4028. Strings.Append(s," not present");
  4029. Error(position,s);
  4030. ELSE
  4031. (* push address of temporary variable *)
  4032. Symbol(variable,destOperand);
  4033. Emit(Push(position,destOperand.op));
  4034. ReleaseOperand(destOperand);
  4035. (* push src *)
  4036. Evaluate(expression.left,srcOperand);
  4037. (*
  4038. Dereference(srcOperand,expression.type.resolved);
  4039. Emit(Push(position,srcOperand.tag));
  4040. *)
  4041. Emit(Push(position,srcOperand.op));
  4042. ReleaseOperand(srcOperand);
  4043. tensorFound := FALSE;
  4044. FOR i := 0 TO parameters.Length()-1 DO
  4045. e := parameters.GetExpression(i);
  4046. IF e IS SyntaxTree.TensorRangeExpression THEN
  4047. tensorFound := TRUE;
  4048. ELSIF e IS SyntaxTree.RangeExpression THEN
  4049. IF tensorFound THEN INC(suffixRanges) ELSE INC(prefixRanges) END;
  4050. ELSE
  4051. IF tensorFound THEN INC(suffixIndices) ELSE INC(prefixIndices) END;
  4052. END;
  4053. END;
  4054. Emit(Push(position,IntermediateCode.Immediate(int32,prefixIndices)));
  4055. Emit(Push(position,IntermediateCode.Immediate(int32,prefixRanges)));
  4056. Emit(Push(position,IntermediateCode.Immediate(int32,suffixIndices)));
  4057. Emit(Push(position,IntermediateCode.Immediate(int32,suffixRanges)));
  4058. StaticCallOperand(procOp,procedure);
  4059. Emit(Call(position,procOp.op,ProcedureParametersSize(system,procedure)));
  4060. ReleaseOperand(procOp);
  4061. END;
  4062. RestoreRegisters(saved);
  4063. END;
  4064. RETURN variable
  4065. END PrepareTensorDescriptor;
  4066. PROCEDURE PushParameter(expression: SyntaxTree.Expression; parameter: SyntaxTree.Parameter; callingConvention: LONGINT; needsParameterBackup: BOOLEAN; VAR parameterBackup: IntermediateCode.Operand; numberRegister: LONGINT);
  4067. VAR
  4068. type, descriptorType, baseType: SyntaxTree.Type;
  4069. operand, tmpOperand, variableOp, variable2Op: Operand;
  4070. baseReg, tmp, dimOp, null, dst: IntermediateCode.Operand;
  4071. variable, variable2: SyntaxTree.Variable;
  4072. dim, i, size: LONGINT;
  4073. (* TODO: needed? *)
  4074. oldArrayDestinationTag: IntermediateCode.Operand;
  4075. oldArrayDestinationDimension: LONGINT;
  4076. position: Position;
  4077. saved: RegisterEntry;
  4078. arrayFlags: SET;
  4079. m, n: LONGINT;
  4080. PROCEDURE Pass(op: IntermediateCode.Operand);
  4081. VAR registerClass: IntermediateCode.RegisterClass; parameterRegister: IntermediateCode.Operand;
  4082. BEGIN
  4083. IF numberRegister >= 0 THEN
  4084. IntermediateCode.InitRegisterClass(registerClass, IntermediateCode.Parameter, SHORT(numberRegister));
  4085. IntermediateCode.InitRegister(parameterRegister, op.type, registerClass, AcquireRegister(op.type, registerClass));
  4086. Emit(Mov(position,parameterRegister, op));
  4087. ELSE
  4088. Emit(Push(position,op))
  4089. END
  4090. END Pass;
  4091. PROCEDURE PushArrayLens(formalType,actualType: SyntaxTree.Type; dim: LONGINT);
  4092. VAR tmp: IntermediateCode.Operand; actualArrayBase: SyntaxTree.Type;
  4093. BEGIN
  4094. formalType := formalType.resolved; actualType := actualType.resolved;
  4095. IF IsOpenArray(formalType)THEN
  4096. IF actualType IS SyntaxTree.StringType THEN
  4097. Pass((IntermediateCode.Immediate(addressType,actualType(SyntaxTree.StringType).length)));
  4098. RETURN;
  4099. ELSIF (actualType IS SyntaxTree.MathArrayType) & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  4100. Pass((IntermediateCode.Immediate(addressType,actualType(SyntaxTree.MathArrayType).staticLength)));
  4101. actualArrayBase := actualType(SyntaxTree.MathArrayType).arrayBase.resolved;
  4102. ELSIF actualType(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  4103. Pass((IntermediateCode.Immediate(addressType,actualType(SyntaxTree.ArrayType).staticLength)));
  4104. actualArrayBase := actualType(SyntaxTree.ArrayType).arrayBase.resolved;
  4105. ELSE
  4106. tmp := baseReg;
  4107. IntermediateCode.AddOffset(tmp,ToMemoryUnits(system,dim*system.addressSize));
  4108. IntermediateCode.MakeMemory(tmp,addressType);
  4109. Pass((tmp));
  4110. actualArrayBase := actualType(SyntaxTree.ArrayType).arrayBase.resolved;
  4111. END;
  4112. PushArrayLens(formalType(SyntaxTree.ArrayType).arrayBase.resolved, actualArrayBase,dim-1);
  4113. END;
  4114. END PushArrayLens;
  4115. PROCEDURE SetSmallArraySizeFlag(VAR flags: SET; size: LONGINT);
  4116. BEGIN
  4117. CASE size OF
  4118. |2: INCL(flags,Size2Flag);
  4119. |3: INCL(flags,Size3Flag);
  4120. |4: INCL(flags,Size4Flag);
  4121. |5: INCL(flags,Size5Flag);
  4122. |6: INCL(flags,Size6Flag);
  4123. |7: INCL(flags,Size7Flag);
  4124. |8: INCL(flags,Size8Flag);
  4125. END;
  4126. END SetSmallArraySizeFlag;
  4127. BEGIN
  4128. IF Trace THEN TraceEnter("PushParameter") END;
  4129. position := expression.position;
  4130. IF expression.resolved # NIL THEN expression := expression.resolved END;
  4131. type := expression.type.resolved;
  4132. ASSERT( ((type IS SyntaxTree.MathArrayType) = (parameter.type.resolved IS SyntaxTree.MathArrayType))
  4133. OR (type IS SyntaxTree.MathArrayType) & (parameter.type.resolved IS SyntaxTree.ArrayType)
  4134. & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  4135. & (parameter.type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.Open)
  4136. );
  4137. (* TODO: needed? *)
  4138. oldArrayDestinationTag := arrayDestinationTag;
  4139. oldArrayDestinationDimension := arrayDestinationDimension;
  4140. IF IsArrayOfSystemByte(parameter.type) THEN
  4141. IF SemanticChecker.HasAddress(expression) OR (callingConvention = SyntaxTree.WinAPICallingConvention) & (expression IS SyntaxTree.NilValue) THEN
  4142. Designate(expression,operand);
  4143. ELSE
  4144. Evaluate(expression, tmpOperand);
  4145. variable := GetTemporaryVariable(expression.type, FALSE);
  4146. Symbol(variable, operand);
  4147. MakeMemory(tmp,operand.op,tmpOperand.op.type,0);
  4148. Emit(Mov(position,tmp, tmpOperand.op));
  4149. ReleaseOperand(tmpOperand);
  4150. END;
  4151. tmp := GetArrayOfBytesSize(expression,operand.tag);
  4152. ReleaseIntermediateOperand(operand.tag);
  4153. operand.tag := tmp;
  4154. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4155. Pass((operand.tag));
  4156. END;
  4157. Pass((operand.op));
  4158. ELSIF IsOpenArray(parameter.type) THEN
  4159. Designate(expression,operand);
  4160. baseReg := operand.tag;
  4161. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4162. PushArrayLens(parameter.type,type,operand.dimOffset+DynamicDim(parameter.type)-1);
  4163. END;
  4164. Pass((operand.op)); (* address of the array *)
  4165. ELSIF parameter.type.resolved IS SyntaxTree.MathArrayType THEN
  4166. (* case 1 *)
  4167. IF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) &
  4168. (parameter.kind IN {SyntaxTree.ValueParameter, SyntaxTree.ConstParameter}) THEN
  4169. size := MathLenOffset + 2*SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4170. size := ToMemoryUnits(system,size*addressType.sizeInBits);
  4171. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,size)));
  4172. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4173. arrayDestinationTag := sp;
  4174. (* case 1b *)
  4175. IF expression IS SyntaxTree.IndexDesignator THEN
  4176. (*
  4177. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4178. descriptorType := GetMathArrayDescriptorType(dim);
  4179. variable := GetTemporaryVariable(descriptorType,expression.position);
  4180. Symbol(variable,variableOp);
  4181. arrayDestinationTag := variableOp.op;
  4182. *)
  4183. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4184. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4185. arrayDestinationDimension := dim;
  4186. Designate(expression,operand);
  4187. (* case 1a *)
  4188. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4189. Designate(expression,operand);
  4190. Emit(Copy(position,arrayDestinationTag,operand.tag,IntermediateCode.Immediate(addressType,size)));
  4191. i := 0;
  4192. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4193. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4194. INC(i);
  4195. END;
  4196. type := expression.type.resolved;
  4197. WHILE (i<dim) DO (* remaining static dimensions *)
  4198. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4199. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4200. ReleaseOperand(tmpOperand);
  4201. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4202. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4203. ReleaseOperand(tmpOperand);
  4204. INC(i);
  4205. END;
  4206. dimOp := IntermediateCode.Immediate(addressType,dim);
  4207. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4208. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset);
  4209. (* case 1d *)
  4210. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4211. Designate(expression,operand);
  4212. Dereference(operand,type.resolved,FALSE);
  4213. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4214. Emit(Copy(position,sp(*arrayDestinationTag*),operand.tag,IntermediateCode.Immediate(addressType,size)));
  4215. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset);
  4216. (* case 1f *)
  4217. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4218. Designate(expression,operand);
  4219. FOR i := 0 TO dim-1 DO
  4220. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4221. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4222. ReleaseOperand(tmpOperand);
  4223. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4224. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4225. ReleaseOperand(tmpOperand);
  4226. END;
  4227. (*******
  4228. identify the cases of small vector and matrices, used for optimizations in FoxArrayBase module (Alexey Morozov)
  4229. *)
  4230. arrayFlags := {StaticFlag};
  4231. IF dim = 1 THEN
  4232. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,0,FALSE,tmpOperand);
  4233. ReleaseOperand(tmpOperand);
  4234. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4235. m := LONGINT(tmpOperand.op.intValue);
  4236. IF (m >= 2) & (m <= 8) THEN INCL(arrayFlags,SmallVectorFlag); SetSmallArraySizeFlag(arrayFlags,m); END;
  4237. ELSIF dim = 2 THEN
  4238. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,0,FALSE,tmpOperand);
  4239. ReleaseOperand(tmpOperand);
  4240. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4241. m := LONGINT(tmpOperand.op.intValue);
  4242. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,1,FALSE,tmpOperand);
  4243. ReleaseOperand(tmpOperand);
  4244. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4245. n := LONGINT(tmpOperand.op.intValue);
  4246. IF (m >= 2) & (m <= 8) & (n >= 2) & (n <= 8) THEN
  4247. INCL(arrayFlags,SmallMatrixFlag);
  4248. IF m = n THEN SetSmallArraySizeFlag(arrayFlags,m); END;
  4249. END;
  4250. END;
  4251. (*******)
  4252. dimOp := IntermediateCode.Immediate(addressType,dim);
  4253. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4254. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4255. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4256. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,arrayFlags)),MathFlagsOffset);
  4257. baseType := SemanticChecker.ArrayBase(type,dim);
  4258. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4259. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4260. ELSE HALT(100);
  4261. END;
  4262. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) & (parameter.kind = SyntaxTree.VarParameter) THEN
  4263. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4264. (* case 2b *)
  4265. IF expression IS SyntaxTree.IndexDesignator THEN
  4266. descriptorType := GetMathArrayDescriptorType(dim);
  4267. variable := GetTemporaryVariable(descriptorType, FALSE);
  4268. Symbol(variable,variableOp);
  4269. arrayDestinationTag := variableOp.op;
  4270. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4271. arrayDestinationDimension := dim;
  4272. NeedDescriptor := TRUE;
  4273. Designate(expression,operand);
  4274. Pass((operand.tag));
  4275. NeedDescriptor := FALSE;
  4276. (* case 2a *)
  4277. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4278. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4279. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4280. INC(i);
  4281. END;
  4282. IF i = dim THEN
  4283. Designate(expression,operand);
  4284. Pass((operand.tag));
  4285. ELSE (* open-static *)
  4286. type := expression.type.resolved;
  4287. descriptorType := GetMathArrayDescriptorType(dim);
  4288. variable := GetTemporaryVariable(descriptorType, FALSE);
  4289. Symbol(variable,variableOp);
  4290. arrayDestinationTag := variableOp.op;
  4291. Designate(expression,operand);
  4292. FOR i := 0 TO dim-1 DO
  4293. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4294. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4295. ReleaseOperand(tmpOperand);
  4296. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4297. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4298. ReleaseOperand(tmpOperand);
  4299. END;
  4300. dimOp := IntermediateCode.Immediate(addressType,dim);
  4301. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4302. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4303. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4304. PutMathArrayField(arrayDestinationTag, IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4305. baseType := SemanticChecker.ArrayBase(type,dim);
  4306. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4307. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4308. Pass((arrayDestinationTag));
  4309. END;
  4310. (* case 2d *)
  4311. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4312. Designate(expression,operand);
  4313. Dereference(operand,type.resolved,FALSE);
  4314. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4315. Pass((operand.tag));
  4316. (* case 2f *)
  4317. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4318. descriptorType := GetMathArrayDescriptorType(dim);
  4319. variable := GetTemporaryVariable(descriptorType, FALSE);
  4320. Symbol(variable,variableOp);
  4321. arrayDestinationTag := variableOp.op;
  4322. Designate(expression,operand);
  4323. FOR i := 0 TO dim-1 DO
  4324. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4325. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4326. ReleaseOperand(tmpOperand);
  4327. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4328. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4329. ReleaseOperand(tmpOperand);
  4330. END;
  4331. (*
  4332. identify the cases of small vector and matrices, used for optimizations in FoxArrayBase module (Alexey Morozov)
  4333. *)
  4334. arrayFlags := {StaticFlag};
  4335. IF dim = 1 THEN
  4336. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,0,FALSE,tmpOperand);
  4337. ReleaseOperand(tmpOperand);
  4338. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4339. m := LONGINT(tmpOperand.op.intValue);
  4340. IF (m >= 2) & (m <= 8) THEN INCL(arrayFlags,SmallVectorFlag); SetSmallArraySizeFlag(arrayFlags,m); END;
  4341. ELSIF dim = 2 THEN
  4342. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,0,FALSE,tmpOperand);
  4343. ReleaseOperand(tmpOperand);
  4344. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4345. m := LONGINT(tmpOperand.op.intValue);
  4346. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,1,FALSE,tmpOperand);
  4347. ReleaseOperand(tmpOperand);
  4348. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4349. n := LONGINT(tmpOperand.op.intValue);
  4350. IF (m >= 2) & (m <= 8) & (n >= 2) & (n <= 8) THEN
  4351. INCL(arrayFlags,SmallMatrixFlag);
  4352. IF m = n THEN SetSmallArraySizeFlag(arrayFlags,m); END;
  4353. END;
  4354. END;
  4355. (*******)
  4356. dimOp := IntermediateCode.Immediate(addressType,dim);
  4357. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4358. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4359. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4360. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,arrayFlags)),MathFlagsOffset);
  4361. baseType := SemanticChecker.ArrayBase(type,dim);
  4362. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4363. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4364. Pass((arrayDestinationTag));
  4365. ELSE HALT(100);
  4366. END;
  4367. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (parameter.kind IN {SyntaxTree.ConstParameter,SyntaxTree.ValueParameter}) THEN
  4368. dim := SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static});
  4369. (* case 3b *)
  4370. IF (expression IS SyntaxTree.IndexDesignator) & (type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  4371. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN (* indexer of form a[e,....,?] *)
  4372. variable := PrepareTensorDescriptor(expression(SyntaxTree.IndexDesignator));
  4373. Symbol(variable,variableOp);
  4374. LoadValue(variableOp,system.addressType);
  4375. ELSE
  4376. descriptorType := GetMathArrayDescriptorType(dim);
  4377. variable := GetTemporaryVariable(descriptorType, FALSE);
  4378. Symbol(variable,variableOp);
  4379. END;
  4380. arrayDestinationTag := variableOp.op;
  4381. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4382. arrayDestinationDimension := 0;
  4383. Designate(expression,operand);
  4384. Pass((operand.tag));
  4385. (* case 3a *)
  4386. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4387. i := 0;
  4388. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4389. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4390. INC(i);
  4391. END;
  4392. IF i = dim THEN
  4393. Designate(expression,operand);
  4394. Pass((operand.tag));
  4395. ELSE (* open-static *)
  4396. type := expression.type.resolved;
  4397. descriptorType := GetMathArrayDescriptorType(dim);
  4398. variable := GetTemporaryVariable(descriptorType, FALSE);
  4399. Symbol(variable,variableOp);
  4400. arrayDestinationTag := variableOp.op;
  4401. Designate(expression,operand);
  4402. FOR i := 0 TO dim-1 DO
  4403. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4404. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4405. ReleaseOperand(tmpOperand);
  4406. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4407. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4408. ReleaseOperand(tmpOperand);
  4409. END;
  4410. dimOp := IntermediateCode.Immediate(addressType,dim);
  4411. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4412. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4413. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4414. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset); (* static flag ? *)
  4415. baseType := SemanticChecker.ArrayBase(type,dim);
  4416. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4417. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4418. Pass((arrayDestinationTag));
  4419. END;
  4420. (* case 3d *)
  4421. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4422. Designate(expression,operand);
  4423. Dereference(operand,type.resolved,FALSE);
  4424. (*
  4425. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4426. *)
  4427. Pass((operand.tag));
  4428. (* case 3f *)
  4429. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4430. descriptorType := GetMathArrayDescriptorType(dim);
  4431. variable := GetTemporaryVariable(descriptorType, FALSE);
  4432. Symbol(variable,variableOp);
  4433. arrayDestinationTag := variableOp.op;
  4434. Designate(expression,operand);
  4435. IF operand.op.type.length >1 THEN (* vector register *)
  4436. variable2 := GetTemporaryVariable(type, FALSE);
  4437. Symbol(variable2, variable2Op);
  4438. MakeMemory(tmp,variable2Op.op,operand.op.type,0);
  4439. Emit(Mov(position,tmp, operand.op));
  4440. ReleaseOperand(operand);
  4441. Symbol(variable2, operand);
  4442. END;
  4443. FOR i := 0 TO dim-1 DO
  4444. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4445. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4446. ReleaseOperand(tmpOperand);
  4447. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4448. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4449. ReleaseOperand(tmpOperand);
  4450. END;
  4451. dimOp := IntermediateCode.Immediate(addressType,dim);
  4452. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4453. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4454. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4455. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4456. baseType := SemanticChecker.ArrayBase(type,dim);
  4457. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4458. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4459. Pass((arrayDestinationTag));
  4460. ELSE HALT(100);
  4461. END;
  4462. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (parameter.kind = SyntaxTree.VarParameter) THEN
  4463. dim := SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static});
  4464. (* case 4b *)
  4465. IF (expression IS SyntaxTree.IndexDesignator) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  4466. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN (* indexer of form a[e,....,?] *)
  4467. variable := PrepareTensorDescriptor(expression(SyntaxTree.IndexDesignator));
  4468. Symbol(variable,variableOp);
  4469. LoadValue(variableOp,system.addressType);
  4470. ELSE
  4471. descriptorType := GetMathArrayDescriptorType(dim);
  4472. variable := GetTemporaryVariable(descriptorType, FALSE);
  4473. Symbol(variable,variableOp);
  4474. END;
  4475. arrayDestinationTag := variableOp.op;
  4476. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4477. arrayDestinationDimension := 0;
  4478. Designate(expression,operand);
  4479. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4480. Symbol(variable,variableOp);
  4481. ELSE
  4482. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4483. Symbol(variable,variableOp);
  4484. MakeMemory(tmp,variableOp.op,addressType,0);
  4485. Emit(Mov(position,tmp,operand.tag));
  4486. ReleaseIntermediateOperand(tmp);
  4487. END;
  4488. Pass((variableOp.op));
  4489. ReleaseOperand(variableOp);
  4490. (* case 4a *)
  4491. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4492. i := 0;
  4493. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4494. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4495. INC(i);
  4496. END;
  4497. IF i = dim THEN
  4498. Designate(expression,operand);
  4499. arrayDestinationTag := operand.tag;
  4500. ELSE (* open-static *)
  4501. type := expression.type.resolved;
  4502. descriptorType := GetMathArrayDescriptorType(dim);
  4503. variable := GetTemporaryVariable(descriptorType, FALSE);
  4504. Symbol(variable,variableOp);
  4505. arrayDestinationTag := variableOp.op;
  4506. Designate(expression,operand);
  4507. FOR i := 0 TO dim-1 DO
  4508. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4509. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4510. ReleaseOperand(tmpOperand);
  4511. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4512. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4513. ReleaseOperand(tmpOperand);
  4514. END;
  4515. dimOp := IntermediateCode.Immediate(addressType,dim);
  4516. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4517. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4518. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4519. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset);
  4520. baseType := SemanticChecker.ArrayBase(type,dim);
  4521. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4522. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4523. END;
  4524. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4525. Symbol(variable,variableOp);
  4526. MakeMemory(tmp,variableOp.op,addressType,0);
  4527. Emit(Mov(position,tmp,arrayDestinationTag));
  4528. ReleaseIntermediateOperand(tmp);
  4529. Pass((variableOp.op));
  4530. ReleaseOperand(variableOp);
  4531. (* case 4d *)
  4532. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4533. Designate(expression,operand);
  4534. (*
  4535. Dereference(operand,type.resolved,FALSE);
  4536. *)
  4537. (*
  4538. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4539. *)
  4540. Pass((operand.op));
  4541. (* case 4f *)
  4542. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4543. descriptorType := GetMathArrayDescriptorType(dim);
  4544. variable := GetTemporaryVariable(descriptorType, FALSE);
  4545. Symbol(variable,variableOp);
  4546. arrayDestinationTag := variableOp.op;
  4547. Designate(expression,operand);
  4548. FOR i := 0 TO dim-1 DO
  4549. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4550. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4551. ReleaseOperand(tmpOperand);
  4552. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4553. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4554. ReleaseOperand(tmpOperand);
  4555. END;
  4556. dimOp := IntermediateCode.Immediate(addressType,dim);
  4557. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4558. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4559. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4560. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4561. baseType := SemanticChecker.ArrayBase(type,dim);
  4562. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4563. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4564. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4565. Symbol(variable,variableOp);
  4566. MakeMemory(tmp,variableOp.op,addressType,0);
  4567. Emit(Mov(position,tmp,arrayDestinationTag));
  4568. ReleaseIntermediateOperand(tmp);
  4569. Pass((variableOp.op));
  4570. ReleaseOperand(variableOp);
  4571. ELSE HALT(100);
  4572. END;
  4573. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (parameter.kind = SyntaxTree.ValueParameter) THEN
  4574. ASSERT(type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static);
  4575. Designate(expression,operand);
  4576. IF operand.op.type.length > 1 THEN
  4577. Emit(Push(position, operand.op));
  4578. ELSE
  4579. size := system.SizeOf(type);
  4580. Basic.Align(size,system.AlignmentOf(system.parameterAlignment,type));
  4581. size := ToMemoryUnits(system,size);
  4582. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,size)));
  4583. arrayDestinationTag := sp;
  4584. Emit(Copy(position,arrayDestinationTag,operand.op,IntermediateCode.Immediate(addressType,size)));
  4585. END;
  4586. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (parameter.kind IN {SyntaxTree.VarParameter,SyntaxTree.ConstParameter}) THEN
  4587. Designate(expression,operand);
  4588. IF operand.op.type.length > 1 THEN (* need temporary to pass register *)
  4589. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4590. Symbol(variable,variableOp);
  4591. MakeMemory(tmp,variableOp.op,operand.op.type,0);
  4592. Emit(Mov(position,tmp,operand.op));
  4593. Emit(Push(position,variableOp.op));
  4594. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4595. Pass((operand.op));
  4596. ELSE Error(position,"Forbidden non-static actual type. Conversion involved?");
  4597. END;
  4598. ELSE HALT(200)
  4599. END;
  4600. ELSIF parameter.type.resolved IS SyntaxTree.RangeType THEN
  4601. IF parameter.kind = SyntaxTree.VarParameter THEN
  4602. ASSERT(~(expression IS SyntaxTree.RangeExpression));
  4603. Designate(expression, operand);
  4604. Pass((operand.op));
  4605. ELSE
  4606. ASSERT((parameter.kind = SyntaxTree.ValueParameter) OR (parameter.kind = SyntaxTree.ConstParameter));
  4607. Evaluate(expression, operand);
  4608. Pass((operand.extra)); (* step *)
  4609. Pass((operand.tag)); (* last *)
  4610. Pass((operand.op)) (* first *)
  4611. END
  4612. ELSIF parameter.type.resolved IS SyntaxTree.ComplexType THEN
  4613. IF parameter.kind = SyntaxTree.VarParameter THEN
  4614. Designate(expression, operand);
  4615. Pass((operand.op));
  4616. ELSE
  4617. ASSERT((parameter.kind = SyntaxTree.ValueParameter) OR (parameter.kind = SyntaxTree.ConstParameter));
  4618. Evaluate(expression, operand);
  4619. Pass((operand.tag)); (* real part *)
  4620. Pass((operand.op)) (* imaginary part *)
  4621. END
  4622. ELSE
  4623. IF (parameter.kind = SyntaxTree.ValueParameter) OR (parameter.kind = SyntaxTree.ConstParameter) & ~(parameter.type.resolved IS SyntaxTree.RecordType) & ~(parameter.type.resolved IS SyntaxTree.ArrayType) THEN
  4624. IF (type IS SyntaxTree.RecordType) OR IsStaticArray(parameter.type) THEN
  4625. Designate(expression,operand);
  4626. size := ToMemoryUnits(system,system.SizeOf(parameter.type));
  4627. (* stack allocation *)
  4628. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,size + (-size) MOD (system.addressSize DIV system.dataUnit))));
  4629. (*! parameter alignment to be discussed ... *)
  4630. IF type IS SyntaxTree.StringType THEN (* source potentially shorter than destination *)
  4631. size := type(SyntaxTree.StringType).length;
  4632. END;
  4633. IF backend.cooperative & parameter.NeedsTrace() THEN
  4634. dst := NewRegisterOperand (addressType);
  4635. Emit(Mov(position,dst, sp));
  4636. IntermediateCode.InitImmediate(null, byteType, 0);
  4637. Emit(Fill(position, dst, IntermediateCode.Immediate(addressType,size), null));
  4638. ReleaseIntermediateOperand(dst);
  4639. SaveRegisters();ReleaseUsedRegisters(saved);
  4640. (* register dst has been freed before SaveRegisters already *)
  4641. CallAssignMethod(dst, operand.op, parameter.type);
  4642. RestoreRegisters(saved);
  4643. END;
  4644. Emit(Copy(position,sp,operand.op,IntermediateCode.Immediate(addressType,size)));
  4645. ELSIF IsOpenArray(parameter.type) THEN
  4646. Designate(expression,operand);
  4647. baseReg := operand.tag;
  4648. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4649. PushArrayLens(parameter.type,type,operand.dimOffset+DynamicDim(parameter.type)-1);
  4650. END;
  4651. Pass((operand.op)); (* address of the array *)
  4652. ELSIF IsDelegate(parameter.type) THEN
  4653. Evaluate(expression,operand);
  4654. IF backend.cooperative & parameter.NeedsTrace() THEN
  4655. Emit(Push(position, nil));
  4656. dst := NewRegisterOperand (addressType);
  4657. Emit(Mov(position,dst, sp));
  4658. ReleaseIntermediateOperand(dst);
  4659. SaveRegisters();ReleaseUsedRegisters(saved);
  4660. Emit(Push(position, dst));
  4661. (* register dst has been freed before SaveRegisters already *)
  4662. Emit(Push(position, operand.tag));
  4663. CallThis(position,"GarbageCollector","Assign",2);
  4664. RestoreRegisters(saved);
  4665. ELSE
  4666. Pass((operand.tag));
  4667. END;
  4668. Pass((operand.op));
  4669. ELSE
  4670. Evaluate(expression,operand);
  4671. IF backend.cooperative & parameter.NeedsTrace() & (operand.op.mode # IntermediateCode.ModeImmediate) THEN
  4672. Emit(Push(position, nil));
  4673. dst := NewRegisterOperand (addressType);
  4674. Emit(Mov(position,dst, sp));
  4675. ReleaseIntermediateOperand(dst);
  4676. SaveRegisters();ReleaseUsedRegisters(saved);
  4677. Emit(Push(position, dst));
  4678. (* register dst has been freed before SaveRegisters already *)
  4679. Emit(Push(position, operand.op));
  4680. CallThis(position,"GarbageCollector","Assign",2);
  4681. RestoreRegisters(saved);
  4682. ELSE
  4683. Pass((operand.op));
  4684. END;
  4685. END;
  4686. ELSIF expression IS SyntaxTree.NilValue THEN (* for special WinAPI rule *)
  4687. Evaluate(expression,operand);
  4688. Pass((operand.op));
  4689. ELSE (* var parameter *)
  4690. Designate(expression,operand);
  4691. IF (type IS SyntaxTree.RecordType) & (parameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.VarParameter}) THEN
  4692. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4693. Pass((operand.tag));
  4694. END;
  4695. END;
  4696. Pass((operand.op));
  4697. END;
  4698. END;
  4699. (* TODO: needed? *)
  4700. arrayDestinationTag := oldArrayDestinationTag;
  4701. arrayDestinationDimension := oldArrayDestinationDimension;
  4702. IF needsParameterBackup THEN
  4703. (* IF dump # NIL THEN dump.String("backup parameter"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4704. ReuseCopy(parameterBackup, operand.op)
  4705. END;
  4706. ReleaseOperand(operand);
  4707. IF Trace THEN TraceExit("PushParameter") END;
  4708. END PushParameter;
  4709. PROCEDURE VisitStatementDesignator(x: SyntaxTree.StatementDesignator);
  4710. VAR prevConditional: BOOLEAN;
  4711. BEGIN
  4712. prevConditional := conditional;
  4713. conditional := FALSE;
  4714. IF (x.result # NIL) & ( x.result IS SyntaxTree.SymbolDesignator) & (x.result(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) & (x.result(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).useRegister) THEN
  4715. Expression(x.result); (* use register *)
  4716. END;
  4717. Statement(x.statement);
  4718. conditional := prevConditional;
  4719. IF x.result # NIL THEN Expression(x.result) END;
  4720. IF (x.result # NIL) & (x.result IS SyntaxTree.SymbolDesignator) & (x.result(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) & (x.result(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).useRegister) THEN
  4721. ReleaseIntermediateOperand(result.op);
  4722. END;
  4723. END VisitStatementDesignator;
  4724. PROCEDURE InlineProcedureCall(x: SyntaxTree.ProcedureCallDesignator): BOOLEAN;
  4725. VAR
  4726. procedure: SyntaxTree.Procedure;
  4727. procedureType: SyntaxTree.ProcedureType;
  4728. wasInline: BOOLEAN;
  4729. actualParameters: SyntaxTree.ExpressionList;
  4730. formalParameter: SyntaxTree.Parameter;
  4731. actualParameter: SyntaxTree.Expression;
  4732. i: LONGINT;
  4733. localVariable: SyntaxTree.Variable;
  4734. variableDesignator, returnDesignator: SyntaxTree.Expression;
  4735. src, dest: Operand;
  4736. prevInlineExit : Label;
  4737. prevMapper: SymbolMapper;
  4738. tooComplex: BOOLEAN;
  4739. resultDesignator: SyntaxTree.Expression;
  4740. PROCEDURE SimpleExpression(e: SyntaxTree.Expression): BOOLEAN;
  4741. BEGIN
  4742. IF e = NIL THEN RETURN TRUE
  4743. ELSIF (e IS SyntaxTree.SymbolDesignator) THEN RETURN SimpleExpression(e(SyntaxTree.SymbolDesignator).left)
  4744. ELSIF (e IS SyntaxTree.Value) THEN RETURN TRUE
  4745. ELSIF (e IS SyntaxTree.SelfDesignator) THEN RETURN TRUE
  4746. ELSIF (e IS SyntaxTree.ResultDesignator) THEN RETURN TRUE
  4747. ELSIF (e IS SyntaxTree.DereferenceDesignator) THEN RETURN SimpleExpression(e(SyntaxTree.DereferenceDesignator).left)
  4748. ELSE RETURN FALSE
  4749. END;
  4750. END SimpleExpression;
  4751. PROCEDURE FitsInRegister(type: SyntaxTree.Type): BOOLEAN;
  4752. BEGIN
  4753. RETURN checker.CanPassInRegister(type)
  4754. END FitsInRegister;
  4755. PROCEDURE GetTemp(type: SyntaxTree.Type; tryRegister: BOOLEAN): SyntaxTree.Expression;
  4756. VAR
  4757. variable: SyntaxTree.Variable;
  4758. variableDesignator: SyntaxTree.Designator;
  4759. BEGIN
  4760. variable := GetTemporaryVariable(type, tryRegister & FitsInRegister(type));
  4761. variableDesignator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, NIL, variable);
  4762. variableDesignator.SetType(type);
  4763. RETURN variableDesignator
  4764. END GetTemp;
  4765. BEGIN
  4766. resultDesignator := procedureResultDesignator; procedureResultDesignator := NIL;
  4767. wasInline := currentIsInline;
  4768. prevInlineExit := currentInlineExit;
  4769. prevMapper := currentMapper;
  4770. currentInlineExit := NewLabel();
  4771. tooComplex := FALSE;
  4772. NEW(currentMapper);
  4773. currentIsInline := TRUE;
  4774. procedure := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  4775. procedureType := procedure.type(SyntaxTree.ProcedureType);
  4776. formalParameter := procedureType.firstParameter;
  4777. actualParameters := x.parameters;
  4778. i := 0;
  4779. WHILE (i < actualParameters.Length()) & ~tooComplex DO
  4780. actualParameter := actualParameters.GetExpression(i);
  4781. IF actualParameter.resolved # NIL THEN
  4782. actualParameter := actualParameter.resolved
  4783. END;
  4784. (*
  4785. if expression is simple and can be passed immediately
  4786. or if type fits in register then we can proceed
  4787. otherwise we escape to ordinary procedure call.
  4788. *)
  4789. (* cases where the expression can be mapped identically *)
  4790. IF SimpleExpression(actualParameter) & (formalParameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.VarParameter}) THEN
  4791. currentMapper.Add(formalParameter, actualParameter, NIL,FALSE);
  4792. ELSIF FitsInRegister(actualParameter.type) & (formalParameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.ValueParameter}) THEN
  4793. variableDesignator := GetTemp(formalParameter.type, TRUE);
  4794. (*
  4795. Assign(variableDesignator, actualParameter);
  4796. *)
  4797. Evaluate(actualParameter, src);
  4798. Designate(variableDesignator, dest);
  4799. Emit(Mov(x.position, dest.op, src.op));
  4800. ReleaseOperand(dest);
  4801. ReleaseOperand(src);
  4802. (* the src operand should now have been completely released ! *)
  4803. currentMapper.Add(formalParameter, variableDesignator, NIL, FALSE);
  4804. ELSE tooComplex := TRUE
  4805. END;
  4806. (*
  4807. ELSIF (formalParameter.kind = SyntaxTree.ConstParameter) & IsSimple(actualParameter) THEN
  4808. currentMapper.Add(formalParameter, actualParameter, NIL);
  4809. ELSIF (formalParameter.kind = SyntaxTree.VarParameter) OR formalParameter.type.IsComposite() & (formalParameter.kind = SyntaxTree.ConstParameter) THEN
  4810. variableDesignator := GetTemp(system.addressType, FALSE);
  4811. Designate(actualParameter, src);
  4812. Designate(variableDesignator, dest);
  4813. IntermediateCode.MakeMemory(dest.op,addressType);
  4814. Emit(Mov(x.position, dest.op, src.op));
  4815. ReleaseOperand(dest);
  4816. IF src.tag.mode # IntermediateCode.Undefined THEN
  4817. tagDesignator := GetTemp(system.addressType, FALSE);
  4818. Designate(tagDesignator, dest);
  4819. IntermediateCode.MakeMemory(dest.op,addressType);
  4820. Emit(Mov(x.position, dest.op, src.op));
  4821. END;
  4822. ReleaseOperand(dest); ReleaseOperand(src);
  4823. currentMapper.Add(formalParameter, variableDesignator, tagDesignator);
  4824. END;
  4825. *)
  4826. formalParameter := formalParameter.nextParameter;
  4827. INC(i);
  4828. END;
  4829. IF ~tooComplex & (procedureType.returnType # NIL) THEN
  4830. IF resultDesignator # NIL THEN
  4831. returnDesignator := resultDesignator
  4832. ELSE
  4833. returnDesignator := GetTemp(procedureType.returnType, TRUE);
  4834. END;
  4835. currentMapper.Add(NIL, returnDesignator, NIL, resultDesignator # NIL);
  4836. END;
  4837. localVariable := procedure.procedureScope.firstVariable;
  4838. WHILE ~tooComplex & (localVariable # NIL) DO
  4839. variableDesignator := GetTemp(localVariable.type, FALSE);
  4840. currentMapper.Add(localVariable, variableDesignator, NIL, FALSE);
  4841. localVariable := localVariable.nextVariable;
  4842. END;
  4843. IF ~tooComplex THEN
  4844. VisitStatementBlock(procedure.procedureScope.body);
  4845. SetLabel(currentInlineExit);
  4846. IF procedureType.returnType # NIL THEN
  4847. Designate(returnDesignator, result);
  4848. IF conditional THEN
  4849. ASSERT (procedureType.returnType.resolved IS SyntaxTree.BooleanType);
  4850. ValueToCondition(result)
  4851. END;
  4852. END;
  4853. END;
  4854. currentMapper := prevMapper;
  4855. currentInlineExit := prevInlineExit;
  4856. currentIsInline := wasInline;
  4857. RETURN ~tooComplex
  4858. END InlineProcedureCall;
  4859. PROCEDURE VisitProcedureCallDesignator(x: SyntaxTree.ProcedureCallDesignator);
  4860. VAR
  4861. parameters: SyntaxTree.ExpressionList;
  4862. d, resultDesignator, actualParameter: SyntaxTree.Expression;
  4863. designator: SyntaxTree.Designator;
  4864. procedureType: SyntaxTree.ProcedureType;
  4865. formalParameter: SyntaxTree.Parameter;
  4866. operand, returnValue: Operand;
  4867. reg, size, mask, dest: IntermediateCode.Operand;
  4868. saved: RegisterEntry;
  4869. symbol: SyntaxTree.Symbol;
  4870. variable: SyntaxTree.Variable;
  4871. i, parametersSize, returnTypeSize : LONGINT;
  4872. structuredReturnType: BOOLEAN;
  4873. firstWriteBackCall, currentWriteBackCall: WriteBackCall;
  4874. tempVariableDesignator: SyntaxTree.Designator;
  4875. gap, alignment: LONGINT; (*fld*)
  4876. (* TODO: remove unnecessary backup variables *)
  4877. oldResult: Operand;
  4878. oldCurrentScope: SyntaxTree.Scope;
  4879. oldArrayDestinationTag: IntermediateCode.Operand;
  4880. oldArrayDestinationDimension: LONGINT;
  4881. oldConstantDeclaration: SyntaxTree.Symbol;
  4882. oldDestination: IntermediateCode.Operand;
  4883. oldCurrentLoop: Label;
  4884. oldConditional: BOOLEAN;
  4885. oldTrueLabel, oldFalseLabel: Label;
  4886. oldLocked: BOOLEAN;
  4887. usedRegisters,oldUsedRegisters: RegisterEntry;
  4888. return: IntermediateCode.Operand;
  4889. parameterBackups: ARRAY 2 OF IntermediateCode.Operand;
  4890. arg: IntermediateCode.Operand;
  4891. dummy: IntermediateCode.Operand;
  4892. recordType: SyntaxTree.RecordType;
  4893. operatorSelectionProcedureOperand: Operand;
  4894. operatorSelectionProcedure: SyntaxTree.Procedure;
  4895. fingerPrint: SyntaxTree.FingerPrint;
  4896. isCallOfDynamicOperator, hasDynamicOperands: BOOLEAN;
  4897. identifierNumber: LONGINT;
  4898. parameterRegister: Backend.Registers;
  4899. parameterRegisters: LONGINT;
  4900. passByRegister: BOOLEAN; registerNumber,stackSize: LONGINT;
  4901. procedure: SyntaxTree.Procedure;
  4902. PROCEDURE BackupGlobalState;
  4903. BEGIN
  4904. oldResult := result;
  4905. oldCurrentScope := currentScope;
  4906. oldArrayDestinationTag := arrayDestinationTag;
  4907. oldArrayDestinationDimension := arrayDestinationDimension;
  4908. oldConstantDeclaration := constantDeclaration;
  4909. oldDestination := destination;
  4910. oldCurrentLoop := currentLoop;
  4911. oldConditional := conditional;
  4912. oldTrueLabel := trueLabel;
  4913. oldFalseLabel := falseLabel;
  4914. oldLocked := locked;
  4915. oldUsedRegisters := usedRegisters
  4916. END BackupGlobalState;
  4917. PROCEDURE RestoreGlobalState;
  4918. BEGIN
  4919. result := oldResult;
  4920. currentScope := oldCurrentScope;
  4921. arrayDestinationTag := oldArrayDestinationTag;
  4922. arrayDestinationDimension := oldArrayDestinationDimension;
  4923. constantDeclaration := oldConstantDeclaration;
  4924. destination := oldDestination;
  4925. currentLoop := oldCurrentLoop;
  4926. conditional := oldConditional;
  4927. trueLabel := oldTrueLabel;
  4928. falseLabel := oldFalseLabel;
  4929. locked := oldLocked;
  4930. usedRegisters := oldUsedRegisters
  4931. END RestoreGlobalState;
  4932. (** do preparations before parameter push for array-structured object types (ASOTs):
  4933. if ASOT is passed as VAR parameter:
  4934. - allocate temporary variable of math array type
  4935. - copy contents of ASOT to be passed to temporary variable
  4936. - use temporary variable as the actual parameter instead
  4937. - create and store a write-back call in a list (an index operator call that writes the contents of the temp. variable back into the ASOT)
  4938. **)
  4939. PROCEDURE PrepareParameter(VAR actualParameter: SyntaxTree.Expression; formalParameter: SyntaxTree.Parameter);
  4940. VAR
  4941. expression: SyntaxTree.Expression;
  4942. BEGIN
  4943. IF actualParameter IS SyntaxTree.Designator THEN
  4944. designator := actualParameter(SyntaxTree.Designator);
  4945. IF (formalParameter.kind = SyntaxTree.VarParameter) & (designator.relatedAsot # NIL) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  4946. (* actual parameter is an index read operator call on ASOT and formal parameter is of VAR kind *)
  4947. (* IF dump # NIL THEN dump.String("!!! BEGIN ASOT VAR parameter preparation"); dump.Ln; dump.Update END; *) (* TENTATIVE*)
  4948. ASSERT(checker # NIL);
  4949. checker.SetCurrentScope(currentScope);
  4950. (* allocate temporary variable *)
  4951. ASSERT(actualParameter.type # NIL);
  4952. ASSERT(actualParameter.type.resolved IS SyntaxTree.MathArrayType);
  4953. variable := GetTemporaryVariable(actualParameter.type.resolved, FALSE);
  4954. tempVariableDesignator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, NIL, variable);
  4955. tempVariableDesignator.SetType(actualParameter.type.resolved);
  4956. ASSERT(tempVariableDesignator IS SyntaxTree.SymbolDesignator);
  4957. ASSERT(tempVariableDesignator.type # NIL);
  4958. ASSERT(tempVariableDesignator.type.resolved IS SyntaxTree.MathArrayType);
  4959. (* copy math array stored in actual parameter to temporary variable *)
  4960. BackupGlobalState;
  4961. AssignMathArray(tempVariableDesignator, actualParameter);
  4962. RestoreGlobalState;
  4963. (* use temporary variable as actual parameter instead of the original one *)
  4964. actualParameter := tempVariableDesignator;
  4965. (* create write-back call and store it in linked list *)
  4966. (* create new list entry *)
  4967. IF firstWriteBackCall = NIL THEN
  4968. NEW(firstWriteBackCall);
  4969. currentWriteBackCall := firstWriteBackCall
  4970. ELSE
  4971. ASSERT(currentWriteBackCall # NIL);
  4972. NEW(currentWriteBackCall.next);
  4973. currentWriteBackCall := currentWriteBackCall.next
  4974. END;
  4975. expression := checker.NewIndexOperatorCall(Basic.invalidPosition, designator.relatedAsot, designator.relatedIndexList, tempVariableDesignator);
  4976. ASSERT(expression.type = NIL);
  4977. currentWriteBackCall.call := expression(SyntaxTree.ProcedureCallDesignator);
  4978. ELSIF (formalParameter.kind = SyntaxTree.VarParameter) & (designator.relatedAsot # NIL) THEN
  4979. (* prepare writeback for any other "normal" indexer *)
  4980. variable := GetTemporaryVariable(actualParameter.type.resolved, FALSE);
  4981. tempVariableDesignator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, NIL, variable);
  4982. tempVariableDesignator.SetType(actualParameter.type.resolved);
  4983. Assign(tempVariableDesignator, actualParameter);
  4984. actualParameter := tempVariableDesignator;
  4985. IF firstWriteBackCall = NIL THEN
  4986. NEW(firstWriteBackCall);
  4987. currentWriteBackCall := firstWriteBackCall
  4988. ELSE
  4989. ASSERT(currentWriteBackCall # NIL);
  4990. NEW(currentWriteBackCall.next);
  4991. currentWriteBackCall := currentWriteBackCall.next
  4992. END;
  4993. expression := checker.NewObjectOperatorCall(Basic.invalidPosition, designator.relatedAsot, 0, designator.relatedIndexList, tempVariableDesignator);
  4994. currentWriteBackCall.call := expression(SyntaxTree.ProcedureCallDesignator);
  4995. END
  4996. END
  4997. END PrepareParameter;
  4998. BEGIN
  4999. IF Trace THEN TraceEnter("VisitProcedureCallDesignator") END;
  5000. IF (x.left IS SyntaxTree.SymbolDesignator) & (x.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  5001. procedure := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  5002. IF procedure.isOberonInline (* & (procedure.scope.ownerModule = module.module) *) THEN
  5003. IF InlineProcedureCall(x) THEN
  5004. RETURN
  5005. ELSE
  5006. Warning(x.position, "Inline call with complex parameters not yet implemented. Emitting ordinary procedure call.")
  5007. END
  5008. END;
  5009. END;
  5010. resultDesignator := procedureResultDesignator; procedureResultDesignator := NIL;
  5011. procedureType := x.left.type.resolved(SyntaxTree.ProcedureType);
  5012. dest := destination; destination := emptyOperand;
  5013. SaveRegisters();ReleaseUsedRegisters(saved);
  5014. parameters := x.parameters;
  5015. IF (x.left IS SyntaxTree.SymbolDesignator) & (x.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Operator) THEN
  5016. (* an operator is called *)
  5017. (* IF dump # NIL THEN dump.String("*** begin of operator call ***"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5018. ASSERT(procedureType.callingConvention = SyntaxTree.OberonCallingConvention);
  5019. (* check if a dynamic operator call should be performed *)
  5020. isCallOfDynamicOperator := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Operator).isDynamic;
  5021. ELSE
  5022. isCallOfDynamicOperator := FALSE
  5023. END;
  5024. IF backend.cooperative & (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) THEN
  5025. Emit(Push(position, ap));
  5026. END;
  5027. alignment := procedureType.stackAlignment;
  5028. IF alignment > 1 THEN
  5029. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  5030. Emit(Mov(position,reg, sp));
  5031. gap := ParametersSize(system, procedureType, FALSE) + ToMemoryUnits(system,system.offsetFirstParameter) + ToMemoryUnits(system,system.addressSize);
  5032. Emit(Sub(position,sp, sp, IntermediateCode.Immediate(addressType,gap)));
  5033. IntermediateCode.InitImmediate(mask,addressType,-alignment);
  5034. Emit(And(position,sp, sp, mask));
  5035. Emit(Add(position, sp, sp, IntermediateCode.Immediate(addressType, gap)));
  5036. Emit(Push(position,reg));
  5037. (*
  5038. IntermediateCode.InitMemory(mem, addressType, sp, 0);
  5039. Emit(Mov(position,mem,reg));
  5040. *)
  5041. ReleaseIntermediateOperand(reg);
  5042. END;
  5043. IF SysvABI(procedureType.callingConvention) & (system.addressSize = 32) THEN
  5044. (* align stack to 16-byte boundary *)
  5045. IntermediateCode.InitImmediate(mask,addressType,-16);
  5046. Emit(And(position,sp, sp, mask));
  5047. gap := (-ParametersSize( system, procedureType, FALSE )) MOD 16;
  5048. IF gap # 0 THEN
  5049. IntermediateCode.InitImmediate(size,addressType,gap);
  5050. Emit(Sub(position,sp,sp,size))
  5051. END;
  5052. END;
  5053. IF x.left IS SyntaxTree.SupercallDesignator THEN
  5054. symbol := x.left(SyntaxTree.SupercallDesignator).left(SyntaxTree.SymbolDesignator).symbol;
  5055. ELSIF x.left IS SyntaxTree.IndexDesignator THEN
  5056. symbol := x.left(SyntaxTree.IndexDesignator).left(SyntaxTree.SymbolDesignator).symbol;
  5057. ELSE
  5058. symbol := x.left(SyntaxTree.SymbolDesignator).symbol;
  5059. END;
  5060. Evaluate(x.left, operand);
  5061. IF symbol IS SyntaxTree.Procedure THEN
  5062. IF (procedureType.selfParameter # NIL) THEN
  5063. Emit(Push(position,operand.tag));
  5064. ELSIF x.left IS SyntaxTree.SupercallDesignator THEN
  5065. Emit(Push(position,operand.tag));
  5066. ELSIF (procedureType.isDelegate) THEN
  5067. Emit(Push(position,operand.tag));
  5068. END;
  5069. ELSIF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  5070. IF (procedureType.selfParameter # NIL) THEN
  5071. Emit(Push(position,operand.tag));
  5072. ELSIF (procedureType.isDelegate) THEN (* push self pointer only if procedure is a method *)
  5073. Emit(Push(position,operand.tag));
  5074. END;
  5075. ELSE HALT(200);
  5076. END;
  5077. ReleaseIntermediateOperand(operand.tag);
  5078. operand.tag := emptyOperand;
  5079. (* determine if a structured return type is needed *)
  5080. structuredReturnType := SemanticChecker.StructuredReturnType(procedureType);
  5081. IF structuredReturnType THEN
  5082. IF resultDesignator # NIL THEN
  5083. d := resultDesignator;
  5084. ELSE
  5085. variable := GetTemporaryVariable(procedureType.returnType, FALSE);
  5086. variable.SetUntraced(procedureType.hasUntracedReturn);
  5087. d := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,variable);
  5088. d.SetType(variable.type);
  5089. END;
  5090. IF (procedureType.returnType.resolved IS SyntaxTree.RecordType) THEN
  5091. Designate(d,returnValue);
  5092. returnTypeSize := system.SizeOf(procedureType.returnType.resolved);
  5093. size := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,returnTypeSize));
  5094. Emit(Push(position,size));
  5095. Emit(Push(position,returnValue.op));
  5096. ReleaseOperand(returnValue);
  5097. ELSE
  5098. PushParameter(d,procedureType.returnParameter,procedureType.callingConvention, FALSE, dummy,-1)
  5099. END;
  5100. END;
  5101. firstWriteBackCall := NIL; (* reset write-back call list *)
  5102. IF procedureType.callingConvention # SyntaxTree.OberonCallingConvention THEN
  5103. parameterRegister := backend.GetParameterRegisters(procedureType.callingConvention);
  5104. IF parameterRegister = NIL THEN parameterRegisters := 0
  5105. ELSE parameterRegisters := LEN(parameterRegister)
  5106. END;
  5107. passByRegister := parameterRegisters > 0;
  5108. registerNumber := 0;
  5109. formalParameter := procedureType.lastParameter;
  5110. FOR i := parameters.Length() - 1 TO 0 BY -1 DO
  5111. actualParameter := parameters.GetExpression(i);
  5112. PrepareParameter(actualParameter, formalParameter);
  5113. IF passByRegister & (i < parameterRegisters) THEN
  5114. IF ~PassInRegister(formalParameter) THEN
  5115. Error(actualParameter.position,"cannot be passed by register")
  5116. ELSE
  5117. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, FALSE, dummy,parameterRegister[i]);
  5118. END;
  5119. INC(registerNumber);
  5120. ELSE
  5121. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  5122. END;
  5123. formalParameter := formalParameter.prevParameter;
  5124. END;
  5125. IF passByRegister & (registerNumber > 0) & ~SysvABI(procedureType.callingConvention) THEN
  5126. stackSize := ToMemoryUnits(system,parameterRegisters*addressType.sizeInBits);
  5127. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,stackSize)));
  5128. END;
  5129. ELSE
  5130. hasDynamicOperands := FALSE;
  5131. formalParameter := procedureType.firstParameter;
  5132. FOR i := 0 TO parameters.Length() - 1 DO
  5133. actualParameter := parameters.GetExpression(i);
  5134. IF formalParameter # NIL THEN (* TENTATIVE *)
  5135. PrepareParameter(actualParameter, formalParameter);
  5136. IF isCallOfDynamicOperator & IsStrictlyPointerToRecord(formalParameter.type) & (formalParameter.access # SyntaxTree.Hidden) THEN (* TODO: remove hidden parameters *)
  5137. ASSERT(i < 2);
  5138. hasDynamicOperands := TRUE;
  5139. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, TRUE, parameterBackups[i],-1)
  5140. ELSE
  5141. IF passByRegister & (registerNumber > 0) THEN
  5142. stackSize := ToMemoryUnits(system,registerNumber*addressType.sizeInBits);
  5143. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,stackSize)));
  5144. END;
  5145. passByRegister := FALSE;
  5146. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  5147. END;
  5148. formalParameter := formalParameter.nextParameter;
  5149. END;
  5150. END;
  5151. END;
  5152. IF symbol IS SyntaxTree.Procedure THEN
  5153. IF IsNested(symbol(SyntaxTree.Procedure)) THEN
  5154. GetBaseRegister(reg,currentScope,symbol.scope); (* static link, may be fp or [fp+8] (if nested proc calls itself) *)
  5155. Emit(Push(position,reg));
  5156. ReleaseIntermediateOperand(reg);
  5157. END;
  5158. parametersSize := ProcedureParametersSize(system,symbol(SyntaxTree.Procedure));
  5159. ELSIF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  5160. parametersSize := ParametersSize(system,procedureType,FALSE);
  5161. END;
  5162. IF isCallOfDynamicOperator & hasDynamicOperands THEN
  5163. (*
  5164. dynamic operator overloading:
  5165. first push parameters, regularly:
  5166. [self]
  5167. par1
  5168. par2
  5169. then push parameters for GetOperator
  5170. identifier
  5171. ptr1
  5172. tag
  5173. ptr2
  5174. tag
  5175. call GetOperatorRuntimeProc
  5176. call Operator
  5177. *)
  5178. IF dump # NIL THEN dump.String("++++++++++ dynamic operator call ++++++++++"); dump.Ln; dump.Update END; (* TENTATIVE *)
  5179. (* push ID *)
  5180. (* IF dump # NIL THEN dump.String("push ID"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5181. ASSERT(x.left IS SyntaxTree.SymbolDesignator);
  5182. identifierNumber := Global.GetSymbol(module.module.case, x.left(SyntaxTree.SymbolDesignator).symbol.name);
  5183. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), identifierNumber)));
  5184. formalParameter := procedureType.firstParameter;
  5185. FOR i := 0 TO parameters.Length() - 1 DO
  5186. IF formalParameter.access # SyntaxTree.Hidden THEN
  5187. ASSERT(i < 2);
  5188. IF IsStrictlyPointerToRecord(formalParameter.type) THEN
  5189. (* push pointer *)
  5190. (* IF dump # NIL THEN dump.String("push pointer"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5191. IF formalParameter.kind = SyntaxTree.VarParameter THEN
  5192. (* add dereference *)
  5193. (* IF dump # NIL THEN dump.String("dereference pointer"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5194. (*! better: do refer to stack above than using parameter backups !!*)
  5195. ReleaseIntermediateOperand(parameterBackups[i]);
  5196. MakeMemory(parameterBackups[i], parameterBackups[i], addressType, 0)
  5197. END;
  5198. Emit(Push(position,parameterBackups[i]));
  5199. ReleaseIntermediateOperand(parameterBackups[i]);
  5200. (* push typetag *)
  5201. (* IF dump # NIL THEN dump.String("push typetag"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5202. recordType := formalParameter.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  5203. arg := TypeDescriptorAdr(recordType);
  5204. IF ~newObjectFile THEN IntermediateCode.MakeMemory(arg, addressType) END;
  5205. Emit(Push(position,arg));
  5206. ELSE
  5207. (* push 'NonPointer' *)
  5208. (* IF dump # NIL THEN dump.String("push 'NonPointer'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5209. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NonPointer)));
  5210. (* push fingerprint *)
  5211. (* IF dump # NIL THEN dump.String("push fingerprint"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5212. fingerPrint := fingerPrinter.TypeFP(formalParameter.type.resolved);
  5213. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), fingerPrint.shallow))) (* TODO: push the type's fingerprint *)
  5214. END
  5215. END;
  5216. formalParameter := formalParameter.nextParameter
  5217. END;
  5218. (* for unary operators: complete the information for the second parameter *)
  5219. IF procedureType.numberParameters < 2 THEN
  5220. (* push 'NonPointer' *)
  5221. (* IF dump # NIL THEN dump.String("push 'NonPointer'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5222. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NonPointer)));
  5223. (* push 'NoType' *)
  5224. (* IF dump # NIL THEN dump.String("push 'NoType'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5225. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NoType)));
  5226. END;
  5227. (* call operator selection procedure *)
  5228. IF GetRuntimeProcedure("FoxOperatorRuntime", "SelectOperator", operatorSelectionProcedure, TRUE) THEN
  5229. StaticCallOperand(operatorSelectionProcedureOperand, operatorSelectionProcedure);
  5230. Emit(Call(position,operatorSelectionProcedureOperand.op, ProcedureParametersSize(system, operatorSelectionProcedure)));
  5231. ReleaseOperand(operatorSelectionProcedureOperand);
  5232. (* use the address that the operator selection procedure returned as the target address of the call *)
  5233. InitOperand(operand, ModeValue);
  5234. operand.op := IntermediateCode.Register(addressType, IntermediateCode.GeneralPurposeRegister, AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  5235. Emit(Result(position,operand.op))
  5236. END
  5237. END;
  5238. ReleaseParameterRegisters();
  5239. IF (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) OR SysvABI(procedureType.callingConvention) THEN
  5240. Emit(Call(position,operand.op,0));
  5241. ELSE
  5242. Emit(Call(position,operand.op,parametersSize));
  5243. END;
  5244. ReleaseOperand(operand);
  5245. IF procedureType.noReturn THEN
  5246. EmitTrap(position,NoReturnTrap);
  5247. END;
  5248. IF (procedureType.returnType # NIL) & ~structuredReturnType THEN
  5249. return := NewRegisterOperand(IntermediateCode.GetType(system,procedureType.returnType));
  5250. Emit(Result(position,return));
  5251. END;
  5252. (* === return parameter space === *)
  5253. IF (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) & passByRegister THEN
  5254. parametersSize := ToMemoryUnits(system,parameters.Length()*addressType.sizeInBits);
  5255. IF parametersSize < 32 THEN
  5256. (* allocated space for all parameter registers *)
  5257. parametersSize := 32
  5258. END;
  5259. size := IntermediateCode.Immediate(addressType,parametersSize);
  5260. Emit(Add(position,sp,sp,size))
  5261. END;
  5262. IF SysvABI(procedureType.callingConvention) THEN
  5263. IF passByRegister THEN
  5264. IF parameters.Length() > parameterRegisters THEN
  5265. parametersSize := ToMemoryUnits(system,(parameters.Length()-parameterRegisters)*addressType.sizeInBits)
  5266. ELSE
  5267. parametersSize := 0
  5268. END;
  5269. ELSE
  5270. parametersSize := ToMemoryUnits(system,parameters.Length()*addressType.sizeInBits);
  5271. INC( parametersSize, (-parametersSize) MOD 16 )
  5272. END;
  5273. IF parametersSize > 0 THEN
  5274. size := IntermediateCode.Immediate(addressType,parametersSize);
  5275. Emit(Add(position,sp,sp,size))
  5276. END;
  5277. END;
  5278. IF (resultDesignator = NIL) & (procedureType.returnType # NIL) THEN
  5279. IF structuredReturnType THEN
  5280. (* stack pointer rewinding done by callee
  5281. size := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.SizeOfParameter(procedureType.returnParameter)));
  5282. Emit(Add(position,sp,sp,size));
  5283. *)
  5284. RestoreRegisters(saved);
  5285. InitOperand(result,ModeReference);
  5286. Symbol(variable,result);
  5287. ELSE
  5288. RestoreRegisters(saved);
  5289. InitOperand(result,ModeValue);
  5290. result.op := return;
  5291. END;
  5292. END;
  5293. IF alignment > 1 THEN
  5294. Emit(Pop(position,sp));
  5295. END;
  5296. IF backend.cooperative & (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) THEN
  5297. Emit(Pop(position, ap));
  5298. END;
  5299. IF conditional & (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  5300. ValueToCondition(result);
  5301. END;
  5302. destination := dest;
  5303. (* perform all write-back calls in the list *)
  5304. BackupGlobalState;
  5305. currentWriteBackCall := firstWriteBackCall;
  5306. WHILE currentWriteBackCall # NIL DO
  5307. VisitProcedureCallDesignator(currentWriteBackCall.call);
  5308. currentWriteBackCall := currentWriteBackCall.next
  5309. END;
  5310. RestoreGlobalState;
  5311. (* TENATIVE *)
  5312. (*
  5313. IF isOperatorCall THEN
  5314. IF dump # NIL THEN dump.String("*** end of operator call ***"); dump.Ln; dump.Update END
  5315. END;
  5316. *)
  5317. IF Trace THEN TraceExit("VisitProcedureCallDesignator") END;
  5318. END VisitProcedureCallDesignator;
  5319. PROCEDURE TypeDescriptorAdr(t: SyntaxTree.Type): IntermediateCode.Operand;
  5320. VAR res: IntermediateCode.Operand; offset: LONGINT; name: Basic.SegmentedName;
  5321. td: SyntaxTree.Symbol;
  5322. PROCEDURE GetHiddenPointerType(): SyntaxTree.Type;
  5323. VAR scope: SyntaxTree.RecordScope; variable: SyntaxTree.Variable; typeDeclaration: SyntaxTree.TypeDeclaration;
  5324. BEGIN
  5325. IF (hiddenPointerType = NIL) OR (hiddenPointerType.typeDeclaration.scope.ownerModule # module.module) THEN
  5326. scope := SyntaxTree.NewRecordScope(module.module.moduleScope);
  5327. variable := SyntaxTree.NewVariable(Basic.invalidPosition,SyntaxTree.NewIdentifier("@Any"));
  5328. variable.SetType(system.anyType);
  5329. scope.AddVariable(variable);
  5330. hiddenPointerType := SyntaxTree.NewRecordType(Basic.invalidPosition,NIL,scope);
  5331. typeDeclaration := SyntaxTree.NewTypeDeclaration(Basic.invalidPosition,SyntaxTree.NewIdentifier("@HdPtrDesc"));
  5332. typeDeclaration.SetDeclaredType(hiddenPointerType);
  5333. typeDeclaration.SetScope(module.module.moduleScope);
  5334. hiddenPointerType.SetTypeDeclaration(typeDeclaration);
  5335. hiddenPointerType.SetState(SyntaxTree.Resolved);
  5336. END;
  5337. RETURN hiddenPointerType;
  5338. END GetHiddenPointerType;
  5339. PROCEDURE GetDelegateType(): SyntaxTree.Type;
  5340. VAR scope: SyntaxTree.RecordScope; variable: SyntaxTree.Variable; typeDeclaration: SyntaxTree.TypeDeclaration;
  5341. BEGIN
  5342. IF (delegatePointerType = NIL) OR (delegatePointerType.typeDeclaration.scope.ownerModule # module.module) THEN
  5343. scope := SyntaxTree.NewRecordScope(module.module.moduleScope);
  5344. variable := SyntaxTree.NewVariable(Basic.invalidPosition,SyntaxTree.NewIdentifier("@Procedure"));
  5345. variable.SetType(SyntaxTree.NewProcedureType(Basic.invalidPosition,NIL));
  5346. scope.AddVariable(variable);
  5347. variable := SyntaxTree.NewVariable(Basic.invalidPosition,SyntaxTree.NewIdentifier("@Any"));
  5348. variable.SetType(system.anyType);
  5349. scope.AddVariable(variable);
  5350. delegatePointerType := SyntaxTree.NewRecordType(Basic.invalidPosition,NIL,scope);
  5351. typeDeclaration := SyntaxTree.NewTypeDeclaration(Basic.invalidPosition,SyntaxTree.NewIdentifier("@Delegate"));
  5352. typeDeclaration.SetDeclaredType(delegatePointerType);
  5353. typeDeclaration.SetScope(module.module.moduleScope);
  5354. delegatePointerType.SetTypeDeclaration(typeDeclaration);
  5355. delegatePointerType.SetState(SyntaxTree.Resolved);
  5356. END;
  5357. RETURN delegatePointerType
  5358. END GetDelegateType;
  5359. PROCEDURE GetBackendType(x: SyntaxTree.Type; VAR offset: LONGINT; VAR name: Basic.SegmentedName): SyntaxTree.Symbol;
  5360. (* create anonymous type declaration for types that need a type descriptor but have been declared anonymously
  5361. such as in VAR a: RECORD ... END;
  5362. reason: type desciptors in Sections are then accessible via a type declaration symbol and for types
  5363. and variables, constants and procedures the same mechanism can be used for fixups etc.
  5364. *)
  5365. VAR source: Sections.Section;null: HUGEINT; td: SyntaxTree.TypeDeclaration;
  5366. op: IntermediateCode.Operand; baseRecord: SyntaxTree.RecordType;
  5367. BEGIN (* no code emission *)
  5368. source := NIL;
  5369. x := x.resolved;
  5370. IF (x IS SyntaxTree.AnyType) OR (x IS SyntaxTree.PointerType) THEN
  5371. x := GetHiddenPointerType();
  5372. ELSIF IsDelegate(x) THEN
  5373. x := GetDelegateType();
  5374. ELSIF (x IS SyntaxTree.RecordType) OR (x IS SyntaxTree.CellType) THEN
  5375. ELSE HALT(200);
  5376. END;
  5377. td := x.typeDeclaration;
  5378. IF td = NIL THEN
  5379. ASSERT(x(SyntaxTree.RecordType).pointerType # NIL);
  5380. td := x(SyntaxTree.RecordType).pointerType.resolved.typeDeclaration;
  5381. ASSERT(td # NIL);
  5382. END;
  5383. IF newObjectFile THEN
  5384. GetCodeSectionNameForSymbol(td,name);
  5385. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  5386. meta.CheckTypeDeclaration(x);
  5387. source := NewSection(module.allSections, Sections.ConstSection, name,td,commentPrintout # NIL);
  5388. ELSE
  5389. source := NewSection(module.importedSections, Sections.ConstSection,name,td,commentPrintout # NIL);
  5390. END;
  5391. IF backend.cooperative OR meta.simple THEN
  5392. offset := 0;
  5393. ELSE
  5394. IF x IS SyntaxTree.CellType THEN
  5395. baseRecord := x(SyntaxTree.CellType).GetBaseRecord();
  5396. IF baseRecord = NIL THEN
  5397. offset := ToMemoryUnits(system, meta.GetTypeRecordBaseOffset(0));
  5398. ELSE
  5399. offset := ToMemoryUnits(system, meta.GetTypeRecordBaseOffset(baseRecord.recordScope.numberMethods)*system.addressSize);
  5400. END;
  5401. ELSE
  5402. offset := ToMemoryUnits(system,meta.GetTypeRecordBaseOffset(x(SyntaxTree.RecordType).recordScope.numberMethods)*system.addressSize);
  5403. END;
  5404. END;
  5405. ELSE
  5406. offset := 0;
  5407. source := module.allSections.FindBySymbol(td); (*TODO*)
  5408. IF source = NIL THEN
  5409. null := 0;
  5410. GetCodeSectionNameForSymbol(td,name);
  5411. Basic.SuffixSegmentedName (name, Basic.MakeString("@"));
  5412. Basic.SuffixSegmentedName (name, module.module.name);
  5413. source := NewSection(module.allSections, Sections.ConstSection, name,td,commentPrintout # NIL);
  5414. IntermediateCode.InitImmediate(op,addressType,0);
  5415. source(IntermediateCode.Section).Emit(Data(position,op));
  5416. source.SetReferenced(FALSE)
  5417. ELSE
  5418. name := source.name;
  5419. END;
  5420. END;
  5421. RETURN td
  5422. END GetBackendType;
  5423. BEGIN
  5424. (*td := t.typeDeclaration;*)
  5425. td := GetBackendType(t,offset,name); (*! do not dereference a pointer here as the type descriptor for the pointer might be asked for *)
  5426. (*
  5427. IF t IS SyntaxTree.PointerType THEN
  5428. source := GetBackendType(t(SyntaxTree.PointerType).pointerBase.resolved);
  5429. ELSE
  5430. source := GetBackendType(t);
  5431. END;
  5432. *)
  5433. IF newObjectFile THEN
  5434. IntermediateCode.InitAddress(res, addressType, name, GetFingerprint(td), 0 (*
  5435. 1+t(SyntaxTree.RecordType).recordScope.numberMethods+16+1 *));
  5436. IntermediateCode.SetOffset(res,offset);
  5437. ELSE
  5438. IntermediateCode.InitAddress(res, addressType, name, GetFingerprint(td), 0);
  5439. END;
  5440. (*
  5441. IntermediateCode.MakeMemory(res,IntermediateCode.UnsignedInteger,addressType.sizeInBits);
  5442. make memory should be used when tag is used, not earlier
  5443. *)
  5444. RETURN res
  5445. END TypeDescriptorAdr;
  5446. (*
  5447. PROCEDURE MakeTypeTag(VAR operand: Operand);
  5448. VAR result: IntermediateCode.Operand;
  5449. BEGIN
  5450. IF operand.tag.mode = IntermediateCode.Undefined THEN
  5451. operand.tag := TypeDescriptorAdr(operand.type);
  5452. IntermediateCode.MakeMemory(operand.tag,addressType);
  5453. UseIntermediateOperand(operand.tag);
  5454. END;
  5455. END MakeTypeTag;
  5456. *)
  5457. PROCEDURE ProfilerInit;
  5458. VAR reg: IntermediateCode.Operand;
  5459. BEGIN
  5460. IntermediateCode.InitAddress(reg, addressType, profileInit.name , GetFingerprint(profileInit.symbol), 0);
  5461. Emit(Call(position,reg,0));
  5462. END ProfilerInit;
  5463. PROCEDURE ProfilerEnterExit(procedureNumber: LONGINT; enter: BOOLEAN);
  5464. VAR reg: IntermediateCode.Operand; result: Operand; procedure: SyntaxTree.Procedure;
  5465. BEGIN
  5466. IF enter & GetRuntimeProcedure("FoxProfiler","EnterProcedure",procedure,TRUE)
  5467. OR ~enter & GetRuntimeProcedure("FoxProfiler","ExitProcedure",procedure,TRUE)
  5468. THEN
  5469. IntermediateCode.InitAddress(reg, addressType, profileId.name , GetFingerprint(profileId.symbol), 0);
  5470. IntermediateCode.MakeMemory(reg, IntermediateCode.GetType(system,system.longintType));
  5471. Emit(Push(position,reg));
  5472. IntermediateCode.InitImmediate(reg, IntermediateCode.GetType(system,system.longintType), procedureNumber);
  5473. Emit(Push(position,reg));
  5474. StaticCallOperand(result,procedure);
  5475. Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  5476. ReleaseOperand(result);
  5477. END;
  5478. END ProfilerEnterExit;
  5479. PROCEDURE ProfilerAddProcedure(procedureNumber: LONGINT; CONST name: ARRAY OF CHAR);
  5480. VAR string: SyntaxTree.String; reg: IntermediateCode.Operand; result: Operand; procedure: SyntaxTree.Procedure; sv: SyntaxTree.StringValue;type: SyntaxTree.Type;
  5481. BEGIN
  5482. IF GetRuntimeProcedure("FoxProfiler","AddProcedure",procedure,TRUE) THEN
  5483. IntermediateCode.InitAddress(reg, addressType, profileId.name , GetFingerprint(profileId.symbol), 0);
  5484. IntermediateCode.MakeMemory(reg, IntermediateCode.GetType(system,system.longintType));
  5485. profileInit.Emit(Push(position,reg));
  5486. IntermediateCode.InitImmediate(reg, IntermediateCode.GetType(system,system.longintType), procedureNumber);
  5487. profileInit.Emit(Push(position,reg));
  5488. NEW(string, LEN(name)); COPY(name, string^);
  5489. sv := SyntaxTree.NewStringValue(Basic.invalidPosition,string);
  5490. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,Strings.Length(name));
  5491. sv.SetType(type);
  5492. Designate(sv,result);
  5493. profileInit.Emit(Push(position,result.tag));
  5494. profileInit.Emit(Push(position,result.op));
  5495. StaticCallOperand(result,procedure);
  5496. profileInit.Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  5497. ReleaseOperand(result);
  5498. END;
  5499. END ProfilerAddProcedure;
  5500. PROCEDURE ProfilerAddModule(CONST name: ARRAY OF CHAR);
  5501. VAR string: SyntaxTree.String; sv: SyntaxTree.StringValue; type: SyntaxTree.Type; result: Operand; reg: IntermediateCode.Operand; procedure: SyntaxTree.Procedure;
  5502. BEGIN
  5503. IF GetRuntimeProcedure("FoxProfiler","AddModule",procedure,TRUE) THEN
  5504. IntermediateCode.InitAddress(reg, addressType, profileId.name , GetFingerprint(profileId.symbol), 0);
  5505. profileInit.Emit(Push(position,reg));
  5506. profileInitPatchPosition := profileInit.pc;
  5507. profileInit.Emit(Nop(position)); (* placeholder, will be patched by number of procedures *)
  5508. NEW(string, LEN(name)); COPY(name, string^);
  5509. sv := SyntaxTree.NewStringValue(Basic.invalidPosition, string);
  5510. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,Strings.Length(name));
  5511. sv.SetType(type);
  5512. Designate(sv,result);
  5513. profileInit.Emit(Push(position,result.tag));
  5514. profileInit.Emit(Push(position,result.op));
  5515. StaticCallOperand(result,procedure);
  5516. profileInit.Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  5517. ReleaseOperand(result);
  5518. END;
  5519. END ProfilerAddModule;
  5520. PROCEDURE ProfilerPatchInit;
  5521. VAR reg: IntermediateCode.Operand;
  5522. BEGIN
  5523. IntermediateCode.InitImmediate(reg, IntermediateCode.GetType(system,system.longintType), numberProcedures);
  5524. profileInit.EmitAt(profileInitPatchPosition,Push(position,reg));
  5525. EmitLeave(profileInit,position,NIL,0);
  5526. profileInit.Emit(Exit(position,0,0,0));
  5527. END ProfilerPatchInit;
  5528. (** if operator can be overloaded dynamically, emit code that registers it in the runtime **)
  5529. PROCEDURE RegisterDynamicOperator(operator: SyntaxTree.Operator);
  5530. VAR
  5531. id: LONGINT;
  5532. leftType, rightType: SyntaxTree.Type;
  5533. procedureType: SyntaxTree.ProcedureType;
  5534. runtimeProcedure: SyntaxTree.Procedure;
  5535. runtimeProcedureOperand, operatorOperand: Operand;
  5536. kind: SET;
  5537. PROCEDURE PushTypeInfo(type: SyntaxTree.Type);
  5538. VAR
  5539. arg: IntermediateCode.Operand;
  5540. recordType: SyntaxTree.RecordType;
  5541. fingerPrint: SyntaxTree.FingerPrint;
  5542. BEGIN
  5543. IF type = NIL THEN
  5544. (* no type: push 'NoType' *)
  5545. (* IF dump # NIL THEN dump.String("push 'NoType'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5546. arg := IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NoType)
  5547. ELSIF IsStrictlyPointerToRecord(type) THEN
  5548. (* pointer to record type: push typetag *)
  5549. (* IF dump # NIL THEN dump.String("push typetag"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5550. recordType := type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  5551. arg := TypeDescriptorAdr(recordType);
  5552. IF ~newObjectFile THEN IntermediateCode.MakeMemory(arg, addressType) END;
  5553. ELSE
  5554. (* non-pointer to record type: push fingerprint *)
  5555. (* IF dump # NIL THEN dump.String("push fingerprint"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5556. fingerPrint := fingerPrinter.TypeFP(type.resolved);
  5557. arg := IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), fingerPrint.shallow)
  5558. END;
  5559. operatorInitializationCodeSection.Emit(Push(position,arg))
  5560. END PushTypeInfo;
  5561. BEGIN
  5562. ASSERT(operatorInitializationCodeSection # NIL);
  5563. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  5564. procedureType := operator.type(SyntaxTree.ProcedureType);
  5565. (* determine types *)
  5566. leftType := procedureType.firstParameter.type;
  5567. IF procedureType.numberParameters = 2 THEN
  5568. ASSERT(procedureType.firstParameter.nextParameter # NIL);
  5569. rightType := procedureType.firstParameter.nextParameter.type;
  5570. ELSE
  5571. rightType := NIL
  5572. END;
  5573. (* determine operator kind *)
  5574. IF IsStrictlyPointerToRecord(leftType) THEN
  5575. kind := {LhsIsPointer}
  5576. ELSE
  5577. kind := {}
  5578. END;
  5579. IF IsStrictlyPointerToRecord(rightType) THEN
  5580. kind := kind + {RhsIsPointer}
  5581. END;
  5582. IF kind # {} THEN (* TODO: to be removed later on *)
  5583. (* at least one of the types is a pointer to record *)
  5584. (* emit a code that registers this specific operator in the runtime *)
  5585. dump := operatorInitializationCodeSection.comments;
  5586. (* IF dump # NIL THEN dump.String("*** begin of operator registration ***"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5587. IF GetRuntimeProcedure("FoxOperatorRuntime", "RegisterOperator", runtimeProcedure, TRUE) THEN
  5588. (* push ID *)
  5589. (* IF dump # NIL THEN dump.String("push ID"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5590. id := Global.GetSymbol(module.module.case, operator.name);
  5591. operatorInitializationCodeSection.Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), id)));
  5592. (* push kind *)
  5593. (* IF dump # NIL THEN dump.String("push kind"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5594. operatorInitializationCodeSection.Emit(Push(position,IntermediateCode.Immediate(setType, SYSTEM.VAL(LONGINT, kind))));
  5595. (* push type infos *)
  5596. PushTypeInfo(leftType);
  5597. PushTypeInfo(rightType);
  5598. (* push operator address *)
  5599. (* IF dump # NIL THEN dump.String("push operator address"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5600. StaticCallOperand(operatorOperand, operator);
  5601. operatorInitializationCodeSection.Emit(Push(position,operatorOperand.op));
  5602. ReleaseOperand(operatorOperand);
  5603. StaticCallOperand(runtimeProcedureOperand, runtimeProcedure);
  5604. operatorInitializationCodeSection.Emit(Call(position,runtimeProcedureOperand.op, ProcedureParametersSize(system, runtimeProcedure)));
  5605. ReleaseOperand(runtimeProcedureOperand)
  5606. END
  5607. (* IF dump # NIL THEN dump.String("*** end of operator registration ***"); dump.Ln; dump.Update END *) (* TENTATIVE *)
  5608. END
  5609. END RegisterDynamicOperator;
  5610. PROCEDURE SystemTrace(x: SyntaxTree.ExpressionList; pos: Position);
  5611. VAR
  5612. traceModule: SyntaxTree.Module;
  5613. procedure: SyntaxTree.Procedure;
  5614. procedureVariable: SyntaxTree.Variable;
  5615. s,msg: Basic.MessageString;
  5616. res: Operand;
  5617. i: LONGINT;
  5618. sv: SyntaxTree.StringValue;
  5619. type: SyntaxTree.Type;
  5620. recordType: SyntaxTree.RecordType;
  5621. printout: Printout.Printer;
  5622. stringWriter: Streams.StringWriter;
  5623. expression: SyntaxTree.Expression;
  5624. PROCEDURE GetProcedure(CONST procedureName: ARRAY OF CHAR): BOOLEAN;
  5625. BEGIN
  5626. procedure := traceModule.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  5627. IF procedure = NIL THEN
  5628. procedureVariable := traceModule.moduleScope.FindVariable(SyntaxTree.NewIdentifier(procedureName));
  5629. END;
  5630. IF (procedure = NIL) & (procedureVariable = NIL) THEN
  5631. s := "procedure ";
  5632. Strings.Append(s,backend.traceModuleName);
  5633. Strings.Append(s,".");
  5634. Strings.Append(s,procedureName);
  5635. Strings.Append(s," not present");
  5636. Error(position,s);
  5637. RETURN FALSE
  5638. ELSE
  5639. RETURN TRUE
  5640. END;
  5641. END GetProcedure;
  5642. PROCEDURE CallProcedure;
  5643. VAR size: LONGINT;
  5644. BEGIN
  5645. IF procedure # NIL THEN
  5646. StaticCallOperand(result,procedure);
  5647. size := ProcedureParametersSize(system,procedure);
  5648. ELSE
  5649. Symbol(procedureVariable, result);
  5650. LoadValue(result, procedureVariable.type.resolved);
  5651. size := ParametersSize(system, procedureVariable.type.resolved(SyntaxTree.ProcedureType), FALSE);
  5652. END;
  5653. Emit(Call(position,result.op,size));
  5654. END CallProcedure;
  5655. PROCEDURE String(CONST s: ARRAY OF CHAR);
  5656. VAR res: Operand; string: SyntaxTree.String;
  5657. BEGIN
  5658. IF GetProcedure("String") THEN
  5659. NEW(string, LEN(s)); COPY(s, string^);
  5660. sv := SyntaxTree.NewStringValue(Basic.invalidPosition,string);
  5661. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,Strings.Length(s));
  5662. sv.SetType(type);
  5663. Designate(sv,res);
  5664. Emit(Push(position,res.tag));
  5665. Emit(Push(position,res.op));
  5666. ReleaseOperand(res);
  5667. CallProcedure;
  5668. END;
  5669. END String;
  5670. PROCEDURE Integer(op: IntermediateCode.Operand);
  5671. BEGIN
  5672. IF GetProcedure("Int") THEN
  5673. Emit(Push(position,op));
  5674. Emit(Push(position,IntermediateCode.Immediate(int32,1)));
  5675. CallProcedure;
  5676. END;
  5677. END Integer;
  5678. PROCEDURE Float(op: IntermediateCode.Operand);
  5679. BEGIN
  5680. IF GetProcedure("HIntHex") THEN
  5681. Emit(Push(position,op));
  5682. Emit(Push(position,IntermediateCode.Immediate(int32,16)));
  5683. CallProcedure;
  5684. END;
  5685. END Float;
  5686. PROCEDURE Set(op: IntermediateCode.Operand);
  5687. BEGIN
  5688. IF GetProcedure("Set") THEN
  5689. Emit(Push(position,op));
  5690. (*
  5691. Emit(Push(position,IntermediateCode.Immediate(int32,0))); (* ofs *)
  5692. Emit(Push(position,IntermediateCode.Immediate(int32,32))); (* n *)
  5693. *)
  5694. CallProcedure;
  5695. END;
  5696. END Set;
  5697. PROCEDURE Boolean(op: IntermediateCode.Operand);
  5698. BEGIN
  5699. IF GetProcedure("Boolean") THEN
  5700. Emit(Push(position,op));
  5701. CallProcedure;
  5702. END;
  5703. END Boolean;
  5704. PROCEDURE Char(op: IntermediateCode.Operand);
  5705. BEGIN
  5706. IF GetProcedure("Char") THEN
  5707. Emit(Push(position,op));
  5708. CallProcedure;
  5709. END;
  5710. END Char;
  5711. PROCEDURE Address(op: IntermediateCode.Operand);
  5712. BEGIN
  5713. IF GetProcedure("Address") THEN
  5714. Emit(Push(position,op));
  5715. CallProcedure;
  5716. END;
  5717. END Address;
  5718. PROCEDURE StringOperand(op: Operand; type: SyntaxTree.Type);
  5719. VAR len: IntermediateCode.Operand;
  5720. BEGIN
  5721. IF GetProcedure("String") THEN
  5722. len := GetArrayLength(type, op.tag);
  5723. Emit(Push(position,len));
  5724. ReleaseIntermediateOperand(len);
  5725. Emit(Push(position,op.op));
  5726. CallProcedure;
  5727. END;
  5728. END StringOperand;
  5729. PROCEDURE Ln;
  5730. BEGIN
  5731. IF GetProcedure("Ln") THEN
  5732. CallProcedure;
  5733. END;
  5734. END Ln;
  5735. BEGIN
  5736. IF backend.traceModuleName = "" THEN RETURN END;
  5737. IF AddImport(backend.traceModuleName,traceModule,TRUE) THEN
  5738. IF GetProcedure("Enter") THEN
  5739. CallProcedure
  5740. END;
  5741. NEW(stringWriter,LEN(s));
  5742. FOR i := 0 TO x.Length()-1 DO
  5743. msg := "";
  5744. expression := x.GetExpression(i);
  5745. IF currentScope IS SyntaxTree.ProcedureScope THEN
  5746. Global.GetSymbolName(currentScope(SyntaxTree.ProcedureScope).ownerProcedure, s)
  5747. ELSE
  5748. Global.GetModuleName(module.module, s);
  5749. END;
  5750. IF i = 0 THEN
  5751. stringWriter.String(s); stringWriter.String("@"); stringWriter.Int(pos.start,1);
  5752. stringWriter.String(":");
  5753. END;
  5754. printout := Printout.NewPrinter(stringWriter,Printout.SourceCode,FALSE);
  5755. IF ~(expression IS SyntaxTree.StringValue) THEN
  5756. printout.Expression(expression);
  5757. stringWriter.Get(s);
  5758. Strings.Append(msg,s);
  5759. Strings.Append(msg,"= ");
  5760. ELSE stringWriter.Get(s); (* remove from string writer *)
  5761. Strings.Append(msg, s);
  5762. END;
  5763. String(msg);
  5764. IF SemanticChecker.IsStringType(expression.type) THEN
  5765. Designate(expression,res);
  5766. StringOperand(res, expression.type);
  5767. ELSE
  5768. Evaluate(expression,res);
  5769. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  5770. IF res.op.type.sizeInBits < IntermediateCode.Bits64 THEN
  5771. Convert(res.op,int64);
  5772. END;
  5773. Integer(res.op);
  5774. ELSIF expression.type.resolved IS SyntaxTree.BooleanType THEN
  5775. Boolean(res.op);
  5776. ELSIF expression.type.resolved IS SyntaxTree.SetType THEN
  5777. Set(res.op);
  5778. ELSIF expression.type.resolved IS SyntaxTree.FloatType THEN
  5779. IF res.op.type.sizeInBits = IntermediateCode.Bits32 THEN
  5780. Convert(res.op,float64);
  5781. END;
  5782. Float(res.op);
  5783. ELSIF (expression.type.resolved IS SyntaxTree.CharacterType) & (expression.type.resolved.sizeInBits = 8) THEN
  5784. Char(res.op);
  5785. ELSIF expression.type.resolved IS SyntaxTree.AddressType THEN
  5786. Address(res.op);String("H");
  5787. ELSIF expression.type.resolved IS SyntaxTree.SizeType THEN
  5788. Address(res.op);String("H");
  5789. ELSIF (expression.type.resolved IS SyntaxTree.PointerType) OR IsPointerToRecord(expression.type,recordType) THEN
  5790. Address(res.op);String("H");
  5791. ELSIF (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  5792. Address(res.op);String("H");
  5793. ELSIF expression.type.resolved IS SyntaxTree.NilType THEN
  5794. String("NIL");
  5795. ELSE HALT(200);
  5796. END;
  5797. END;
  5798. ReleaseOperand(res);
  5799. String("; ");
  5800. END;
  5801. IF GetProcedure("Exit") THEN
  5802. CallProcedure
  5803. ELSE
  5804. Ln;
  5805. END;
  5806. END;
  5807. END SystemTrace;
  5808. PROCEDURE InitFields(type: SyntaxTree.Type; CONST adr: IntermediateCode.Operand; offset: LONGINT);
  5809. VAR baseType: SyntaxTree.Type; imm,mem: IntermediateCode.Operand; dim,size: LONGINT;
  5810. variable: SyntaxTree.Variable; i: LONGINT; initializerOp: Operand;
  5811. BEGIN
  5812. type := type.resolved;
  5813. IF type IS SyntaxTree.RecordType THEN
  5814. WITH type: SyntaxTree.RecordType DO
  5815. baseType := type.baseType;
  5816. IF baseType # NIL THEN
  5817. baseType := baseType.resolved;
  5818. IF baseType IS SyntaxTree.PointerType THEN baseType := baseType(SyntaxTree.PointerType).pointerBase END;
  5819. InitFields(baseType,adr,offset);
  5820. END;
  5821. variable := type.recordScope.firstVariable;
  5822. WHILE variable # NIL DO
  5823. IF variable.initializer # NIL THEN
  5824. Evaluate(variable.initializer,initializerOp);
  5825. MakeMemory(mem,adr,IntermediateCode.GetType(system,variable.type),offset+ ToMemoryUnits(system,variable.offsetInBits));
  5826. Emit(Mov(position,mem,initializerOp.op));
  5827. ReleaseOperand(initializerOp);
  5828. ReleaseIntermediateOperand(mem);
  5829. END;
  5830. InitFields(variable.type, adr, offset+ ToMemoryUnits(system,variable.offsetInBits));
  5831. variable := variable.nextVariable
  5832. END;
  5833. END;
  5834. ELSIF (type IS SyntaxTree.ArrayType) THEN
  5835. WITH type: SyntaxTree.ArrayType DO
  5836. IF type.form = SyntaxTree.Static THEN
  5837. baseType := type.arrayBase;
  5838. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  5839. FOR i := 0 TO type.staticLength-1 DO
  5840. InitFields(baseType,adr,offset+i*size);
  5841. END;
  5842. END;
  5843. END;
  5844. ELSIF type IS SyntaxTree.MathArrayType THEN
  5845. WITH type: SyntaxTree.MathArrayType DO
  5846. IF type.form = SyntaxTree.Open THEN
  5847. dim := DynamicDim(type);
  5848. imm := IntermediateCode.Immediate(addressType,dim);
  5849. PutMathArrayFieldOffset(adr,imm,MathDimOffset,offset);
  5850. baseType := SemanticChecker.ArrayBase(type,dim);
  5851. IF baseType = NIL THEN size := 0 ELSE size := system.AlignedSizeOf(baseType) END;
  5852. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,size));
  5853. PutMathArrayFieldOffset(adr,imm,MathElementSizeOffset,offset);
  5854. ReleaseIntermediateOperand(imm);
  5855. (* flags remain empty (=0) for open array *)
  5856. ELSIF type.form = SyntaxTree.Static THEN
  5857. baseType := type.arrayBase;
  5858. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  5859. ASSERT(type.staticLength < 1024*1024*1024);
  5860. FOR i := 0 TO type.staticLength-1 DO
  5861. InitFields(baseType,adr,offset+i*size);
  5862. END;
  5863. END;
  5864. END;
  5865. END;
  5866. END InitFields;
  5867. PROCEDURE InitVariable(VAR variable: SyntaxTree.Variable);
  5868. VAR type: SyntaxTree.Type; operand: Operand; tmp: IntermediateCode.Operand; reference: SyntaxTree.Expression;
  5869. BEGIN
  5870. type := variable.type.resolved;
  5871. IF (type IS SyntaxTree.MathArrayType) THEN
  5872. WITH type: SyntaxTree.MathArrayType DO
  5873. IF type.form = SyntaxTree.Open THEN
  5874. Symbol(variable,operand);
  5875. InitFields(type, operand.tag,0);
  5876. ELSIF type.form = SyntaxTree.Tensor THEN
  5877. Symbol(variable, operand);
  5878. MakeMemory(tmp,operand.op,addressType,0);
  5879. ReleaseOperand(operand);
  5880. Emit(Mov(position,tmp, nil ) );
  5881. ReleaseIntermediateOperand(tmp);
  5882. END;
  5883. END;
  5884. ELSE
  5885. Symbol(variable,operand);
  5886. IF variable.initializer # NIL THEN
  5887. reference := SyntaxTree.NewSymbolDesignator(variable.initializer.position,NIL,variable);
  5888. reference.SetType(variable.type.resolved);
  5889. reference.SetAssignable(TRUE);
  5890. Assign(reference,variable.initializer);
  5891. END;
  5892. InitFields(type, operand.op,0);
  5893. ReleaseOperand(operand);
  5894. END;
  5895. END InitVariable;
  5896. PROCEDURE MathArrayDim(type: SyntaxTree.MathArrayType; CONST base: IntermediateCode.Operand; VAR result: Operand);
  5897. VAR end: Label;
  5898. BEGIN
  5899. IF type.form = SyntaxTree.Tensor THEN
  5900. InitOperand(result,ModeValue);
  5901. ReuseCopy(result.op,base);
  5902. end := NewLabel();
  5903. BreqL(end,result.op,IntermediateCode.Immediate(addressType,0));
  5904. Emit(MovReplace(position,result.op,IntermediateCode.Memory(addressType,result.op,ToMemoryUnits(system,MathDimOffset*addressType.sizeInBits))));
  5905. SetLabel(end);
  5906. Convert(result.op,int32);
  5907. ELSE
  5908. InitOperand(result,ModeValue);
  5909. IntermediateCode.InitImmediate(result.op, int32, SemanticChecker.Dimension(type,{SyntaxTree.Open, SyntaxTree.Static}));
  5910. END
  5911. END MathArrayDim;
  5912. PROCEDURE PutMathArrayField(base,value: IntermediateCode.Operand; fieldOffset: LONGINT);
  5913. VAR mem: IntermediateCode.Operand; offset: LONGINT;
  5914. BEGIN
  5915. offset := ToMemoryUnits(system,fieldOffset*addressType.sizeInBits);
  5916. MakeMemory(mem,base,addressType,offset);
  5917. Emit(Mov(position,mem,value));
  5918. ReleaseIntermediateOperand(mem);
  5919. END PutMathArrayField;
  5920. PROCEDURE PutMathArrayFieldOffset(base,value: IntermediateCode.Operand; fieldOffset, offset: LONGINT);
  5921. VAR mem: IntermediateCode.Operand;
  5922. BEGIN
  5923. offset := offset + ToMemoryUnits(system,fieldOffset*addressType.sizeInBits);
  5924. MakeMemory(mem,base,addressType,offset);
  5925. Emit(Mov(position,mem,value));
  5926. ReleaseIntermediateOperand(mem);
  5927. END PutMathArrayFieldOffset;
  5928. PROCEDURE GetMathArrayField(VAR value: IntermediateCode.Operand; base: IntermediateCode.Operand; offset: LONGINT);
  5929. BEGIN
  5930. offset := ToMemoryUnits(system,offset*addressType.sizeInBits);
  5931. MakeMemory(value,base,addressType,offset);
  5932. END GetMathArrayField;
  5933. PROCEDURE PutMathArrayLenOrIncr(CONST base,value,dim: IntermediateCode.Operand; incr: BOOLEAN);
  5934. VAR offset: LONGINT; reg,mem: IntermediateCode.Operand;
  5935. BEGIN
  5936. IF incr THEN
  5937. offset := ToMemoryUnits(system,MathIncrOffset*addressType.sizeInBits);
  5938. ELSE
  5939. offset := ToMemoryUnits(system,MathLenOffset*addressType.sizeInBits);
  5940. END;
  5941. IF dim.mode=IntermediateCode.ModeImmediate THEN
  5942. PutMathArrayField(base,value,offset + ToMemoryUnits(system,SHORT(dim.intValue) * 2 * addressType.sizeInBits));
  5943. ELSE
  5944. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  5945. Emit(Mov(position,reg,dim));
  5946. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,2*addressType.sizeInBits))));
  5947. Emit(Add(position,reg,reg,base));
  5948. MakeMemory(mem, reg, addressType, offset);
  5949. ReleaseIntermediateOperand(reg);
  5950. Emit(Mov(position,mem,value));
  5951. ReleaseIntermediateOperand(mem);
  5952. END;
  5953. END PutMathArrayLenOrIncr;
  5954. PROCEDURE PutMathArrayLength(base,value: IntermediateCode.Operand; dim: LONGINT);
  5955. BEGIN
  5956. PutMathArrayField(base,value,MathLenOffset + dim * 2);
  5957. END PutMathArrayLength;
  5958. PROCEDURE PutMathArrayIncrement(base,value: IntermediateCode.Operand; dim: LONGINT);
  5959. BEGIN
  5960. PutMathArrayField(base,value,MathIncrOffset + dim * 2);
  5961. END PutMathArrayIncrement;
  5962. PROCEDURE GetMathArrayIncrement(type: SyntaxTree.MathArrayType; CONST operand: Operand; VAR dim: IntermediateCode.Operand; check: BOOLEAN; VAR result: Operand);
  5963. BEGIN
  5964. MathArrayLenOrIncr(type,operand,dim,TRUE,check,result);
  5965. END GetMathArrayIncrement;
  5966. PROCEDURE GetMathArrayLength(type: SyntaxTree.MathArrayType; CONST operand: Operand; VAR dim: IntermediateCode.Operand; check: BOOLEAN; VAR result: Operand);
  5967. BEGIN
  5968. MathArrayLenOrIncr(type,operand,dim,FALSE,check,result);
  5969. END GetMathArrayLength;
  5970. PROCEDURE GetMathArrayLengthAt(type: SyntaxTree.MathArrayType; CONST operand: Operand; dim: LONGINT; check: BOOLEAN; VAR result: Operand);
  5971. VAR dimOp: IntermediateCode.Operand;
  5972. BEGIN
  5973. dimOp := IntermediateCode.Immediate(int32, dim);
  5974. GetMathArrayLength(type, operand, dimOp, check, result);
  5975. END GetMathArrayLengthAt;
  5976. PROCEDURE GetMathArrayIncrementAt(type: SyntaxTree.MathArrayType; CONST operand: Operand; dim: LONGINT; check: BOOLEAN; VAR result: Operand);
  5977. VAR dimOp: IntermediateCode.Operand;
  5978. BEGIN
  5979. dimOp := IntermediateCode.Immediate(int32, dim);
  5980. GetMathArrayIncrement(type, operand, dimOp, check, result);
  5981. END GetMathArrayIncrementAt;
  5982. PROCEDURE MathArrayLenOrIncr(type: SyntaxTree.MathArrayType; CONST operand: Operand; VAR dim: IntermediateCode.Operand; increment: BOOLEAN; check: BOOLEAN; VAR result: Operand );
  5983. VAR val: LONGINT; res,res2: IntermediateCode.Operand; end,next: Label; t: SyntaxTree.Type; imm: IntermediateCode.Operand; hasDynamicPart: BOOLEAN;
  5984. offset: LONGINT;
  5985. BEGIN
  5986. IF increment THEN
  5987. offset := MathIncrOffset;
  5988. ELSE
  5989. offset := MathLenOffset;
  5990. END;
  5991. INC(offset,operand.dimOffset*2);
  5992. IF check & (type.form = SyntaxTree.Tensor) & ~isUnchecked THEN
  5993. TrapC(BrneL,operand.tag,IntermediateCode.Immediate(addressType,0),IndexCheckTrap);
  5994. END;
  5995. (* static dimension *)
  5996. IF dim.mode = IntermediateCode.ModeImmediate THEN
  5997. IF check & (type.form = SyntaxTree.Tensor) THEN
  5998. DimensionCheck(operand.tag,dim,BrltL);
  5999. END;
  6000. val := SHORT(dim.intValue);
  6001. IF type.form # SyntaxTree.Tensor THEN
  6002. t := SemanticChecker.ArrayBase(type,val);
  6003. type := t.resolved(SyntaxTree.MathArrayType);
  6004. IF type.form = SyntaxTree.Static THEN
  6005. IF increment THEN
  6006. res := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,type.staticIncrementInBits));
  6007. ELSE
  6008. res := IntermediateCode.Immediate(addressType,type.staticLength);
  6009. END;
  6010. InitOperand(result,ModeValue);
  6011. result.op := res;
  6012. RETURN;
  6013. END;
  6014. END;
  6015. offset := ToMemoryUnits(system, (val*2+offset)*addressType.sizeInBits);
  6016. MakeMemory(res,operand.tag,addressType,offset);
  6017. (*
  6018. res := IntermediateCode.Memory(addressType,operand.tag,offset);
  6019. *)
  6020. InitOperand(result,ModeValue);
  6021. result.op := res;
  6022. ELSE
  6023. Convert(dim,addressType);
  6024. IF check THEN
  6025. IF type.form = SyntaxTree.Tensor THEN
  6026. DimensionCheck(operand.tag,dim,BrltL);
  6027. ELSIF isUnchecked THEN (* do nothing *)
  6028. ELSE
  6029. TrapC(BrltL,dim,IntermediateCode.Immediate(addressType,SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static})), IndexCheckTrap);
  6030. END;
  6031. END;
  6032. end := NewLabel(); next := NIL;
  6033. IntermediateCode.InitRegister(res,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6034. Emit(Mov(position,res,dim));
  6035. Convert(res,int32);
  6036. t := type; val := operand.dimOffset; hasDynamicPart := FALSE;
  6037. WHILE t IS SyntaxTree.MathArrayType DO
  6038. type := t(SyntaxTree.MathArrayType);
  6039. IF type.form = SyntaxTree.Static THEN
  6040. imm := IntermediateCode.Immediate(int32,val);
  6041. next := NewLabel();
  6042. BrneL(next,imm,res);
  6043. IF increment THEN
  6044. imm := IntermediateCode.Immediate(int32,ToMemoryUnits(system,type.staticIncrementInBits));
  6045. ELSE
  6046. imm := IntermediateCode.Immediate(int32,type.staticLength);
  6047. END;
  6048. Emit(MovReplace(position,res,imm));
  6049. BrL(end);
  6050. ELSE hasDynamicPart := TRUE;
  6051. END;
  6052. t := type.arrayBase.resolved;
  6053. val := val + 1;
  6054. IF next # NIL THEN SetLabel(next) END;
  6055. END;
  6056. IF hasDynamicPart THEN
  6057. IntermediateCode.InitRegister(res2,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6058. Emit(Mov(position,res2,dim));
  6059. Emit(Mul(position,res2,res2,IntermediateCode.Immediate(addressType,2*ToMemoryUnits(system,addressType.sizeInBits))));
  6060. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,offset*addressType.sizeInBits));
  6061. Emit(Add(position,res2,res2,imm));
  6062. Emit(Add(position,res2,res2,operand.tag));
  6063. IntermediateCode.MakeMemory(res2,int32);
  6064. Emit(MovReplace(position,res,res2));
  6065. ReleaseIntermediateOperand(res2);
  6066. END;
  6067. SetLabel(end);
  6068. Convert(res,int32);
  6069. InitOperand(result,ModeValue);
  6070. result.op := res;
  6071. END;
  6072. END MathArrayLenOrIncr;
  6073. PROCEDURE ArrayLen(type: SyntaxTree.ArrayType; VAR operand: Operand; VAR dim: IntermediateCode.Operand; VAR result: Operand );
  6074. VAR val: LONGINT; res,res2: IntermediateCode.Operand; end,next: Label; t: SyntaxTree.Type; imm: IntermediateCode.Operand; hasDynamicPart: BOOLEAN;
  6075. offset: LONGINT;
  6076. BEGIN
  6077. offset := operand.dimOffset+DynamicDim(type)-1;
  6078. IF dim.mode = IntermediateCode.ModeImmediate THEN
  6079. ASSERT(type.form IN {SyntaxTree.Open, SyntaxTree.SemiDynamic});
  6080. val := SHORT(dim.intValue);
  6081. t := SemanticChecker.ArrayBase(type,val);
  6082. type := t.resolved(SyntaxTree.ArrayType);
  6083. IF type.form = SyntaxTree.Static THEN
  6084. res := IntermediateCode.Immediate(addressType,type.staticLength);
  6085. ELSE
  6086. offset := ToMemoryUnits(system, (offset-val)*addressType.sizeInBits);
  6087. res := IntermediateCode.Memory(addressType,operand.tag,offset);
  6088. END;
  6089. UseIntermediateOperand(res);
  6090. InitOperand(result,ModeValue);
  6091. result.op := res;
  6092. ELSE
  6093. Convert(dim,addressType);
  6094. IF ~isUnchecked THEN
  6095. TrapC(BrltL,dim,IntermediateCode.Immediate(addressType,SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static})), IndexCheckTrap);
  6096. END;
  6097. end := NewLabel(); next := NIL;
  6098. (* ReuseCopy(dim,res); *)
  6099. IntermediateCode.InitRegister(res,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType, IntermediateCode.GeneralPurposeRegister));
  6100. Emit(Mov(position,res,dim));
  6101. Convert(res,int32);
  6102. Convert(res,int32);
  6103. t := type; val := operand.dimOffset; hasDynamicPart := FALSE;
  6104. WHILE t IS SyntaxTree.ArrayType DO
  6105. type := t(SyntaxTree.ArrayType);
  6106. IF type.form = SyntaxTree.Static THEN
  6107. imm := IntermediateCode.Immediate(int32,val);
  6108. next := NewLabel();
  6109. BrneL(next,imm,res);
  6110. imm := IntermediateCode.Immediate(int32,type.staticLength);
  6111. Emit(MovReplace(position,res,imm));
  6112. BrL(end);
  6113. ELSE hasDynamicPart := TRUE;
  6114. END;
  6115. t := type.arrayBase.resolved;
  6116. val := val + 1;
  6117. IF next # NIL THEN SetLabel(next) END;
  6118. END;
  6119. IF hasDynamicPart THEN
  6120. ReuseCopy(res2,dim); (* dim is now in register res2 *)
  6121. Convert(res2,addressType);
  6122. Emit(Mul(position,res2,res2,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,addressType.sizeInBits))));
  6123. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,offset*addressType.sizeInBits));
  6124. Emit(Sub(position,res2,imm,res2));
  6125. Emit(Add(position,res2,res2,operand.tag));
  6126. IntermediateCode.MakeMemory(res2,int32);
  6127. Emit(MovReplace(position,res,res2));
  6128. ReleaseIntermediateOperand(res2);
  6129. END;
  6130. SetLabel(end);
  6131. Convert(res,int32);
  6132. InitOperand(result,ModeValue);
  6133. result.op := res;
  6134. END;
  6135. END ArrayLen;
  6136. (**
  6137. create a temporary variable in current scope
  6138. **)
  6139. PROCEDURE GetTemporaryVariable(type: SyntaxTree.Type; register: BOOLEAN): SyntaxTree.Variable;
  6140. VAR name: SyntaxTree.Identifier; string: SyntaxTree.IdentifierString ; variable: SyntaxTree.Variable;
  6141. scope: SyntaxTree.Scope; duplicate: BOOLEAN; offset, index: LONGINT;
  6142. BEGIN
  6143. IF ~register THEN
  6144. variable := temporaries.GetFreeVariable(type, index);
  6145. ELSE
  6146. index := temporaries.registerIndex; INC(temporaries.registerIndex);
  6147. END;
  6148. scope := currentScope;
  6149. IF variable = NIL THEN
  6150. COPY("@hiddenIRVar",string);
  6151. Basic.AppendNumber(string,index);
  6152. name := SyntaxTree.NewIdentifier(string);
  6153. variable := SyntaxTree.NewVariable(Basic.invalidPosition,name);
  6154. variable.SetType(type);
  6155. variable.SetAccess(SyntaxTree.Hidden);
  6156. IF ~register THEN
  6157. temporaries.AddVariable(variable);
  6158. IF scope.lastVariable # NIL THEN
  6159. offset := scope.lastVariable.offsetInBits;
  6160. ELSE
  6161. offset := 0;
  6162. END;
  6163. DEC(offset,system.SizeOf(variable.type));
  6164. Basic.Align(offset,-system.AlignmentOf(system.variableAlignment,variable.type));
  6165. variable(SyntaxTree.Variable).SetOffset(offset);
  6166. scope.AddVariable(variable(SyntaxTree.Variable));
  6167. scope.EnterSymbol(variable, duplicate);
  6168. ASSERT(~duplicate);
  6169. InitVariable(variable(SyntaxTree.Variable));
  6170. ELSE
  6171. variable.SetUseRegister(TRUE);
  6172. variable(SyntaxTree.Variable).SetOffset(0);
  6173. END;
  6174. ELSE
  6175. InitVariable(variable(SyntaxTree.Variable));
  6176. (*
  6177. ASSERT(variable.type.resolved = type.resolved)
  6178. *)
  6179. END;
  6180. RETURN variable(SyntaxTree.Variable)
  6181. END GetTemporaryVariable;
  6182. PROCEDURE GetMathArrayDescriptorType(dimensions: LONGINT): SyntaxTree.Type;
  6183. VAR name: ARRAY 32 OF CHAR; symbol: SyntaxTree.Symbol; typeDeclaration: SyntaxTree.TypeDeclaration;
  6184. recordType: SyntaxTree.RecordType; type: SyntaxTree.Type;
  6185. recordScope: SyntaxTree.RecordScope; parentScope: SyntaxTree.Scope; identifier: SyntaxTree.Identifier;
  6186. i: LONGINT; duplicate: BOOLEAN;
  6187. PROCEDURE AddVariable(CONST name: ARRAY OF CHAR; type: SyntaxTree.Type);
  6188. VAR variable: SyntaxTree.Variable;
  6189. BEGIN
  6190. variable := SyntaxTree.NewVariable(Basic.invalidPosition,SyntaxTree.NewIdentifier(name));
  6191. variable.SetType(type);
  6192. recordScope.AddVariable(variable);
  6193. END AddVariable;
  6194. BEGIN
  6195. name := "@ArrayDescriptor";
  6196. Basic.AppendNumber(name,dimensions);
  6197. identifier := SyntaxTree.NewIdentifier(name);
  6198. parentScope := module.module.moduleScope;
  6199. symbol := parentScope.FindSymbol(identifier);
  6200. IF symbol # NIL THEN
  6201. typeDeclaration := symbol(SyntaxTree.TypeDeclaration);
  6202. type := typeDeclaration.declaredType;
  6203. ELSE
  6204. typeDeclaration := SyntaxTree.NewTypeDeclaration(Basic.invalidPosition,SyntaxTree.NewIdentifier(name));
  6205. typeDeclaration.SetAccess(SyntaxTree.Hidden);
  6206. recordScope := SyntaxTree.NewRecordScope(parentScope);
  6207. recordType := SyntaxTree.NewRecordType( Basic.invalidPosition, parentScope, recordScope);
  6208. recordType.SetTypeDeclaration(typeDeclaration);
  6209. recordType.SetState(SyntaxTree.Resolved);
  6210. typeDeclaration.SetDeclaredType(recordType);
  6211. AddVariable("@ptr",system.anyType);
  6212. AddVariable("@adr",system.addressType);
  6213. AddVariable("@flags",system.addressType);
  6214. AddVariable("@dim",system.addressType);
  6215. AddVariable("@elementSize",system.addressType);
  6216. FOR i := 0 TO dimensions-1 DO
  6217. name := "@len";
  6218. Basic.AppendNumber(name,i);
  6219. AddVariable(name,system.addressType);
  6220. name := "@incr";
  6221. Basic.AppendNumber(name,i);
  6222. AddVariable(name,system.addressType);
  6223. END;
  6224. parentScope.AddTypeDeclaration(typeDeclaration);
  6225. parentScope.EnterSymbol(typeDeclaration,duplicate);
  6226. ASSERT(~duplicate);
  6227. type := recordType;
  6228. END;
  6229. RETURN type
  6230. END GetMathArrayDescriptorType;
  6231. PROCEDURE NewMathArrayDescriptor(op: Operand; dimensions: LONGINT);
  6232. VAR reg: IntermediateCode.Operand; type: SyntaxTree.Type;
  6233. BEGIN
  6234. type := GetMathArrayDescriptorType(dimensions);
  6235. Emit(Push(position,op.op));
  6236. (* push type descriptor *)
  6237. reg := TypeDescriptorAdr(type);
  6238. IF ~newObjectFile THEN
  6239. IntermediateCode.MakeMemory(reg,addressType);
  6240. END;
  6241. Emit(Push(position,reg));
  6242. ReleaseIntermediateOperand(reg);
  6243. (* push realtime flag: false by default *)
  6244. Emit(Push(position,false));
  6245. CallThis(position,"Heaps","NewRec",3);
  6246. END NewMathArrayDescriptor;
  6247. PROCEDURE PushConstString(CONST s: ARRAY OF CHAR);
  6248. VAR res: Operand; string: SyntaxTree.String; sv: SyntaxTree.StringValue; type: SyntaxTree.Type;
  6249. BEGIN
  6250. NEW(string, LEN(s)); COPY(s, string^);
  6251. sv := SyntaxTree.NewStringValue(Basic.invalidPosition,string);
  6252. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,Strings.Length(s));
  6253. sv.SetType(type);
  6254. Designate(sv,res);
  6255. Emit(Push(position,res.tag));
  6256. Emit(Push(position,res.op));
  6257. ReleaseOperand(res);
  6258. END PushConstString;
  6259. PROCEDURE PushConstBoolean(b: BOOLEAN);
  6260. BEGIN
  6261. IF b THEN
  6262. Emit(Push(Basic.invalidPosition, true));
  6263. ELSE
  6264. Emit(Push(Basic.invalidPosition, false));
  6265. END;
  6266. END PushConstBoolean;
  6267. PROCEDURE PushConstSet(v: SET);
  6268. VAR value: SyntaxTree.Value; op: Operand;
  6269. BEGIN
  6270. value := SyntaxTree.NewSetValue(Basic.invalidPosition, v);
  6271. value.SetType(system.setType);
  6272. Evaluate(value, op);
  6273. Emit(Push(Basic.invalidPosition, op.op));
  6274. ReleaseOperand(op);
  6275. END PushConstSet;
  6276. PROCEDURE PushConstInteger(v: LONGINT);
  6277. VAR value: SyntaxTree.Value; op: Operand;
  6278. BEGIN
  6279. value := SyntaxTree.NewIntegerValue(Basic.invalidPosition, v);
  6280. value.SetType(system.longintType);
  6281. Evaluate(value, op);
  6282. Emit(Push(Basic.invalidPosition, op.op));
  6283. ReleaseOperand(op);
  6284. END PushConstInteger;
  6285. PROCEDURE OpenInitializer(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope): IntermediateCode.Section;
  6286. VAR name: Basic.SegmentedName; procedure: SyntaxTree.Procedure; procedureScope: SyntaxTree.ProcedureScope;
  6287. section: IntermediateCode.Section;
  6288. BEGIN
  6289. procedureScope := SyntaxTree.NewProcedureScope(scope);
  6290. Global.GetSymbolSegmentedName(symbol, name);
  6291. Basic.SuffixSegmentedName(name, Basic.MakeString("@Initializer"));
  6292. procedure := SyntaxTree.NewProcedure(Basic.invalidPosition, SyntaxTree.NewIdentifier(""), procedureScope);
  6293. procedure.SetScope(moduleScope);
  6294. procedure.SetType(SyntaxTree.NewProcedureType(Basic.invalidPosition,scope));
  6295. procedure.type(SyntaxTree.ProcedureType).SetDelegate(TRUE);
  6296. procedure.SetAccess(SyntaxTree.Hidden);
  6297. currentScope := procedureScope;
  6298. section := NewSection(module.allSections, Sections.CodeSection, name, NIL,commentPrintout # NIL);
  6299. EmitEnter(section, Basic.invalidPosition,procedure,0,0,0);
  6300. RETURN section;
  6301. END OpenInitializer;
  6302. PROCEDURE CloseInitializer(prev: IntermediateCode.Section);
  6303. BEGIN
  6304. EmitLeave(section, Basic.invalidPosition, NIL, 0 );
  6305. Emit(Exit(Basic.invalidPosition,0 ,0, 0));
  6306. section := prev;
  6307. END CloseInitializer;
  6308. PROCEDURE AddPorts(cell: SyntaxTree.Symbol; x: SyntaxTree.CellType);
  6309. VAR name: SyntaxTree.IdentifierString;
  6310. variable: SyntaxTree.Variable;
  6311. parameter: SyntaxTree.Parameter;
  6312. type: SyntaxTree.Type;
  6313. PROCEDURE Field(symbol: SyntaxTree.Symbol; VAR op: Operand);
  6314. BEGIN
  6315. InitOperand(op,ModeReference);
  6316. op.op := fp;
  6317. IntermediateCode.AddOffset(op.op,ToMemoryUnits(system,2*addressType.sizeInBits));
  6318. Dereference(op, x, FALSE);
  6319. result := op;
  6320. Symbol(symbol, op);
  6321. END Field;
  6322. PROCEDURE Direction(direction: LONGINT): SET;
  6323. BEGIN
  6324. IF direction = SyntaxTree.OutPort THEN RETURN {0}
  6325. ELSIF direction = SyntaxTree.InPort THEN RETURN {1}
  6326. ELSE HALT(100);
  6327. END;
  6328. END Direction;
  6329. PROCEDURE AddPortProperty(port: SyntaxTree.Parameter; modifier: SyntaxTree.Modifier; value: SyntaxTree.Expression);
  6330. VAR name: ARRAY 256 OF CHAR; op: Operand;
  6331. BEGIN
  6332. Field(port, op);
  6333. ToMemory(op.op,addressType,0);
  6334. Emit(Push(Basic.invalidPosition, op.op));
  6335. ReleaseOperand(op);
  6336. Basic.GetString(modifier.identifier, name);
  6337. PushConstString(name);
  6338. IF SemanticChecker.IsStringType(modifier.expression.type) THEN
  6339. ASSERT(SemanticChecker.IsStringType(value.type));
  6340. Designate(value, op);
  6341. Emit(Push(modifier.position, op.tag));
  6342. Emit(Push(modifier.position, op.op));
  6343. ReleaseOperand(op);
  6344. CallThis(position,"ActiveCellsRuntime","AddPortStringProperty",5);
  6345. ELSIF (modifier.expression.type.resolved IS SyntaxTree.IntegerType) THEN
  6346. ASSERT(value.type.resolved IS SyntaxTree.IntegerType);
  6347. Evaluate(value, op);
  6348. Emit(Push(modifier.position, op.op));
  6349. ReleaseOperand(op);
  6350. CallThis(position,"ActiveCellsRuntime","AddPortIntegerProperty",4);
  6351. ELSE
  6352. CallThis(position,"ActiveCellsRuntime","AddPortFlagProperty",3);
  6353. END;
  6354. END AddPortProperty;
  6355. PROCEDURE AddPortProperties(parameter: SyntaxTree.Parameter);
  6356. VAR modifier: SyntaxTree.Modifier;
  6357. BEGIN
  6358. modifier := parameter.modifiers;
  6359. WHILE modifier # NIL DO
  6360. AddPortProperty(parameter,modifier, modifier.expression);
  6361. modifier := modifier.nextModifier;
  6362. END;
  6363. END AddPortProperties;
  6364. PROCEDURE Parameter(name: ARRAY OF CHAR; parameter: SyntaxTree.Parameter);
  6365. VAR op : Operand; portType: SyntaxTree.PortType; baseType: SyntaxTree.Type;
  6366. size, reg: IntermediateCode.Operand; dim, len: LONGINT;
  6367. PROCEDURE PushLens(type: SyntaxTree.Type);
  6368. BEGIN
  6369. IF IsSemiDynamicArray(type) THEN
  6370. PushLens(type(SyntaxTree.ArrayType).arrayBase.resolved);
  6371. Evaluate(type(SyntaxTree.ArrayType).length, op);
  6372. Emit(Push(Basic.invalidPosition, op.op));
  6373. ReleaseOperand(op);
  6374. INC(dim);
  6375. ELSIF IsStaticArray(type) THEN
  6376. len := len * type(SyntaxTree.ArrayType).staticLength;
  6377. PushLens(type(SyntaxTree.ArrayType).arrayBase.resolved);
  6378. INC(dim);
  6379. ELSE
  6380. baseType := type;
  6381. END;
  6382. END PushLens;
  6383. BEGIN
  6384. (* cell *)
  6385. IF parameter.type IS SyntaxTree.ArrayType THEN
  6386. type := parameter.type;
  6387. dim := 0;
  6388. len := 1;
  6389. PushLens(type);
  6390. portType := baseType.resolved(SyntaxTree.PortType);
  6391. ELSE
  6392. portType := parameter.type(SyntaxTree.PortType);
  6393. END;
  6394. PushSelfPointer();
  6395. (* port / array of ports *)
  6396. IF IsStaticArray(type) THEN
  6397. PushConstInteger(len);
  6398. END;
  6399. Field(parameter, op);
  6400. (*left := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,left,cell); left.SetType(system.anyType);
  6401. left := SyntaxTree.NewDereferenceDesignator(Basic.invalidPosition, left); left.SetType(x);
  6402. d := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, left, parameter); d.SetType(parameter.type);
  6403. Designate(d, op);*)
  6404. Emit(Push(Basic.invalidPosition, op.op));
  6405. ReleaseOperand(op);
  6406. (* name *)
  6407. PushConstString(name);
  6408. (* inout *)
  6409. PushConstSet(Direction(portType.direction));
  6410. (* width *)
  6411. PushConstInteger(portType.sizeInBits);
  6412. IF parameter.type IS SyntaxTree.PortType THEN
  6413. CallThis(parameter.position,"ActiveCellsRuntime","AddPort",6);
  6414. AddPortProperties(parameter);
  6415. ELSIF IsStaticArray(type)THEN
  6416. CallThis(parameter.position,"ActiveCellsRuntime","AddStaticPortArray",7);
  6417. ELSIF IsSemiDynamicArray(type) THEN
  6418. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6419. size := IntermediateCode.Immediate(addressType, ToMemoryUnits(system,6*addressType.sizeInBits));
  6420. Emit(Add(position,reg, sp, size));
  6421. (* dim *)
  6422. PushConstInteger(dim);
  6423. (* len array *)
  6424. Emit(Push(position, reg));
  6425. ReleaseIntermediateOperand(reg);
  6426. CallThis(position,"ActiveCellsRuntime","AddPortArray",8);
  6427. size := IntermediateCode.Immediate(addressType, ToMemoryUnits(system,dim*addressType.sizeInBits));
  6428. Emit(Add(position, sp,sp, size));
  6429. ELSE
  6430. HALT(100);
  6431. END;
  6432. END Parameter;
  6433. BEGIN
  6434. IF backend.cellsAreObjects THEN
  6435. IF (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.CellType) THEN
  6436. AddPorts(cell, x.baseType.resolved(SyntaxTree.CellType));
  6437. END;
  6438. parameter := x.firstParameter;
  6439. WHILE (parameter # NIL) DO
  6440. type := parameter.type.resolved;
  6441. WHILE (type IS SyntaxTree.ArrayType) DO
  6442. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  6443. END;
  6444. IF (type IS SyntaxTree.PortType) THEN (* port found *)
  6445. Global.GetSymbolNameInScope(parameter,x.cellScope,name);
  6446. Parameter(name,parameter);
  6447. END;
  6448. parameter := parameter.nextParameter;
  6449. END;
  6450. ELSE HALT(200)
  6451. END;
  6452. END AddPorts;
  6453. PROCEDURE AddProperty(cellType: SyntaxTree.CellType; cell: SyntaxTree.Symbol; property: SyntaxTree.Property; value: SyntaxTree.Expression);
  6454. VAR name: ARRAY 256 OF CHAR; op: Operand; left, d: SyntaxTree.Designator;
  6455. BEGIN
  6456. Symbol(cell,op);
  6457. ToMemory(op.op,addressType,0);
  6458. Emit(Push(position,op.op));
  6459. ReleaseOperand(op);
  6460. property.GetName(name);
  6461. (* does not work when inheritance is used:
  6462. Global.GetSymbolNameInScope(property, cellType.cellScope , name);
  6463. *)
  6464. PushConstString(name);
  6465. IF (value # NIL) THEN
  6466. ASSERT(
  6467. SemanticChecker.IsStringType(property.type)
  6468. OR (property.type.resolved IS SyntaxTree.IntegerType)
  6469. OR (property.type.resolved IS SyntaxTree.FloatType)
  6470. OR (property.type.resolved IS SyntaxTree.BooleanType)
  6471. OR (property.type.resolved IS SyntaxTree.SetType)
  6472. );
  6473. left := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,left,cell); left.SetType(system.anyType);
  6474. left := SyntaxTree.NewDereferenceDesignator(Basic.invalidPosition, left); left.SetType(cellType);
  6475. d := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, left, property); d.SetType(property.type);
  6476. Designate(d, op);
  6477. IF SemanticChecker.IsStringType(property.type) THEN
  6478. Emit(Push(Basic.invalidPosition, op.tag))
  6479. END;
  6480. Emit(Push(Basic.invalidPosition, op.op));
  6481. ReleaseOperand(op);
  6482. END;
  6483. IF value = NIL THEN
  6484. CallThis(position,"ActiveCellsRuntime","AddFlagProperty",3);
  6485. ELSIF SemanticChecker.IsStringType(property.type) THEN
  6486. ASSERT(SemanticChecker.IsStringType(value.type));
  6487. Designate(value, op);
  6488. PushString(op, value.type);
  6489. ReleaseOperand(op);
  6490. CallThis(position,"ActiveCellsRuntime","AddStringProperty",7);
  6491. ELSIF (property.type.resolved IS SyntaxTree.IntegerType) THEN
  6492. ASSERT(value.type.resolved IS SyntaxTree.IntegerType);
  6493. Evaluate(value, op);
  6494. Emit(Push(property.position, op.op));
  6495. ReleaseOperand(op);
  6496. CallThis(position,"ActiveCellsRuntime","AddIntegerProperty",5);
  6497. ELSIF (property.type.resolved IS SyntaxTree.BooleanType) THEN
  6498. ASSERT(value.type.resolved IS SyntaxTree.BooleanType);
  6499. Evaluate(value, op);
  6500. Emit(Push(property.position, op.op));
  6501. ReleaseOperand(op);
  6502. CallThis(position,"ActiveCellsRuntime","AddBooleanProperty",5);
  6503. ELSIF (property.type.resolved IS SyntaxTree.FloatType) THEN
  6504. ASSERT((value.type.resolved IS SyntaxTree.FloatType) & (value.type.resolved(SyntaxTree.FloatType).sizeInBits =64));
  6505. Evaluate(value, op);
  6506. Emit(Push(property.position, op.op));
  6507. ReleaseOperand(op);
  6508. CallThis(position,"ActiveCellsRuntime","AddRealProperty",-1); (* must import *)
  6509. ELSIF (property.type.resolved IS SyntaxTree.SetType) THEN
  6510. ASSERT((value.type.resolved IS SyntaxTree.SetType));
  6511. Evaluate(value, op);
  6512. Emit(Push(property.position, op.op));
  6513. ReleaseOperand(op);
  6514. CallThis(position,"ActiveCellsRuntime","AddSetProperty",-1); (* must import *)
  6515. ELSE
  6516. HALT(200);
  6517. END;
  6518. END AddProperty;
  6519. PROCEDURE AddModifiers(cellType: SyntaxTree.CellType; cell: SyntaxTree.Symbol; modifier: SyntaxTree.Modifier);
  6520. VAR symbol: SyntaxTree.Symbol;
  6521. BEGIN
  6522. WHILE modifier # NIL DO
  6523. symbol := cellType.FindProperty(modifier.identifier);
  6524. ASSERT ((symbol # NIL) & (symbol IS SyntaxTree.Property));
  6525. AddProperty(cellType, cell, symbol(SyntaxTree.Property), modifier.expression);
  6526. modifier := modifier.nextModifier;
  6527. END;
  6528. END AddModifiers;
  6529. PROCEDURE AppendModifier(VAR to: SyntaxTree.Modifier; this: SyntaxTree.Modifier);
  6530. VAR last: SyntaxTree.Modifier;
  6531. BEGIN
  6532. IF to = NIL THEN
  6533. to := SyntaxTree.NewModifier(this.position, this.identifier, this.expression);
  6534. ELSE
  6535. last := to;
  6536. WHILE (last.nextModifier # NIL) & (this.identifier # last.identifier) DO
  6537. last := last.nextModifier;
  6538. END;
  6539. IF last.identifier # this.identifier THEN
  6540. ASSERT(last.nextModifier = NIL);
  6541. last.SetNext(SyntaxTree.NewModifier(this.position, this.identifier, this.expression));
  6542. END;
  6543. END;
  6544. END AppendModifier;
  6545. PROCEDURE AppendModifiers(VAR to: SyntaxTree.Modifier; this: SyntaxTree.Modifier);
  6546. BEGIN
  6547. WHILE this # NIL DO
  6548. AppendModifier(to, this);
  6549. this := this.nextModifier;
  6550. END;
  6551. END AppendModifiers;
  6552. PROCEDURE AppendCellTypeModifiers(VAR to: SyntaxTree.Modifier; c: SyntaxTree.CellType);
  6553. VAR base: SyntaxTree.Type;
  6554. BEGIN
  6555. AppendModifiers(to, c.modifiers);
  6556. base := c.GetBaseValueType();
  6557. IF (base # NIL) & (base IS SyntaxTree.CellType) THEN
  6558. AppendCellTypeModifiers(to, base(SyntaxTree.CellType))
  6559. END;
  6560. END AppendCellTypeModifiers;
  6561. PROCEDURE AddPortProperty(modifier: SyntaxTree.Modifier; value: SyntaxTree.Expression);
  6562. VAR name: ARRAY 256 OF CHAR; op: Operand;
  6563. BEGIN
  6564. Basic.GetString(modifier.identifier, name);
  6565. PushConstString(name);
  6566. IF SemanticChecker.IsStringType(modifier.expression.type) THEN
  6567. ASSERT(SemanticChecker.IsStringType(value.type));
  6568. Designate(value, op);
  6569. PushString(op, value.type);
  6570. ReleaseOperand(op);
  6571. CallThisChecked(position,"ActiveCellsRuntime","AddPortStringProperty",4,FALSE);
  6572. ELSIF (modifier.expression.type.resolved IS SyntaxTree.IntegerType) THEN
  6573. ASSERT(value.type.resolved IS SyntaxTree.IntegerType);
  6574. Evaluate(value, op);
  6575. Emit(Push(modifier.position, op.op));
  6576. ReleaseOperand(op);
  6577. CallThisChecked(position,"ActiveCellsRuntime","AddPortIntegerProperty",3,FALSE);
  6578. ELSE
  6579. CallThisChecked(position,"ActiveCellsRuntime","AddPortFlagProperty",2,FALSE);
  6580. END;
  6581. END AddPortProperty;
  6582. PROCEDURE AddPortProperties(modifier: SyntaxTree.Modifier);
  6583. BEGIN
  6584. WHILE modifier # NIL DO
  6585. AddPortProperty(modifier, modifier.expression);
  6586. modifier := modifier.nextModifier;
  6587. END;
  6588. END AddPortProperties;
  6589. PROCEDURE PushPort(p: SyntaxTree.Expression);
  6590. VAR op: Operand;
  6591. BEGIN
  6592. Evaluate(p, op);
  6593. Emit(Push(p.position, op.op));
  6594. ReleaseOperand(op);
  6595. IF p IS SyntaxTree.Designator THEN
  6596. AddPortProperties(p(SyntaxTree.Designator).modifiers)
  6597. END;
  6598. END PushPort;
  6599. PROCEDURE PushString(op: Operand; actualType: SyntaxTree.Type);
  6600. VAR tmp: IntermediateCode.Operand;
  6601. BEGIN
  6602. actualType := actualType.resolved;
  6603. IF actualType IS SyntaxTree.StringType THEN
  6604. Emit(Push(position,IntermediateCode.Immediate(addressType,actualType(SyntaxTree.StringType).length)));
  6605. ELSIF actualType(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  6606. Emit(Push(position,IntermediateCode.Immediate(addressType,actualType(SyntaxTree.ArrayType).staticLength)));
  6607. ELSE
  6608. tmp := op.tag;
  6609. IntermediateCode.MakeMemory(tmp,addressType);
  6610. Emit(Push(position,tmp));
  6611. END;
  6612. Emit(Push(position,op.op))
  6613. END PushString;
  6614. PROCEDURE VisitBuiltinCallDesignator(x: SyntaxTree.BuiltinCallDesignator);
  6615. VAR
  6616. p0,p1,p2,parameter: SyntaxTree.Expression; len,val: LONGINT; l,r: Operand; res,adr,reg: IntermediateCode.Operand; type, componentType: SyntaxTree.Type;
  6617. constructor: SyntaxTree.Procedure; s0,s1,s2: Operand; hint: HUGEINT;
  6618. i: LONGINT; formalParameter: SyntaxTree.Parameter;
  6619. tmp:IntermediateCode.Operand;
  6620. size: LONGINT; dim,openDim: LONGINT; pointer: IntermediateCode.Operand; t,t0,t1,t2: SyntaxTree.Type; trueL,falseL,ignore: Label;
  6621. exit,else,end: Label; procedureType: SyntaxTree.ProcedureType;
  6622. name: Basic.SegmentedName; symbol: Sections.Section; operand: Operand;
  6623. dest: IntermediateCode.Operand;
  6624. staticLength: LONGINT; itype: IntermediateCode.Type;
  6625. convert,isTensor: BOOLEAN;
  6626. recordType: SyntaxTree.RecordType;
  6627. baseType: SyntaxTree.Type;
  6628. flags: SET;
  6629. left: SyntaxTree.Expression;
  6630. call: SyntaxTree.Designator;
  6631. procedure: SyntaxTree.Procedure;
  6632. temporaryVariable: SyntaxTree.Variable;
  6633. dummy: IntermediateCode.Operand;
  6634. customBuiltin: SyntaxTree.CustomBuiltin;
  6635. isVarPar: ARRAY 3 OF BOOLEAN;
  6636. callsection: Sections.Section;
  6637. segmentedName: Basic.SegmentedName;
  6638. needsTrace: BOOLEAN;
  6639. n: ARRAY 256 OF CHAR;
  6640. modifier: SyntaxTree.Modifier;
  6641. previous, init: IntermediateCode.Section;
  6642. prevScope: SyntaxTree.Scope;
  6643. firstPar: LONGINT;
  6644. PROCEDURE CallBodies(self: IntermediateCode.Operand; type: SyntaxTree.Type);
  6645. VAR recordScope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure; body: SyntaxTree.Body; flags: LONGINT;
  6646. priority: IntermediateCode.Operand;
  6647. op,callop: Operand;
  6648. BEGIN
  6649. IF type = NIL THEN RETURN END;
  6650. type := type.resolved;
  6651. IF type IS SyntaxTree.PointerType THEN
  6652. type := type(SyntaxTree.PointerType).pointerBase.resolved
  6653. END;
  6654. IF type IS SyntaxTree.MathArrayType THEN RETURN END;
  6655. CallBodies(self,type(SyntaxTree.RecordType).baseType);
  6656. recordScope := type(SyntaxTree.RecordType).recordScope;
  6657. IF recordScope.bodyProcedure # NIL THEN
  6658. procedure := recordScope.bodyProcedure;
  6659. body := procedure.procedureScope.body;
  6660. Emit(Push(position,self));
  6661. IF body.isActive THEN
  6662. StaticCallOperand(callop,procedure);
  6663. Emit(Push(position,callop.op));
  6664. IF body.priority # NIL THEN Evaluate(body.priority,op); priority := op.op;
  6665. Convert(priority,sizeType);
  6666. ELSE priority := IntermediateCode.Immediate(sizeType,0)
  6667. END;
  6668. Emit(Push(position,priority));
  6669. ReleaseIntermediateOperand(priority);
  6670. IF backend.cooperative THEN
  6671. Emit(Push(position,self));
  6672. CallThis(position,"Activities","Create",3)
  6673. ELSE
  6674. flags := 0;
  6675. IF body.isSafe THEN
  6676. flags := 1;
  6677. END;
  6678. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.setType),flags)));
  6679. Emit(Push(position,self));
  6680. CallThis(position,"Objects","CreateProcess",4)
  6681. END;
  6682. ELSE
  6683. Emit(Push(position,self));
  6684. StaticCallOperand(callop,procedure);
  6685. Emit(Call(position,callop.op,ProcedureParametersSize(system,procedure)));
  6686. END;
  6687. Emit(Pop(position,self));
  6688. END;
  6689. END CallBodies;
  6690. PROCEDURE PushTD(type: SyntaxTree.Type);
  6691. VAR op: IntermediateCode.Operand;
  6692. BEGIN
  6693. IF type = NIL THEN Emit(Push(position,IntermediateCode.Immediate(addressType,0)))
  6694. ELSIF type.resolved IS SyntaxTree.AnyType THEN Emit(Push(position,IntermediateCode.Immediate(addressType,1)))
  6695. ELSE
  6696. IF type.resolved IS SyntaxTree.PointerType THEN
  6697. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  6698. END;
  6699. op := TypeDescriptorAdr(type.resolved);
  6700. IF ~newObjectFile THEN
  6701. IntermediateCode.MakeMemory(op,addressType);
  6702. END;
  6703. Emit(Push(position,op));
  6704. END
  6705. END PushTD;
  6706. BEGIN
  6707. IF Trace THEN TraceEnter("VisitBuiltinCallDesignator") END;
  6708. dest := destination; destination := emptyOperand;
  6709. p0 := NIL; p1 := NIL; p2 := NIL; len := x.parameters.Length();
  6710. IF len > 0 THEN p0 := x.parameters.GetExpression(0); t0 := p0.type.resolved END;
  6711. IF len > 1 THEN p1 := x.parameters.GetExpression(1); t1 := p1.type.resolved END;
  6712. IF len > 2 THEN p2 := x.parameters.GetExpression(2); t2 := p2.type.resolved END;
  6713. CASE x.id OF
  6714. (* ---- COPY ----- *)
  6715. |Global.Copy:
  6716. CopyString(p1,p0); (* roles exchanged: COPY ( src => dest ) *)
  6717. (* ---- EXCL, INCL----- *)
  6718. |Global.Excl,Global.Incl:
  6719. Evaluate(p0,s0);
  6720. Evaluate(p1,s1);
  6721. Convert(s1.op,setType);
  6722. IF (s1.op.mode # IntermediateCode.ModeImmediate) & ~isUnchecked THEN
  6723. TrapC(BrltL,s1.op,IntermediateCode.Immediate(setType,setType.sizeInBits),IndexCheckTrap);
  6724. END;
  6725. ReuseCopy(res,s0.op);
  6726. ReleaseOperand(s0);
  6727. Reuse1(tmp,s1.op);
  6728. ReleaseOperand(s1);
  6729. Emit(Shl(position,tmp,IntermediateCode.Immediate(setType,1),s1.op));
  6730. IF x.id = Global.Excl THEN
  6731. Emit(Not(position,tmp,tmp));
  6732. Emit(And(position,res,res,tmp));
  6733. ELSE
  6734. Emit(Or(position,res,res,tmp));
  6735. END;
  6736. ReleaseIntermediateOperand(tmp);
  6737. Designate(p0,s0);
  6738. ToMemory(s0.op,setType,0);
  6739. Emit(Mov(position,s0.op,res));
  6740. ReleaseOperand(s0); ReleaseIntermediateOperand(res);
  6741. (* ---- DISPOSE ----- *)
  6742. |Global.Dispose:
  6743. Designate(p0,s0);
  6744. Emit(Push(position,s0.op));
  6745. ReleaseOperand(s0);
  6746. CallThis(position,DefaultRuntimeModuleName,"Dispose", 1);
  6747. (* ---- GETPROCEDURE ----- *)
  6748. |Global.GetProcedure:
  6749. Designate(p0,s0);
  6750. PushString(s0,p0.type);
  6751. Designate(p1,s1);
  6752. PushString(s1,p1.type);
  6753. procedureType := p2.type.resolved(SyntaxTree.ProcedureType);
  6754. IF (procedureType.firstParameter = NIL) OR (procedureType.firstParameter.access = SyntaxTree.Hidden) THEN PushTD(NIL)
  6755. ELSE PushTD(procedureType.firstParameter.type)
  6756. END;
  6757. PushTD(procedureType.returnType);
  6758. Designate(p2,s2);
  6759. Emit(Push(position,s2.op));
  6760. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  6761. CallThis(position,"Modules","GetProcedure", 7);
  6762. (* ---- ASH, LSH, ROT ----- *)
  6763. |Global.Ash, Global.Asr, Global.Lsh, Global.Rot, Global.Ror:
  6764. Evaluate(p0,s0);
  6765. IF (x.id = Global.Lsh) OR (x.id = Global.Rot) OR (x.id = Global.Ror)THEN
  6766. (* make unsigned arguments in order to produced a logical shift *)
  6767. IF s0.op.type.form = IntermediateCode.SignedInteger THEN
  6768. convert:= TRUE;
  6769. itype := s0.op.type;
  6770. IntermediateCode.InitType(itype,IntermediateCode.UnsignedInteger,s0.op.type.sizeInBits);
  6771. Convert(s0.op,itype);
  6772. ELSE
  6773. convert := FALSE;
  6774. END;
  6775. END;
  6776. Evaluate(p1,s1);
  6777. IF IsIntegerConstant(p1,hint) THEN
  6778. ReuseCopy(reg,s0.op);
  6779. IF hint > 0 THEN
  6780. IntermediateCode.InitImmediate(s1.op,s1.op.type,hint);
  6781. IF x.id = Global.Ash THEN Emit(Shl(position,reg,s0.op,s1.op))
  6782. ELSIF x.id = Global.Lsh THEN Emit(Shl(position,reg,s0.op,s1.op))
  6783. ELSIF x.id = Global.Rot THEN Emit(Rol(position,reg,s0.op,s1.op))
  6784. ELSIF x.id = Global.Ror THEN Emit(Ror(position,reg,s0.op,s1.op))
  6785. ELSIF x.id = Global.Asr THEN Emit(Shr(position,reg,s0.op,s1.op))
  6786. END;
  6787. ELSIF hint < 0 THEN
  6788. IntermediateCode.InitImmediate(s1.op,s1.op.type,-hint);
  6789. IF x.id = Global.Ash THEN Emit(Shr(position,reg,s0.op,s1.op));
  6790. ELSIF x.id = Global.Lsh THEN Emit(Shr(position,reg,s0.op,s1.op));
  6791. ELSIF x.id = Global.Rot THEN Emit(Ror(position,reg,s0.op,s1.op));
  6792. ELSIF x.id = Global.Ror THEN Emit(Rol(position,reg,s0.op,s1.op))
  6793. ELSIF x.id = Global.Asr THEN Emit(Shl(position,reg,s0.op,s1.op))
  6794. END;
  6795. END;
  6796. ReleaseOperand(s0); ReleaseOperand(s1);
  6797. ELSE
  6798. exit := NewLabel();
  6799. end := NewLabel();
  6800. ReuseCopy(reg,s0.op);
  6801. BrgeL(exit,s1.op,IntermediateCode.Immediate(IntermediateCode.GetType(system,p1.type),0));
  6802. Reuse1(tmp,s1.op);
  6803. Emit(Neg(position,tmp,s1.op));
  6804. Convert(tmp,s1.op.type);
  6805. IF x.id = Global.Ash THEN Emit(Shr(position,reg,reg,tmp))
  6806. ELSIF x.id = Global.Lsh THEN Emit(Shr(position,reg,reg,tmp))
  6807. ELSIF x.id = Global.Rot THEN Emit(Ror(position,reg,reg,tmp))
  6808. ELSIF x.id = Global.Ror THEN Emit(Rol(position,reg,reg,tmp))
  6809. ELSIF x.id = Global.Asr THEN Emit(Shl(position,reg,reg,tmp))
  6810. END;
  6811. ReleaseIntermediateOperand(tmp);
  6812. BrL(end);
  6813. SetLabel(exit);
  6814. ReuseCopy(tmp,s1.op);
  6815. Convert(tmp,s1.op.type);
  6816. IF x.id = Global.Ash THEN Emit(Shl(position,reg,reg,tmp))
  6817. ELSIF x.id = Global.Lsh THEN Emit(Shl(position,reg,reg,tmp))
  6818. ELSIF x.id = Global.Rot THEN Emit(Rol(position,reg,reg,tmp))
  6819. ELSIF x.id = Global.Ror THEN Emit(Ror(position,reg,reg,tmp))
  6820. ELSIF x.id = Global.Asr THEN Emit(Shr(position,reg,reg,tmp))
  6821. END;
  6822. ReleaseIntermediateOperand(tmp);
  6823. SetLabel(end);
  6824. ReleaseOperand(s0); ReleaseOperand(s1);
  6825. END;
  6826. InitOperand(result,ModeValue);
  6827. IF convert THEN
  6828. itype := reg.type;
  6829. IntermediateCode.InitType(itype,IntermediateCode.SignedInteger,reg.type.sizeInBits);
  6830. Convert(reg,itype);
  6831. END;
  6832. result.op := reg;
  6833. (* ---- CAP ----- *)
  6834. |Global.Cap:
  6835. Evaluate(p0,result);
  6836. ReuseCopy(reg,result.op);
  6837. ReleaseIntermediateOperand(result.op);
  6838. ignore := NewLabel();
  6839. BrltL(ignore, reg,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.characterType),ORD("a")));
  6840. BrltL(ignore,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.characterType),ORD("z")),reg);
  6841. Emit(And(position,reg,reg,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.characterType),5FH)));
  6842. SetLabel(ignore);
  6843. result.op := reg;
  6844. (* ---- CHR ----- *)
  6845. |Global.Chr, Global.Chr32:
  6846. Evaluate(p0,result);
  6847. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6848. |Global.Entier, Global.EntierH:
  6849. Evaluate(p0,result);
  6850. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6851. (* ---- MIN and MAX ----- *)
  6852. |Global.Max,Global.Min:
  6853. Evaluate(p0,s0);
  6854. Evaluate(p1,s1);
  6855. Reuse2(res,s0.op,s1.op);
  6856. else := NewLabel();
  6857. IF x.id = Global.Max THEN BrltL(else,s0.op,s1.op);
  6858. ELSE BrltL(else,s1.op,s0.op) END;
  6859. Emit(Mov(position,res,s0.op));
  6860. ReleaseOperand(s0);
  6861. end := NewLabel();
  6862. BrL(end);
  6863. SetLabel(else);
  6864. Emit(MovReplace(position,res,s1.op));
  6865. SetLabel(end);
  6866. ReleaseOperand(s1);
  6867. InitOperand(result,ModeValue);
  6868. result.op := res;
  6869. (* ---- ODD ----- *)
  6870. |Global.Odd:
  6871. IF ~conditional THEN
  6872. ConditionToValue(x)
  6873. ELSE
  6874. Evaluate(p0,result);
  6875. res := IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1);
  6876. Reuse1(res,result.op);
  6877. Emit(And(position,res,result.op,IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1)));
  6878. ReleaseIntermediateOperand(result.op);
  6879. result.op := res;
  6880. BreqL(trueLabel,IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1),result.op);
  6881. ReleaseOperand(result);
  6882. BrL(falseLabel);
  6883. END;
  6884. (* ---- ORD ----- *)
  6885. |Global.Ord, Global.Ord32:
  6886. Evaluate(p0,result);
  6887. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6888. (* ---- SHORT, LONG ----- *)
  6889. |Global.Short, Global.Long:
  6890. Evaluate(p0,result);
  6891. IF x.type IS SyntaxTree.ComplexType THEN
  6892. componentType := x.type(SyntaxTree.ComplexType).componentType;
  6893. Convert(result.op, IntermediateCode.GetType(system, componentType));
  6894. Convert(result.tag, IntermediateCode.GetType(system, componentType));
  6895. ELSE
  6896. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6897. END
  6898. (* ---- HALT, SYSTEM.HALT----- *)
  6899. |Global.Halt, Global.systemHalt:
  6900. val := p0.resolved(SyntaxTree.IntegerValue).value;
  6901. EmitTrap (position, val);
  6902. (* ---- ASSERT ----- *)
  6903. |Global.Assert:
  6904. IF ~backend.noAsserts & (p0.resolved = NIL) THEN
  6905. trueL := NewLabel();
  6906. falseL := NewLabel();
  6907. Condition(p0,trueL,falseL);
  6908. IF p1 = NIL THEN val := AssertTrap
  6909. ELSE val := p1.resolved(SyntaxTree.IntegerValue).value;
  6910. END;
  6911. SetLabel(falseL);
  6912. EmitTrap(position,val);
  6913. SetLabel(trueL);
  6914. END;
  6915. (*
  6916. Emit(TrapC(result.op,val);
  6917. *)
  6918. (* ---- INC, DEC----- *)
  6919. |Global.Inc,Global.Dec:
  6920. Expression(p0); adr := result.op;
  6921. LoadValue(result,p0.type); l := result;
  6922. IF p1 = NIL THEN r.op := IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1);
  6923. ELSE Expression(p1); LoadValue(result,p1.type); r := result;
  6924. END;
  6925. IF x.id = Global.Inc THEN
  6926. Emit(Add(position,l.op,l.op,r.op));
  6927. ELSE
  6928. Emit(Sub(position,l.op,l.op,r.op));
  6929. END;
  6930. ReleaseOperand(l); ReleaseOperand(r);
  6931. (* ---- LEN ----- *)
  6932. |Global.Len: (* dynamic length, static length done by checker *)
  6933. Designate(p0,operand);
  6934. IF p1 = NIL THEN
  6935. InitOperand(l,ModeValue);
  6936. l.op := IntermediateCode.Immediate(int32,0);
  6937. ELSE
  6938. Evaluate(p1,l);
  6939. END;
  6940. IF p0.type.resolved IS SyntaxTree.ArrayType THEN
  6941. IF (p0.type.resolved(SyntaxTree.ArrayType).form= SyntaxTree.SemiDynamic) THEN
  6942. Dereference(operand, p0.type.resolved, FALSE);
  6943. END;
  6944. ArrayLen(p0.type.resolved(SyntaxTree.ArrayType),operand,l.op, result);
  6945. ReleaseOperand(operand); ReleaseOperand(l);
  6946. ELSIF p0.type.resolved IS SyntaxTree.MathArrayType THEN
  6947. ASSERT(p1 # NIL);
  6948. IF p0.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  6949. Dereference(operand,p0.type.resolved,FALSE);
  6950. END;
  6951. GetMathArrayLength(p0.type.resolved(SyntaxTree.MathArrayType),operand, l.op, TRUE, result);
  6952. ReleaseOperand(operand); ReleaseOperand(l);
  6953. ELSE HALT(100);
  6954. END;
  6955. Convert(result.op,IntermediateCode.GetType(system, x.type));
  6956. (* ---- FIRST ---- *)
  6957. |Global.First:
  6958. IF p0 IS SyntaxTree.RangeExpression THEN (* optimization, could also work with designate *)
  6959. Evaluate(p0(SyntaxTree.RangeExpression).first, result)
  6960. ELSE
  6961. Designate(p0, result)
  6962. END
  6963. (* ---- LAST ---- *)
  6964. |Global.Last:
  6965. IF p0 IS SyntaxTree.RangeExpression THEN (* optimization, could also work with designate *)
  6966. Evaluate(p0(SyntaxTree.RangeExpression).last, result)
  6967. ELSE
  6968. Designate(p0, result);
  6969. (* make sure result.op is a register *)
  6970. tmp := result.op;
  6971. ReuseCopy(result.op, result.op);
  6972. ReleaseIntermediateOperand(tmp);
  6973. (* add offset to result.op *)
  6974. IntermediateCode.AddOffset(result.op, ToMemoryUnits(system, system.SizeOf(system.longintType)))
  6975. END
  6976. (* ---- STEP ---- *)
  6977. |Global.Step:
  6978. IF p0 IS SyntaxTree.RangeExpression THEN (* optimization, could also work with designate *)
  6979. Evaluate(p0(SyntaxTree.RangeExpression).step, result)
  6980. ELSE
  6981. Designate(p0, result);
  6982. (* make sure result.op is a register *)
  6983. tmp := result.op;
  6984. ReuseCopy(result.op, result.op);
  6985. ReleaseIntermediateOperand(tmp);
  6986. (* add offset to result.op *)
  6987. IntermediateCode.AddOffset(result.op, 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)))
  6988. END
  6989. (* ---- RE ---- *)
  6990. |Global.Re:
  6991. IF p0.type.resolved IS SyntaxTree.ComplexType THEN
  6992. Designate(p0, result)
  6993. ELSE
  6994. Evaluate(p0, result)
  6995. END
  6996. (* ---- IM ---- *)
  6997. |Global.Im:
  6998. ASSERT(p0.type.resolved IS SyntaxTree.ComplexType);
  6999. componentType := p0.type.resolved(SyntaxTree.ComplexType).componentType;
  7000. Designate(p0, result);
  7001. (* make sure result.op is a register *)
  7002. tmp := result.op;
  7003. ReuseCopy(result.op, result.op);
  7004. ReleaseIntermediateOperand(tmp);
  7005. (* add offset to result.op *)
  7006. IntermediateCode.AddOffset(result.op, ToMemoryUnits(system, system.SizeOf(componentType)));
  7007. (* ---- ABS ----- *)
  7008. |Global.Abs:
  7009. Evaluate(p0,operand);
  7010. type := p0.type.resolved;
  7011. InitOperand(result,ModeValue);
  7012. Reuse1a(result.op,operand.op,dest);
  7013. Emit(Abs(position,result.op,operand.op));
  7014. ReleaseOperand(operand);
  7015. (* ---- WAIT ----- *)
  7016. |Global.Wait:
  7017. Evaluate(p0,operand);
  7018. Emit(Push(position,operand.op));
  7019. ReleaseOperand(operand);
  7020. CallThis(position,"Activities","Wait", 1);
  7021. (* ---- NEW ----- *)
  7022. |Global.New:
  7023. (*! the following code is only correct for "standard" Oberon calling convention *)
  7024. IF x.type # NIL THEN
  7025. type := x.type.resolved;
  7026. firstPar := 0;
  7027. ELSE
  7028. type := p0.type.resolved;
  7029. firstPar := 1;
  7030. END;
  7031. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  7032. THEN
  7033. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  7034. IF backend.cooperative THEN
  7035. size := ToMemoryUnits(system,system.SizeOf(recordType));
  7036. IF ~type(SyntaxTree.PointerType).isPlain THEN
  7037. IF recordType.isObject THEN
  7038. INC (size, BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  7039. IF recordType.IsActive() THEN
  7040. INC (size, ActionTypeSize * ToMemoryUnits(system,addressType.sizeInBits))
  7041. END;
  7042. IF recordType.IsProtected() THEN
  7043. INC (size, MonitorTypeSize * ToMemoryUnits(system,addressType.sizeInBits))
  7044. END;
  7045. ELSE
  7046. INC (size, BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  7047. END;
  7048. END;
  7049. Emit(Push(position,IntermediateCode.Immediate(sizeType,size)));
  7050. CallThis(position,"Runtime","New", 1);
  7051. pointer := NewRegisterOperand(IntermediateCode.GetType(system, type));
  7052. Emit(Result(position, pointer));
  7053. exit := NewLabel();
  7054. BreqL(exit,pointer,nil);
  7055. IF ~type(SyntaxTree.PointerType).isPlain THEN
  7056. GetRecordTypeName (recordType,name);
  7057. IF ~recordType.isObject THEN
  7058. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  7059. END;
  7060. IntermediateCode.InitAddress(adr, addressType, name , 0, 0);
  7061. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,0),adr));
  7062. IF recordType.isObject THEN
  7063. IF recordType.IsProtected() THEN
  7064. DEC (size, MonitorTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  7065. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,MonitorOffset * ToMemoryUnits(system,addressType.sizeInBits)), IntermediateCode.RegisterOffset(addressType,IntermediateCode.GeneralPurposeRegister,pointer.register,size)));
  7066. END;
  7067. IF recordType.IsActive() THEN
  7068. DEC (size, ActionTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  7069. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ActionOffset * ToMemoryUnits(system,addressType.sizeInBits)), IntermediateCode.RegisterOffset(addressType,IntermediateCode.GeneralPurposeRegister,pointer.register,size)));
  7070. END;
  7071. END;
  7072. END;
  7073. (* initialize fields *)
  7074. IF type(SyntaxTree.PointerType).isPlain THEN
  7075. size := 0;
  7076. ELSIF recordType.isObject THEN
  7077. size := BaseObjectTypeSize;
  7078. ELSE
  7079. size := BaseRecordTypeSize;
  7080. END;
  7081. InitFields(recordType, pointer,size*ToMemoryUnits(system,addressType.sizeInBits));
  7082. (* call initializer *)
  7083. constructor := GetConstructor(recordType);
  7084. IF constructor # NIL THEN
  7085. (*! should be unified with ProcedureCallDesignator *)
  7086. Emit(Push(position,pointer));
  7087. ReleaseIntermediateOperand(pointer);
  7088. formalParameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7089. FOR i := firstPar TO x.parameters.Length()-1 DO
  7090. PushParameter(x.parameters.GetExpression(i), formalParameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  7091. formalParameter := formalParameter.nextParameter;
  7092. END;
  7093. (* static call of the constructor *)
  7094. GetCodeSectionNameForSymbol(constructor,name);
  7095. ASSERT(~constructor.isInline);
  7096. IF constructor.scope.ownerModule # module.module THEN
  7097. symbol := NewSection(module.importedSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  7098. ELSE
  7099. symbol := NewSection(module.allSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  7100. END;
  7101. Emit(Call(position,IntermediateCode.Address(addressType, symbol.name, GetFingerprint(constructor), 0),ProcedureParametersSize(system,constructor) - ToMemoryUnits(system,addressType.sizeInBits)));
  7102. IntermediateCode.InitRegister(pointer,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7103. Emit(Pop(position,pointer));
  7104. END;
  7105. (* call bodies *)
  7106. CallBodies(pointer,type);
  7107. SetLabel(exit);
  7108. needsTrace := p0.NeedsTrace();
  7109. IF needsTrace THEN ModifyAssignments(true) END;
  7110. IF ~type(SyntaxTree.PointerType).isDisposable THEN
  7111. Emit(Push(position, pointer));
  7112. CallThisChecked(position,"GarbageCollector","Watch",0,FALSE);
  7113. Emit(Pop(position, pointer));
  7114. END;
  7115. Designate(p0,l);
  7116. IF needsTrace THEN
  7117. CallAssignPointer(l.op, pointer);
  7118. ELSE
  7119. ToMemory(l.op,addressType,0);
  7120. Emit(Mov(position,l.op,pointer));
  7121. END;
  7122. ReleaseIntermediateOperand(pointer);
  7123. ReleaseOperand(l);
  7124. IF needsTrace THEN ModifyAssignments(false) END;
  7125. ELSE (* not cooperative backend *)
  7126. temporaryVariable := GetTemporaryVariable(type, FALSE);
  7127. IF temporaryVariable # NIL THEN
  7128. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7129. ELSE
  7130. Designate(p0,l);
  7131. END;
  7132. (* l.op contains address of pointer to record *)
  7133. Emit(Push(position,l.op)); (* address for use after syscall *)
  7134. Emit(Push(position,l.op));
  7135. ReleaseOperand(l);
  7136. (* push type descriptor *)
  7137. reg := TypeDescriptorAdr(recordType);
  7138. IF ~newObjectFile THEN
  7139. IntermediateCode.MakeMemory(reg,addressType);
  7140. END;
  7141. Emit(Push(position,reg));
  7142. ReleaseIntermediateOperand(reg);
  7143. (* push realtime flag *)
  7144. IF type.resolved.isRealtime THEN Emit(Push(position,true));
  7145. ELSE Emit(Push(position,false));
  7146. END;
  7147. CallThis(position,"Heaps","NewRec", 3);
  7148. (* check allocation success, if not successful then do not call initializers and bodies *)
  7149. IntermediateCode.InitRegister(pointer,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7150. Emit(Pop(position,pointer));
  7151. MakeMemory(reg,pointer,addressType,0);
  7152. ReleaseIntermediateOperand(pointer);
  7153. pointer := reg;
  7154. exit := NewLabel();
  7155. BreqL(exit,pointer,nil);
  7156. Emit(Push(position,pointer));
  7157. (* initialize fields *)
  7158. InitFields(recordType, pointer,0);
  7159. (* call initializer *)
  7160. constructor := GetConstructor(recordType);
  7161. IF constructor # NIL THEN
  7162. (*! should be unified with ProcedureCallDesignator *)
  7163. Emit(Push(position,pointer));
  7164. ReleaseIntermediateOperand(pointer);
  7165. formalParameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7166. FOR i := firstPar TO x.parameters.Length()-1 DO
  7167. PushParameter(x.parameters.GetExpression(i), formalParameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  7168. formalParameter := formalParameter.nextParameter;
  7169. END;
  7170. (* static call of the constructor *)
  7171. GetCodeSectionNameForSymbol(constructor,name);
  7172. ASSERT(~constructor.isInline);
  7173. IF constructor.scope.ownerModule # module.module THEN
  7174. symbol := NewSection(module.importedSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  7175. ELSE
  7176. symbol := NewSection(module.allSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  7177. END;
  7178. Emit(Call(position,IntermediateCode.Address(addressType, symbol.name, GetFingerprint(constructor), 0),ProcedureParametersSize(system,constructor)));
  7179. ELSE
  7180. ReleaseIntermediateOperand(pointer);
  7181. END;
  7182. IntermediateCode.InitRegister(pointer,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7183. Emit(Pop(position,pointer));
  7184. IF (temporaryVariable # NIL) & (x.type = NIL) THEN
  7185. Designate(p0,l);
  7186. ToMemory(l.op,addressType,0);
  7187. Emit(Mov(position,l.op,pointer));
  7188. ReleaseOperand(l);
  7189. result.tag := emptyOperand;
  7190. ELSIF (x.type # NIL) THEN
  7191. result := l; (* temporary variable is the result of NEW Type() *)
  7192. END;
  7193. (* call bodies *)
  7194. CallBodies(pointer,type);
  7195. ReleaseIntermediateOperand(pointer);
  7196. IF (temporaryVariable # NIL) & (x.type = NIL) THEN
  7197. end := NewLabel();
  7198. BrL(end);
  7199. SetLabel(exit);
  7200. Designate(p0,l);
  7201. ToMemory(l.op,addressType,0);
  7202. Emit(Mov(position,l.op,nil)); (* write NIL to adr *)
  7203. ReleaseOperand(l);
  7204. SetLabel(end);
  7205. ELSE
  7206. SetLabel(exit);
  7207. END;
  7208. END;
  7209. ELSIF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN
  7210. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  7211. dim := 0;
  7212. IntermediateCode.InitOperand(reg);
  7213. IF p1 # NIL THEN
  7214. FOR i := firstPar TO x.parameters.Length()-1 DO
  7215. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  7216. parameter := x.parameters.GetExpression(i);
  7217. Evaluate(parameter,r);
  7218. IF (r.op.mode # IntermediateCode.ModeImmediate) & ~isUnchecked THEN
  7219. IntermediateCode.InitImmediate(tmp,IntermediateCode.GetType(system,parameter.type),0);
  7220. TrapC(BrgeL,r.op,tmp,ArraySizeTrap);
  7221. END;
  7222. Emit(Push(position,r.op));
  7223. IF i=1 THEN
  7224. CopyInt(reg,r.op);
  7225. ELSE
  7226. MulInt(reg, reg, r.op);
  7227. END;
  7228. ReleaseOperand(r);
  7229. INC(dim);
  7230. END;
  7231. Convert(reg,addressType);
  7232. ELSE
  7233. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7234. Emit(Mov(position,reg,IntermediateCode.Immediate(addressType,1)));
  7235. END;
  7236. openDim := dim;
  7237. ASSERT(~(type IS SyntaxTree.ArrayType) OR (type(SyntaxTree.ArrayType).form = SyntaxTree.Static));
  7238. IF backend.cooperative THEN
  7239. size := ToMemoryUnits(system,system.SizeOf(type));
  7240. WHILE type IS SyntaxTree.ArrayType DO
  7241. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  7242. END;
  7243. size := size DIV ToMemoryUnits(system,system.SizeOf(type));
  7244. IF (size # 1) THEN
  7245. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  7246. END;
  7247. Emit(Push(position,reg));
  7248. size := ToMemoryUnits(system,system.SizeOf(type));
  7249. IF (size # 1) THEN
  7250. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  7251. END;
  7252. AddInt(reg, reg, IntermediateCode.Immediate(addressType,ToMemoryUnits(system,(BaseArrayTypeSize + openDim)* system.addressSize)));
  7253. (*Emit(Add(position,reg,reg,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,(BaseArrayTypeSize + openDim)* system.addressSize))));*)
  7254. Emit(Push(position,reg));
  7255. ReleaseIntermediateOperand(reg);
  7256. CallThis(position,"Runtime","New", 1);
  7257. pointer := NewRegisterOperand(IntermediateCode.GetType(system, p0.type));
  7258. Emit(Result(position, pointer));
  7259. exit := NewLabel();
  7260. else := NewLabel();
  7261. BreqL(else,pointer,nil);
  7262. IF ~type.hasPointers THEN
  7263. Basic.ToSegmentedName ("BaseTypes.Array",name);
  7264. ELSIF type IS SyntaxTree.RecordType THEN
  7265. Basic.ToSegmentedName ("BaseTypes.RecordArray",name);
  7266. ELSIF type IS SyntaxTree.ProcedureType THEN
  7267. Basic.ToSegmentedName ("BaseTypes.DelegateArray",name);
  7268. ELSE
  7269. Basic.ToSegmentedName ("BaseTypes.PointerArray",name);
  7270. END;
  7271. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,0),IntermediateCode.Address(addressType,name,0,0)));
  7272. Emit(Pop(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,LengthOffset * system.addressSize))));
  7273. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,DataOffset * system.addressSize)),IntermediateCode.RegisterOffset(addressType,IntermediateCode.GeneralPurposeRegister,pointer.register,ToMemoryUnits(system,(BaseArrayTypeSize + openDim)* system.addressSize))));
  7274. IF type IS SyntaxTree.RecordType THEN
  7275. GetRecordTypeName(type(SyntaxTree.RecordType),name);
  7276. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,DescriptorOffset * system.addressSize)),IntermediateCode.Address(addressType,name,0,0)));
  7277. ELSE
  7278. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,DescriptorOffset * system.addressSize)),nil));
  7279. END;
  7280. i := openDim;
  7281. WHILE i > 0 DO
  7282. DEC (i);
  7283. Emit(Pop(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,(BaseArrayTypeSize + i)* system.addressSize))));
  7284. END;
  7285. needsTrace := p0.NeedsTrace();
  7286. IF needsTrace THEN ModifyAssignments(true) END;
  7287. IF ~p0.type.resolved(SyntaxTree.PointerType).isDisposable THEN
  7288. Emit(Push(position, pointer));
  7289. CallThisChecked(position,"GarbageCollector","Watch",0,FALSE);
  7290. Emit(Pop(position, pointer));
  7291. END;
  7292. Designate(p0,l);
  7293. IF needsTrace THEN
  7294. CallAssignPointer(l.op, pointer);
  7295. ModifyAssignments(false);
  7296. ELSE
  7297. ToMemory(l.op,addressType,0);
  7298. Emit(Mov(position,l.op,pointer));
  7299. END;
  7300. ReleaseIntermediateOperand(pointer);
  7301. ReleaseOperand(l);
  7302. BrL(exit);
  7303. SetLabel(else);
  7304. Emit(Add(position,sp,sp,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,(openDim+1)*system.addressSize))));
  7305. Designate(p0,l);
  7306. IF needsTrace THEN
  7307. CallResetProcedure(l.op,l.tag,p0.type.resolved);
  7308. ELSE
  7309. ToMemory(l.op,addressType,0);
  7310. Emit(Mov(position,l.op,pointer));
  7311. END;
  7312. ReleaseOperand(l);
  7313. SetLabel(exit);
  7314. ELSE
  7315. (*! the following code is only correct for "standard" Oberon calling convention *)
  7316. IF SemanticChecker.ContainsPointer(type) THEN
  7317. IF type IS SyntaxTree.ArrayType THEN
  7318. staticLength := 1;
  7319. WHILE (type IS SyntaxTree.ArrayType) DO (* static array *)
  7320. staticLength := staticLength * type(SyntaxTree.ArrayType).staticLength;
  7321. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  7322. END;
  7323. tmp := IntermediateCode.Immediate(reg.type,staticLength);
  7324. MulInt(reg,reg,tmp);
  7325. END;
  7326. Designate(p0,l);
  7327. IF openDim > 0 THEN
  7328. Emit(Push(position,l.op)); (* address for use after syscall *)
  7329. END;
  7330. Emit(Push(position,l.op)); (* address *)
  7331. ReleaseOperand(l);
  7332. tmp := TypeDescriptorAdr(type);
  7333. IF ~newObjectFile THEN
  7334. IntermediateCode.MakeMemory(tmp,addressType);
  7335. END;
  7336. Emit(Push(position,tmp)); (* type descriptor *)
  7337. ReleaseIntermediateOperand(tmp);
  7338. Emit(Push(position,reg)); (* number Elements *)
  7339. ReleaseIntermediateOperand(reg);
  7340. tmp := IntermediateCode.Immediate(addressType,dim);
  7341. Emit(Push(position,tmp)); (* dimensions *)
  7342. (* push realtime flag *)
  7343. IF (p0.type.resolved.isRealtime) THEN Emit(Push(position,true));
  7344. ELSE Emit(Push(position,false));
  7345. END;
  7346. CallThis(position,"Heaps","NewArr",5)
  7347. ELSE
  7348. size := ToMemoryUnits(system,system.SizeOf(type));
  7349. IF (size # 1) THEN
  7350. MulInt(reg, reg, IntermediateCode.Immediate(addressType,size));
  7351. (*
  7352. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  7353. *)
  7354. END;
  7355. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,ArrayDimTable * system.addressSize+ system.addressSize+ system.addressSize * 2 * (openDim DIV 2)));
  7356. (* DIV 2 term for some strange alignment, don't understand it at the moment - copied from PCC *)
  7357. AddInt(reg, reg, tmp);
  7358. (*
  7359. Emit(Add(position,reg,reg,tmp));
  7360. *)
  7361. Designate(p0,l);
  7362. IF openDim >0 THEN
  7363. Emit(Push(position,l.op)); (* address for use after syscall *)
  7364. END;
  7365. Emit(Push(position,l.op)); (* address for syscall *)
  7366. ReleaseOperand(l); (* pointer address *)
  7367. Emit(Push(position,reg)); (* size *)
  7368. ReleaseIntermediateOperand(reg);
  7369. (* push realtime flag *)
  7370. IF (p0.type.resolved.isRealtime) THEN Emit(Push(position,true));
  7371. ELSE Emit(Push(position,false));
  7372. END;
  7373. CallThis(position,"Heaps","NewSys", 3)
  7374. END;
  7375. IF openDim > 0 THEN
  7376. IntermediateCode.InitRegister(adr,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7377. Emit(Pop(position,adr));
  7378. ToMemory(adr,addressType,0);
  7379. ReuseCopy(tmp,adr);
  7380. ReleaseIntermediateOperand(adr);
  7381. adr := tmp;
  7382. else := NewLabel();
  7383. BreqL(else,adr,IntermediateCode.Immediate(addressType,0));
  7384. i := openDim-1;
  7385. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7386. WHILE (i >= 0) DO
  7387. Emit(Pop(position,reg));
  7388. IntermediateCode.InitMemory(res,addressType,adr,ToMemoryUnits(system,ArrayDimTable* system.addressSize + system.addressSize*((openDim-1)-i)));
  7389. Emit(Mov(position,res,reg));
  7390. DEC(i);
  7391. END;
  7392. ReleaseIntermediateOperand(adr);
  7393. ReleaseIntermediateOperand(reg);
  7394. exit := NewLabel();
  7395. BrL(exit);
  7396. SetLabel(else);
  7397. (* else part: array could not be allocated *)
  7398. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,openDim*system.addressSize));
  7399. Emit(Add(position,sp,sp,tmp));
  7400. SetLabel(exit);
  7401. END;
  7402. END;
  7403. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  7404. IF t1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  7405. IF GetRuntimeProcedure("FoxArrayBase","AllocateTensorX",procedure,TRUE) THEN
  7406. left := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,procedure);
  7407. procedureType := procedure.type(SyntaxTree.ProcedureType);
  7408. left.SetType(procedure.type);
  7409. formalParameter := procedureType.firstParameter;
  7410. (* push array to allocate *)
  7411. PushParameter(p0, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  7412. formalParameter :=formalParameter.nextParameter;
  7413. (* push length array *)
  7414. PushParameter(p1, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  7415. (* push size *)
  7416. type := t0;
  7417. WHILE (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) DO
  7418. type := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  7419. END;
  7420. tmp := IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType),ToMemoryUnits(system,system.SizeOf(type))); (* alignment *)
  7421. Emit(Push(position,tmp));
  7422. (* *)
  7423. IF SemanticChecker.ContainsPointer(type) THEN
  7424. tmp := TypeDescriptorAdr(type);
  7425. IF ~newObjectFile THEN
  7426. IntermediateCode.MakeMemory(tmp,addressType);
  7427. END;
  7428. ELSE
  7429. tmp := IntermediateCode.Immediate(addressType, 0);
  7430. END;
  7431. Emit(Push(position,tmp)); (* type descriptor *)
  7432. StaticCallOperand(result,procedure);
  7433. Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  7434. ReleaseOperand(result);
  7435. END;
  7436. (*
  7437. designator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.ArrayBaseName);
  7438. designator := SyntaxTree.NewSelectorDesignator(InvalidPosition, designator, SyntaxTree.NewIdentifier(InvalidPosition, "AllocateTensorX"));
  7439. result := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, designator, actualParameters));
  7440. *)
  7441. ELSE
  7442. dim := 0;
  7443. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7444. (* generate geometry descriptor *)
  7445. Designate(p0,l);
  7446. NewMathArrayDescriptor(l, x.parameters.Length()-1);
  7447. ReleaseOperand(l);
  7448. isTensor := TRUE;
  7449. ELSE
  7450. isTensor := FALSE;
  7451. END;
  7452. FOR i := firstPar TO x.parameters.Length()-1 DO
  7453. IF ~isTensor THEN
  7454. type := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  7455. END;
  7456. parameter := x.parameters.GetExpression(i);
  7457. Evaluate(parameter,r);
  7458. IF (r.op.mode # IntermediateCode.ModeImmediate) & ~isUnchecked THEN
  7459. IntermediateCode.InitImmediate(tmp,IntermediateCode.GetType(system,parameter.type),0);
  7460. TrapC(BrgeL,r.op,tmp,ArraySizeTrap);
  7461. END;
  7462. Emit(Push(position,r.op));
  7463. IF i=1 THEN
  7464. CopyInt(reg, r.op);
  7465. ELSE
  7466. MulInt(reg, reg, r.op);
  7467. END;
  7468. ReleaseOperand(r);
  7469. INC(dim);
  7470. END;
  7471. Convert(reg,addressType);
  7472. openDim := dim;
  7473. ASSERT(~(type IS SyntaxTree.MathArrayType) OR (type(SyntaxTree.MathArrayType).form IN {SyntaxTree.Static,SyntaxTree.Tensor}));
  7474. (*! the following code is only correct for "standard" Oberon calling convention *)
  7475. IF SemanticChecker.ContainsPointer(SemanticChecker.ArrayBase(type,MAX(LONGINT))) THEN
  7476. t := type;
  7477. IF ~isTensor & (t IS SyntaxTree.MathArrayType) THEN
  7478. staticLength := 1;
  7479. WHILE (t IS SyntaxTree.MathArrayType) DO (* static array *)
  7480. staticLength := staticLength * t(SyntaxTree.MathArrayType).staticLength;
  7481. t := t(SyntaxTree.MathArrayType).arrayBase.resolved;
  7482. END;
  7483. tmp := IntermediateCode.Immediate(reg.type,staticLength);
  7484. MulInt(reg,reg,tmp);
  7485. END;
  7486. Designate(p0,l);
  7487. IF isTensor THEN
  7488. Dereference(l,type,FALSE);
  7489. t := SemanticChecker.ArrayBase(type,MAX(LONGINT));
  7490. END;
  7491. Emit(Push(position,l.tag)); (* address for use after syscall *)
  7492. Emit(Push(position,l.tag)); (* address *)
  7493. ReleaseOperand(l);
  7494. tmp := TypeDescriptorAdr(t);
  7495. IF ~newObjectFile THEN
  7496. IntermediateCode.MakeMemory(tmp,addressType);
  7497. END;
  7498. Emit(Push(position,tmp)); (* type descriptor *)
  7499. ReleaseIntermediateOperand(tmp);
  7500. Emit(Push(position,reg)); (* number Elements *)
  7501. ReleaseIntermediateOperand(reg);
  7502. tmp := IntermediateCode.Immediate(addressType,0);
  7503. Emit(Push(position,tmp)); (* dimensions = 0, we control dimensions in the geometry descriptor *)
  7504. (* push realtime flag: false by default *)
  7505. Emit(Push(position,false));
  7506. CallThis(position,"Heaps","NewArr",5);
  7507. IntermediateCode.InitRegister(adr,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7508. Emit(Pop(position,adr));
  7509. GetMathArrayField(tmp,adr,MathPtrOffset);
  7510. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7511. AddInt(reg, tmp, IntermediateCode.Immediate(addressType,ToMemoryUnits(system,ArrDataArrayOffset)));
  7512. PutMathArrayField(adr,reg,MathAdrOffset);
  7513. ReleaseIntermediateOperand(tmp);
  7514. ReleaseIntermediateOperand(reg);
  7515. ELSE
  7516. IF isTensor THEN
  7517. size := ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,MAX(LONGINT))));
  7518. ELSE
  7519. size := ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,openDim)));
  7520. END;
  7521. IF (size # 1) THEN
  7522. MulInt(reg,reg,IntermediateCode.Immediate(addressType,size)); (*! optimize the multiplication of immediate operands *)
  7523. END;
  7524. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,SysDataArrayOffset));
  7525. AddInt(reg,reg,tmp);
  7526. Designate(p0,l);
  7527. IF isTensor THEN
  7528. Dereference(l,type,FALSE);
  7529. END;
  7530. Emit(Push(position,l.tag)); (* address for use after syscall *)
  7531. Emit(Push(position,l.tag)); (* address for syscall *)
  7532. ReleaseOperand(l); (* pointer address *)
  7533. Emit(Push(position,reg)); (* size *)
  7534. ReleaseIntermediateOperand(reg);
  7535. (* push realtime flag: false by default *)
  7536. Emit(Push(position,false));
  7537. CallThis(position,"Heaps","NewSys",3);
  7538. IntermediateCode.InitRegister(adr,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7539. Emit(Pop(position,adr));
  7540. GetMathArrayField(tmp,adr,MathPtrOffset);
  7541. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7542. AddInt(reg,tmp,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,SysDataArrayOffset)));
  7543. PutMathArrayField(adr,reg,MathAdrOffset);
  7544. ReleaseIntermediateOperand(tmp);
  7545. ReleaseIntermediateOperand(reg);
  7546. END;
  7547. flags := {};
  7548. IntermediateCode.InitImmediate(tmp,addressType,SYSTEM.VAL(LONGINT,flags));
  7549. PutMathArrayField(adr,tmp,MathFlagsOffset);
  7550. IntermediateCode.InitImmediate(tmp,addressType,openDim);
  7551. PutMathArrayField(adr,tmp,MathDimOffset);
  7552. else := NewLabel();
  7553. BreqL(else,IntermediateCode.Memory(addressType,adr,0),IntermediateCode.Immediate(addressType,0));
  7554. i := openDim-1;
  7555. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7556. IF isTensor THEN
  7557. IntermediateCode.InitImmediate(tmp,addressType,ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,MAX(LONGINT)))));
  7558. ELSE
  7559. IntermediateCode.InitImmediate(tmp,addressType,ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,openDim))));
  7560. END;
  7561. PutMathArrayField(adr,tmp,MathElementSizeOffset);
  7562. WHILE (i >= 0) DO
  7563. Emit(Pop(position,reg));
  7564. PutMathArrayLength(adr,reg,i);
  7565. PutMathArrayIncrement(adr,tmp,i);
  7566. IF i > 0 THEN
  7567. IF i=openDim-1 THEN
  7568. CopyInt(tmp,tmp);
  7569. END;
  7570. MulInt(tmp,tmp,reg);
  7571. END;
  7572. DEC(i);
  7573. END;
  7574. ReleaseIntermediateOperand(adr);
  7575. ReleaseIntermediateOperand(reg);
  7576. ReleaseIntermediateOperand(tmp);
  7577. exit := NewLabel();
  7578. BrL(exit);
  7579. SetLabel(else);
  7580. (* else part: array could not be allocated *)
  7581. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,openDim*system.addressSize));
  7582. Emit(Add(position,sp,sp,tmp));
  7583. SetLabel(exit);
  7584. END;
  7585. ELSIF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.CellType)
  7586. THEN
  7587. IF ~backend.cellsAreObjects THEN RETURN END;
  7588. IF InCellScope(currentScope) THEN
  7589. PushSelfPointer()
  7590. ELSE
  7591. Emit(Push(position, nil));
  7592. END;
  7593. (* push temp address *)
  7594. baseType := type(SyntaxTree.PointerType).pointerBase.resolved;
  7595. temporaryVariable := GetTemporaryVariable(type, FALSE);
  7596. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7597. (* l.op contains address of pointer to record *)
  7598. Emit(Push(position,l.op)); (* address for use after syscall *)
  7599. ReleaseOperand(l);
  7600. (* push type descriptor *)
  7601. reg := TypeDescriptorAdr(baseType);
  7602. IF ~newObjectFile THEN
  7603. IntermediateCode.MakeMemory(reg,addressType);
  7604. END;
  7605. Emit(Push(position,reg));
  7606. ReleaseIntermediateOperand(reg);
  7607. (* push name *)
  7608. (*Global.GetSymbolName(p0, n);*)
  7609. IF currentScope IS SyntaxTree.ProcedureScope THEN
  7610. Global.GetSymbolName(currentScope(SyntaxTree.ProcedureScope).ownerProcedure, n)
  7611. ELSE
  7612. Global.GetModuleName(module.module, n);
  7613. END;
  7614. Strings.Append(n,"@"); Strings.AppendInt(n, p0.position.start);
  7615. (*type.typeDeclaration.GetName(n);*)
  7616. PushConstString(n);
  7617. (* push cellnet boolean *)
  7618. PushConstBoolean(baseType(SyntaxTree.CellType).isCellNet);
  7619. (* push engine boolean *)
  7620. PushConstBoolean(baseType(SyntaxTree.CellType).FindProperty(Global.NameEngine) # NIL);
  7621. (* allocate *)
  7622. CallThis(position,"ActiveCellsRuntime","Allocate",7);
  7623. (* add capabilities *)
  7624. modifier := p0(SyntaxTree.Designator).modifiers;
  7625. IF (p0 IS SyntaxTree.SymbolDesignator) & (p0(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  7626. (*modifier := p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).modifiers;*)
  7627. AppendModifiers(modifier, p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).modifiers );
  7628. (* AddModifiers(baseType(SyntaxTree.CellType), temporaryVariable, modifier);*)
  7629. END;
  7630. AppendCellTypeModifiers(modifier, baseType(SyntaxTree.CellType));
  7631. (*
  7632. modifier := baseType(SyntaxTree.CellType).modifiers;
  7633. AddProperties(baseType(SyntaxTree.CellType), temporaryVariable, baseType(SyntaxTree.CellType).firstProperty);
  7634. modifier := p0(SyntaxTree.Designator).modifiers;
  7635. *)
  7636. AddModifiers(baseType(SyntaxTree.CellType), temporaryVariable, modifier);
  7637. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7638. (* l.op contains address of pointer to record *)
  7639. ToMemory(l.op,addressType,0);
  7640. (* l.op contains value of pointer to record *)
  7641. Emit(Push(position,l.op)); (* address for use after syscall *)
  7642. ReleaseOperand(l);
  7643. CallThis(position,"ActiveCellsRuntime","FinishedProperties",1);
  7644. prevScope := currentScope;
  7645. init := OpenInitializer(temporaryVariable, baseType(SyntaxTree.CellType).cellScope);
  7646. previous := section;
  7647. section := init;
  7648. (* add ports *)
  7649. AddPorts(temporaryVariable, baseType(SyntaxTree.CellType));
  7650. CloseInitializer(previous);
  7651. currentScope := prevScope;
  7652. Symbol(temporaryVariable,l);
  7653. ToMemory(l.op,addressType,0);
  7654. Emit(Push(position,l.op));
  7655. Emit(Call(position,IntermediateCode.Address(addressType, init.name, 0, 0), ToMemoryUnits(system, addressType.sizeInBits)));
  7656. (*
  7657. constructor := type(SyntaxTree.CellType).cellScope.constructor;
  7658. IF constructor # NIL THEN
  7659. parameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7660. FOR i := 1 TO x.parameters.Length()-1 DO
  7661. p := x.parameters.GetExpression(i);
  7662. Global.GetSymbolName(parameter,name);
  7663. Evaluate(p, value);
  7664. ASSERT(value.type # NIL);
  7665. IF value.type.resolved IS SyntaxTree.IntegerType THEN
  7666. par := instance.AddParameter(name);
  7667. par.SetInteger(value.integer);
  7668. ELSIF value.type.resolved IS SyntaxTree.BooleanType THEN
  7669. par := instance.AddParameter(name);
  7670. par.SetBoolean(value.boolean);
  7671. ELSE Error(x.position,NotYetImplemented)
  7672. END;
  7673. parameter := parameter.nextParameter
  7674. END;
  7675. END;
  7676. *)
  7677. (* call initializer *)
  7678. constructor := baseType(SyntaxTree.CellType).cellScope.constructor (*GetConstructor(p0.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType))*);
  7679. IF constructor # NIL THEN
  7680. (*! should be unified with ProcedureCallDesignator *)
  7681. IF backend.cellsAreObjects THEN
  7682. Symbol(temporaryVariable,l);
  7683. ToMemory(l.op,addressType,0);
  7684. Emit(Push(position,l.op));
  7685. ReleaseOperand(l);
  7686. END;
  7687. formalParameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7688. FOR i := firstPar TO x.parameters.Length()-1 DO
  7689. PushParameter(x.parameters.GetExpression(i), formalParameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  7690. formalParameter := formalParameter.nextParameter;
  7691. END;
  7692. (* static call of the constructor *)
  7693. Global.GetSymbolSegmentedName(constructor,name);
  7694. ASSERT(~constructor.isInline);
  7695. IF constructor.scope.ownerModule # module.module THEN
  7696. symbol := NewSection(module.importedSections, Sections.CodeSection, name, constructor, commentPrintout # NIL);
  7697. ELSE
  7698. symbol := NewSection(module.allSections, Sections.CodeSection, name, constructor, commentPrintout # NIL);
  7699. END;
  7700. Emit(Call(position,IntermediateCode.Address(addressType, symbol.name, GetFingerprint(constructor), 0),ProcedureParametersSize(system,constructor)));
  7701. (*ELSE
  7702. ReleaseIntermediateOperand(pointer);*)
  7703. END;
  7704. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7705. ToMemory(l.op, addressType, 0);
  7706. Designate(p0,s0);
  7707. ToMemory(s0.op,addressType,0);
  7708. Emit(Mov(position,s0.op,l.op));
  7709. ReleaseOperand(l);
  7710. ReleaseOperand(s0);
  7711. result.tag := emptyOperand;
  7712. (* start *)
  7713. IF baseType(SyntaxTree.CellType).cellScope.bodyProcedure # NIL THEN
  7714. (* push cell *)
  7715. Symbol(temporaryVariable, l);
  7716. ToMemory(l.op,addressType,0);
  7717. Emit(Push(Basic.invalidPosition,l.op));
  7718. (* push delegate *)
  7719. Emit(Push(Basic.invalidPosition,l.op));
  7720. ReleaseOperand(l);
  7721. StaticCallOperand(s1,baseType(SyntaxTree.CellType).cellScope.bodyProcedure);
  7722. Emit(Push(position, s1.op));
  7723. ReleaseOperand(s1);
  7724. CallThis(position,"ActiveCellsRuntime","Start",3);
  7725. END;
  7726. (*IF temporaryVariable # NIL THEN
  7727. end := NewLabel();
  7728. BrL(end);
  7729. SetLabel(exit);
  7730. Designate(p0,l);
  7731. ToMemory(l.op,addressType,0);
  7732. Emit(Mov(position,l.op,nil)); (* write NIL to adr *)
  7733. ReleaseOperand(l);
  7734. SetLabel(end);
  7735. ELSE
  7736. SetLabel(exit);
  7737. END;
  7738. *)
  7739. (*Error(p0.position,"cannot be allocated in runtime yet");*)
  7740. ELSE (* no pointer to record, no pointer to array *)
  7741. IF ~backend.cellsAreObjects & (type IS SyntaxTree.CellType) THEN
  7742. (* ignore new statement *)
  7743. Warning(p0.position, "cannot run on final hardware");
  7744. ELSE
  7745. HALT(200);
  7746. END;
  7747. END;
  7748. (* ---- ADDRESSOF----- *)
  7749. |Global.systemAdr:
  7750. Designate(p0,s0);
  7751. s0.mode := ModeValue;
  7752. IF (t0 IS SyntaxTree.MathArrayType) & (t0(SyntaxTree.MathArrayType).form = SyntaxTree.Open) THEN
  7753. ReleaseIntermediateOperand(s0.op);
  7754. s0.op := s0.tag;
  7755. IntermediateCode.InitOperand(s0.tag);
  7756. END;
  7757. Convert(s0.op,IntermediateCode.GetType(system,x.type));
  7758. result := s0;
  7759. (* ---- BIT ----- *)
  7760. |Global.systemBit:
  7761. Evaluate(p0,s0);
  7762. ToMemory(s0.op,addressType,0);
  7763. ReuseCopy(res,s0.op);
  7764. ReleaseOperand(s0);
  7765. Evaluate(p1,s1);
  7766. Emit(Ror(position,res,res,s1.op));
  7767. ReleaseOperand(s1);
  7768. Emit(And(position,res,res,IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1)));
  7769. Convert(res,IntermediateCode.GetType(system,system.booleanType));
  7770. IF ~conditional THEN
  7771. InitOperand(result,ModeValue); result.op := res;
  7772. ELSE
  7773. BreqL(trueLabel,IntermediateCode.Immediate(res.type,1),res);
  7774. BrL(falseLabel);
  7775. ReleaseIntermediateOperand(res);
  7776. END;
  7777. (* --- MSK ----*)
  7778. |Global.systemMsk:
  7779. Evaluate(p0,s0);
  7780. Evaluate(p1,s1);
  7781. ReuseCopy(res,s0.op);
  7782. ReleaseOperand(s0);
  7783. Emit(And(position,res,res,s1.op));
  7784. ReleaseOperand(s1);
  7785. InitOperand(result,ModeValue);
  7786. result.op := res;
  7787. (* ---- SYSTEM.GET8|16|32|64 ----- *)
  7788. |Global.systemGet8, Global.systemGet16, Global.systemGet32, Global.systemGet64:
  7789. Evaluate(p0,s0);
  7790. MakeMemory(res,s0.op,IntermediateCode.GetType(system,x.type),0);
  7791. ReleaseOperand(s0);
  7792. InitOperand(result,ModeValue);
  7793. result.op := res;
  7794. (* ---- SYSTEM.GetStackPointer ----- *)
  7795. |Global.systemGetStackPointer:
  7796. InitOperand(result,ModeValue);
  7797. result.op := sp;
  7798. (* ---- SYSTEM.GetFramePointer ----- *)
  7799. |Global.systemGetFramePointer:
  7800. InitOperand(result,ModeValue);
  7801. result.op := fp;
  7802. (* ---- SYSTEM.GetActivity ----- *)
  7803. |Global.systemGetActivity:
  7804. ASSERT(backend.cooperative);
  7805. InitOperand(result,ModeValue);
  7806. result.op := ap;
  7807. (* ---- SYSTEM.SetStackPointer ----- *)
  7808. |Global.systemSetStackPointer:
  7809. Evaluate(p0,s0); (* *)
  7810. Emit(Mov(position,sp,s0.op));
  7811. ReleaseOperand(s0);
  7812. (* ---- SYSTEM.SetFramePointer ----- *)
  7813. |Global.systemSetFramePointer:
  7814. Evaluate(p0,s0); (* *)
  7815. Emit(Mov(position,fp,s0.op));
  7816. ReleaseOperand(s0);
  7817. (* ---- SYSTEM.Activity ----- *)
  7818. |Global.systemSetActivity:
  7819. ASSERT(backend.cooperative);
  7820. Evaluate(p0,s0); (* *)
  7821. Emit(Mov(position,ap,s0.op));
  7822. ReleaseOperand(s0);
  7823. (* ---- SYSTEM.VAL ----- *)
  7824. |Global.systemVal:
  7825. Expression(p1);
  7826. s1 := result;
  7827. type :=p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  7828. IF s1.mode = ModeReference THEN
  7829. (* nothing to be done if not record type, just take over new type *)
  7830. IF (type IS SyntaxTree.RecordType) THEN
  7831. ReleaseIntermediateOperand(s1.tag);
  7832. s1.tag := TypeDescriptorAdr(type);
  7833. IF ~newObjectFile THEN
  7834. IntermediateCode.MakeMemory(s1.tag,addressType);
  7835. END;
  7836. UseIntermediateOperand(s1.tag);
  7837. END;
  7838. result := s1;
  7839. ELSE (* copy over result to different type, may not use convert *)
  7840. itype := IntermediateCode.GetType(system,type);
  7841. IF itype.sizeInBits = s1.op.type.sizeInBits THEN
  7842. IntermediateCode.InitRegister(s0.op,itype,IntermediateCode.GeneralPurposeRegister,AcquireRegister(itype,IntermediateCode.GeneralPurposeRegister));
  7843. Emit(Mov(position,s0.op,s1.op));
  7844. ReleaseOperand(s1);
  7845. InitOperand(result,ModeValue);
  7846. result.op := s0.op;
  7847. ELSE (* different size, must convert *)
  7848. (*! this is not very clean, should we forbid conversions between operands with different sizes or should we implement a cutting move ??? *)
  7849. Convert(s1.op, IntermediateCode.GetType(system,type));
  7850. result := s1;
  7851. END;
  7852. END;
  7853. (* ---- SYSTEM.GET ----- *)
  7854. |Global.systemGet:
  7855. Evaluate(p0,s0); (* adr *)
  7856. Designate(p1,s1); (* variable *)
  7857. ToMemory(s0.op,IntermediateCode.GetType(system,p1.type),0);
  7858. ToMemory(s1.op,IntermediateCode.GetType(system,p1.type),0);
  7859. Emit(Mov(position,s1.op,s0.op));
  7860. ReleaseOperand(s1);
  7861. ReleaseOperand(s0);
  7862. (* ---- SYSTEM.PUT 8|16|32|64 ----- *)
  7863. |Global.systemPut, Global.systemPut64, Global.systemPut32, Global.systemPut16, Global.systemPut8:
  7864. Evaluate(p0,s0); (* *)
  7865. Evaluate(p1,s1); (* variable *)
  7866. IF p1.type.resolved IS SyntaxTree.ComplexType THEN
  7867. componentType := p1.type.resolved(SyntaxTree.ComplexType).componentType;
  7868. (* real part *)
  7869. MakeMemory(res, s0.op, IntermediateCode.GetType(system, componentType), 0);
  7870. Emit(Mov(position,res, s1.op));
  7871. ReleaseIntermediateOperand(res);
  7872. (* imaginary part *)
  7873. MakeMemory(res, s0.op, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  7874. Emit(Mov(position,res, s1.tag));
  7875. ReleaseIntermediateOperand(res);
  7876. ReleaseOperand(s1);
  7877. ReleaseOperand(s0);
  7878. ELSE
  7879. MakeMemory(res,s0.op,IntermediateCode.GetType(system,p1.type),0);
  7880. ReleaseOperand(s0);
  7881. Emit(Mov(position,res,s1.op));
  7882. ReleaseIntermediateOperand(res);
  7883. ReleaseOperand(s1);
  7884. END;
  7885. (* ---- SYSTEM.MOVE ----- *)
  7886. |Global.systemMove:
  7887. Evaluate(p0,s0);
  7888. Evaluate(p1,s1);
  7889. Evaluate(p2,s2);
  7890. Emit(Copy(position,s1.op,s0.op,s2.op));
  7891. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  7892. (* ---- SYSTEM.NEW ----- *)
  7893. |Global.systemNew:
  7894. Designate(p0,s0);
  7895. Emit(Push(position,s0.op));
  7896. ReleaseOperand(s0);
  7897. Evaluate(p1,s1);
  7898. Emit(Push(position,s1.op));
  7899. ReleaseOperand(s1);
  7900. (* push realtime flag: false by default *)
  7901. Emit(Push(position,false));
  7902. CallThis(position,"Heaps","NewSys",3);
  7903. (* ---- SYSTEM.CALL ----- *)
  7904. |Global.systemRef:
  7905. Basic.ToSegmentedName(p0(SyntaxTree.StringValue).value^, segmentedName);
  7906. callsection := NewSection(module.allSections, Sections.CodeSection, segmentedName, NIL,commentPrintout # NIL);
  7907. s0.mode := ModeValue;
  7908. IntermediateCode.InitAddress(s0.op, addressType, callsection.name, 0, 0);
  7909. result := s0
  7910. (* ---- INCR ----- *)
  7911. |Global.Incr:
  7912. Designate(p0,operand);
  7913. IF p0.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7914. Dereference(operand,p0.type.resolved,FALSE);
  7915. END;
  7916. ASSERT(p1 # NIL);
  7917. Evaluate(p1,l);
  7918. GetMathArrayIncrement(p0.type.resolved(SyntaxTree.MathArrayType),operand, l.op,TRUE, result);
  7919. ReleaseOperand(operand); ReleaseOperand(l);
  7920. Convert(result.op,IntermediateCode.GetType(system, x.type));
  7921. (* ---- SUM ----- *)
  7922. |Global.Sum: HALT(200);
  7923. (* ---- ALL ----- *)
  7924. |Global.All: HALT(200);
  7925. (* ---- CAS ----- *)
  7926. |Global.Cas:
  7927. needsTrace := p0.NeedsTrace();
  7928. IF needsTrace THEN ModifyAssignments(true) END;
  7929. Designate(p0,s0);
  7930. Evaluate(p1,s1);
  7931. Evaluate(p2,s2);
  7932. IF needsTrace THEN
  7933. Emit(Push(position, s0.op));
  7934. Emit(Push(position, s1.op));
  7935. Emit(Push(position, s2.op));
  7936. CallThis(position,"GarbageCollector","CompareAndSwap",3);
  7937. ELSE
  7938. Emit(Cas(position,s0.op,s1.op,s2.op));
  7939. END;
  7940. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  7941. IF needsTrace THEN ModifyAssignments(false) END;
  7942. res := NewRegisterOperand(IntermediateCode.GetType(system, p0.type));
  7943. Emit(Result(position, res)); result.op := res; result.mode := ModeValue;
  7944. IF conditional THEN
  7945. BreqL(trueLabel,IntermediateCode.Immediate(res.type,1),res);
  7946. BrL(falseLabel);
  7947. ReleaseIntermediateOperand(res);
  7948. END;
  7949. (* ---- DIM ----- *)
  7950. |Global.Dim:
  7951. ASSERT(~SemanticChecker.IsArrayStructuredObjectType(p0.type));
  7952. Designate(p0,s0);
  7953. IF p0.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7954. Dereference(s0,p0.type.resolved,FALSE);
  7955. END;
  7956. MathArrayDim(p0.type.resolved(SyntaxTree.MathArrayType),s0.tag,result);
  7957. ReleaseOperand(s0);
  7958. (* ---- RESHAPE ----- *)
  7959. |Global.Reshape:
  7960. IF GetRuntimeProcedure("FoxArrayBase","Reshape",procedure,TRUE) THEN
  7961. left := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,procedure);
  7962. left.SetType(procedure.type);
  7963. call := SyntaxTree.NewProcedureCallDesignator(position,left(SyntaxTree.Designator),x.parameters);
  7964. VisitProcedureCallDesignator(call(SyntaxTree.ProcedureCallDesignator));
  7965. END;
  7966. (* ---- SYSTEM.TYPECODE ----- *)
  7967. |Global.systemTypeCode:
  7968. type := p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  7969. IF type.resolved IS SyntaxTree.PointerType THEN
  7970. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  7971. END;
  7972. result.op := TypeDescriptorAdr(type);
  7973. IF ~newObjectFile THEN
  7974. ToMemory(result.op,IntermediateCode.GetType(system,x.type),0);
  7975. ELSE Convert(result.op, IntermediateCode.GetType(system,x.type));
  7976. END;
  7977. result.mode := ModeValue;
  7978. (* ---- SYSTEM.TRACE ----- *)
  7979. |Global.systemTrace:
  7980. SystemTrace(x.parameters, x.position);
  7981. (* ----- CONNECT ------*)
  7982. |Global.Connect:
  7983. IF backend.cellsAreObjects THEN
  7984. PushPort(p0);
  7985. PushPort(p1);
  7986. IF p2 # NIL THEN
  7987. Evaluate(p2, s2);
  7988. Emit(Push(p2.position, s2.op));
  7989. ReleaseOperand(s2);
  7990. ELSE
  7991. Emit(Push(Basic.invalidPosition, IntermediateCode.Immediate(int32, -1)));
  7992. END;
  7993. CallThis(position,"ActiveCellsRuntime","Connect",3);
  7994. ELSE
  7995. Warning(x.position, "cannot run on final hardware");
  7996. END;
  7997. (* ----- DELEGATE ------*)
  7998. |Global.Delegate:
  7999. IF backend.cellsAreObjects THEN
  8000. PushPort(p0);
  8001. PushPort(p1);
  8002. CallThis(position,"ActiveCellsRuntime","Delegate",2);
  8003. ELSE
  8004. Warning(x.position, "cannot run on final hardware");
  8005. END;
  8006. (* ----- SEND ------*)
  8007. |Global.Send:
  8008. Evaluate(p0,s0);
  8009. Evaluate(p1,s1);
  8010. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  8011. Emit(Push(position,s0.op));
  8012. Emit(Push(position,s1.op));
  8013. (*
  8014. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  8015. *)
  8016. IF ~backend.cellsAreObjects THEN
  8017. IF size # 1 THEN Error(p1.position,"send not implemented for complex data types") END;
  8018. END;
  8019. ReleaseOperand(s0);
  8020. ReleaseOperand(s1);
  8021. IF backend.cellsAreObjects THEN
  8022. CallThis(position,"ActiveCellsRuntime","Send",2);
  8023. ELSE
  8024. CallThis(position,ChannelModuleName,"Send",2);
  8025. END;
  8026. (* ----- RECEIVE ------*)
  8027. |Global.Receive:
  8028. Evaluate(p0,s0);
  8029. Emit(Push(position,s0.op));
  8030. Designate(p1,s1);
  8031. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  8032. Emit(Push(position,s1.op));
  8033. IF p2 # NIL THEN
  8034. Designate(p2,s2);
  8035. Emit(Push(position,s2.op));
  8036. END;
  8037. (*
  8038. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  8039. *)
  8040. IF ~backend.cellsAreObjects THEN
  8041. IF size # 1 THEN Error(p1.position,"receive not implemented for complex data types") END;
  8042. END;
  8043. ReleaseOperand(s0);
  8044. ReleaseOperand(s1);
  8045. ReleaseOperand(s2);
  8046. IF backend.cellsAreObjects THEN
  8047. IF p2 = NIL THEN
  8048. CallThis(position,"ActiveCellsRuntime","Receive",2)
  8049. ELSE
  8050. CallThis(position,"ActiveCellsRuntime","ReceiveNonBlockingVar",3)
  8051. END;
  8052. ELSE
  8053. IF p2 = NIL THEN
  8054. CallThis(position,ChannelModuleName,"Receive",2)
  8055. ELSE
  8056. CallThis(position,ChannelModuleName,"ReceiveNonBlockingVar",3)
  8057. END;
  8058. END;
  8059. | Global.systemSpecial:
  8060. customBuiltin := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.CustomBuiltin);
  8061. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  8062. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  8063. (* determine if parameters are of the VAR kind *)
  8064. ASSERT(x.parameters.Length() <= 3);
  8065. formalParameter := procedureType.firstParameter;
  8066. FOR i := 0 TO x.parameters.Length() - 1 DO
  8067. isVarPar[i] := formalParameter.kind = SyntaxTree.VarParameter;
  8068. formalParameter := formalParameter.nextParameter
  8069. END;
  8070. IF p0 # NIL THEN IF isVarPar[0] THEN Designate(p0, s0) ELSE Evaluate(p0,s0) END ELSE InitOperand(s0, ModeValue) END;
  8071. IF p1 # NIL THEN IF isVarPar[1] THEN Designate(p1, s1) ELSE Evaluate(p1,s1) END ELSE InitOperand(s1, ModeValue) END;
  8072. IF p2 # NIL THEN IF isVarPar[2] THEN Designate(p2, s2) ELSE Evaluate(p2,s2) END ELSE InitOperand(s2, ModeValue) END;
  8073. Emit(SpecialInstruction(x.position, customBuiltin.subType,s0.op, s1.op, s2.op));
  8074. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  8075. IF procedureType.returnType # NIL THEN
  8076. res := NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  8077. Emit(Result(position, res));
  8078. (*InitOperand(result,ModeValue);
  8079. result.op := res;
  8080. *)
  8081. IF ~conditional THEN
  8082. InitOperand(result,ModeValue); result.op := res;
  8083. ELSE
  8084. BreqL(trueLabel,IntermediateCode.Immediate(res.type,1),res);
  8085. BrL(falseLabel);
  8086. ReleaseIntermediateOperand(res);
  8087. END;
  8088. END
  8089. ELSE (* function not yet implemented *)
  8090. Error(position,"not yet implemented");
  8091. END;
  8092. destination := dest;
  8093. IF Trace THEN TraceExit("VisitBuiltinCallDesignator") END;
  8094. END VisitBuiltinCallDesignator;
  8095. PROCEDURE VisitTypeGuardDesignator(x: SyntaxTree.TypeGuardDesignator);
  8096. VAR trueL,falseL: Label; recordType: SyntaxTree.RecordType; dest,tag,ptr: IntermediateCode.Operand;
  8097. BEGIN
  8098. IF Trace THEN TraceEnter("VisitTypeGuardDesignator") END;
  8099. dest := destination; destination := emptyOperand;
  8100. Expression(x.left);
  8101. IF x.left.type.resolved = x.type.resolved THEN (* always true: do nothing *)
  8102. ELSIF isUnchecked THEN (* no check *)
  8103. ELSE
  8104. trueL := NewLabel();
  8105. falseL := NewLabel();
  8106. IF IsPointerToRecord(x.left.type,recordType) THEN
  8107. IntermediateCode.InitRegister(tag,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  8108. Emit(Mov(position,tag, result.op));
  8109. IF result.mode # ModeValue THEN
  8110. ptr := tag;
  8111. IntermediateCode.MakeMemory(ptr,addressType);
  8112. Emit(Mov(position,tag, ptr));
  8113. END;
  8114. IF ~backend.cooperative THEN
  8115. IntermediateCode.AddOffset(tag,ToMemoryUnits(system,-addressType.sizeInBits));
  8116. END;
  8117. IntermediateCode.MakeMemory(tag,addressType);
  8118. ELSE
  8119. tag := result.tag;
  8120. UseIntermediateOperand(tag);
  8121. END;
  8122. TypeTest(tag,x.type,trueL,falseL);
  8123. ReleaseIntermediateOperand(tag);
  8124. SetLabel(falseL);
  8125. EmitTrap(position,TypeCheckTrap);
  8126. SetLabel(trueL);
  8127. END;
  8128. destination := dest;
  8129. IF Trace THEN TraceExit("VisitTypeGuardDesignator") END;
  8130. END VisitTypeGuardDesignator;
  8131. PROCEDURE Dereference(VAR operand: Operand; type: SyntaxTree.Type; isUnsafe: BOOLEAN);
  8132. VAR dereferenced: IntermediateCode.Operand; arrayDataOffset: LONGINT;
  8133. PROCEDURE NilCheck(CONST op: IntermediateCode.Operand);
  8134. VAR label: Label; pc: LONGINT;
  8135. BEGIN
  8136. IF backend.cooperative & ~isUnchecked THEN
  8137. pc := section.pc;
  8138. label := NewLabel();
  8139. BrneL(label, operand.op, nil);
  8140. EmitTrap(position, NilPointerTrap);
  8141. SetLabel(label);
  8142. INC(statCoopNilCheck, section.pc - pc);
  8143. END;
  8144. END NilCheck;
  8145. BEGIN
  8146. LoadValue(operand,system.addressType); (* in case operand is not a value yet *)
  8147. ReuseCopy(dereferenced,operand.op);
  8148. ReleaseOperand(operand);
  8149. operand.mode := ModeReference;
  8150. operand.op := dereferenced;
  8151. operand.tag := dereferenced;
  8152. UseIntermediateOperand(operand.tag);
  8153. IF (type=NIL) OR (type IS SyntaxTree.RecordType)OR (type IS SyntaxTree.CellType) THEN
  8154. IF isUnsafe & ((type = NIL) OR ~type(SyntaxTree.RecordType).isObject) THEN
  8155. ReleaseIntermediateOperand(operand.tag);
  8156. operand.tag := TypeDescriptorAdr(type);
  8157. IF ~newObjectFile THEN
  8158. IntermediateCode.MakeMemory(operand.tag,addressType);
  8159. END;
  8160. ELSE
  8161. IF ~backend.cooperative THEN
  8162. IntermediateCode.AddOffset(operand.tag,ToMemoryUnits(system,-addressType.sizeInBits));
  8163. END;
  8164. IntermediateCode.MakeMemory(operand.tag,addressType);
  8165. END;
  8166. NilCheck(operand.op);
  8167. ELSIF type IS SyntaxTree.ArrayType THEN
  8168. IF isUnsafe THEN
  8169. NilCheck(operand.op);
  8170. ReleaseIntermediateOperand(operand.tag);
  8171. IF type(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  8172. IntermediateCode.InitImmediate(operand.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8173. ELSE
  8174. operand.tag := emptyOperand;
  8175. END;
  8176. ELSE
  8177. NilCheck(operand.op);
  8178. IF backend.cooperative THEN
  8179. arrayDataOffset := (BaseArrayTypeSize + DynamicDim(type)) * addressType.sizeInBits;
  8180. IntermediateCode.AddOffset(operand.op,ToMemoryUnits(system,arrayDataOffset));
  8181. IntermediateCode.AddOffset(operand.tag,ToMemoryUnits(system,BaseArrayTypeSize*system.addressSize));
  8182. ELSE
  8183. arrayDataOffset := DynamicDim(type) * addressType.sizeInBits + 3 * addressType.sizeInBits;
  8184. INC(arrayDataOffset, (-arrayDataOffset) MOD ArrayAlignment); (* round up to multiple of ArrayAlignment to ensure that first array element is aligned at 0 MOD ArrayAlignment *)
  8185. IntermediateCode.AddOffset(operand.op,ToMemoryUnits(system,arrayDataOffset));
  8186. IntermediateCode.AddOffset(operand.tag,ToMemoryUnits(system,ArrayDimTable*system.addressSize))
  8187. END;
  8188. END;
  8189. ELSIF type IS SyntaxTree.MathArrayType THEN
  8190. IntermediateCode.AddOffset(operand.op,ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8191. IntermediateCode.MakeMemory(operand.op,addressType);
  8192. ELSE HALT(100);
  8193. END;
  8194. END Dereference;
  8195. PROCEDURE VisitDereferenceDesignator(x: SyntaxTree.DereferenceDesignator);
  8196. VAR type: SyntaxTree.Type; d: Operand; dest: IntermediateCode.Operand;prevIsUnchecked: BOOLEAN;
  8197. BEGIN
  8198. IF Trace THEN TraceEnter("VisitDereferenceDesignator") END;
  8199. dest := destination; destination := emptyOperand;
  8200. Evaluate(x.left,d);
  8201. type := x.type.resolved;
  8202. prevIsUnchecked := isUnchecked;
  8203. IF (x.left # NIL) & (x.left IS SyntaxTree.SelfDesignator) THEN
  8204. isUnchecked := TRUE; (* avoid NIL-pointer-check for SELF pointer *)
  8205. END;
  8206. Dereference(d,type,IsUnsafePointer(x.left.type));
  8207. isUnchecked := prevIsUnchecked;
  8208. result := d;
  8209. IF backend.cooperative & (x.left.type.resolved IS SyntaxTree.PointerType) & ~x.left.type.resolved(SyntaxTree.PointerType).isPlain & ~x.left.type.resolved(SyntaxTree.PointerType).isUnsafe THEN
  8210. IF (type IS SyntaxTree.RecordType) & ~type(SyntaxTree.RecordType).isObject THEN
  8211. IntermediateCode.AddOffset(result.op,BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  8212. END;
  8213. END;
  8214. destination := dest;
  8215. IF Trace THEN TraceExit("VisitDereferenceDesignator") END;
  8216. END VisitDereferenceDesignator;
  8217. PROCEDURE VisitSupercallDesignator(x: SyntaxTree.SupercallDesignator);
  8218. VAR procedure: SyntaxTree.Procedure; tag: IntermediateCode.Operand; dest: IntermediateCode.Operand;
  8219. BEGIN
  8220. IF Trace THEN TraceEnter("VisitSupercallDesignator") END;
  8221. dest := destination; destination := emptyOperand;
  8222. Designate(x.left(SyntaxTree.SymbolDesignator).left,result);
  8223. tag := result.op;
  8224. (*ReleaseIntermediateOperand(result.tag); not necessary -- done in StaticCallOperand *)
  8225. procedure := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  8226. StaticCallOperand(result,procedure.super);
  8227. ReleaseIntermediateOperand(result.tag);
  8228. UseIntermediateOperand(tag); (* necessary ? *)
  8229. result.tag := tag;
  8230. destination := dest;
  8231. IF Trace THEN TraceExit("VisitSupercallDesignator") END;
  8232. END VisitSupercallDesignator;
  8233. PROCEDURE VisitSelfDesignator(x: SyntaxTree.SelfDesignator);
  8234. VAR basereg: IntermediateCode.Operand; scope: SyntaxTree.Scope; dest: IntermediateCode.Operand;
  8235. moduleSection: IntermediateCode.Section; moduleOffset, parametersSize: LONGINT;
  8236. name: Basic.SegmentedName;
  8237. procedure: SyntaxTree.Procedure;
  8238. procedureType: SyntaxTree.ProcedureType;
  8239. BEGIN
  8240. IF Trace THEN TraceEnter("VisitSelfDesignator") END;
  8241. dest := destination; destination := emptyOperand;
  8242. scope := currentScope;
  8243. WHILE (scope.outerScope # NIL) & (scope.outerScope IS SyntaxTree.ProcedureScope) DO
  8244. scope := scope.outerScope;
  8245. END;
  8246. IF scope.outerScope IS SyntaxTree.ModuleScope THEN
  8247. IF newObjectFile THEN
  8248. moduleSection := meta.ModuleSection();
  8249. IF backend.cooperative THEN
  8250. moduleOffset := 0;
  8251. ELSE
  8252. moduleOffset := moduleSection.pc;
  8253. END;
  8254. result.mode := ModeValue;
  8255. result.op := IntermediateCode.Address(addressType, moduleSection.name, GetFingerprint(moduleSection.symbol), moduleOffset);
  8256. ELSE
  8257. Symbol(moduleSelf,result);
  8258. IntermediateCode.MakeMemory(result.op,addressType);
  8259. END
  8260. ELSIF (scope.outerScope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN
  8261. result.mode := ModeValue;
  8262. Global.GetSymbolSegmentedName(scope.outerScope(SyntaxTree.CellScope).ownerCell.typeDeclaration, name);
  8263. result.op := IntermediateCode.Address(addressType, name, 0, moduleOffset);
  8264. ELSE
  8265. GetBaseRegister(basereg,currentScope,scope);
  8266. InitOperand(result,ModeReference);
  8267. result.op := basereg;
  8268. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  8269. procedureType := procedure.type(SyntaxTree.ProcedureType);
  8270. parametersSize := ProcedureParametersSize(system,procedure);
  8271. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,addressType.sizeInBits)*(procedureType.parametersOffset+1)+parametersSize);
  8272. IF backend.cooperative THEN
  8273. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,addressType.sizeInBits));
  8274. END;
  8275. (* tag must be loaded when dereferencing SELF pointer *)
  8276. END;
  8277. destination := dest;
  8278. IF Trace THEN TraceExit("VisitSelfDesignator") END;
  8279. END VisitSelfDesignator;
  8280. PROCEDURE VisitResultDesignator(x: SyntaxTree.ResultDesignator);
  8281. VAR procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; parameter: SyntaxTree.Parameter; map: SymbolMap;
  8282. BEGIN
  8283. IF Trace THEN TraceEnter("VisitResultDesignator") END;
  8284. procedure := currentScope(SyntaxTree.ProcedureScope).ownerProcedure;
  8285. procedureType := procedure.type(SyntaxTree.ProcedureType);
  8286. parameter := procedureType.returnParameter;
  8287. IF currentIsInline THEN
  8288. map := currentMapper.Get(NIL);
  8289. IF map # NIL THEN
  8290. Designate(map.to, result);
  8291. ELSE
  8292. HALT(200);
  8293. END;
  8294. RETURN;
  8295. END;
  8296. VisitParameter(parameter);
  8297. IF Trace THEN TraceExit("VisitResultDesignator") END;
  8298. END VisitResultDesignator;
  8299. (** values *)
  8300. PROCEDURE VisitBooleanValue(x: SyntaxTree.BooleanValue);
  8301. BEGIN
  8302. IF Trace THEN TraceEnter("VisitBooleanValue") END;
  8303. IF conditional THEN
  8304. IF x.value THEN BrL(trueLabel)
  8305. ELSE BrL(falseLabel)
  8306. END;
  8307. ELSE
  8308. InitOperand(result,ModeValue);
  8309. IF x.value THEN result.op := true ELSE result.op := false END;
  8310. END;
  8311. END VisitBooleanValue;
  8312. PROCEDURE GetDataSection*(): IntermediateCode.Section;
  8313. VAR name: Basic.SegmentedName; section: IntermediateCode.Section;
  8314. BEGIN
  8315. Global.GetModuleSegmentedName(module.module, name);
  8316. Basic.SuffixSegmentedName(name,Basic.MakeString("@Immediates"));
  8317. section := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL, dump # NIL);
  8318. RETURN section
  8319. END GetDataSection;
  8320. PROCEDURE GetImmediateMem(VAR vop: IntermediateCode.Operand);
  8321. VAR data: IntermediateCode.Section;pc: LONGINT; type: IntermediateCode.Type;
  8322. BEGIN
  8323. type := vop.type;
  8324. data := GetDataSection();
  8325. pc := EnterImmediate(data,vop);
  8326. IntermediateCode.InitAddress(vop, addressType, data.name, 0, pc);
  8327. IntermediateCode.MakeMemory(vop, type);
  8328. END GetImmediateMem;
  8329. PROCEDURE VisitIntegerValue(x: SyntaxTree.IntegerValue);
  8330. BEGIN
  8331. IF Trace THEN TraceEnter("VisitIntegerValue") END;
  8332. InitOperand(result,ModeValue);
  8333. IntermediateCode.InitImmediate(result.op,IntermediateCode.GetType(system,x.type),x.hvalue);
  8334. IF ~supportedImmediate(result.op) &~inData THEN
  8335. GetImmediateMem(result.op)
  8336. END;
  8337. END VisitIntegerValue;
  8338. PROCEDURE VisitCharacterValue(x: SyntaxTree.CharacterValue);
  8339. BEGIN
  8340. IF Trace THEN TraceEnter("VisitCharacterValue") END;
  8341. InitOperand(result,ModeValue);
  8342. IntermediateCode.InitImmediate(result.op,IntermediateCode.GetType(system,x.type),ORD(x.value));
  8343. END VisitCharacterValue;
  8344. PROCEDURE VisitSetValue(x: SyntaxTree.SetValue);
  8345. BEGIN
  8346. IF Trace THEN TraceEnter("VisitSetValue") END;
  8347. InitOperand(result,ModeValue);
  8348. IntermediateCode.InitImmediate(result.op,IntermediateCode.GetType(system,x.type),SYSTEM.VAL(LONGINT,x.value));
  8349. END VisitSetValue;
  8350. PROCEDURE VisitMathArrayValue(x: SyntaxTree.MathArrayValue);
  8351. VAR irv: IntermediateCode.Section; name:Basic.SegmentedName;
  8352. PROCEDURE RecursiveData(x: SyntaxTree.MathArrayExpression);
  8353. VAR numberElements,i: LONGINT; expression: SyntaxTree.Expression; op: Operand;
  8354. BEGIN
  8355. numberElements := x.elements.Length();
  8356. FOR i := 0 TO numberElements-1 DO
  8357. expression := x.elements.GetExpression(i);
  8358. IF expression IS SyntaxTree.MathArrayExpression THEN
  8359. RecursiveData(expression(SyntaxTree.MathArrayExpression));
  8360. ELSE
  8361. inData := TRUE;
  8362. Evaluate(expression,op);
  8363. irv.Emit(Data(position,op.op));
  8364. inData := FALSE;
  8365. ReleaseOperand(op);
  8366. END;
  8367. END;
  8368. END RecursiveData;
  8369. BEGIN
  8370. IF Trace THEN TraceEnter("VisitMathArrayValue") END;
  8371. IF ~TryConstantDeclaration() THEN
  8372. IF constantDeclaration = NIL THEN constantDeclaration:=BuildConstant(module.module,x,constId) END;
  8373. GetCodeSectionNameForSymbol(constantDeclaration,name);
  8374. IF ~newObjectFile OR (constantDeclaration.scope = NIL) OR (constantDeclaration.scope.ownerModule = module.module) THEN
  8375. irv := NewSection(module.allSections,Sections.ConstSection,name,constantDeclaration,commentPrintout # NIL);
  8376. ELSE
  8377. irv := NewSection(module.importedSections, Sections.ConstSection, name,constantDeclaration,commentPrintout # NIL);
  8378. END;
  8379. RecursiveData(x.array);
  8380. InitOperand(result,ModeReference);
  8381. IntermediateCode.InitAddress(result.op, addressType, irv.name, GetFingerprint(irv.symbol), 0);
  8382. END
  8383. END VisitMathArrayValue;
  8384. PROCEDURE TryConstantDeclaration(): BOOLEAN;
  8385. VAR constant: Sections.Section;
  8386. BEGIN
  8387. IF constantDeclaration = NIL THEN
  8388. RETURN FALSE
  8389. ELSE
  8390. (* Is a constant in this module: did we generate it already? *)
  8391. constant := module.allSections.FindBySymbol(constantDeclaration); (*TODO*)
  8392. IF constant # NIL THEN
  8393. InitOperand(result,ModeReference);
  8394. IntermediateCode.InitAddress(result.op,addressType,constant.name,GetFingerprint(constant.symbol), 0);
  8395. RETURN TRUE;
  8396. END;
  8397. END;
  8398. RETURN FALSE
  8399. END TryConstantDeclaration;
  8400. PROCEDURE VisitConstant(x: SyntaxTree.Constant);
  8401. BEGIN
  8402. constantDeclaration := x;
  8403. x.value.resolved.Accept(SELF);
  8404. END VisitConstant;
  8405. PROCEDURE VisitRealValue(x: SyntaxTree.RealValue);
  8406. BEGIN
  8407. IF Trace THEN TraceEnter("VisitRealValue") END;
  8408. InitOperand(result,ModeValue);
  8409. IntermediateCode.InitFloatImmediate(result.op,IntermediateCode.GetType(system,x.type),x.value);
  8410. END VisitRealValue;
  8411. PROCEDURE VisitComplexValue(x: SyntaxTree.ComplexValue);
  8412. VAR
  8413. componentType: SyntaxTree.Type;
  8414. BEGIN
  8415. IF Trace THEN TraceEnter("VisitComplexValue") END;
  8416. ASSERT(x.type IS SyntaxTree.ComplexType);
  8417. componentType := x.type(SyntaxTree.ComplexType).componentType;
  8418. InitOperand(result,ModeValue);
  8419. IntermediateCode.InitFloatImmediate(result.op,IntermediateCode.GetType(system,componentType),x.realValue); (* real part *)
  8420. IntermediateCode.InitFloatImmediate(result.tag,IntermediateCode.GetType(system,componentType),x.imagValue); (* imaginary part *)
  8421. END VisitComplexValue;
  8422. PROCEDURE VisitStringValue(x: SyntaxTree.StringValue);
  8423. VAR i: LONGINT; name: Basic.SegmentedName;
  8424. irv: IntermediateCode.Section; op: IntermediateCode.Operand;
  8425. BEGIN
  8426. IF Trace THEN TraceEnter("VisitStringValue") END;
  8427. IF ~TryConstantDeclaration() THEN
  8428. IF constantDeclaration = NIL THEN constantDeclaration:=BuildConstant(module.module,x,constId) END;
  8429. GetCodeSectionNameForSymbol(constantDeclaration,name);
  8430. IF ~newObjectFile OR (constantDeclaration.scope = NIL) OR (constantDeclaration.scope.ownerModule = module.module) THEN
  8431. irv := NewSection(module.allSections, Sections.ConstSection, name,constantDeclaration,commentPrintout # NIL);
  8432. ELSE
  8433. irv := NewSection(module.importedSections, Sections.ConstSection, name,constantDeclaration,commentPrintout # NIL);
  8434. END;
  8435. FOR i := 0 TO x.length-1 DO
  8436. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(system,system.characterType),ORD(x.value[i]));
  8437. irv.Emit(Data(position,op));
  8438. END;
  8439. InitOperand(result,ModeReference);
  8440. IntermediateCode.InitAddress(result.op, addressType, irv.name, GetFingerprint(irv.symbol), 0);
  8441. result.tag := IntermediateCode.Immediate(addressType,x.length);
  8442. END
  8443. END VisitStringValue;
  8444. PROCEDURE VisitNilValue(x: SyntaxTree.NilValue);
  8445. BEGIN
  8446. IF Trace THEN TraceEnter("VisitNilValue") END;
  8447. InitOperand(result,ModeValue);
  8448. result.op := IntermediateCode.Immediate(IntermediateCode.GetType(system,x.type),0);
  8449. result.tag := IntermediateCode.Immediate(IntermediateCode.GetType(system,x.type),0);
  8450. END VisitNilValue;
  8451. PROCEDURE VisitEnumerationValue(x: SyntaxTree.EnumerationValue);
  8452. BEGIN
  8453. IF Trace THEN TraceEnter("VisitEnumerationValue") END;
  8454. InitOperand(result,ModeValue);
  8455. result.op := IntermediateCode.Immediate(IntermediateCode.GetType(system,x.type),x.value);
  8456. END VisitEnumerationValue;
  8457. (** symbols *)
  8458. PROCEDURE VisitImport(x: SyntaxTree.Import);
  8459. BEGIN (* nothing to be done, might however be called via some designator module.procedure *)
  8460. END VisitImport;
  8461. PROCEDURE GetBaseRegister(VAR result: IntermediateCode.Operand; scope,baseScope: SyntaxTree.Scope);
  8462. VAR left,right: IntermediateCode.Operand;level: LONGINT;
  8463. BEGIN
  8464. IF scope # baseScope THEN
  8465. (* left := [fp+8] *)
  8466. IntermediateCode.InitMemory(right,addressType,fp,ToMemoryUnits(system,2*addressType.sizeInBits));
  8467. IF backend.cooperative OR backend.preciseGC THEN IntermediateCode.AddOffset (right, ToMemoryUnits(system,addressType.sizeInBits)) END;
  8468. ReuseCopy(left,right);
  8469. ReleaseIntermediateOperand(right);
  8470. scope := scope.outerScope; DEC(level);
  8471. (* { left := [left+8] } *)
  8472. IntermediateCode.InitMemory(right,addressType,left,ToMemoryUnits(system,2*addressType.sizeInBits));
  8473. IF backend.cooperative OR backend.preciseGC THEN IntermediateCode.AddOffset (right, ToMemoryUnits(system,addressType.sizeInBits)) END;
  8474. WHILE (scope # baseScope) & (scope IS SyntaxTree.ProcedureScope) DO
  8475. Emit(Mov(position,left,right));
  8476. scope := scope.outerScope; DEC(level);
  8477. END;
  8478. ASSERT((scope = baseScope) OR (baseScope = NIL));
  8479. result := left;
  8480. ELSE
  8481. result := fp;
  8482. END;
  8483. END GetBaseRegister;
  8484. PROCEDURE VisitVariable(x: SyntaxTree.Variable);
  8485. VAR symbol: Sections.Section; type: SyntaxTree.Type; recordType: SyntaxTree.RecordType; name: Basic.SegmentedName; temp: IntermediateCode.Operand; reg: LONGINT;
  8486. BEGIN
  8487. IF Trace THEN TraceEnter("VisitVariable"); END;
  8488. type := x.type.resolved;
  8489. IF (x.useRegister) THEN
  8490. InitOperand(result, ModeValue);
  8491. IF x.registerNumber < 0 THEN
  8492. x.SetRegisterNumber(AcquireRegister(IntermediateCode.GetType(system, type),IntermediateCode.GeneralPurposeRegister));
  8493. reg := x.registerNumber;
  8494. ELSE
  8495. reg := registerUsageCount.Map(x.registerNumber);
  8496. UseRegister(reg);
  8497. END;
  8498. IntermediateCode.InitRegister(result.op,IntermediateCode.GetType(system, type),IntermediateCode.GeneralPurposeRegister,reg);
  8499. ELSIF x.externalName # NIL THEN
  8500. InitOperand(result,ModeReference);
  8501. Basic.ToSegmentedName(x.externalName^, name);
  8502. IntermediateCode.InitAddress(result.op, addressType, name, 0, 0);
  8503. ELSIF (x.scope IS SyntaxTree.ProcedureScope) THEN (* local variable (potentially via nested procedure) *)
  8504. InitOperand(result,ModeReference);
  8505. GetBaseRegister(result.op,currentScope,x.scope);
  8506. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8507. ELSIF (x.scope = moduleScope) OR (x.scope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN (* global variable *)
  8508. InitOperand(result,ModeReference);
  8509. GetCodeSectionNameForSymbol(x,name);
  8510. symbol := NewSection(module.allSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8511. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0);
  8512. ELSIF x.scope IS SyntaxTree.ModuleScope THEN (* global variable in imported module *)
  8513. InitOperand(result,ModeReference);
  8514. GetCodeSectionNameForSymbol(x,name);
  8515. symbol := NewSection(module.importedSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8516. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0)
  8517. ELSE (* field, left designator must have been emitted *)
  8518. ASSERT(result.mode = ModeReference);
  8519. IF result.op.mode = IntermediateCode.ModeMemory THEN
  8520. ReuseCopy(temp,result.op);
  8521. ReleaseIntermediateOperand(result.op);
  8522. result.op := temp;
  8523. END;
  8524. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8525. IF backend.cooperative & (x.scope IS SyntaxTree.RecordScope) THEN
  8526. recordType := x.scope(SyntaxTree.RecordScope).ownerRecord;
  8527. IF recordType.isObject & ~recordType.pointerType.isPlain THEN
  8528. IntermediateCode.AddOffset(result.op,BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  8529. END;
  8530. END;
  8531. END;
  8532. IF conditional & (x.type.resolved IS SyntaxTree.BooleanType) THEN
  8533. ValueToCondition(result);
  8534. ELSIF type IS SyntaxTree.ProcedureType THEN
  8535. ReleaseIntermediateOperand(result.tag);
  8536. IF type(SyntaxTree.ProcedureType).isDelegate THEN
  8537. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  8538. UseIntermediateOperand(result.tag);
  8539. ELSE
  8540. result.tag := nil; (* nil *)
  8541. END;
  8542. ELSIF (type IS SyntaxTree.ArrayType) THEN
  8543. IF type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic THEN
  8544. IF (x.scope IS SyntaxTree.ModuleScope) OR (x.scope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN
  8545. ReleaseIntermediateOperand(result.tag);
  8546. Global.GetSymbolSegmentedName(x,name);
  8547. Basic.SuffixSegmentedName (name, Basic.MakeString ("@len"));
  8548. symbol := NewSection(module.allSections, Sections.VarSection, name,NIL ,commentPrintout # NIL);
  8549. IntermediateCode.InitAddress(result.tag, addressType, symbol.name,0 , 0);
  8550. ELSE
  8551. END;
  8552. ELSE
  8553. ReleaseIntermediateOperand(result.tag);
  8554. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8555. END;
  8556. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  8557. IF type(SyntaxTree.MathArrayType).form IN {SyntaxTree.Open} THEN
  8558. ReleaseIntermediateOperand(result.tag);
  8559. result.tag := result.op;
  8560. UseIntermediateOperand(result.tag);
  8561. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8562. IntermediateCode.MakeMemory(result.op,addressType);
  8563. END;
  8564. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType = NIL) THEN
  8565. ReleaseIntermediateOperand(result.tag);
  8566. result.tag := TypeDescriptorAdr(type);
  8567. IF ~newObjectFile THEN
  8568. IntermediateCode.MakeMemory(result.tag,addressType);
  8569. END;
  8570. UseIntermediateOperand(result.tag);
  8571. (* tag for pointer type computed not here but during dereferencing *)
  8572. END;
  8573. IF Trace THEN TraceExit("VisitVariable") END;
  8574. END VisitVariable;
  8575. PROCEDURE VisitProperty(property: SyntaxTree.Property);
  8576. BEGIN
  8577. VisitVariable(property);
  8578. END VisitProperty;
  8579. PROCEDURE VisitParameter(x: SyntaxTree.Parameter);
  8580. VAR type: SyntaxTree.Type; basereg, mem: IntermediateCode.Operand; parameter: SyntaxTree.Parameter;adr: LONGINT; symbol: Sections.Section;
  8581. name: Basic.SegmentedName; parameterType, ptype: SyntaxTree.Type; len,inc: LONGINT; temp: IntermediateCode.Operand;
  8582. BEGIN
  8583. type := x.type.resolved;
  8584. IF Trace THEN TraceEnter("VisitParameter") END;
  8585. IF x.ownerType IS SyntaxTree.CellType THEN
  8586. ptype := x.type.resolved;
  8587. IF backend.cellsAreObjects THEN
  8588. ASSERT(result.mode = ModeReference);
  8589. IF result.op.mode = IntermediateCode.ModeMemory THEN
  8590. ReuseCopy(temp,result.op);
  8591. ReleaseIntermediateOperand(result.op);
  8592. result.op := temp;
  8593. END;
  8594. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8595. RETURN;
  8596. ELSE
  8597. InitOperand(result,ModeReference);
  8598. GetCodeSectionNameForSymbol(x,name);
  8599. symbol := NewSection(module.allSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8600. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0);
  8601. RETURN;
  8602. END;
  8603. ELSIF ~backend.cellsAreObjects & (currentScope IS SyntaxTree.ProcedureScope) & (currentScope(SyntaxTree.ProcedureScope).ownerProcedure.isConstructor) & (currentScope.outerScope IS SyntaxTree.CellScope) THEN
  8604. InitOperand(result,ModeReference);
  8605. GetCodeSectionNameForSymbol(x,name);
  8606. symbol := NewSection(module.allSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8607. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0);
  8608. RETURN
  8609. ELSE
  8610. GetBaseRegister(basereg,currentScope,x.scope);
  8611. InitOperand(result,ModeReference);
  8612. result.op := basereg;
  8613. END;
  8614. IF IsOpenArray(type) THEN
  8615. result.tag := basereg;
  8616. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8617. IntermediateCode.MakeMemory(result.op,addressType);
  8618. IF Global.IsOberonProcedure(x.ownerType) THEN
  8619. IntermediateCode.AddOffset(result.tag,ToMemoryUnits(system,x.offsetInBits+addressType.sizeInBits));
  8620. UseIntermediateOperand(result.tag);
  8621. ELSE
  8622. IntermediateCode.InitImmediate(result.tag,addressType,MAX(LONGINT)); (* non-Oberon procedure => unbounded array length *)
  8623. END;
  8624. ELSIF IsStaticArray(type) & (x.kind = SyntaxTree.ValueParameter) THEN
  8625. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8626. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8627. ELSIF IsStaticArray(type) THEN
  8628. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8629. IntermediateCode.MakeMemory(result.op,addressType);
  8630. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8631. ELSIF type IS SyntaxTree.MathArrayType THEN
  8632. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8633. WITH type: SyntaxTree.MathArrayType DO
  8634. IF (x.kind = SyntaxTree.ValueParameter) OR (x.kind = SyntaxTree.ConstParameter) THEN
  8635. IF type.form = SyntaxTree.Tensor THEN
  8636. ELSIF type.form = SyntaxTree.Open THEN
  8637. result.tag := result.op;
  8638. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8639. IntermediateCode.MakeMemory(result.op,addressType);
  8640. UseIntermediateOperand(result.tag);
  8641. ELSIF type.form = SyntaxTree.Static THEN
  8642. IF x.kind = SyntaxTree.ConstParameter THEN
  8643. IntermediateCode.MakeMemory(result.op,addressType);
  8644. END;
  8645. ELSE HALT(100)
  8646. END;
  8647. ELSIF x.kind = SyntaxTree.VarParameter THEN
  8648. IF type.form = SyntaxTree.Tensor THEN
  8649. ToMemory(result.op,addressType,0);
  8650. ELSIF type.form = SyntaxTree.Open THEN
  8651. MakeMemory(mem, result.op, addressType, 0); (* offset already added above *)
  8652. ReuseCopy(result.tag, mem);
  8653. ReleaseIntermediateOperand(mem);
  8654. ReleaseIntermediateOperand(result.op);
  8655. MakeMemory(result.op, result.tag, addressType, ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8656. ELSIF type.form = SyntaxTree.Static THEN
  8657. IntermediateCode.MakeMemory(result.op,addressType);
  8658. ELSE HALT(100)
  8659. END;
  8660. ELSE HALT(100)
  8661. END;
  8662. END;
  8663. ELSIF (x.kind = SyntaxTree.VarParameter) OR (x.kind = SyntaxTree.ConstParameter) & (type IS SyntaxTree.RecordType) THEN
  8664. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8665. IntermediateCode.MakeMemory(result.op,addressType);
  8666. ELSIF (x.kind = SyntaxTree.ValueParameter) OR (x.kind = SyntaxTree.ConstParameter) THEN
  8667. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8668. END;
  8669. IF conditional & (x.type.resolved IS SyntaxTree.BooleanType) THEN
  8670. ValueToCondition(result);
  8671. ELSIF type IS SyntaxTree.ProcedureType THEN
  8672. ReleaseIntermediateOperand(result.tag);
  8673. IF type(SyntaxTree.ProcedureType).isDelegate THEN
  8674. IF x.kind = SyntaxTree.VarParameter THEN
  8675. ReuseCopy(result.tag,result.op);
  8676. IntermediateCode.AddOffset(result.tag,ToMemoryUnits(system,system.addressSize));
  8677. IntermediateCode.MakeMemory(result.tag,addressType);
  8678. ELSE
  8679. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  8680. UseIntermediateOperand(result.tag);
  8681. END;
  8682. ELSE
  8683. result.tag := nil;
  8684. END;
  8685. (* tag for pointer type computed not here but during dereferencing *)
  8686. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType= NIL) & (x.kind IN {SyntaxTree.VarParameter, SyntaxTree.ConstParameter}) & ~(x.selfParameter) THEN
  8687. ReleaseIntermediateOperand(result.tag);
  8688. result.tag := basereg;
  8689. IntermediateCode.AddOffset(result.tag,ToMemoryUnits(system,x.offsetInBits+system.addressSize));
  8690. IntermediateCode.MakeMemory(result.tag,addressType);
  8691. UseIntermediateOperand(result.tag);
  8692. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType= NIL) & ((x.kind = SyntaxTree.ValueParameter) OR x.selfParameter) THEN
  8693. ReleaseIntermediateOperand(result.tag);
  8694. result.tag := TypeDescriptorAdr(type);
  8695. IF ~newObjectFile THEN
  8696. IntermediateCode.MakeMemory(result.tag,addressType);
  8697. END;
  8698. UseIntermediateOperand(result.tag);
  8699. END;
  8700. IF Trace THEN TraceExit("VisitParameter") END;
  8701. END VisitParameter;
  8702. PROCEDURE DynamicCallOperand(VAR operand: Operand; x: SyntaxTree.Procedure);
  8703. VAR tag,reg,tmp: IntermediateCode.Operand; offset: LONGINT; recordType: SyntaxTree.RecordType; name, stackFrame: Basic.SegmentedName;
  8704. BEGIN
  8705. IF Trace THEN TraceEnter("DynamicCallOperand") END;
  8706. (* left.p: left already emitted *)
  8707. tag := result.op; (* value of pointer to left *)
  8708. (* get type desc *)
  8709. tmp := result.tag;
  8710. IntermediateCode.MakeMemory(tmp,addressType);
  8711. (* get method adr *)
  8712. Reuse1(reg,tmp);
  8713. ReleaseIntermediateOperand(tmp);
  8714. IF backend.cooperative THEN
  8715. recordType := x.scope(SyntaxTree.RecordScope).ownerRecord;
  8716. WHILE recordType.baseType # NIL DO
  8717. recordType := recordType.GetBaseRecord ();
  8718. END;
  8719. GetRecordTypeName (recordType,name);
  8720. Basic.ToSegmentedName ("BaseTypes.StackFrame",stackFrame);
  8721. IF (name = stackFrame) OR HasExplicitTraceMethod (recordType) THEN
  8722. offset := 0;
  8723. ELSE
  8724. offset := 2;
  8725. END;
  8726. Emit(Add(position,reg,tmp,IntermediateCode.Immediate(addressType, ToMemoryUnits(system,system.addressSize *(meta.MethodTableOffset + x.methodNumber + offset)))));
  8727. ELSE
  8728. Emit(Add(position,reg,tmp,IntermediateCode.Immediate(addressType, ToMemoryUnits(system,system.addressSize *(meta.MethodTableOffset - x.methodNumber)))));
  8729. END;
  8730. InitOperand(operand,ModeReference);
  8731. (* then operand.op contains the method adr and operand.tag contains the potential self pointer value *)
  8732. operand.op := reg;
  8733. operand.tag := tag;
  8734. IF Trace THEN TraceExit("DynamicCallOperand") END;
  8735. END DynamicCallOperand;
  8736. PROCEDURE StaticCallOperand(VAR operand: Operand; x: SyntaxTree.Procedure);
  8737. VAR source: IntermediateCode.Section; tag,reg: IntermediateCode.Operand; name:Basic.SegmentedName; sectionType: SHORTINT;
  8738. binary: BinaryCode.Section; bits: SyntaxTree.BinaryCode;
  8739. BEGIN
  8740. IF Trace THEN TraceEnter("StaticCallOperand") END;
  8741. IF x.type(SyntaxTree.ProcedureType).isDelegate THEN
  8742. tag := operand.op;
  8743. ReleaseIntermediateOperand(operand.tag);
  8744. ELSE tag := nil
  8745. END;
  8746. IF x.isInline THEN
  8747. sectionType := Sections.InlineCodeSection;
  8748. ELSE
  8749. sectionType := Sections.CodeSection;
  8750. END;
  8751. IF x.externalName # NIL THEN
  8752. Basic.ToSegmentedName(x.externalName^, name);
  8753. IntermediateCode.InitAddress(reg, addressType, name, 0, 0);
  8754. ELSE
  8755. GetCodeSectionNameForSymbol(x, name);
  8756. IF (x.scope.ownerModule = module.module) THEN
  8757. source := NewSection(module.allSections, sectionType, name,x,commentPrintout # NIL);
  8758. ELSIF (sectionType = Sections.InlineCodeSection) & (x.procedureScope.body.code.sourceCode # NIL) THEN
  8759. source := NewSection(module.allSections, sectionType, name,x,commentPrintout # NIL);
  8760. IF source.pc = 0 THEN (* no code yet *)
  8761. source.Emit(Asm(position,x.procedureScope.body.code.sourceCode,NIL,NIL));
  8762. END;
  8763. ELSIF (sectionType = Sections.InlineCodeSection) & (x.procedureScope.body.code.inlineCode # NIL) THEN
  8764. bits := x.procedureScope.body.code.inlineCode;
  8765. source := NewSection(module.allSections, sectionType, name, x, commentPrintout # NIL);
  8766. binary := BinaryCode.NewBinarySection(source.type, source.priority, system.codeUnit, name, FALSE, FALSE);
  8767. binary.CopyBits(bits, 0, bits.GetSize());
  8768. source.SetResolved(binary);
  8769. ELSE
  8770. source := NewSection(module.importedSections, sectionType, name,x,commentPrintout # NIL);
  8771. END;
  8772. IntermediateCode.InitAddress(reg, addressType, source.name , GetFingerprint(source.symbol), 0);
  8773. END;
  8774. InitOperand(operand,ModeValue);
  8775. operand.op := reg;
  8776. operand.tag := tag;
  8777. IF Trace THEN TraceExit("StaticCallOperand") END;
  8778. END StaticCallOperand;
  8779. PROCEDURE VisitProcedure(x: SyntaxTree.Procedure);
  8780. (* handle expressions of the form designator.procedure or procedure *)
  8781. BEGIN
  8782. IF Trace THEN TraceEnter("VisitProcedure") END;
  8783. IF (x.type(SyntaxTree.ProcedureType).isDelegate) & ~SemanticChecker.IsStaticProcedure(x) & ~(result.tag.mode = IntermediateCode.ModeImmediate) THEN
  8784. DynamicCallOperand(result,x);
  8785. ELSIF x.isInline THEN
  8786. StaticCallOperand(result,x);
  8787. ELSE
  8788. StaticCallOperand(result,x);
  8789. END;
  8790. IF Trace THEN TraceExit("VisitProcedure") END;
  8791. END VisitProcedure;
  8792. PROCEDURE VisitOperator(x: SyntaxTree.Operator);
  8793. BEGIN
  8794. VisitProcedure(x);
  8795. END VisitOperator;
  8796. (** statements *)
  8797. PROCEDURE VisitProcedureCallStatement(x: SyntaxTree.ProcedureCallStatement);
  8798. BEGIN
  8799. IF Trace THEN TraceEnter("VisitProcedureCallStatement") END;
  8800. Expression(x.call);
  8801. IF (x.call.type # NIL) THEN (* WINAPI call: procedure returning unused value *)
  8802. ReleaseOperand(result)
  8803. END;
  8804. IF Trace THEN TraceExit("VisitProcedureCallStatement") END;
  8805. END VisitProcedureCallStatement;
  8806. PROCEDURE AssignMathArray(left,right: SyntaxTree.Expression);
  8807. VAR leftType, rightType: SyntaxTree.MathArrayType;
  8808. leftBase, rightBase: SyntaxTree.Type;
  8809. procedureName,s: SyntaxTree.IdentifierString;
  8810. arrayBase: SyntaxTree.Module; saved: RegisterEntry; procedure: SyntaxTree.Procedure; parameter: SyntaxTree.Parameter;
  8811. size: LONGINT; rightKind: LONGINT;
  8812. dummy: IntermediateCode.Operand;
  8813. CONST moduleName = "FoxArrayBase";
  8814. PROCEDURE OpenArray(from: SyntaxTree.MathArrayType): SyntaxTree.MathArrayType;
  8815. VAR result: SyntaxTree.MathArrayType; base: SyntaxTree.Type;
  8816. BEGIN
  8817. base := from(SyntaxTree.MathArrayType).arrayBase.resolved;
  8818. IF base IS SyntaxTree.MathArrayType THEN
  8819. base := OpenArray(base(SyntaxTree.MathArrayType));
  8820. END;
  8821. result := SyntaxTree.NewMathArrayType(left.position,currentScope,SyntaxTree.Open);
  8822. result.SetArrayBase(base);
  8823. RETURN result
  8824. END OpenArray;
  8825. BEGIN
  8826. IF AddImport(moduleName,arrayBase,TRUE) THEN
  8827. SaveRegisters();ReleaseUsedRegisters(saved);
  8828. leftType := left.type.resolved(SyntaxTree.MathArrayType);
  8829. rightType := right.type.resolved(SyntaxTree.MathArrayType);
  8830. leftBase := SemanticChecker.ArrayBase(leftType,MAX(LONGINT));
  8831. rightBase := SemanticChecker.ArrayBase(rightType,MAX(LONGINT));
  8832. ASSERT(leftBase.resolved.SameType(rightBase.resolved));
  8833. IF leftType.form = SyntaxTree.Tensor THEN
  8834. procedureName := "CopyTensor"; rightKind := SyntaxTree.ValueParameter;
  8835. ELSIF leftType.form = SyntaxTree.Open THEN
  8836. procedureName := "CopyArray"; rightKind := SyntaxTree.VarParameter;
  8837. ELSIF leftType.form = SyntaxTree.Static THEN
  8838. procedureName := "CopyArray";rightKind := SyntaxTree.VarParameter;
  8839. leftType := OpenArray(leftType); (* necessary since copy procedure presumes an open array *)
  8840. END;
  8841. procedure := arrayBase.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  8842. IF procedure = NIL THEN
  8843. s := "Instruction not supported on target, emulation procedure ";
  8844. Strings.Append(s,moduleName);
  8845. Strings.Append(s,".");
  8846. Strings.Append(s,procedureName);
  8847. Strings.Append(s," not present");
  8848. Error(position,s);
  8849. ELSE
  8850. parameter := SyntaxTree.NewParameter(left.position,procedure.type(SyntaxTree.ProcedureType),SyntaxTree.NewIdentifier("temp"), SyntaxTree.VarParameter);
  8851. parameter.SetType(leftType);
  8852. parameter.SetAccess(SyntaxTree.Internal);
  8853. PushParameter(left,parameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  8854. parameter.SetKind(rightKind);
  8855. PushParameter(right,parameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  8856. size := ToMemoryUnits(system,system.SizeOf(rightBase));
  8857. Emit(Push(position,IntermediateCode.Immediate(int32,size)));
  8858. StaticCallOperand(result,procedure);
  8859. Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  8860. ReleaseOperand(result);
  8861. END;
  8862. RestoreRegisters(saved);
  8863. END;
  8864. END AssignMathArray;
  8865. VAR modifyAssignmentCounter := 0: LONGINT;
  8866. PROCEDURE ModifyAssignments(CONST value: IntermediateCode.Operand);
  8867. VAR processor,mem,dst: IntermediateCode.Operand;
  8868. BEGIN
  8869. IF value.intValue = true.intValue THEN
  8870. INC(modifyAssignmentCounter);
  8871. IF (modifyAssignmentCounter > 1) THEN RETURN END;
  8872. modifyAssignmentsPC := section.pc;
  8873. ELSE
  8874. DEC(modifyAssignmentCounter);
  8875. IF (modifyAssignmentCounter > 0) THEN RETURN END;
  8876. INC(statCoopModifyAssignments , section.pc - modifyAssignmentsPC);
  8877. END;
  8878. IntermediateCode.InitMemory (processor, IntermediateCode.SignedIntegerType(addressType.sizeInBits), ap, ToMemoryUnits(system, ProcessorOffset * addressType.sizeInBits));
  8879. dst := NewRegisterOperand (addressType);
  8880. Emit(Mov(position,dst, processor));
  8881. IntermediateCode.InitMemory(mem,bool, dst, 0);
  8882. Emit(Mov(position,mem, value));
  8883. ReleaseIntermediateOperand(dst);
  8884. END ModifyAssignments;
  8885. PROCEDURE CopySize(left: SyntaxTree.Expression): IntermediateCode.Operand;
  8886. VAR type: SyntaxTree.Type; procedureType: SyntaxTree.ProcedureType; parameter: SyntaxTree.Parameter;
  8887. BEGIN
  8888. type := left.type.resolved;
  8889. IF (type IS SyntaxTree.RecordType) & (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  8890. parameter := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter);
  8891. procedureType := parameter.ownerType.resolved(SyntaxTree.ProcedureType);
  8892. IF procedureType.returnParameter = parameter THEN
  8893. RETURN IntermediateCode.Memory(addressType, fp, ToMemoryUnits(system, parameter.offsetInBits + system.addressSize));
  8894. END;
  8895. END;
  8896. RETURN IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.SizeOf(type)));
  8897. END CopySize;
  8898. PROCEDURE Assign(left,right: SyntaxTree.Expression);
  8899. VAR
  8900. leftO, rightO: Operand;
  8901. mem, sizeOp: IntermediateCode.Operand;
  8902. leftType, rightType, componentType: SyntaxTree.Type;
  8903. size: LONGINT;
  8904. parameters: SyntaxTree.ExpressionList;
  8905. procedure: SyntaxTree.Procedure;
  8906. call: SyntaxTree.ProcedureCallDesignator;
  8907. designator: SyntaxTree.Designator;
  8908. PROCEDURE CanPassAsResultParameter(right: SyntaxTree.Expression): BOOLEAN;
  8909. VAR procedureType: SyntaxTree.ProcedureType;
  8910. BEGIN
  8911. IF SemanticChecker.ReturnedAsParameter(right.type) THEN
  8912. IF right IS SyntaxTree.ProcedureCallDesignator THEN
  8913. procedureType := right(SyntaxTree.ProcedureCallDesignator).left.type.resolved(SyntaxTree.ProcedureType);
  8914. RETURN procedureType.callingConvention = SyntaxTree.OberonCallingConvention
  8915. ELSIF right IS SyntaxTree.BuiltinCallDesignator THEN
  8916. WITH right: SyntaxTree.BuiltinCallDesignator DO
  8917. IF right.id = Global.Reshape THEN RETURN TRUE
  8918. END;
  8919. END;
  8920. END;
  8921. END;
  8922. RETURN FALSE
  8923. END CanPassAsResultParameter;
  8924. BEGIN
  8925. ASSERT(left.type # NIL); ASSERT(right.type # NIL);
  8926. leftType := left.type.resolved; rightType:= right.type.resolved;
  8927. IF backend.cooperative & left.NeedsTrace() THEN
  8928. ModifyAssignments(true);
  8929. IF (leftType IS SyntaxTree.RecordType) OR IsStaticArray(leftType) THEN
  8930. Designate(right, rightO);
  8931. Designate(left, leftO);
  8932. ASSERT(leftO.mode = ModeReference);
  8933. TransferToRegister(leftO.op, leftO.op);
  8934. TransferToRegister(rightO.op, rightO.op);
  8935. Emit(Push(position, leftO.op));
  8936. Emit(Push(position, rightO.op));
  8937. CallAssignMethod(leftO.op, rightO.op, left.type);
  8938. Emit(Pop(position, rightO.op));
  8939. Emit(Pop(position, leftO.op));
  8940. sizeOp := CopySize(left);
  8941. Emit(Copy(position,leftO.op,rightO.op,sizeOp));
  8942. ReleaseOperand(leftO);
  8943. ReleaseOperand(rightO);
  8944. ELSE
  8945. Evaluate(right,rightO);
  8946. Designate(left,leftO);
  8947. ASSERT(leftO.mode = ModeReference);
  8948. IF (leftType IS SyntaxTree.ProcedureType) THEN
  8949. (* copy procedure address first *)
  8950. MakeMemory(mem,leftO.op,addressType,0);
  8951. Emit(Mov(position,mem,rightO.op));
  8952. ReleaseIntermediateOperand(mem);
  8953. (* copy pointer address *)
  8954. IntermediateCode.MakeAddress(leftO.tag, addressType);
  8955. CallAssignPointer(leftO.tag, rightO.tag);
  8956. ELSE
  8957. ASSERT(system.SizeOf(left.type) = system.addressSize);
  8958. CallAssignPointer(leftO.op, rightO.op);
  8959. END;
  8960. ReleaseOperand(leftO);
  8961. ReleaseOperand(rightO);
  8962. END;
  8963. ModifyAssignments(false);
  8964. RETURN;
  8965. END;
  8966. IF CanPassAsResultParameter(right) THEN
  8967. procedureResultDesignator := left(SyntaxTree.Designator);
  8968. Expression(right);
  8969. procedureResultDesignator := NIL;
  8970. ELSIF (right IS SyntaxTree.UnaryExpression) & (right(SyntaxTree.UnaryExpression).operator = Scanner.Alias) THEN
  8971. (* left <-- ALIAS OF right: zerocopy *)
  8972. IF GetRuntimeProcedure("FoxArrayBase","ZeroCopy",procedure,TRUE) THEN
  8973. designator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, NIL, procedure);
  8974. designator.SetType(procedure.type);
  8975. parameters := SyntaxTree.NewExpressionList(); parameters.AddExpression(right(SyntaxTree.UnaryExpression).left); parameters.AddExpression(left);
  8976. call := SyntaxTree.NewProcedureCallDesignator(position,designator(SyntaxTree.Designator),parameters);
  8977. VisitProcedureCallDesignator(call(SyntaxTree.ProcedureCallDesignator));
  8978. END;
  8979. ELSIF leftType IS SyntaxTree.RangeType THEN
  8980. (* LHS is of array range type *)
  8981. ASSERT(rightType IS SyntaxTree.RangeType); (* ensured by the checker *)
  8982. Evaluate(right, rightO);
  8983. Designate(left, leftO);(* The order is crucial. Do not reorder emission of left and right *)
  8984. (* first *)
  8985. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, system.longintType), 0);
  8986. Emit(Mov(position,mem, rightO.op));
  8987. ReleaseIntermediateOperand(mem);
  8988. (* last *)
  8989. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, system.longintType), ToMemoryUnits(system, system.SizeOf(system.longintType)));
  8990. Emit(Mov(position,mem, rightO.tag));
  8991. ReleaseIntermediateOperand(mem);
  8992. (* step *)
  8993. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, system.longintType), 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)));
  8994. Emit(Mov(position,mem, rightO.extra));
  8995. ReleaseIntermediateOperand(mem);
  8996. ReleaseOperand(rightO);
  8997. ReleaseOperand(leftO)
  8998. ELSIF leftType IS SyntaxTree.ComplexType THEN
  8999. ASSERT(leftType.SameType(rightType)); (* ensured by the checker *)
  9000. Evaluate(right, rightO);
  9001. Designate(left, leftO); (* The order is crucial. Do not reorder emission of left and right *)
  9002. componentType := leftType(SyntaxTree.ComplexType).componentType;
  9003. (* real part *)
  9004. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, componentType), 0);
  9005. Emit(Mov(position,mem, rightO.op));
  9006. ReleaseIntermediateOperand(mem);
  9007. (* imaginary part *)
  9008. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  9009. Emit(Mov(position,mem, rightO.tag));
  9010. ReleaseIntermediateOperand(mem);
  9011. ReleaseOperand(rightO);
  9012. ReleaseOperand(leftO)
  9013. ELSIF (leftType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.PointerType) OR (leftType IS SyntaxTree.EnumerationType)
  9014. OR (leftType IS SyntaxTree.PortType) THEN
  9015. (* rightO := leftO;*)
  9016. Evaluate(right,rightO);
  9017. (* DO NOT REORDER EMISSION OF LEFT AND RIGHT OPERAND *)
  9018. Designate(left,leftO);
  9019. IF leftO.mode = ModeReference THEN
  9020. MakeMemory(mem,leftO.op,IntermediateCode.GetType(system,left.type),0);
  9021. destination := mem;
  9022. ELSE
  9023. destination := leftO.op;
  9024. END;
  9025. ReleaseOperand(leftO);
  9026. IF destination.mode # IntermediateCode.Undefined THEN
  9027. Emit(Mov(position,destination,rightO.op));
  9028. END;
  9029. ReleaseOperand(rightO);
  9030. ReleaseIntermediateOperand(mem);
  9031. IntermediateCode.InitOperand(destination);
  9032. ELSIF (leftType IS SyntaxTree.ProcedureType) THEN
  9033. Evaluate(right,rightO);
  9034. Designate(left,leftO);
  9035. MakeMemory(mem,leftO.op,addressType,0);
  9036. Emit(Mov(position,mem,rightO.op));
  9037. ReleaseIntermediateOperand(mem);
  9038. IF leftType(SyntaxTree.ProcedureType).isDelegate THEN
  9039. (* delegate *)
  9040. (*
  9041. MakeMemory(leftO.tag,leftO.tag,addressType); no! is already memory
  9042. *)
  9043. Emit(Mov(position,leftO.tag,rightO.tag));
  9044. END;
  9045. ReleaseOperand(leftO);
  9046. ReleaseOperand(rightO);
  9047. ELSIF (leftType IS SyntaxTree.RecordType) THEN
  9048. Designate(right,rightO);
  9049. Designate(left,leftO);
  9050. sizeOp := CopySize(left);
  9051. Emit(Copy(position,leftO.op,rightO.op,sizeOp));
  9052. ReleaseOperand(leftO); ReleaseOperand(rightO);
  9053. ELSIF (leftType IS SyntaxTree.ArrayType) THEN
  9054. IF (rightType IS SyntaxTree.StringType) THEN
  9055. CopyString(left,right);
  9056. ELSIF ((rightType IS SyntaxTree.ArrayType) & (rightType(SyntaxTree.ArrayType).staticLength # 0) OR (rightType IS SyntaxTree.MathArrayType) & (rightType(SyntaxTree.MathArrayType).staticLength # 0)) & (leftType(SyntaxTree.ArrayType).staticLength # 0) THEN
  9057. Designate(right,rightO);
  9058. Designate(left,leftO);
  9059. size := ToMemoryUnits(system,system.SizeOf(rightType));
  9060. Emit(Copy(position,leftO.op, rightO.op, IntermediateCode.Immediate(addressType,size)));
  9061. ReleaseOperand(leftO); ReleaseOperand(rightO);
  9062. ELSE
  9063. HALT(201)
  9064. END;
  9065. ELSIF (leftType IS SyntaxTree.MathArrayType) THEN
  9066. IF (leftType(SyntaxTree.MathArrayType).staticLength # 0) & (rightType IS SyntaxTree.MathArrayType) & (rightType(SyntaxTree.MathArrayType).staticLength # 0) THEN
  9067. Designate(right,rightO);
  9068. Designate(left,leftO);
  9069. size := ToMemoryUnits(system,system.SizeOf(rightType));
  9070. Emit(Copy(position,leftO.op, rightO.op, IntermediateCode.Immediate(addressType,size)));
  9071. ReleaseOperand(leftO); ReleaseOperand(rightO);
  9072. END;
  9073. AssignMathArray(left,right);
  9074. ELSE
  9075. HALT(200);
  9076. END;
  9077. END Assign;
  9078. PROCEDURE VisitAssignment(x: SyntaxTree.Assignment);
  9079. BEGIN
  9080. IF Trace THEN TraceEnter("VisitAssignment") END;
  9081. Assign(x.left,x.right);
  9082. IF Trace THEN TraceExit("VisitAssignment") END;
  9083. END VisitAssignment;
  9084. PROCEDURE EmitCooperativeSwitch;
  9085. VAR quantum, offset, zero: IntermediateCode.Operand; skip: Label; pc: LONGINT;
  9086. BEGIN
  9087. ASSERT (cooperativeSwitches);
  9088. pc := section.pc;
  9089. IF lastSwitchPC = section.pc THEN RETURN END;
  9090. IntermediateCode.InitMemory (quantum, IntermediateCode.SignedIntegerType(addressType.sizeInBits), ap, ToMemoryUnits(system, QuantumOffset * addressType.sizeInBits));
  9091. IntermediateCode.InitImmediate(offset, quantum.type, section.pc - lastSwitchPC); IntermediateCode.InitImmediate(zero, quantum.type, 0);
  9092. Emit(Sub(position,quantum,quantum, offset)); skip := NewLabel(); BrgeL(skip, quantum, zero);
  9093. lastSwitchPC := section.pc; CallThis(position,"Activities","Switch",0); SetLabel(skip);
  9094. INC(statCoopSwitch, section.pc - pc);
  9095. END EmitCooperativeSwitch;
  9096. PROCEDURE VisitCommunicationStatement(communication: SyntaxTree.CommunicationStatement);
  9097. VAR p0,p1,tmp: SyntaxTree.Expression; s0,s1: Operand; size: LONGINT;
  9098. BEGIN
  9099. p0 := communication.left; p1 := communication.right;
  9100. IF (communication.op = Scanner.ExclamationMark) OR (communication.op = Scanner.LessLess) & (communication.left.type.resolved IS SyntaxTree.PortType) THEN
  9101. Evaluate(p0,s0);
  9102. Evaluate(p1,s1);
  9103. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  9104. Emit(Push(position,s0.op));
  9105. Emit(Push(position,s1.op));
  9106. (*
  9107. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  9108. *)
  9109. IF ~backend.cellsAreObjects THEN
  9110. IF size > ToMemoryUnits(system, system.addressSize) THEN Error(p1.position,"send not implemented for complex data types") END;
  9111. END;
  9112. ReleaseOperand(s0);
  9113. ReleaseOperand(s1);
  9114. IF backend.cellsAreObjects THEN
  9115. CallThis(position,"ActiveCellsRuntime","Send",2);
  9116. ELSE
  9117. CallThis(position,ChannelModuleName,"Send",2);
  9118. END;
  9119. (* ----- RECEIVE ------*)
  9120. ELSE
  9121. IF (communication.op = Scanner.LessLess) & (communication.right.type.resolved IS SyntaxTree.PortType) THEN
  9122. tmp := p0; p0 := p1; p1 := tmp;
  9123. END;
  9124. Evaluate(p0,s0);
  9125. Emit(Push(position,s0.op));
  9126. Designate(p1,s1);
  9127. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  9128. Emit(Push(position,s1.op));
  9129. (*
  9130. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  9131. *)
  9132. IF ~backend.cellsAreObjects THEN
  9133. IF size > ToMemoryUnits(system, system.addressSize) THEN Error(p1.position,"receive not implemented for complex data types") END;
  9134. END;
  9135. ReleaseOperand(s0);
  9136. ReleaseOperand(s1);
  9137. IF backend.cellsAreObjects THEN
  9138. CallThis(position,"ActiveCellsRuntime","Receive",2);
  9139. ELSE
  9140. CallThis(position,ChannelModuleName,"Receive",2)
  9141. END;
  9142. END;
  9143. END VisitCommunicationStatement;
  9144. PROCEDURE VisitIfStatement(x: SyntaxTree.IfStatement);
  9145. VAR end: Label; i,elsifs: LONGINT; elsif: SyntaxTree.IfPart; escape: BOOLEAN;
  9146. PROCEDURE IfPart(if: SyntaxTree.IfPart);
  9147. VAR true, false: Label; condition, value: BOOLEAN;
  9148. BEGIN
  9149. condition := ~SemanticChecker.IsBooleanValue(if.condition, value);
  9150. IF condition THEN
  9151. true := NewLabel();
  9152. false := NewLabel();
  9153. Condition(if.condition,true,false);
  9154. SetLabel(true);
  9155. StatementSequence(if.statements);
  9156. BrL(end);
  9157. SetLabel(false);
  9158. ELSE
  9159. IF value THEN (* always true *)
  9160. escape := TRUE;
  9161. StatementSequence(if.statements);
  9162. (* no branch necessary -- rest skipped *)
  9163. END;
  9164. END;
  9165. END IfPart;
  9166. BEGIN
  9167. IF Trace THEN TraceEnter("VisitIfStatement") END;
  9168. end := NewLabel();
  9169. elsifs := x.ElsifParts();
  9170. IfPart(x.ifPart);
  9171. FOR i := 0 TO elsifs-1 DO
  9172. IF ~escape THEN
  9173. elsif := x.GetElsifPart(i);
  9174. IfPart(elsif);
  9175. END;
  9176. END;
  9177. IF (x.elsePart # NIL) & ~escape THEN
  9178. StatementSequence(x.elsePart);
  9179. END;
  9180. SetLabel(end);
  9181. IF Trace THEN TraceExit("VisitIfStatement") END;
  9182. END VisitIfStatement;
  9183. PROCEDURE WithPart(x: SyntaxTree.WithPart; VAR falseL, endL: Label);
  9184. VAR trueL: Label; res: Operand; recordType: SyntaxTree.RecordType;
  9185. BEGIN
  9186. (*IF x.variable.type.resolved = x.type.resolved THEN
  9187. (* always true, do nothing *)
  9188. ELSE*)
  9189. Designate(x.variable,res);
  9190. IF IsPointerToRecord(x.variable.type,recordType) THEN
  9191. Dereference(res,recordType,IsUnsafePointer(x.variable.type))
  9192. END;
  9193. trueL := NewLabel();
  9194. TypeTest(res.tag,x.type,trueL,falseL);
  9195. ReleaseOperand(res);
  9196. SetLabel(trueL);
  9197. StatementSequence(x.statements);
  9198. BrL(endL);
  9199. END WithPart;
  9200. PROCEDURE VisitWithStatement(x: SyntaxTree.WithStatement);
  9201. VAR endL,falseL: Label;i: LONGINT;
  9202. BEGIN
  9203. IF Trace THEN TraceEnter("VisitWithStatement") END;
  9204. endL := NewLabel();
  9205. FOR i := 0 TO x.WithParts()-1 DO
  9206. falseL := NewLabel();
  9207. WithPart(x.GetWithPart(i),falseL,endL);
  9208. SetLabel(falseL);
  9209. END;
  9210. IF x.elsePart = NIL THEN
  9211. IF ~isUnchecked THEN
  9212. EmitTrap(position,WithTrap);
  9213. END;
  9214. ELSE
  9215. StatementSequence(x.elsePart)
  9216. END;
  9217. SetLabel(endL);
  9218. IF Trace THEN TraceExit("VisitWithStatement") END;
  9219. END VisitWithStatement;
  9220. PROCEDURE VisitCaseStatement(x: SyntaxTree.CaseStatement);
  9221. VAR var: Operand; jmp,res,op,tmp: IntermediateCode.Operand; j,i,size: LONGINT; part: SyntaxTree.CasePart; constant: SyntaxTree.CaseConstant;
  9222. out,else: Label; label: Label;
  9223. fixups: POINTER TO ARRAY OF Label; section: IntermediateCode.Section; name: Basic.SegmentedName; string: ARRAY 32 OF CHAR;
  9224. symbol: SyntaxTree.Symbol;
  9225. BEGIN
  9226. (*! split case statement into if-elsif statements for large case label lists *)
  9227. IF Trace THEN TraceEnter("VisitCaseStatement") END;
  9228. size := x.max-x.min+1;
  9229. IF (size<0) OR (size > 1024*1024) THEN Error(x.position,"implementation restriction: case table size too large"); RETURN
  9230. END;
  9231. Evaluate(x.variable,var);
  9232. ReuseCopy(tmp,var.op);
  9233. ReleaseIntermediateOperand(var.op);
  9234. var.op := tmp;
  9235. Emit(Sub(position,var.op,var.op,IntermediateCode.Immediate(IntermediateCode.GetType(system,x.variable.type),x.min)));
  9236. Convert(var.op,addressType);
  9237. size := x.max-x.min+1;
  9238. else := NewLabel();
  9239. BrgeL(else,var.op,IntermediateCode.Immediate(addressType,size));
  9240. (*
  9241. UniqueId(name,module.module,"case",caseId);
  9242. *)
  9243. string := "@case"; Basic.AppendNumber(string, caseId); INC(caseId);
  9244. Global.GetModuleSegmentedName(module.module, name);
  9245. Basic.SuffixSegmentedName(name,Basic.MakeString(string));
  9246. symbol := SyntaxTree.NewSymbol(name[1]);
  9247. symbol.SetScope(moduleScope);
  9248. NEW(fixups,size); FOR i := 0 TO size-1 DO fixups[i] := NIL END;
  9249. section := NewSection(module.allSections, Sections.ConstSection,name,SyntaxTree.NewSymbol(name[1]),commentPrintout # NIL);
  9250. section.isCaseTable := TRUE;
  9251. IntermediateCode.InitAddress(jmp, addressType, section.name, GetFingerprint(section.symbol), 0);
  9252. ReuseCopy(res,var.op);
  9253. ReleaseOperand(var);
  9254. Emit(Mul(position,res,res,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.addressSize))));
  9255. Emit(Add(position,res,res,jmp));
  9256. IntermediateCode.MakeMemory(res,addressType);
  9257. Emit(Br(position,res));
  9258. ReleaseIntermediateOperand(res);
  9259. out := NewLabel();
  9260. FOR i := 0 TO x.caseParts.Length()-1 DO (* case parts *)
  9261. part := x.GetCasePart(i);
  9262. constant := part.firstConstant;
  9263. label := NewLabel();
  9264. SetLabel(label);
  9265. WHILE(constant # NIL) DO (* case labels for this case part *)
  9266. FOR j := constant.min TO constant.max DO
  9267. fixups[j-x.min] := label;
  9268. END;
  9269. constant := constant.next;
  9270. END;
  9271. StatementSequence(part.statements);
  9272. BrL(out);
  9273. END;
  9274. SetLabel(else);
  9275. FOR i := 0 TO size-1 DO
  9276. IF fixups[i] = NIL THEN
  9277. fixups[i] := else;
  9278. END;
  9279. END;
  9280. IF x.elsePart # NIL THEN
  9281. StatementSequence(x.elsePart);
  9282. ELSIF ~isUnchecked THEN
  9283. EmitTrap(position,CaseTrap);
  9284. END;
  9285. SetLabel(out);
  9286. FOR i := 0 TO size-1 DO
  9287. IntermediateCode.InitAddress(op, addressType, fixups[i].section.name, GetFingerprint(fixups[i].section.symbol), fixups[i].pc);
  9288. section.Emit(Data(position,op));
  9289. END;
  9290. IF Trace THEN TraceExit("VisitCaseStatement") END;
  9291. END VisitCaseStatement;
  9292. PROCEDURE VisitWhileStatement(x: SyntaxTree.WhileStatement);
  9293. VAR start: Label; true,false: Label;
  9294. BEGIN
  9295. IF Trace THEN TraceEnter("VisitWhileStatement") END;
  9296. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9297. start := NewLabel();
  9298. true := NewLabel();
  9299. false := NewLabel();
  9300. SetLabel(start);
  9301. Condition(x.condition,true,false);
  9302. SetLabel(true);
  9303. StatementSequence(x.statements);
  9304. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9305. BrL(start);
  9306. SetLabel(false);
  9307. IF Trace THEN TraceExit("VisitWhileStatement") END;
  9308. END VisitWhileStatement;
  9309. PROCEDURE VisitRepeatStatement(x: SyntaxTree.RepeatStatement);
  9310. VAR false,true: Label;
  9311. BEGIN
  9312. IF Trace THEN TraceEnter("VisitRepeatStatement") END;
  9313. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9314. true := NewLabel();
  9315. false := NewLabel();
  9316. SetLabel(false);
  9317. StatementSequence(x.statements);
  9318. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9319. Condition(x.condition,true,false);
  9320. SetLabel(true);
  9321. IF Trace THEN TraceExit("VisitRepeatStatement") END;
  9322. END VisitRepeatStatement;
  9323. PROCEDURE VisitForStatement(x: SyntaxTree.ForStatement);
  9324. VAR
  9325. binary: SyntaxTree.BinaryExpression; start,true,false : Label; cmp: LONGINT; by: HUGEINT;
  9326. temporaryVariable: SyntaxTree.Variable;
  9327. temporaryVariableDesignator : SyntaxTree.Designator;
  9328. BEGIN
  9329. IF Trace THEN TraceEnter("VisitForStatement") END;
  9330. true := NewLabel();
  9331. false := NewLabel();
  9332. start := NewLabel();
  9333. Assign(x.variable,x.from);
  9334. temporaryVariable := GetTemporaryVariable(x.variable.type, FALSE);
  9335. temporaryVariableDesignator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, NIL, temporaryVariable);
  9336. temporaryVariableDesignator.SetType(x.variable.type.resolved);
  9337. Assign(temporaryVariableDesignator,x.to);
  9338. IF x.by = NIL THEN by := 1 ELSE by := x.by.resolved(SyntaxTree.IntegerValue).hvalue END;
  9339. IF by > 0 THEN
  9340. cmp := Scanner.LessEqual
  9341. ELSE
  9342. cmp := Scanner.GreaterEqual
  9343. END;
  9344. binary := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.variable,temporaryVariableDesignator,cmp);
  9345. binary.SetType(system.booleanType);
  9346. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9347. SetLabel(start);
  9348. Condition(binary,true,false);
  9349. SetLabel(true);
  9350. StatementSequence(x.statements);
  9351. binary := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.variable,x.by,Scanner.Plus);
  9352. binary.SetType(x.variable.type);
  9353. Assign(x.variable,binary);
  9354. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9355. BrL(start);
  9356. SetLabel(false);
  9357. IF Trace THEN TraceExit("VisitForStatement") END;
  9358. END VisitForStatement;
  9359. PROCEDURE VisitExitableBlock(x: SyntaxTree.ExitableBlock);
  9360. VAR prevLoop: Label;
  9361. BEGIN
  9362. IF Trace THEN TraceEnter("VisitExitableBlock") END;
  9363. prevLoop := currentLoop;
  9364. currentLoop := NewLabel();
  9365. StatementSequence(x.statements);
  9366. SetLabel(currentLoop);
  9367. currentLoop := prevLoop;
  9368. IF Trace THEN TraceExit("VisitExitableBlock") END;
  9369. END VisitExitableBlock;
  9370. PROCEDURE VisitLoopStatement(x: SyntaxTree.LoopStatement);
  9371. VAR prevLoop,start: Label;
  9372. BEGIN
  9373. IF Trace THEN TraceEnter("VisitLoopStatement") END;
  9374. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9375. start := NewLabel();
  9376. prevLoop := currentLoop;
  9377. SetLabel(start);
  9378. currentLoop := NewLabel();
  9379. StatementSequence(x.statements);
  9380. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9381. BrL(start);
  9382. SetLabel(currentLoop);
  9383. currentLoop := prevLoop;
  9384. IF Trace THEN TraceExit("VisitLoopStatement") END;
  9385. END VisitLoopStatement;
  9386. PROCEDURE VisitExitStatement(x: SyntaxTree.ExitStatement);
  9387. VAR outer: SyntaxTree.Statement;
  9388. BEGIN
  9389. IF Trace THEN TraceEnter("VisitExitStatement") END;
  9390. IF locked THEN (* r if we jump out of an exclusive block *)
  9391. outer := x.outer;
  9392. WHILE ~(outer IS SyntaxTree.ExitableBlock) & ~((outer IS SyntaxTree.StatementBlock) & outer(SyntaxTree.StatementBlock).isExclusive) DO
  9393. outer := outer.outer;
  9394. END;
  9395. IF ~(outer IS SyntaxTree.ExitableBlock) THEN
  9396. Lock(FALSE);
  9397. END;
  9398. END;
  9399. BrL(currentLoop);
  9400. IF Trace THEN TraceExit("VisitExitStatement") END;
  9401. END VisitExitStatement;
  9402. PROCEDURE VisitReturnStatement(x: SyntaxTree.ReturnStatement);
  9403. VAR
  9404. expression, parameterDesignator: SyntaxTree.Expression;
  9405. type, componentType: SyntaxTree.Type;
  9406. res, right: Operand;
  9407. left, mem, reg: IntermediateCode.Operand;
  9408. parameter: SyntaxTree.Parameter;
  9409. procedure: SyntaxTree.Procedure;
  9410. procedureType: SyntaxTree.ProcedureType;
  9411. returnTypeOffset: LONGINT;
  9412. delegate: BOOLEAN;
  9413. map: SymbolMap;
  9414. cc, parametersSize: LONGINT;
  9415. BEGIN
  9416. IF Trace THEN TraceEnter("VisitReturnStatement") END;
  9417. expression := x.returnValue;
  9418. procedure := currentScope(SyntaxTree.ProcedureScope).ownerProcedure;
  9419. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9420. IF currentIsInline THEN
  9421. IF expression # NIL THEN
  9422. map := currentMapper.Get(NIL);
  9423. IF map # NIL THEN
  9424. Assign(map.to, expression);
  9425. ELSE
  9426. Evaluate(expression,res);
  9427. Emit(Return(position,res.op));
  9428. ReleaseOperand(res);
  9429. END;
  9430. END;
  9431. BrL(currentInlineExit);
  9432. RETURN;
  9433. END;
  9434. IF expression # NIL THEN
  9435. type := expression.type.resolved;
  9436. IF (expression IS SyntaxTree.ResultDesignator) THEN
  9437. IF locked THEN Lock(FALSE) END;
  9438. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9439. (* "RETURN RESULT" -> no assignment, it is assumed that result has been written to return parameter via structured return type *)
  9440. ELSIF (type IS SyntaxTree.BasicType) & ~(type IS SyntaxTree.RangeType) & ~(type IS SyntaxTree.ComplexType) & ~type.IsPointer() OR (procedureType.callingConvention # SyntaxTree.OberonCallingConvention) THEN
  9441. (* return without structured return parameter *)
  9442. Evaluate(expression,res);
  9443. delegate := (type IS SyntaxTree.ProcedureType) & (type(SyntaxTree.ProcedureType).isDelegate);
  9444. IF locked OR profile THEN
  9445. Emit(Push(position,res.op));
  9446. IF delegate THEN HALT(200) END;
  9447. ReleaseOperand(res);
  9448. IF locked THEN Lock(FALSE) END;
  9449. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9450. reg := NewRegisterOperand(res.op.type);
  9451. Emit(Pop(position,reg));
  9452. Emit(Return(position,reg));
  9453. ReleaseIntermediateOperand(reg);
  9454. ELSE
  9455. Emit(Return(position,res.op));
  9456. ReleaseOperand(res);
  9457. END;
  9458. ELSIF (type IS SyntaxTree.RecordType) OR (type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.RangeType) OR (type IS SyntaxTree.ComplexType) OR type.IsPointer()
  9459. THEN
  9460. (* return using structured return parameter *)
  9461. ASSERT((type IS SyntaxTree.RecordType) OR (type IS SyntaxTree.RangeType) OR (type IS SyntaxTree.ComplexType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  9462. OR SemanticChecker.IsPointerType(type));
  9463. (* parameter := currentScope(SyntaxTree.ProcedureScope).FindParameter(Global.ReturnParameterName); *)
  9464. parameter :=procedureType.returnParameter;
  9465. ASSERT(parameter # NIL);
  9466. returnTypeOffset := parameter.offsetInBits;
  9467. (*
  9468. IF parameter# NIL THEN
  9469. returnTypeOffset := parameter.offsetInBits + system.SizeOfParameter(parameter);
  9470. INC(returnTypeOffset,(-returnTypeOffset) MOD system.AlignmentOf(system.parameterAlignment,parameter.type));
  9471. ELSE
  9472. returnTypeOffset := system.offsetFirstParameter
  9473. END;
  9474. *)
  9475. left := IntermediateCode.Memory(addressType,fp,ToMemoryUnits(system,returnTypeOffset));
  9476. IF type IS SyntaxTree.RangeType THEN
  9477. (* array range type *)
  9478. Evaluate(expression, right);
  9479. MakeMemory(mem, left, IntermediateCode.GetType(system, system.longintType), 0);
  9480. Emit(Mov(position,mem, right.op)); (* first *)
  9481. ReleaseIntermediateOperand(mem);
  9482. MakeMemory(mem, left, IntermediateCode.GetType(system, system.longintType), ToMemoryUnits(system, system.SizeOf(system.longintType)));
  9483. Emit(Mov(position,mem, right.tag)); (* last *)
  9484. ReleaseIntermediateOperand(mem);
  9485. MakeMemory(mem, left, IntermediateCode.GetType(system, system.longintType), 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)));
  9486. Emit(Mov(position,mem, right.extra)); (* step *)
  9487. ReleaseIntermediateOperand(mem);
  9488. ReleaseOperand(right);
  9489. ELSIF type IS SyntaxTree.ComplexType THEN
  9490. Evaluate(expression, right);
  9491. componentType := type(SyntaxTree.ComplexType).componentType;
  9492. MakeMemory(mem, left, IntermediateCode.GetType(system, componentType), 0);
  9493. Emit(Mov(position,mem, right.op)); (* real part *)
  9494. ReleaseIntermediateOperand(mem);
  9495. MakeMemory(mem, left, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  9496. Emit(Mov(position,mem, right.tag)); (* imaginary part *)
  9497. ReleaseIntermediateOperand(mem);
  9498. ReleaseOperand(right);
  9499. ELSE (* covers cases: pointer / record / array *)
  9500. parameter := procedureType.returnParameter;
  9501. checker.SetCurrentScope(currentScope);
  9502. ASSERT(parameter # NIL);
  9503. parameterDesignator := checker.NewSymbolDesignator(expression.position,NIL,parameter);
  9504. Assign(parameterDesignator,expression);
  9505. END;
  9506. ReleaseIntermediateOperand(left);
  9507. IF locked THEN Lock(FALSE) END;
  9508. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9509. ELSIF (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.ProcedureType) THEN
  9510. parameter := procedureType.returnParameter;
  9511. checker.SetCurrentScope(currentScope);
  9512. IF parameter = NIL THEN
  9513. Error(procedure.position, "structured return of parameter of procedure not found");
  9514. ELSE
  9515. parameterDesignator := checker.NewSymbolDesignator(expression.position,NIL,parameter);
  9516. Assign(parameterDesignator,expression);
  9517. END;
  9518. IF locked THEN Lock(FALSE) END;
  9519. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9520. ELSE
  9521. HALT(200);
  9522. END;
  9523. ELSE
  9524. IF locked THEN Lock(FALSE) END;
  9525. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9526. END;
  9527. IF backend.cooperative THEN
  9528. BrL(exitLabel);
  9529. ELSE
  9530. cc := procedureType(SyntaxTree.ProcedureType).callingConvention;
  9531. IF cc = SyntaxTree.WinAPICallingConvention THEN
  9532. parametersSize := ProcedureParametersSize(backend.system,procedure);
  9533. ELSE
  9534. parametersSize := 0;
  9535. END;
  9536. EmitLeave(section, position,procedure, procedure.type(SyntaxTree.ProcedureType).callingConvention);
  9537. Emit(Exit(position,procedure.type(SyntaxTree.ProcedureType).pcOffset,procedure.type(SyntaxTree.ProcedureType).callingConvention, parametersSize));
  9538. END;
  9539. IF Trace THEN TraceExit("VisitReturnStatement") END;
  9540. END VisitReturnStatement;
  9541. PROCEDURE MakeAwaitProcedure(x: SyntaxTree.AwaitStatement): SyntaxTree.Procedure;
  9542. VAR procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; procedureScope: SyntaxTree.ProcedureScope;
  9543. identifier: SyntaxTree.Identifier; body: SyntaxTree.Body; returnStatement : SyntaxTree.ReturnStatement;
  9544. statements: SyntaxTree.StatementSequence;
  9545. name, suffix: SyntaxTree.IdentifierString;
  9546. BEGIN
  9547. Strings.IntToStr(awaitProcCounter,suffix);
  9548. Strings.Concat("@AwaitProcedure",suffix,name);
  9549. identifier := SyntaxTree.NewIdentifier(name);
  9550. INC(awaitProcCounter);
  9551. ASSERT(currentScope IS SyntaxTree.ProcedureScope);
  9552. procedureScope := SyntaxTree.NewProcedureScope(currentScope);
  9553. ASSERT(procedureScope.outerScope IS SyntaxTree.ProcedureScope);
  9554. procedure := SyntaxTree.NewProcedure(x.position,identifier,procedureScope);
  9555. procedure.SetAccess(SyntaxTree.Hidden);
  9556. procedure.SetScope(currentScope);
  9557. procedureType := SyntaxTree.NewProcedureType(x.position,currentScope);
  9558. procedureType.SetReturnType(system.booleanType);
  9559. procedure.SetType(procedureType);
  9560. body := SyntaxTree.NewBody(x.position,procedureScope);
  9561. procedureScope.SetBody(body);
  9562. returnStatement := SyntaxTree.NewReturnStatement(x.position,body);
  9563. returnStatement.SetReturnValue(x.condition);
  9564. statements := SyntaxTree.NewStatementSequence();
  9565. statements.AddStatement(returnStatement);
  9566. body.SetStatementSequence(statements);
  9567. currentScope.AddProcedure(procedure);
  9568. RETURN procedure
  9569. END MakeAwaitProcedure;
  9570. PROCEDURE VisitAwaitStatement(x: SyntaxTree.AwaitStatement);
  9571. VAR proc: SyntaxTree.Procedure; res: IntermediateCode.Operand; symbol: Sections.Section;
  9572. call: IntermediateCode.Operand; label, start, true, false: Label; name: Basic.SegmentedName;
  9573. BEGIN
  9574. IF Trace THEN TraceEnter("VisitAwaitStatement") END;
  9575. IF profile THEN ProfilerEnterExit(numberProcedures, FALSE) END;
  9576. IF backend.cooperative THEN
  9577. start := NewLabel();
  9578. true := NewLabel();
  9579. false := NewLabel();
  9580. SetLabel(start);
  9581. Condition(x.condition,true,false);
  9582. SetLabel(false);
  9583. PushSelfPointer();
  9584. CallThis(position,"ExclusiveBlocks","Await",1);
  9585. BrL(start);
  9586. SetLabel(true);
  9587. PushSelfPointer();
  9588. CallThis(position,"ExclusiveBlocks","FinalizeAwait",1);
  9589. ELSE
  9590. proc := MakeAwaitProcedure(x);
  9591. Emit(Push(position,fp));
  9592. GetCodeSectionNameForSymbol(proc,name);
  9593. symbol := NewSection(module.allSections, Sections.CodeSection, name,proc,commentPrintout # NIL);
  9594. IntermediateCode.InitAddress(call,addressType,name, GetFingerprint(proc), 0);
  9595. res := NewRegisterOperand(IntermediateCode.GetType(system,system.booleanType));
  9596. Emit(Call(position,call,ProcedureParametersSize(system,proc)));
  9597. Emit(Result(position,res));
  9598. (*
  9599. AcquireThisRegister(IntermediateCode.GetType(system,system.booleanType),IntermediateCode.Result);
  9600. IntermediateCode.InitRegister(res,IntermediateCode.GetType(system,system.booleanType),IntermediateCode.Result);
  9601. *)
  9602. InitOperand(result,ModeValue);
  9603. result.op := res;
  9604. label := NewLabel();
  9605. BreqL(label, result.op, SELF.true);
  9606. ReleaseOperand(result);
  9607. symbol := NewSection(module.allSections, Sections.CodeSection, name,proc,commentPrintout # NIL);
  9608. IntermediateCode.InitAddress(res, addressType, name,GetFingerprint(proc), 0);
  9609. Emit(Push(position,res));
  9610. Emit(Push(position,fp));
  9611. PushSelfPointer();
  9612. Emit(Push(position,nil));
  9613. CallThis(position,"Objects","Await",4);
  9614. SetLabel(label);
  9615. END;
  9616. IF profile THEN ProfilerEnterExit(numberProcedures, TRUE) END;
  9617. IF Trace THEN TraceExit("VisitAwaitStatement") END;
  9618. END VisitAwaitStatement;
  9619. PROCEDURE StatementSequence(x: SyntaxTree.StatementSequence);
  9620. VAR statement: SyntaxTree.Statement; i: LONGINT; (* pos: LONGINT; *)
  9621. BEGIN
  9622. FOR i := 0 TO x.Length() - 1 DO
  9623. statement := x.GetStatement( i );
  9624. Statement(statement);
  9625. IF cooperativeSwitches & (section.pc - lastSwitchPC > 1000) THEN EmitCooperativeSwitch END;
  9626. END;
  9627. END StatementSequence;
  9628. PROCEDURE PushSelfPointer;
  9629. VAR scope: SyntaxTree.Scope; op: Operand; moduleSection: IntermediateCode.Section; moduleOffset, parametersSize: LONGINT; procedure: SyntaxTree.Procedure;
  9630. procedureType: SyntaxTree.ProcedureType;
  9631. BEGIN
  9632. scope := currentScope;
  9633. WHILE(scope.outerScope IS SyntaxTree.ProcedureScope) DO
  9634. scope := scope.outerScope;
  9635. END;
  9636. IF scope.outerScope IS SyntaxTree.ModuleScope THEN
  9637. IF ~newObjectFile THEN
  9638. Symbol(moduleSelf,op);
  9639. IntermediateCode.MakeMemory(op.op,addressType);
  9640. ELSE
  9641. moduleSection := meta.ModuleSection();
  9642. IF backend.cooperative THEN
  9643. moduleOffset := 0;
  9644. ELSE
  9645. moduleOffset := moduleSection.pc;
  9646. END;
  9647. op.op := IntermediateCode.Address(addressType, moduleSection.name, GetFingerprint(moduleSection.symbol), moduleOffset);
  9648. END;
  9649. ELSE
  9650. GetBaseRegister(op.op,currentScope,scope);
  9651. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  9652. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9653. parametersSize := ProcedureParametersSize(system,procedure);
  9654. IntermediateCode.AddOffset(op.op,ToMemoryUnits(system,addressType.sizeInBits)*(procedureType.parametersOffset+1)+parametersSize);
  9655. IF backend.cooperative THEN
  9656. IntermediateCode.AddOffset(op.op,ToMemoryUnits(system,addressType.sizeInBits));
  9657. END;
  9658. IntermediateCode.MakeMemory(op.op,addressType);
  9659. END;
  9660. Emit(Push(position,op.op));
  9661. ReleaseOperand(op);
  9662. END PushSelfPointer;
  9663. PROCEDURE Lock(lock: BOOLEAN);
  9664. BEGIN
  9665. IF Trace THEN TraceEnter("Lock") END;
  9666. IF profile THEN ProfilerEnterExit(numberProcedures, FALSE) END;
  9667. CheckRegistersFree(); (* no register may be in use as operands should not be preserved over the lock / unlock boundary *)
  9668. ASSERT(modifyAssignmentCounter = 0);
  9669. IF dump # NIL THEN
  9670. IF lock THEN dump.String("lock") ELSE dump.String("unlock") END;
  9671. dump.Ln;dump.Update;
  9672. END;
  9673. PushSelfPointer;
  9674. IF backend.cooperative THEN
  9675. Emit(Push(position,IntermediateCode.Immediate(sizeType, 1)));
  9676. IF lock THEN CallThis(position,"ExclusiveBlocks","Enter",2)
  9677. ELSE CallThis(position,"ExclusiveBlocks","Exit",2);
  9678. END;
  9679. ELSE
  9680. Emit(Push(position,true));
  9681. IF lock THEN CallThis(position,"Objects","Lock",2)
  9682. ELSE CallThis(position,"Objects","Unlock",2);
  9683. END;
  9684. END;
  9685. IF profile THEN ProfilerEnterExit(numberProcedures, TRUE) END;
  9686. IF Trace THEN TraceExit("Lock") END;
  9687. END Lock;
  9688. PROCEDURE VisitStatementBlock(x: SyntaxTree.StatementBlock);
  9689. VAR previouslyUnchecked, previouslyCooperativeSwitches: BOOLEAN;
  9690. BEGIN
  9691. IF Trace THEN TraceEnter("VisitStatementBlock") END;
  9692. IF emitLabels THEN Emit(LabelInstruction(x.position)) END;
  9693. previouslyUnchecked := isUnchecked;
  9694. isUnchecked := isUnchecked OR x.isUnchecked;
  9695. previouslyCooperativeSwitches := cooperativeSwitches;
  9696. cooperativeSwitches := cooperativeSwitches & ~x.isUncooperative;
  9697. IF x.isExclusive THEN Lock(TRUE); ASSERT(~locked); locked := TRUE; END;
  9698. IF x.statements # NIL THEN
  9699. StatementSequence(x.statements);
  9700. END;
  9701. IF x.isExclusive THEN Lock(FALSE); ASSERT(locked); locked := FALSE; END;
  9702. isUnchecked := previouslyUnchecked;
  9703. cooperativeSwitches := previouslyCooperativeSwitches;
  9704. IF Trace THEN TraceExit("VisitStatementBlock") END;
  9705. END VisitStatementBlock;
  9706. PROCEDURE VisitCode(x: SyntaxTree.Code);
  9707. VAR (* inline: Sections.CellNet; symbol: SyntaxTree.Symbol; *)
  9708. in, out: IntermediateCode.Rules; statement: SyntaxTree.Statement; i: LONGINT; operand,par: Operand; str: POINTER TO ARRAY OF CHAR;
  9709. result, mem: IntermediateCode.Operand; scope: SyntaxTree.Scope; procedureType: SyntaxTree.ProcedureType; return: IntermediateCode.Operand;
  9710. procedure: SyntaxTree.Procedure;
  9711. map: SymbolMap;
  9712. cc, parametersSize: LONGINT;
  9713. BEGIN
  9714. scope := currentScope;
  9715. WHILE ~(scope IS SyntaxTree.ProcedureScope) DO scope := scope.outerScope END;
  9716. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  9717. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9718. return := emptyOperand;
  9719. IF Trace THEN TraceEnter("VisitCode") END;
  9720. IF (x.inRules # NIL) & (x.inRules.Length()>0) THEN
  9721. NEW(in, x.inRules.Length());
  9722. FOR i := 0 TO LEN(in)-1 DO
  9723. statement := x.inRules.GetStatement(i);
  9724. WITH statement: SyntaxTree.Assignment DO
  9725. Evaluate(statement.right, operand);
  9726. result := operand.op;
  9727. NEW(str, 64);
  9728. Basic.GetString(statement.left(SyntaxTree.IdentifierDesignator).identifier, str^);
  9729. in[i] := result; IntermediateCode.SetString(in[i], str);
  9730. ReleaseIntermediateOperand(operand.tag);
  9731. END;
  9732. END;
  9733. ELSE in := NIL
  9734. END;
  9735. IF (x.outRules # NIL) & (x.outRules.Length()>0) THEN
  9736. NEW(out, x.outRules.Length());
  9737. FOR i := 0 TO LEN(out)-1 DO
  9738. statement := x.outRules.GetStatement(i);
  9739. IF statement IS SyntaxTree.StatementBlock THEN statement := statement(SyntaxTree.StatementBlock).statements.GetStatement(0) END;
  9740. WITH statement: SyntaxTree.Assignment DO
  9741. Designate(statement.left, operand);
  9742. MakeMemory(result, operand.op, IntermediateCode.GetType(system,statement.left.type) , 0);
  9743. NEW(str, 64);
  9744. Basic.GetString(statement.right(SyntaxTree.IdentifierDesignator).identifier, str^);
  9745. out[i] := result; IntermediateCode.SetString(out[i], str);
  9746. ReleaseOperand(operand); (* implicit increase of use of operand.op in MakeMemory *)
  9747. |statement: SyntaxTree.ReturnStatement DO
  9748. NEW(str, 64);
  9749. Basic.GetString(statement.returnValue(SyntaxTree.IdentifierDesignator).identifier, str^);
  9750. IF currentIsInline THEN
  9751. map := currentMapper.Get(NIL);
  9752. Designate(map.to, operand);
  9753. IF map.isAddress THEN
  9754. MakeMemory(result, operand.op, IntermediateCode.GetType(system,map.to.type) , 0);
  9755. ELSE
  9756. result := operand.op;
  9757. END;
  9758. (*! only if it does not fit into register
  9759. MakeMemory(result, operand.op, IntermediateCode.GetType(system,map.to.type) , 0);
  9760. *)
  9761. (*Evaluate(map.to, operand);*)
  9762. out[i] := result;
  9763. ELSE
  9764. out[i] :=NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  9765. END;
  9766. IntermediateCode.SetString(out[i], str);
  9767. ReleaseIntermediateOperand(operand.tag);
  9768. return := out[i];
  9769. ELSE
  9770. END;
  9771. END;
  9772. ELSE out := NIL
  9773. END;
  9774. Emit(Asm(x.position,x.sourceCode, in, out));
  9775. IF in # NIL THEN
  9776. FOR i := 0 TO LEN(in)-1 DO
  9777. ReleaseIntermediateOperand(in[i]);
  9778. END;
  9779. END;
  9780. IF out # NIL THEN
  9781. FOR i := 0 TO LEN(out)-1 DO
  9782. WITH statement: SyntaxTree.Assignment DO
  9783. ReleaseIntermediateOperand(out[i]);
  9784. |statement: SyntaxTree.ReturnStatement DO
  9785. (* release happens below *)
  9786. ELSE
  9787. END;
  9788. statement := x.outRules.GetStatement(i);
  9789. END;
  9790. END;
  9791. IF return.mode # IntermediateCode.Undefined THEN
  9792. IF currentIsInline THEN
  9793. ELSIF SemanticChecker.ReturnedAsParameter(procedureType.returnType) THEN
  9794. Symbol(procedureType.returnParameter, par);
  9795. MakeMemory(mem, par.op, return.type, 0);
  9796. ReleaseOperand(par);
  9797. Emit(Mov(position, mem, return));
  9798. ReleaseIntermediateOperand(mem);
  9799. ELSE
  9800. Emit(Return(position,return));
  9801. END;
  9802. ReleaseIntermediateOperand(return);
  9803. IF currentIsInline THEN RETURN END;
  9804. cc := procedureType(SyntaxTree.ProcedureType).callingConvention;
  9805. IF cc = SyntaxTree.WinAPICallingConvention THEN
  9806. parametersSize := ProcedureParametersSize(backend.system,procedure);
  9807. ELSE
  9808. parametersSize := 0;
  9809. END;
  9810. EmitLeave(section, position,NIL, cc);
  9811. Emit(Exit(position,procedureType(SyntaxTree.ProcedureType).pcOffset,cc, parametersSize));
  9812. END;
  9813. IF Trace THEN TraceExit("VisitCode") END;
  9814. END VisitCode;
  9815. PROCEDURE ParameterCopies(x: SyntaxTree.ProcedureType);
  9816. VAR parameter: SyntaxTree.Parameter; type, base: SyntaxTree.Type;
  9817. op: Operand; temp,size,par,dst, length,null: IntermediateCode.Operand;
  9818. const, call: IntermediateCode.Operand;
  9819. parameterDesignator: SyntaxTree.Expression;
  9820. saved: RegisterEntry;
  9821. name: Basic.SegmentedName;
  9822. BEGIN
  9823. IF Trace THEN TraceEnter("ParameterCopies") END;
  9824. parameter := x.firstParameter;
  9825. WHILE parameter # NIL DO
  9826. IF parameter.kind = SyntaxTree.ValueParameter THEN
  9827. type := parameter.type.resolved;
  9828. IF IsOpenArray(type) THEN
  9829. VisitParameter(parameter);
  9830. op := result;
  9831. IF backend.cooperative & parameter.NeedsTrace() THEN
  9832. length := GetArrayLength(type, op.tag);
  9833. size := NewRegisterOperand(addressType);
  9834. base := ArrayBaseType(type);
  9835. const := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.SizeOf(base)));
  9836. Emit(Mul(position, size, length, const));
  9837. dst := NewRegisterOperand (addressType);
  9838. Emit(Mov(position, dst, size));
  9839. const := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,-system.addressSize)); (* alignment *)
  9840. Emit(Sub(position,dst,sp,dst));
  9841. Emit(And(position,dst,dst,const));
  9842. Emit(Mov(position,sp,dst));
  9843. par := fp;
  9844. IntermediateCode.AddOffset(par,ToMemoryUnits(system,parameter.offsetInBits));
  9845. IntermediateCode.InitImmediate(null, byteType, 0);
  9846. Emit(Fill(position, dst, size, null));
  9847. ReleaseIntermediateOperand(dst);
  9848. ReleaseIntermediateOperand(length);
  9849. SaveRegisters();ReleaseUsedRegisters(saved);
  9850. (* register dst has been freed before SaveRegisters already *)
  9851. base := ArrayBaseType(type);
  9852. (* assign method of open array *)
  9853. IF base.IsRecordType() THEN
  9854. Emit (Push(position, length));
  9855. Emit (Push(position, dst));
  9856. Emit (Push(position, op.op));
  9857. GetRecordTypeName (base.resolved(SyntaxTree.RecordType),name);
  9858. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  9859. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  9860. IntermediateCode.InitAddress(call, addressType, name , 0, 0);
  9861. Emit(Call(position,call,ToMemoryUnits(system, 3*system.addressSize)));
  9862. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  9863. Emit (Push(position,length));
  9864. Emit (Push(position, dst));
  9865. Emit (Push(position, length));
  9866. Emit (Push(position, op.op));
  9867. CallThis(position,"GarbageCollector","AssignDelegateArray", 4);
  9868. ELSE
  9869. Emit (Push(position, length));
  9870. Emit (Push(position, dst));
  9871. Emit (Push(position, length));
  9872. Emit (Push(position, op.op));
  9873. CallThis(position,"GarbageCollector","AssignPointerArray", 4);
  9874. ASSERT(ArrayBaseType(type).IsPointer());
  9875. END;
  9876. RestoreRegisters(saved);
  9877. ELSE
  9878. temp := GetDynamicSize(type,op.tag);
  9879. ReuseCopy(size,temp);
  9880. ReleaseIntermediateOperand(temp);
  9881. const := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,-system.addressSize)); (* alignment *)
  9882. Emit(Sub(position,size,sp,size));
  9883. Emit(And(position,size,size,const));
  9884. Emit(Mov(position,sp,size));
  9885. par := fp;
  9886. IntermediateCode.AddOffset(par,ToMemoryUnits(system,parameter.offsetInBits));
  9887. ReleaseIntermediateOperand(size);
  9888. size := GetDynamicSize(type,op.tag);
  9889. END;
  9890. Emit(Copy(position,sp,op.op,size));
  9891. ReleaseIntermediateOperand(size);
  9892. ReleaseOperand(op);
  9893. IntermediateCode.MakeMemory(par,addressType);
  9894. Emit(Mov(position,par,sp));
  9895. ELSIF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  9896. checker.SetCurrentScope(currentScope);
  9897. parameterDesignator := checker.NewSymbolDesignator(position,NIL,parameter);
  9898. Assign(parameterDesignator,parameterDesignator);
  9899. END;
  9900. END;
  9901. parameter := parameter.nextParameter;
  9902. END;
  9903. IF Trace THEN TraceExit("ParameterCopies") END;
  9904. END ParameterCopies;
  9905. PROCEDURE InitVariables(scope: SyntaxTree.Scope);
  9906. VAR x: SyntaxTree.Variable;
  9907. BEGIN
  9908. x := scope.firstVariable;
  9909. WHILE x # NIL DO
  9910. InitVariable(x);
  9911. x := x.nextVariable;
  9912. END;
  9913. END InitVariables;
  9914. PROCEDURE GetFingerprint(symbol: SyntaxTree.Symbol): LONGINT;
  9915. BEGIN
  9916. IF (symbol # NIL) THEN
  9917. RETURN fingerPrinter.SymbolFP(symbol).shallow
  9918. ELSE
  9919. RETURN 0
  9920. END;
  9921. END GetFingerprint;
  9922. PROCEDURE Body(x: SyntaxTree.Body; scope: SyntaxTree.Scope; ir: IntermediateCode.Section; moduleBody: BOOLEAN);
  9923. VAR prevScope: SyntaxTree.Scope; procedureType: SyntaxTree.ProcedureType; procedure: SyntaxTree.Procedure;
  9924. cellScope: SyntaxTree.CellScope; op: Operand; string: SyntaxTree.IdentifierString;
  9925. saved: RegisterEntry; left, right: IntermediateCode.Operand;
  9926. name: Basic.SegmentedName;
  9927. offset: LONGINT;
  9928. BEGIN
  9929. IF Trace THEN TraceEnter("Body") END;
  9930. ReleaseUsedRegisters(saved); (* just in case ... *)
  9931. section := ir;
  9932. exitLabel := NewLabel ();
  9933. IF moduleBody THEN moduleBodySection := section END;
  9934. IF ir.comments # NIL THEN
  9935. commentPrintout := Printout.NewPrinter(ir.comments,Printout.SourceCode,FALSE);
  9936. commentPrintout.SingleStatement(TRUE);
  9937. dump := ir.comments;
  9938. ELSE
  9939. commentPrintout := NIL;
  9940. dump := NIL;
  9941. END;
  9942. prevScope := currentScope;
  9943. currentScope := scope;
  9944. lastSwitchPC := 0;
  9945. cooperativeSwitches := backend.cooperative;
  9946. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  9947. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9948. IF x # NIL THEN
  9949. IF emitLabels THEN Emit(LabelInstruction(x.position)) END;
  9950. IF profile & (x.code = NIL) THEN (* do not profile assembler code sections *)
  9951. IF moduleBody THEN
  9952. ProfilerInit();
  9953. ELSE
  9954. Basic.SegmentedNameToString(ir.name, string);
  9955. ProfilerAddProcedure(numberProcedures,string);
  9956. ProfilerEnterExit(numberProcedures,TRUE);
  9957. END;
  9958. END;
  9959. IF moduleBody & (operatorInitializationCodeSection # NIL) THEN
  9960. Emit(Call(position,IntermediateCode.Address(addressType, operatorInitializationCodeSection.name, GetFingerprint(operatorInitializationCodeSection.symbol), 0), 0))
  9961. END;
  9962. section.SetPositionOrAlignment(procedure.fixed, procedure.alignment);
  9963. IF moduleBody & ~newObjectFile THEN
  9964. InitVariables(moduleScope)
  9965. END;
  9966. IF (scope.outerScope # NIL) & (scope.outerScope IS SyntaxTree.CellScope) THEN
  9967. cellScope := scope.outerScope(SyntaxTree.CellScope);
  9968. IF procedure = cellScope.bodyProcedure THEN
  9969. IF (cellScope.constructor # NIL) & ~backend.cellsAreObjects THEN
  9970. StaticCallOperand(op, cellScope.constructor);
  9971. Emit(Call(position,op.op,0));
  9972. END;
  9973. END;
  9974. END;
  9975. ParameterCopies(procedureType);
  9976. InitVariables(scope);
  9977. IF backend.preciseGC & (x.code = NIL) & (~procedureType.noPAF) & ~procedure.isEntry & ~procedure.isExit THEN
  9978. GetCodeSectionNameForSymbol(procedure, name);
  9979. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Descriptor"));
  9980. IntermediateCode.InitAddress(right, addressType, name, 0, 0);
  9981. offset := ToMemoryUnits(module.system,meta.RecordBaseOffset*module.system.addressSize)+1;
  9982. IntermediateCode.SetOffset(right,offset); (* tag *)
  9983. IntermediateCode.InitMemory(left,addressType,fp,0);
  9984. Emit(Mov(position, left, right));
  9985. END;
  9986. IF x.code = NIL THEN
  9987. VisitStatementBlock(x);
  9988. ELSE
  9989. VisitCode(x.code)
  9990. END;
  9991. IF x.finally # NIL THEN (*! mark finally block for object file *)
  9992. ir.SetFinally(ir.pc);
  9993. StatementSequence(x.finally)
  9994. END;
  9995. IF profile & (x.code = NIL) & ~moduleBody THEN (* do not profile assembler code sections *)
  9996. IF ~backend.cooperative THEN
  9997. ProfilerEnterExit(numberProcedures,FALSE);
  9998. END;
  9999. INC(numberProcedures);
  10000. END;
  10001. END;
  10002. IF backend.cooperative THEN
  10003. IF HasPointers (procedure.procedureScope) THEN CreateResetMethod (procedure.procedureScope) END;
  10004. IF HasPointers (procedure.procedureScope) OR HasVariableParameters (procedure.procedureScope) OR IsNested (procedure) THEN CreateProcedureDescriptor (procedure) END;
  10005. END;
  10006. IF x # NIL THEN
  10007. SELF.position := x.position;
  10008. END;
  10009. CheckRegistersFree();
  10010. ASSERT(modifyAssignmentCounter = 0);
  10011. currentScope := prevScope;
  10012. IF Trace THEN TraceExit("Body") END;
  10013. END Body;
  10014. END ImplementationVisitor;
  10015. MetaDataGenerator=OBJECT
  10016. VAR
  10017. implementationVisitor: ImplementationVisitor;
  10018. declarationVisitor: DeclarationVisitor;
  10019. module: Sections.Module;
  10020. moduleName: ARRAY 128 OF CHAR;
  10021. moduleNamePool: Basic.HashTableInt;
  10022. moduleNamePoolSection: IntermediateCode.Section;
  10023. modulePointerSection: IntermediateCode.Section;
  10024. modulePointerSizePC: LONGINT;
  10025. modulePointerSectionOffset: LONGINT;
  10026. modulePointers: LONGINT;
  10027. simple: BOOLEAN; (* simple = no methods, no module loading, no reflection *)
  10028. RecordBaseOffset: LONGINT;
  10029. MethodTableOffset: LONGINT; (* method table offset from zero *)
  10030. BaseTypesTableOffset: LONGINT; (* table with all record extensions offset *)
  10031. TypeTags: LONGINT; (* type extension level support *)
  10032. TypeRecordBaseOffset: LONGINT; (* offset of type zero offset (without method entries) *)
  10033. patchInfoPC: LONGINT;
  10034. PROCEDURE &InitMetaDataGenerator(implementationVisitor: ImplementationVisitor; declarationVisitor: DeclarationVisitor; simple: BOOLEAN);
  10035. BEGIN
  10036. IF implementationVisitor.backend.cooperative THEN
  10037. TypeTags := MAX(LONGINT);
  10038. BaseTypesTableOffset := 0;
  10039. MethodTableOffset := 2;
  10040. TypeRecordBaseOffset := 0;
  10041. RecordBaseOffset := 0;
  10042. ELSIF simple THEN
  10043. TypeTags := 3; (* only 3 extensions allowed *)
  10044. BaseTypesTableOffset := 1;
  10045. MethodTableOffset := BaseTypesTableOffset+TypeTags;
  10046. TypeRecordBaseOffset := 0;
  10047. RecordBaseOffset := 1;
  10048. ELSE
  10049. TypeTags := 16;
  10050. BaseTypesTableOffset := -2; (* typeInfo and size field *)
  10051. MethodTableOffset := -TypeTags+BaseTypesTableOffset;
  10052. TypeRecordBaseOffset := TypeTags + 2; (* MPO, typeInfo *)
  10053. RecordBaseOffset := 8;
  10054. END;
  10055. SELF.simple := simple;
  10056. SELF.implementationVisitor := implementationVisitor;
  10057. SELF.declarationVisitor := declarationVisitor;
  10058. implementationVisitor.meta := SELF;
  10059. declarationVisitor.meta := SELF;
  10060. END InitMetaDataGenerator;
  10061. PROCEDURE SetModule(module: Sections.Module);
  10062. VAR namePoolOffset, offset: LONGINT; name: Basic.SegmentedName;
  10063. BEGIN
  10064. SELF.module := module;
  10065. Global.GetModuleName(module.module,moduleName);
  10066. IF ReflectionSupport & implementationVisitor.newObjectFile & ~simple & ~implementationVisitor.backend.cooperative THEN
  10067. NEW(moduleNamePool, 32);
  10068. (*! require GC protection *)
  10069. modulePointerSection := Block("Heaps","ArrayBlockDesc",".@ModulePointerArray", modulePointerSectionOffset);
  10070. name := "Heaps.AnyPtr";
  10071. offset := ToMemoryUnits(module.system,TypeRecordBaseOffset*module.system.addressSize);
  10072. (* set base pointer *)
  10073. NamedSymbolAt(modulePointerSection, modulePointerSectionOffset -1, name, NIL, 0, offset);
  10074. ArrayBlock(modulePointerSection, modulePointerSizePC, "", TRUE);
  10075. modulePointers := 0;
  10076. moduleNamePoolSection := Block("Heaps","SystemBlockDesc",".@ModuleNamePool", namePoolOffset);
  10077. AddPointer(moduleNamePoolSection, namePoolOffset);
  10078. END;
  10079. END SetModule;
  10080. PROCEDURE AddPointer(section: IntermediateCode.Section; offset: LONGINT);
  10081. BEGIN
  10082. IF ~implementationVisitor.backend.cooperative THEN
  10083. NamedSymbol(modulePointerSection, section.name, NIL, 0, offset);
  10084. INC(modulePointers);
  10085. (* optimization hint: this can be done once at the end but for consistency of the first tests we keep it like this *)
  10086. PatchSize(modulePointerSection, modulePointerSizePC, modulePointers);
  10087. END;
  10088. END AddPointer;
  10089. PROCEDURE GetTypeRecordBaseOffset(numberMethods: LONGINT): LONGINT;
  10090. BEGIN
  10091. IF implementationVisitor.backend.cooperative OR simple THEN RETURN 0 ELSE RETURN TypeRecordBaseOffset + numberMethods END;
  10092. END GetTypeRecordBaseOffset;
  10093. PROCEDURE HeapBlock(CONST moduleName, typeName: ARRAY OF CHAR; section: IntermediateCode.Section; dataAdrOffset: LONGINT);
  10094. VAR offset: LONGINT; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol;
  10095. BEGIN
  10096. INC(dataAdrOffset,6);
  10097. Info(section,"headerAdr");
  10098. Address(section,0);
  10099. Info(section,"typeDesc");
  10100. symbol := implementationVisitor.GetTypeDescriptor(moduleName,typeName, name);
  10101. offset := ToMemoryUnits(module.system,TypeRecordBaseOffset*module.system.addressSize);
  10102. NamedSymbol(section, name, symbol, 0, offset);
  10103. Info(section,"mark: LONGINT;");
  10104. Longint(section,-1);
  10105. IF module.system.addressType.sizeInBits = 64 THEN Longint(section, 0);END;
  10106. Info(section,"dataAdr-: ADDRESS");
  10107. Symbol(section,section, dataAdrOffset,0);
  10108. Info(section,"size-: SIZE");
  10109. Address(section,0);
  10110. Info(section,"nextRealtime: HeapBlock;");
  10111. Address(section,0);
  10112. END HeapBlock;
  10113. PROCEDURE ProtectedHeapBlock(CONST moduleName, typeName: ARRAY OF CHAR; section: IntermediateCode.Section; dataAdrOffset: LONGINT);
  10114. VAR i: LONGINT;
  10115. BEGIN
  10116. INC(dataAdrOffset,14); (*! change this when changing data structure below *)
  10117. HeapBlock(moduleName,typeName,section,dataAdrOffset);
  10118. Info(section,"count*: LONGINT");
  10119. Longint(section,0);
  10120. Info(section,"locked*: BOOLEAN");
  10121. Longint(section,0);
  10122. Info(section,"awaitingLock*: ProcessQueue");
  10123. Address(section,0);
  10124. Address(section,0);
  10125. Info(section,"awaitingCond*: ProcessQueue");
  10126. Address(section,0);
  10127. Address(section,0);
  10128. Info(section,"lockedBy*: ANY");
  10129. Address(section,0);
  10130. Info(section,"waitingPriorities*: ARRAY NumPriorities OF LONGINT");
  10131. Longint(section,1);
  10132. FOR i := 2 TO 6 DO
  10133. Longint(section,0);
  10134. END;
  10135. Info(section,"lock*: ANY");
  10136. Address(section,0);
  10137. END ProtectedHeapBlock;
  10138. PROCEDURE Info(section: IntermediateCode.Section; CONST s: ARRAY OF CHAR);
  10139. BEGIN
  10140. IF section.comments # NIL THEN section.comments.String(s); section.comments.Ln; section.comments.Update END;
  10141. END Info;
  10142. PROCEDURE Address(section: IntermediateCode.Section; value: LONGINT);
  10143. VAR op: IntermediateCode.Operand;
  10144. BEGIN
  10145. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),value);
  10146. section.Emit(Data(Basic.invalidPosition,op));
  10147. END Address;
  10148. PROCEDURE Size(section: IntermediateCode.Section; value: LONGINT);
  10149. VAR op: IntermediateCode.Operand;
  10150. BEGIN
  10151. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.sizeType),value);
  10152. section.Emit(Data(Basic.invalidPosition,op));
  10153. END Size;
  10154. PROCEDURE Set(section: IntermediateCode.Section; value: SET);
  10155. VAR op: IntermediateCode.Operand;
  10156. BEGIN
  10157. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.longintType),SYSTEM.VAL(LONGINT,value));
  10158. section.Emit(Data(Basic.invalidPosition,op));
  10159. END Set;
  10160. PROCEDURE Longint(section: IntermediateCode.Section; value: LONGINT);
  10161. VAR op: IntermediateCode.Operand;
  10162. BEGIN
  10163. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.longintType),value);
  10164. section.Emit(Data(Basic.invalidPosition,op));
  10165. END Longint;
  10166. PROCEDURE PatchAddress(section: IntermediateCode.Section; pc: LONGINT; value: LONGINT);
  10167. VAR op,noOperand: IntermediateCode.Operand;
  10168. BEGIN
  10169. IntermediateCode.InitOperand(noOperand);
  10170. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),value);
  10171. section.PatchOperands(pc,op,noOperand,noOperand);
  10172. END PatchAddress;
  10173. PROCEDURE PatchSize(section: IntermediateCode.Section; pc: LONGINT; value: LONGINT);
  10174. VAR op,noOperand: IntermediateCode.Operand;
  10175. BEGIN
  10176. IntermediateCode.InitOperand(noOperand);
  10177. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.sizeType),value);
  10178. section.PatchOperands(pc,op,noOperand,noOperand);
  10179. END PatchSize;
  10180. PROCEDURE PatchLongint(section: IntermediateCode.Section; pc: LONGINT; value: LONGINT);
  10181. VAR op,noOperand: IntermediateCode.Operand;
  10182. BEGIN
  10183. IntermediateCode.InitOperand(noOperand);
  10184. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.longintType),value);
  10185. section.PatchOperands(pc,op,noOperand,noOperand);
  10186. END PatchLongint;
  10187. PROCEDURE PatchSymbol(section: IntermediateCode.Section; pc: LONGINT; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol; virtualOffset, realOffset: LONGINT);
  10188. VAR op, noOperand: IntermediateCode.Operand;
  10189. BEGIN
  10190. IntermediateCode.InitOperand(noOperand);
  10191. IntermediateCode.InitAddress(op, IntermediateCode.GetType(module.system, module.system.addressType), name,implementationVisitor.GetFingerprint(symbol), virtualOffset);
  10192. section.PatchOperands(pc,op,noOperand,noOperand);
  10193. END PatchSymbol;
  10194. PROCEDURE Boolean(section: IntermediateCode.Section; value: BOOLEAN);
  10195. VAR op: IntermediateCode.Operand; intValue: LONGINT;
  10196. BEGIN
  10197. IF value = FALSE THEN intValue := 0 ELSE intValue :=1 END;
  10198. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.booleanType),intValue);
  10199. section.Emit(Data(Basic.invalidPosition,op));
  10200. END Boolean;
  10201. PROCEDURE Char(section: IntermediateCode.Section; char: CHAR);
  10202. VAR op: IntermediateCode.Operand;
  10203. BEGIN
  10204. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.characterType),ORD(char));
  10205. section.Emit(Data(Basic.invalidPosition,op));
  10206. END Char;
  10207. PROCEDURE Integer(section: IntermediateCode.Section; int: LONGINT);
  10208. VAR op: IntermediateCode.Operand;
  10209. BEGIN
  10210. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.integerType),int);
  10211. section.Emit(Data(Basic.invalidPosition,op));
  10212. END Integer;
  10213. PROCEDURE String(section: IntermediateCode.Section; CONST str: ARRAY OF CHAR);
  10214. VAR i: LONGINT;
  10215. BEGIN
  10216. Info(section,str);
  10217. i := 0;
  10218. WHILE(str[i] # 0X) DO
  10219. Char(section,str[i]);
  10220. INC(i);
  10221. END;
  10222. Char(section,0X);
  10223. END String;
  10224. PROCEDURE String0(section: IntermediateCode.Section; str: StringPool.Index);
  10225. VAR s: Basic.SectionName;
  10226. BEGIN
  10227. StringPool.GetString(str, s);
  10228. String(section, s);
  10229. END String0;
  10230. PROCEDURE NamedSymbol(section: IntermediateCode.Section; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol; virtualOffset, realOffset: LONGINT);
  10231. VAR op: IntermediateCode.Operand;
  10232. BEGIN
  10233. IntermediateCode.InitAddress(op, IntermediateCode.GetType(module.system, module.system.addressType), name,implementationVisitor.GetFingerprint(symbol), virtualOffset);
  10234. IntermediateCode.SetOffset(op,realOffset);
  10235. section.Emit(Data(Basic.invalidPosition,op));
  10236. END NamedSymbol;
  10237. PROCEDURE NamedSymbolAt(section: IntermediateCode.Section; pc: LONGINT; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol; virtualOffset, realOffset: LONGINT);
  10238. VAR op: IntermediateCode.Operand;
  10239. BEGIN
  10240. IntermediateCode.InitAddress(op, IntermediateCode.GetType(module.system, module.system.addressType), name,implementationVisitor.GetFingerprint(symbol), virtualOffset);
  10241. IntermediateCode.SetOffset(op,realOffset);
  10242. section.EmitAt(pc, Data(Basic.invalidPosition,op));
  10243. END NamedSymbolAt;
  10244. PROCEDURE Symbol(section: IntermediateCode.Section; symbol: Sections.Section; virtualOffset, realOffset: LONGINT);
  10245. BEGIN
  10246. IF symbol= NIL THEN
  10247. Address( section, realOffset);
  10248. ASSERT(virtualOffset = 0);
  10249. ELSE
  10250. NamedSymbol(section, symbol.name, symbol.symbol, virtualOffset, realOffset)
  10251. END;
  10252. END Symbol;
  10253. (* OutPointers delivers
  10254. {pointerOffset}
  10255. *)
  10256. PROCEDURE Pointers(offset: LONGINT; symbol: Sections.Section; section: IntermediateCode.Section; type: SyntaxTree.Type; VAR numberPointers: LONGINT);
  10257. VAR variable: SyntaxTree.Variable; i,n,size: LONGINT; base: SyntaxTree.Type; property: SyntaxTree.Property; parameter: SyntaxTree.Parameter;
  10258. BEGIN
  10259. type := type.resolved;
  10260. IF (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.ObjectType) THEN
  10261. Symbol(section, symbol, 0, (offset )); INC(numberPointers);
  10262. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1); D.Ln; END;
  10263. ELSIF (type IS SyntaxTree.PortType) & implementationVisitor.backend.cellsAreObjects THEN
  10264. Symbol(section, symbol, 0, offset); INC(numberPointers);
  10265. ELSIF type IS SyntaxTree.PointerType THEN
  10266. Symbol(section, symbol, 0, (offset )); INC(numberPointers);
  10267. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1);D.Ln; END;
  10268. ELSIF (type IS SyntaxTree.ProcedureType) & (type(SyntaxTree.ProcedureType).isDelegate) THEN
  10269. Symbol(section, symbol, 0, (offset )+ToMemoryUnits(module.system,module.system.addressSize)); INC(numberPointers);
  10270. IF Trace THEN D.Str("ptr at offset="); D.Int(offset+ToMemoryUnits(module.system,module.system.addressSize),1); END;
  10271. ELSIF (type IS SyntaxTree.RecordType) THEN
  10272. (* never treat a record like a pointer, even if the pointer field is set! *)
  10273. WITH type: SyntaxTree.RecordType DO
  10274. base := type.GetBaseRecord();
  10275. IF base # NIL THEN
  10276. Pointers(offset,symbol,section, base,numberPointers);
  10277. END;
  10278. variable := type.recordScope.firstVariable;
  10279. WHILE(variable # NIL) DO
  10280. IF ~(variable.untraced) THEN
  10281. Pointers(offset+ToMemoryUnits(module.system,variable.offsetInBits), symbol, section, variable.type,numberPointers);
  10282. END;
  10283. variable := variable.nextVariable;
  10284. END;
  10285. END;
  10286. ELSIF (type IS SyntaxTree.CellType) THEN
  10287. WITH type: SyntaxTree.CellType DO
  10288. base := type.GetBaseRecord();
  10289. IF base # NIL THEN
  10290. Pointers(offset,symbol,section, base,numberPointers);
  10291. END;
  10292. variable := type.cellScope.firstVariable;
  10293. WHILE(variable # NIL) DO
  10294. IF ~(variable.untraced) THEN
  10295. Pointers(offset+ToMemoryUnits(module.system,variable.offsetInBits), symbol, section, variable.type,numberPointers);
  10296. END;
  10297. variable := variable.nextVariable;
  10298. END;
  10299. property := type.firstProperty;
  10300. WHILE(property # NIL) DO
  10301. IF ~(property.untraced) THEN
  10302. Pointers(offset+ToMemoryUnits(module.system,property.offsetInBits), symbol, section, property.type,numberPointers);
  10303. END;
  10304. property := property.nextProperty;
  10305. END;
  10306. parameter := type.firstParameter;
  10307. WHILE(parameter # NIL) DO
  10308. IF ~(parameter.untraced) THEN
  10309. Pointers(offset+ToMemoryUnits(module.system,parameter.offsetInBits), symbol, section, parameter.type,numberPointers);
  10310. END;
  10311. parameter := parameter.nextParameter;
  10312. END;
  10313. END;
  10314. ELSIF (type IS SyntaxTree.ArrayType) THEN
  10315. WITH type: SyntaxTree.ArrayType DO
  10316. IF type.form= SyntaxTree.Static THEN
  10317. n := type.staticLength;
  10318. base := type.arrayBase.resolved;
  10319. WHILE(base IS SyntaxTree.ArrayType) DO
  10320. type := base(SyntaxTree.ArrayType);
  10321. n := n* type.staticLength;
  10322. base := type.arrayBase.resolved;
  10323. END;
  10324. size := ToMemoryUnits(module.system,module.system.AlignedSizeOf(base));
  10325. IF SemanticChecker.ContainsPointer(base) THEN
  10326. ASSERT(n<1000000); (* not more than one million pointers on the stack ... *)
  10327. FOR i := 0 TO n-1 DO
  10328. Pointers(offset+i*size, symbol, section, base,numberPointers);
  10329. END;
  10330. END;
  10331. ELSE
  10332. Symbol( section, symbol, 0, (offset )); INC(numberPointers);
  10333. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1); D.Ln; END;
  10334. END;
  10335. END;
  10336. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  10337. WITH type: SyntaxTree.MathArrayType DO
  10338. IF type.form = SyntaxTree.Static THEN
  10339. n := type.staticLength;
  10340. base := type.arrayBase.resolved;
  10341. WHILE(base IS SyntaxTree.MathArrayType) DO
  10342. type := base(SyntaxTree.MathArrayType);
  10343. n := n* type.staticLength;
  10344. base := type.arrayBase.resolved;
  10345. END;
  10346. size := ToMemoryUnits(module.system,module.system.AlignedSizeOf(base));
  10347. IF SemanticChecker.ContainsPointer(base) THEN
  10348. ASSERT(n<1000000); (* not more than one million pointers on the stack ... *)
  10349. FOR i := 0 TO n-1 DO
  10350. Pointers(offset+i*size, symbol, section, base,numberPointers);
  10351. END;
  10352. END;
  10353. ELSE
  10354. Symbol(section, symbol, 0, (offset )); INC(numberPointers); (* GC relevant pointer is at offset 0 *)
  10355. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1); D.Ln; END;
  10356. END
  10357. END;
  10358. (* ELSE no pointers in type *)
  10359. END;
  10360. END Pointers;
  10361. PROCEDURE EnterDynamicName(source: IntermediateCode.Section; CONST name: ARRAY OF CHAR; index: LONGINT; pool: Basic.HashTableInt): LONGINT;
  10362. VAR position,i: LONGINT; ch: CHAR;
  10363. BEGIN
  10364. IF pool.Has(index) THEN
  10365. RETURN pool.GetInt(index)
  10366. ELSE
  10367. position := source.pc;
  10368. pool.PutInt(index, position);
  10369. Info(source, name);
  10370. i := 0;
  10371. REPEAT
  10372. ch := name[i]; INC(i);
  10373. Char( source, ch);
  10374. UNTIL ch = 0X;
  10375. END;
  10376. RETURN position;
  10377. END EnterDynamicName;
  10378. PROCEDURE DynamicName(source: IntermediateCode.Section; index: StringPool.Index; pool: Basic.HashTableInt): LONGINT;
  10379. VAR name: Basic.SectionName; position: LONGINT;
  10380. BEGIN
  10381. IF pool.Has(index) THEN
  10382. RETURN pool.GetInt(index)
  10383. ELSE
  10384. StringPool.GetString(index, name);
  10385. position := EnterDynamicName(source,name,index, pool);
  10386. END;
  10387. RETURN position;
  10388. END DynamicName;
  10389. PROCEDURE NamedBlock(CONST mName, typeName: ARRAY OF CHAR; name: Basic.SegmentedName; VAR offset: LONGINT): IntermediateCode.Section;
  10390. VAR section: IntermediateCode.Section;
  10391. BEGIN
  10392. section := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name, NIL, declarationVisitor.dump);
  10393. IF implementationVisitor.backend.cooperative THEN
  10394. Info(section, "TypeDescriptor");
  10395. Basic.ToSegmentedName("BaseTypes.Array", name);
  10396. NamedSymbol(section, name,NIL, 0, 0);
  10397. BasePointer(section);
  10398. offset := 0;
  10399. ELSE
  10400. HeapBlock(mName,typeName,section,2);
  10401. Info(section, "HeapBlock");
  10402. (*
  10403. Symbol(section,section,2,0);
  10404. *)
  10405. Address(section,0); (* empty such that GC does not go on traversing *)
  10406. Info(section, "TypeDescriptor");
  10407. Address(section,0);
  10408. offset := section.pc;
  10409. END;
  10410. RETURN section
  10411. END NamedBlock;
  10412. PROCEDURE Block(CONST mName, typeName, suffix: ARRAY OF CHAR; VAR offset: LONGINT): IntermediateCode.Section;
  10413. VAR name: ARRAY 128 OF CHAR; pooledName: Basic.SegmentedName;
  10414. BEGIN
  10415. COPY(moduleName,name);
  10416. Strings.Append(name,suffix);
  10417. Basic.ToSegmentedName(name, pooledName);
  10418. RETURN NamedBlock(mName, typeName, pooledName, offset);
  10419. END Block;
  10420. PROCEDURE ArrayBlock(source: IntermediateCode.Section; VAR sizePC: LONGINT; CONST baseType: ARRAY OF CHAR; hasPointer: BOOLEAN);
  10421. VAR name: Basic.SegmentedName;
  10422. BEGIN
  10423. Info(source,"ArrayHeader");
  10424. IF implementationVisitor.backend.cooperative THEN
  10425. sizePC := source.pc;
  10426. Address(source,0);
  10427. NamedSymbol(source,source.name,NIL,0,ToMemoryUnits(implementationVisitor.system,(BaseArrayTypeSize + 1)*implementationVisitor.addressType.sizeInBits));
  10428. IF baseType # "" THEN
  10429. Basic.ToSegmentedName(baseType, name);
  10430. NamedSymbol(source, name,NIL, 0, 0);
  10431. ELSE
  10432. Address(source,0);
  10433. END;
  10434. Address(source,0);
  10435. ELSE
  10436. Address(source,0);
  10437. Address(source,0);
  10438. (* first pointer for GC *)
  10439. IF hasPointer THEN
  10440. (* points to first element in the array, this is NOT the base type descriptor *)
  10441. NamedSymbol(source,source.name, NIL,source.pc+2,0);
  10442. ELSE
  10443. Address(source,0);
  10444. END;
  10445. sizePC := source.pc;
  10446. Address(source,0);
  10447. Info(source,"array data");
  10448. END;
  10449. END ArrayBlock;
  10450. PROCEDURE PatchArray(section: IntermediateCode.Section; pc: LONGINT; size: LONGINT);
  10451. BEGIN
  10452. IF implementationVisitor.backend.cooperative THEN
  10453. PatchSize(section, pc, size);
  10454. PatchSize(section, pc + 3, size);
  10455. ELSE
  10456. PatchSize(section, pc-3, size); (* actually only for arrays with pointers, but does not harm... *)
  10457. PatchSize(section, pc, size);
  10458. END;
  10459. END PatchArray;
  10460. PROCEDURE ExportDesc(source: IntermediateCode.Section);
  10461. VAR
  10462. i: LONGINT; section: Sections.Section; fingerPrinter : FingerPrinter.FingerPrinter;
  10463. sectionArray: POINTER TO ARRAY OF Sections.Section;
  10464. poolMap: Basic.HashTableInt;
  10465. namePool: IntermediateCode.Section;
  10466. namePoolOffset: LONGINT;
  10467. PROCEDURE Compare(VAR s1, s2: Sections.Section): BOOLEAN;
  10468. VAR n1, n2: Basic.SectionName; index: LONGINT; ch1, ch2: CHAR;
  10469. BEGIN
  10470. Basic.SegmentedNameToString(s1.name,n1);
  10471. Basic.SegmentedNameToString(s2.name,n2);
  10472. index := 0;
  10473. ch1 := n1[index];
  10474. ch2 := n2[index];
  10475. WHILE (ch1 # 0X) & (ch1 = ch2) DO
  10476. INC(index);
  10477. ch1 := n1[index];
  10478. ch2 := n2[index];
  10479. END;
  10480. RETURN ch1 < ch2;
  10481. END Compare;
  10482. PROCEDURE QuickSort(VAR list: ARRAY OF Sections.Section; lo, hi: LONGINT);
  10483. VAR
  10484. i, j: LONGINT;
  10485. x, t: Sections.Section;
  10486. BEGIN
  10487. IF lo < hi THEN
  10488. i := lo; j := hi; x:= list[(lo+hi) DIV 2];
  10489. WHILE i <= j DO
  10490. WHILE Compare(list[i], x) DO INC(i) END;
  10491. WHILE Compare(x, list[j]) DO DEC(j) END;
  10492. IF i <= j THEN
  10493. t := list[i]; list[i] := list[j]; list[j] := t; (* swap i and j *)
  10494. INC(i); DEC(j)
  10495. END
  10496. END;
  10497. IF lo < j THEN QuickSort(list, lo, j) END;
  10498. IF i < hi THEN QuickSort(list, i, hi) END
  10499. END;
  10500. END QuickSort;
  10501. (*
  10502. ExportDesc* = RECORD
  10503. fp*: ADDRESS;
  10504. name* {UNTRACED}: DynamicName;
  10505. adr*: ADDRESS;
  10506. exports*: LONGINT;
  10507. dsc* {UNTRACED}: ExportArray
  10508. END;
  10509. ExportArray* = POINTER {UNSAFE} TO ARRAY OF ExportDesc;
  10510. *)
  10511. PROCEDURE ExportDesc2(
  10512. source: IntermediateCode.Section;
  10513. namePool: IntermediateCode.Section;
  10514. fingerPrinter: FingerPrinter.FingerPrinter;
  10515. symbol: Sections.Section;
  10516. name: StringPool.Index;
  10517. VAR patchAdr: LONGINT
  10518. ): BOOLEAN;
  10519. VAR fingerPrint: SyntaxTree.FingerPrint;
  10520. BEGIN
  10521. (*IF (implementationVisitor.backend.cooperative) & (symbol.symbol = NIL) OR (symbol.symbol # NIL) & (symbol.type # Sections.InitCodeSection)
  10522. & (symbol.type # Sections.InlineCodeSection)
  10523. THEN
  10524. *)
  10525. IF (symbol = NIL) OR ( (implementationVisitor.backend.cooperative) & (symbol.symbol = NIL) OR (symbol.symbol # NIL) & (symbol.type # Sections.InitCodeSection)
  10526. & (symbol.type # Sections.InlineCodeSection))
  10527. THEN
  10528. IF (symbol = NIL) OR (symbol # NIL) & (symbol.type # Sections.InlineCodeSection) THEN
  10529. IF (symbol # NIL) & (symbol.symbol # NIL) THEN
  10530. fingerPrint := fingerPrinter.SymbolFP(symbol.symbol);
  10531. Longint(source,fingerPrint.shallow);
  10532. ELSE
  10533. Longint(source, 0);
  10534. END;
  10535. IF module.system.addressType.sizeInBits = 64 THEN Longint(source, 0);END;
  10536. Symbol(source, namePool, DynamicName(namePool, name, poolMap), 0); (* reference to dynamic name *)
  10537. Symbol(source, symbol,0,0);
  10538. patchAdr := source.pc;
  10539. Longint(source, 0);
  10540. IF module.system.addressType.sizeInBits = 64 THEN Longint(source, 0); END;
  10541. Address(source,0);
  10542. END;
  10543. RETURN TRUE
  10544. ELSE
  10545. RETURN FALSE
  10546. END;
  10547. END ExportDesc2;
  10548. PROCEDURE Export(CONST sections: ARRAY OF Sections.Section);
  10549. VAR level, olevel, s: LONGINT; prev, this: Basic.SegmentedName; name: ARRAY 256 OF CHAR;
  10550. scopes: ARRAY LEN(prev)+1 OF Scope; arrayName: ARRAY 32 OF CHAR;
  10551. sym: Sections.Section; offset: LONGINT; symbol: Sections.Section;
  10552. nextPatch: LONGINT;
  10553. TYPE
  10554. Scope = RECORD
  10555. elements: LONGINT;
  10556. gelements: LONGINT;
  10557. section: IntermediateCode.Section;
  10558. patchAdr: LONGINT;
  10559. arraySizePC: LONGINT;
  10560. beginPC: LONGINT; (* current scope start pc *)
  10561. END;
  10562. BEGIN
  10563. Basic.InitSegmentedName(prev);
  10564. olevel := -1;
  10565. scopes[0].section := source;
  10566. scopes[0].arraySizePC := MIN(LONGINT);
  10567. FOR s := 0 TO LEN(sections)-1 DO
  10568. symbol := sections[s];
  10569. IF (symbol # NIL) & (implementationVisitor.backend.cooperative) & (symbol.symbol = NIL) OR (symbol.symbol # NIL) & (symbol.type # Sections.InitCodeSection) & (symbol.type # Sections.InlineCodeSection) THEN
  10570. this := sections[s].name;
  10571. level := 0;
  10572. WHILE (this[level] > 0) DO
  10573. WHILE (this[level] > 0) & (prev[level] = this[level]) DO
  10574. INC(level);
  10575. END;
  10576. WHILE level < olevel DO
  10577. (*TRACE("closing",olevel,scopes[olevel].elements); *)
  10578. IF olevel > 0 THEN
  10579. PatchLongint(scopes[olevel-1].section,scopes[olevel-1].patchAdr, scopes[olevel].elements);
  10580. nextPatch := scopes[olevel-1].patchAdr+1;
  10581. IF module.system.addressType.sizeInBits = 64 THEN INC(nextPatch) END;
  10582. PatchSymbol(scopes[olevel-1].section,nextPatch, scopes[olevel].section.name, scopes[olevel].section.symbol, scopes[olevel].beginPC, 0);
  10583. END;
  10584. scopes[olevel].gelements := scopes[olevel].gelements + scopes[olevel].elements;
  10585. DEC(olevel);
  10586. END;
  10587. IF (this[level] > 0) THEN
  10588. IF level > olevel THEN
  10589. (*TRACE("opening",level); *)
  10590. IF scopes[level].section = NIL THEN
  10591. arrayName := ".@ExportArray";
  10592. Strings.AppendInt(arrayName, level);
  10593. scopes[level].section := Block("Heaps","SystemBlockDesc",arrayName,offset);
  10594. AddPointer(scopes[level].section,offset);
  10595. ArrayBlock(scopes[level].section,scopes[level].arraySizePC,"Modules.ExportDesc", FALSE);
  10596. END;
  10597. scopes[level].beginPC := scopes[level].section.pc;
  10598. olevel := level;
  10599. scopes[olevel].elements := 0;
  10600. END;
  10601. IF (level = LEN(this)-1) OR (this[level+1] <= 0) THEN
  10602. sym := sections[s];
  10603. ELSE
  10604. sym := NIL;
  10605. END;
  10606. IF ExportDesc2(scopes[level].section, namePool, fingerPrinter, sym, this[level], scopes[level].patchAdr)
  10607. THEN
  10608. INC(scopes[olevel].elements);
  10609. END;
  10610. (* enter string in scope *)
  10611. INC(level);
  10612. END;
  10613. END;
  10614. Basic.SegmentedNameToString(this, name);
  10615. prev := this;
  10616. END;
  10617. END;
  10618. WHILE 0 <= olevel DO
  10619. (*TRACE("closing",olevel,scopes[olevel].elements); *)
  10620. IF olevel > 0 THEN
  10621. PatchLongint(scopes[olevel-1].section,scopes[olevel-1].patchAdr, scopes[olevel].elements);
  10622. nextPatch := scopes[olevel-1].patchAdr+1;
  10623. IF module.system.addressType.sizeInBits = 64 THEN INC(nextPatch) END;
  10624. PatchSymbol(scopes[olevel-1].section,nextPatch, scopes[olevel].section.name, scopes[olevel].section.symbol, scopes[olevel].beginPC, 0);
  10625. END;
  10626. scopes[olevel].gelements := scopes[olevel].gelements + scopes[olevel].elements;
  10627. DEC(olevel);
  10628. END;
  10629. level := 0;
  10630. WHILE (level < LEN(scopes)) DO
  10631. IF (scopes[level].section # NIL) & (scopes[level].arraySizePC # MIN(LONGINT)) THEN
  10632. PatchArray(scopes[level].section, scopes[level].arraySizePC, scopes[level].gelements);
  10633. END;
  10634. INC(level);
  10635. END;
  10636. END Export;
  10637. BEGIN
  10638. NEW(fingerPrinter);
  10639. NEW(poolMap, 64);
  10640. (* this is the name pool private to the export table -- it is sorted and should not be mixed / used for other names in a module *)
  10641. namePool := Block("Heaps","SystemBlockDesc",".@NamePool",namePoolOffset);
  10642. NEW(sectionArray, module.allSections.Length());
  10643. FOR i := 0 TO module.allSections.Length() - 1 DO
  10644. section := module.allSections.GetSection(i);
  10645. sectionArray[i] := section;
  10646. END;
  10647. QuickSort(sectionArray^,0,module.allSections.Length()-1);
  10648. Export(sectionArray^);
  10649. END ExportDesc;
  10650. PROCEDURE ExceptionArray(source: IntermediateCode.Section);
  10651. VAR
  10652. p: Sections.Section; finallyPC, sizePC, size, i: LONGINT;
  10653. BEGIN
  10654. Info(source, "exception table offsets array descriptor");
  10655. size := 0;
  10656. ArrayBlock(source,sizePC,"Modules.ExceptionTableEntry", FALSE);
  10657. Info(source, "exception table content");
  10658. FOR i := 0 TO module.allSections.Length() - 1 DO
  10659. p := module.allSections.GetSection(i);
  10660. IF p.type = Sections.CodeSection THEN
  10661. finallyPC := p(IntermediateCode.Section).finally;
  10662. IF finallyPC>=0 THEN
  10663. Symbol( source, p, 0,0);
  10664. Symbol( source, p, finallyPC, 0);
  10665. Symbol( source, p, finallyPC,0);
  10666. INC(size);
  10667. END;
  10668. END
  10669. END;
  10670. PatchArray(source,sizePC,size);
  10671. END ExceptionArray;
  10672. PROCEDURE Name(section: IntermediateCode.Section; CONST name: ARRAY OF CHAR);
  10673. VAR i: LONGINT; ch: CHAR;
  10674. BEGIN
  10675. i := 0;
  10676. REPEAT
  10677. ch := name[i]; INC(i);
  10678. Char( section, ch);
  10679. UNTIL ch = 0X;
  10680. WHILE i < 32 DO
  10681. Char( section, 0X); INC(i);
  10682. END;
  10683. END Name;
  10684. PROCEDURE References(section: IntermediateCode.Section);
  10685. CONST
  10686. sfTypeNone = 0X;
  10687. sfTypeCHAR = 01X;
  10688. sfTypeCHAR8 = 02X;
  10689. sfTypeCHAR16 = 03X;
  10690. sfTypeCHAR32 = 04X;
  10691. sfTypeRANGE = 05X;
  10692. sfTypeSHORTINT = 06X;
  10693. sfTypeINTEGER = 07X;
  10694. sfTypeLONGINT = 08X;
  10695. sfTypeHUGEINT = 09X;
  10696. sfTypeWORD = 0AX;
  10697. sfTypeLONGWORD = 0BX;
  10698. sfTypeSIGNED8 = 0CX;
  10699. sfTypeSIGNED16 = 0DX;
  10700. sfTypeSIGNED32 = 0EX;
  10701. sfTypeSIGNED64 = 0FX;
  10702. sfTypeUNSIGNED8 = 10X;
  10703. sfTypeUNSIGNED16 = 11X;
  10704. sfTypeUNSIGNED32 = 12X;
  10705. sfTypeUNSIGNED64 = 13X;
  10706. sfTypeREAL = 14X;
  10707. sfTypeLONGREAL = 15X;
  10708. sfTypeCOMPLEX = 16X;
  10709. sfTypeLONGCOMPLEX = 17X;
  10710. sfTypeBOOLEAN = 18X;
  10711. sfTypeSET = 19X;
  10712. sfTypeANY = 1AX;
  10713. sfTypeOBJECT = 1BX;
  10714. sfTypeBYTE = 1CX;
  10715. sfTypeADDRESS = 1DX;
  10716. sfTypeSIZE = 1EX;
  10717. sfTypeIndirect = 1FX;
  10718. sfTypeRecord = 20X;
  10719. sfTypePointerToRecord = 21X;
  10720. sfTypePointerToArray = 22X;
  10721. sfTypeOpenArray = 23X;
  10722. sfTypeStaticArray = 24X;
  10723. sfTypeDynamicArray = 25X;
  10724. sfTypeMathStaticArray = 26X;
  10725. sfTypeMathOpenArray = 27X;
  10726. sfTypeMathTensor = 28X;
  10727. sfTypeDelegate = 29X;
  10728. sfTypeENUM = 2AX;
  10729. sfTypeCELL = 2BX;
  10730. sfTypePORT = 2CX;
  10731. sfIN = 0X;
  10732. sfOUT = 1X;
  10733. flagDelegate = 0;
  10734. flagConstructor = 1;
  10735. (* variable / parameter addressing modes *)
  10736. sfAbsolute = 0X; (* global vars *)
  10737. sfRelative = 1X; (* variables, value parameters *)
  10738. sfIndirect = 2X; (* var parameters *)
  10739. sfScopeBegin = 0F0X;
  10740. sfScopeEnd = 0F1X;
  10741. sfProcedure = 0F2X;
  10742. sfVariable = 0F3X;
  10743. sfTypeDeclaration = 0F4X;
  10744. sfModule = 0FFX;
  10745. RefInfo = TRUE;
  10746. VAR
  10747. s: Sections.Section; sizePC, i, startPC, lastOffset: LONGINT;
  10748. indirectTypes: Basic.HashTable;
  10749. PROCEDURE CurrentIndex(): SIZE;
  10750. VAR i: LONGINT;
  10751. BEGIN
  10752. FOR i := startPC TO section.pc -1 DO
  10753. ASSERT (section.instructions[i].opcode = IntermediateCode.data);
  10754. INC(lastOffset, ToMemoryUnits(module.system, section.instructions[i].op1.type.sizeInBits));
  10755. END;
  10756. startPC := section.pc;
  10757. RETURN lastOffset;
  10758. END CurrentIndex;
  10759. (*
  10760. Scope = sfScopeBegin {Variable} {Procedure} {TypeDeclaration} sfScopeEnd.
  10761. Module = sfModule prevSymbol:SIZE name:String Scope.
  10762. Procedure = sfProcedure prevSymbol:SIZE name:STRING from:ADDRESS to:ADDRESS {Parameter} returnType:Type Scope.
  10763. Variable = sfVariable prevSymbol:SIZE name:STRING (sfAbsolute address:ADDRESS| sfIndirect offset:SIZE | sfRelative offset:SIZE) Type.
  10764. TypeDeclaration = sfTypeDeclaration prevSymbol:SIZE name:STRING td:ADDRESS Scope.
  10765. Type =
  10766. sfTypePointerToRecord
  10767. | sfTypePointerToArray Type
  10768. | sfTypeOpenArray Type
  10769. | sfTypeDynamicArray Type
  10770. | sfTypeStaticArray length:SIZE Type
  10771. | sfTypeMathOpenArray Type
  10772. | sfTypeMathStaticArray length:SIZE Type
  10773. | sfTypeMathTensor Type
  10774. | sfTypeRecord tdAdr:ADDRESS
  10775. | sfTypeDelegate {Parameter} return:Type
  10776. | sfTypePort (sfIN | sfOUT)
  10777. | sfTypeBOOLEAN
  10778. | sfTypeCHAR | sfTypeCHAR8 | sfTypeCHAR16 | sfTypeCHAR32
  10779. | sfTypeSHORTINT | sfTypeINTEGER | sfTypeLONGINT | sfTypeHUGEINT
  10780. | sfTypeSIGNED8 | sfTypeSIGNED16 | sfTypeSIGNED32 | sfTypeSIGNED64
  10781. | sfTypeUNSIGNED8 | sfTypeUNSIGNED16 | sfTypeUNSIGNED32 | sfTypeUNSIGNED64
  10782. | sfTypeWORD | sfTypeLONGWORD
  10783. | sfTypeREAL | sfTypeLONGREAL
  10784. | sfTypeCOMPLEX | sfTypeLONGCOMPLEX
  10785. | sfTypeSET | sfTypeANY | sfTypeOBJECT | sfTypeBYTE | sfTypeADDRESS | sfTypeSIZE
  10786. | sfTypeIndirect offset:SIZE.
  10787. *)
  10788. PROCEDURE Indirect(type: SyntaxTree.Type): BOOLEAN;
  10789. VAR offset: SIZE;
  10790. BEGIN
  10791. IF indirectTypes.Has(type) THEN
  10792. offset := indirectTypes.GetInt(type);
  10793. Char(section, sfTypeIndirect);
  10794. Size(section, offset);
  10795. RETURN TRUE;
  10796. ELSE
  10797. indirectTypes.PutInt(type, CurrentIndex());
  10798. RETURN FALSE;
  10799. END;
  10800. END Indirect;
  10801. PROCEDURE NType(type: SyntaxTree.Type);
  10802. VAR size: SIZE; td: SyntaxTree.TypeDeclaration; tir: Sections.Section;
  10803. segmentedName: Basic.SegmentedName; offset: SIZE; parameter: SyntaxTree.Parameter;
  10804. BEGIN
  10805. IF type = NIL THEN
  10806. Char(section, sfTypeNone)
  10807. ELSE
  10808. type := type.resolved;
  10809. size := type.sizeInBits;
  10810. WITH type:SyntaxTree.PointerType DO
  10811. IF type.pointerBase.resolved IS SyntaxTree.RecordType THEN
  10812. IF RefInfo THEN Info(section,"PointerToRecord") END;
  10813. Char(section, sfTypePointerToRecord);
  10814. (*! do we ever need the pointer base? NType(type.pointerBase);*)
  10815. ELSE
  10816. IF RefInfo THEN Info(section,"PointerToArray") END;
  10817. Char(section, sfTypePointerToArray);
  10818. NType(type.pointerBase);
  10819. END;
  10820. | type: SyntaxTree.ArrayType DO
  10821. IF ~Indirect(type) THEN
  10822. IF type.form = SyntaxTree.Open THEN
  10823. IF RefInfo THEN Info(section,"OpenArray") END;
  10824. Char(section, sfTypeOpenArray);
  10825. ELSIF type.form = SyntaxTree.SemiDynamic THEN
  10826. IF RefInfo THEN Info(section,"DynamicArray") END;
  10827. Char(section, sfTypeDynamicArray);
  10828. ELSIF type.form = SyntaxTree.Static THEN
  10829. IF RefInfo THEN Info(section,"StaticArray") END;
  10830. Char(section, sfTypeStaticArray);
  10831. Size(section, type.staticLength);
  10832. ELSE
  10833. HALT(100);
  10834. END;
  10835. NType(type.arrayBase);
  10836. END;
  10837. | type: SyntaxTree.MathArrayType DO
  10838. IF ~Indirect(type) THEN
  10839. IF type.form = SyntaxTree.Open THEN
  10840. IF RefInfo THEN Info(section,"MathOpenArray") END;
  10841. Char(section, sfTypeMathOpenArray);
  10842. ELSIF type.form = SyntaxTree.Static THEN
  10843. IF RefInfo THEN Info(section,"MathStaticArray") END;
  10844. Char(section, sfTypeMathStaticArray);
  10845. Size(section, type.staticLength);
  10846. ELSIF type.form = SyntaxTree.Tensor THEN
  10847. IF RefInfo THEN Info(section,"MathTensor") END;
  10848. Char(section, sfTypeMathTensor);
  10849. ELSE
  10850. HALT(100);
  10851. END;
  10852. NType(type.arrayBase);
  10853. END;
  10854. | type: SyntaxTree.RecordType DO
  10855. IF ~Indirect(type) THEN
  10856. IF type.pointerType # NIL (* OBJECT *) THEN
  10857. IF RefInfo THEN Info(section,"PointerToRecord") END;
  10858. Char(section, sfTypePointerToRecord)
  10859. ELSE
  10860. IF RefInfo THEN Info(section,"Record") END;
  10861. Char(section, sfTypeRecord);
  10862. td := type.typeDeclaration;
  10863. IF RefInfo THEN Info(section,"TD") END;
  10864. IF (td # NIL) THEN
  10865. Global.GetSymbolSegmentedName(td,segmentedName);
  10866. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  10867. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10868. ELSE
  10869. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10870. END;
  10871. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(type(SyntaxTree.RecordType).recordScope.numberMethods)*module.system.addressSize);
  10872. Symbol(section, tir, 0, offset);
  10873. ELSE
  10874. Address(section, 0);
  10875. END;
  10876. END;
  10877. END;
  10878. | type: SyntaxTree.CellType DO
  10879. IF ~Indirect(type) THEN
  10880. IF RefInfo THEN Info(section,"Record") END;
  10881. Char(section, sfTypeRecord);
  10882. td := type.typeDeclaration;
  10883. IF RefInfo THEN Info(section,"TD") END;
  10884. IF (td # NIL) THEN
  10885. Global.GetSymbolSegmentedName(td,segmentedName);
  10886. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  10887. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10888. ELSE
  10889. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10890. END;
  10891. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(0)*module.system.addressSize);
  10892. Symbol(section, tir, 0, offset);
  10893. ELSE
  10894. Address(section, 0);
  10895. END;
  10896. END;
  10897. | type: SyntaxTree.PortType DO
  10898. Char(section, sfTypePORT);
  10899. IF type.direction = SyntaxTree.OutPort THEN
  10900. Char(section, sfOUT)
  10901. ELSE
  10902. Char(section, sfIN)
  10903. END;
  10904. | type: SyntaxTree.ProcedureType DO
  10905. IF ~Indirect(type) THEN
  10906. Char(section, sfTypeDelegate);
  10907. parameter := type.firstParameter;
  10908. WHILE(parameter # NIL) DO
  10909. NParameter(parameter, -1);
  10910. parameter := parameter.nextParameter;
  10911. END;
  10912. NType(type.returnType);
  10913. END;
  10914. | type:SyntaxTree.EnumerationType DO
  10915. Char(section, sfTypeENUM);
  10916. | type: SyntaxTree.BasicType DO
  10917. WITH type: SyntaxTree.BooleanType DO
  10918. IF RefInfo THEN Info(section,"Boolean") END;
  10919. Char(section, sfTypeBOOLEAN);
  10920. | type: SyntaxTree.CharacterType DO
  10921. IF type = module.system.characterType THEN
  10922. IF RefInfo THEN Info(section,"CHAR") END;
  10923. Char(section, sfTypeCHAR);
  10924. ELSIF (type = module.system.characterType8) OR (type.sizeInBits= 8) THEN
  10925. IF RefInfo THEN Info(section,"CHAR8") END;
  10926. Char(section, sfTypeCHAR8)
  10927. ELSIF (type = module.system.characterType16) OR (type.sizeInBits= 16) THEN
  10928. IF RefInfo THEN Info(section,"CHAR16") END;
  10929. Char(section, sfTypeCHAR16);
  10930. ELSIF (type = module.system.characterType32) OR (type.sizeInBits = 32) THEN
  10931. IF RefInfo THEN Info(section,"CHAR32") END;
  10932. Char(section, sfTypeCHAR32);
  10933. ELSE
  10934. HALT(100);
  10935. END;
  10936. |type: SyntaxTree.IntegerType DO
  10937. IF type(SyntaxTree.IntegerType).signed THEN
  10938. IF (type = module.system.shortintType) THEN
  10939. IF RefInfo THEN Info(section,"SHORTINT") END;
  10940. Char(section, sfTypeSHORTINT)
  10941. ELSIF (type = module.system.integerType) THEN
  10942. IF RefInfo THEN Info(section,"INTEGER") END;
  10943. Char(section, sfTypeINTEGER)
  10944. ELSIF (type = module.system.longintType) THEN
  10945. IF RefInfo THEN Info(section,"LONGINT") END;
  10946. Char(section, sfTypeLONGINT)
  10947. ELSIF (type = module.system.hugeintType) THEN
  10948. IF RefInfo THEN Info(section,"HUGEINT") END;
  10949. Char(section, sfTypeHUGEINT)
  10950. ELSIF (type = module.system.wordType) THEN
  10951. IF RefInfo THEN Info(section,"WORD") END;
  10952. Char(section, sfTypeWORD)
  10953. ELSIF (type = module.system.longWordType) THEN
  10954. IF RefInfo THEN Info(section,"LONGWORD") END;
  10955. Char(section, sfTypeLONGWORD);
  10956. ELSIF (type = Global.Integer8) OR (type.sizeInBits = 8 ) THEN
  10957. IF RefInfo THEN Info(section,"SIGNED8") END;
  10958. Char(section, sfTypeSIGNED8)
  10959. ELSIF (type = Global.Integer16) OR (type.sizeInBits = 16 ) THEN
  10960. IF RefInfo THEN Info(section,"SIGNED16") END;
  10961. Char(section, sfTypeSIGNED16)
  10962. ELSIF (type = Global.Integer32) OR (type.sizeInBits = 32 ) THEN
  10963. IF RefInfo THEN Info(section,"SIGNED32") END;
  10964. Char(section, sfTypeSIGNED32)
  10965. ELSIF (type = Global.Integer64) OR (type.sizeInBits = 64 ) THEN
  10966. IF RefInfo THEN Info(section,"SIGNED64") END;
  10967. Char(section, sfTypeSIGNED64)
  10968. ELSE
  10969. HALT(100);
  10970. END
  10971. ELSE (* unsigned *)
  10972. IF (type = Global.Unsigned8) OR (type.sizeInBits = 8 ) THEN
  10973. IF RefInfo THEN Info(section,"UNSIGNED8") END;
  10974. Char(section, sfTypeUNSIGNED8)
  10975. ELSIF (type = Global.Unsigned16) OR (type.sizeInBits = 16 ) THEN
  10976. IF RefInfo THEN Info(section,"UNSIGNED16") END;
  10977. Char(section, sfTypeUNSIGNED16)
  10978. ELSIF (type = Global.Unsigned32) OR (type.sizeInBits = 32 ) THEN
  10979. IF RefInfo THEN Info(section,"UNSIGNED32") END;
  10980. Char(section, sfTypeUNSIGNED32)
  10981. ELSIF (type = Global.Unsigned64) OR (type.sizeInBits = 64 ) THEN
  10982. IF RefInfo THEN Info(section,"UNSIGNED64") END;
  10983. Char(section, sfTypeUNSIGNED64)
  10984. ELSE
  10985. HALT(100)
  10986. END
  10987. END;
  10988. | type: SyntaxTree.FloatType DO
  10989. IF (type = module.system.realType) OR (type.sizeInBits = 32) THEN
  10990. IF RefInfo THEN Info(section,"REAL") END;
  10991. Char(section, sfTypeREAL);
  10992. ELSIF (type = module.system.longrealType) OR (type.sizeInBits = 64) THEN
  10993. IF RefInfo THEN Info(section,"LONGREAL") END;
  10994. Char(section, sfTypeLONGREAL);
  10995. ELSE
  10996. HALT(100);
  10997. END;
  10998. |type: SyntaxTree.ComplexType DO
  10999. IF (type = module.system.complexType) OR (type.sizeInBits = 64) THEN
  11000. IF RefInfo THEN Info(section,"COMPLEX") END;
  11001. Char(section, sfTypeCOMPLEX);
  11002. ELSIF (type = module.system.longcomplexType) OR (type.sizeInBits = 12) THEN
  11003. IF RefInfo THEN Info(section,"LONGCOMPLEX") END;
  11004. Char(section, sfTypeLONGCOMPLEX);
  11005. ELSE
  11006. HALT(100);
  11007. END;
  11008. |type:SyntaxTree.SetType DO
  11009. IF RefInfo THEN Info(section,"SET") END;
  11010. Char(section, sfTypeSET);
  11011. |type:SyntaxTree.AnyType DO
  11012. IF RefInfo THEN Info(section,"ANY") END;
  11013. Char(section, sfTypeANY);
  11014. |type:SyntaxTree.ObjectType DO
  11015. IF RefInfo THEN Info(section,"OBJECT") END;
  11016. Char(section, sfTypeOBJECT);
  11017. |type:SyntaxTree.ByteType DO
  11018. IF RefInfo THEN Info(section,"BYTE") END;
  11019. Char(section, sfTypeBYTE);
  11020. |type:SyntaxTree.RangeType DO
  11021. IF RefInfo THEN Info(section,"RANGE") END;
  11022. Char(section, sfTypeRANGE)
  11023. |type:SyntaxTree.AddressType DO
  11024. IF RefInfo THEN Info(section,"ADDRESS") END;
  11025. Char(section, sfTypeADDRESS)
  11026. |type:SyntaxTree.SizeType DO
  11027. IF RefInfo THEN Info(section,"SIZE") END;
  11028. Char(section, sfTypeSIZE)
  11029. ELSE
  11030. HALT(100)
  11031. END;
  11032. ELSE HALT(101);
  11033. END;
  11034. END;
  11035. END NType;
  11036. (*
  11037. Parameter = sfVariable prevSymbol:SIZE name:STRING (sfIndirec|sfRelative) offset:SIZE Type.
  11038. *)
  11039. PROCEDURE NParameter(parameter: SyntaxTree.Parameter; procOffset: LONGINT);
  11040. VAR pos: LONGINT; type: SyntaxTree.Type;
  11041. BEGIN
  11042. IF RefInfo THEN Info(section, "Parameter") END;
  11043. Char(section, sfVariable);
  11044. Size(section, procOffset);
  11045. String0(section, parameter.name);
  11046. type := parameter.type.resolved;
  11047. IF parameter.kind = SyntaxTree.VarParameter THEN
  11048. IF IsOpenArray(type) THEN Char(section, sfRelative)
  11049. ELSE Char(section, sfIndirect)
  11050. END;
  11051. ELSIF parameter.kind = SyntaxTree.ConstParameter THEN
  11052. IF (type IS SyntaxTree.RecordType) OR IsStaticArray(type) THEN
  11053. Char(section, sfIndirect);
  11054. ELSE
  11055. Char(section, sfRelative);
  11056. END;
  11057. ELSE
  11058. Char(section, sfRelative);
  11059. END;
  11060. Size(section, ToMemoryUnits(module.system,parameter.offsetInBits));
  11061. NType(parameter.type);
  11062. END NParameter;
  11063. (*
  11064. Procedure = sfProcedure prevSymbol:SIZE name:STRING from:ADDRESS to:ADDRESS {Parameter} returnType:Type Scope.
  11065. *)
  11066. PROCEDURE NProcedure(procedure: SyntaxTree.Procedure; scopeOffset: LONGINT);
  11067. VAR s: Sections.Section; procedureType: SyntaxTree.ProcedureType; parameter: SyntaxTree.Parameter; pos: LONGINT;
  11068. name: Basic.SegmentedName; flags: SET;
  11069. BEGIN
  11070. IF RefInfo THEN Info(section, "Procedure") END;
  11071. pos := CurrentIndex();
  11072. procedureType := procedure.type.resolved(SyntaxTree.ProcedureType);
  11073. Char(section, sfProcedure);
  11074. Size(section, scopeOffset);
  11075. String0(section,procedure.name);
  11076. s := module.allSections.FindBySymbol(procedure);
  11077. Symbol(section,s,0,0); (* start *)
  11078. Symbol(section,s,s(IntermediateCode.Section).pc,0); (* end *)
  11079. flags := {};
  11080. IF procedureType.isDelegate THEN
  11081. INCL(flags, flagDelegate);
  11082. END;
  11083. IF procedure.isConstructor THEN
  11084. INCL(flags, flagConstructor);
  11085. END;
  11086. Set(section, flags);
  11087. Global.GetSymbolSegmentedName(procedure,name);
  11088. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Descriptor"));
  11089. IF RefInfo THEN Info(section, "Parameters") END;
  11090. parameter := procedureType.firstParameter;
  11091. WHILE(parameter # NIL) DO
  11092. NParameter(parameter, pos);
  11093. parameter := parameter.nextParameter;
  11094. END;
  11095. IF RefInfo THEN Info(section, "ReturnType") END;
  11096. NType(procedureType.returnType);
  11097. NScope(procedure.procedureScope, pos);
  11098. END NProcedure;
  11099. (*
  11100. Variable = sfVariable prevSymbol:SIZE name:STRING (sfAbsolute address:ADDRESS| sfRelative offset:SIZE) Type.
  11101. *)
  11102. PROCEDURE NVariable(variable: SyntaxTree.Variable; scopeOffset: LONGINT);
  11103. VAR s: Sections.Section; sn: Basic.SegmentedName; pos: LONGINT;
  11104. BEGIN
  11105. IF RefInfo THEN Info(section, "Variable") END;
  11106. pos := CurrentIndex();
  11107. Char(section, sfVariable);
  11108. Size(section, scopeOffset);
  11109. String0(section, variable.name);
  11110. IF (variable.scope # NIL) & (variable.scope IS SyntaxTree.ModuleScope) THEN
  11111. Char(section, sfAbsolute);
  11112. implementationVisitor.GetCodeSectionNameForSymbol(variable, sn);
  11113. NamedSymbol(section, sn,variable, 0,0);
  11114. ELSE
  11115. Char(section, sfRelative);
  11116. Size(section, ToMemoryUnits(module.system,variable.offsetInBits));
  11117. END;
  11118. NType(variable.type);
  11119. s := module.allSections.FindBySymbol(variable);
  11120. END NVariable;
  11121. (*
  11122. TypeDeclaration = sfTypeDeclaration prevSymbol:SIZE name:STRING td:ADDRESS Scope.
  11123. *)
  11124. PROCEDURE NTypeDeclaration(typeDeclaration: SyntaxTree.TypeDeclaration; scopeOffset: LONGINT);
  11125. VAR declared: SyntaxTree.Type; s: Sections.Section; offset: LONGINT; name: Basic.SegmentedName; pos: LONGINT;
  11126. BEGIN
  11127. IF typeDeclaration = NIL THEN RETURN END;
  11128. pos := CurrentIndex();
  11129. s := module.allSections.FindBySymbol(typeDeclaration);
  11130. IF s = NIL THEN RETURN END; (*! duplicate, what to do? *)
  11131. IF RefInfo THEN Info(section, "TypeDeclaration") END;
  11132. Char(section, sfTypeDeclaration);
  11133. Size(section, scopeOffset);
  11134. String0(section, typeDeclaration.name);
  11135. declared := typeDeclaration.declaredType.resolved;
  11136. IF (declared IS SyntaxTree.PointerType) THEN
  11137. declared := declared(SyntaxTree.PointerType).pointerBase.resolved;
  11138. END;
  11139. WITH declared: SyntaxTree.RecordType DO
  11140. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(declared.recordScope.numberMethods)*module.system.addressSize);
  11141. Symbol(section, s, 0, offset);
  11142. Global.GetSymbolSegmentedName(typeDeclaration,name);
  11143. Basic.AppendToSegmentedName(name,".@Info");
  11144. s := module.allSections.FindByName(name);
  11145. IF s # NIL THEN (* does not work for coop *)
  11146. PatchSize(s(IntermediateCode.Section), patchInfoPC, pos);
  11147. NScope(declared.recordScope, pos);
  11148. END;
  11149. |declared: SyntaxTree.CellType DO
  11150. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(0)*module.system.addressSize);
  11151. Symbol(section, s, 0, offset);
  11152. Global.GetSymbolSegmentedName(typeDeclaration,name);
  11153. Basic.AppendToSegmentedName(name,".@Info");
  11154. s := module.allSections.FindByName(name);
  11155. IF s # NIL THEN
  11156. PatchSize(s(IntermediateCode.Section), patchInfoPC, pos);
  11157. NScope(declared.cellScope, pos);
  11158. END;
  11159. ELSE
  11160. Address(section, 0);
  11161. END;
  11162. END NTypeDeclaration;
  11163. PROCEDURE NModule(module: SyntaxTree.Module; prevSymbol: LONGINT);
  11164. VAR pos: LONGINT;
  11165. BEGIN
  11166. pos := CurrentIndex();
  11167. Char(section,sfModule);
  11168. Size(section, prevSymbol);
  11169. String0(section, module.name);
  11170. NScope(module.moduleScope, pos);
  11171. END NModule;
  11172. (*
  11173. Scope = sfScopeBegin {Variable} {Procedure} {TypeDeclaration} sfScopeEnd.
  11174. *)
  11175. PROCEDURE NScope(scope: SyntaxTree.Scope; prevSymbol: LONGINT);
  11176. VAR bodyProcedure, procedure: SyntaxTree.Procedure; variable: SyntaxTree.Variable; typeDeclaration: SyntaxTree.TypeDeclaration; pos: LONGINT;
  11177. BEGIN
  11178. IF scope = NIL THEN RETURN END;
  11179. IF RefInfo THEN Info(section, "Scope") END;
  11180. Char(section, sfScopeBegin);
  11181. variable := scope.firstVariable;
  11182. WHILE (variable # NIL) DO
  11183. NVariable(variable, prevSymbol);
  11184. variable := variable.nextVariable;
  11185. END;
  11186. WITH scope: SyntaxTree.ModuleScope DO
  11187. bodyProcedure := scope.bodyProcedure;
  11188. |scope: SyntaxTree.RecordScope DO
  11189. bodyProcedure := scope.bodyProcedure;
  11190. ELSE
  11191. bodyProcedure := NIL;
  11192. END;
  11193. IF bodyProcedure # NIL THEN
  11194. NProcedure(bodyProcedure, prevSymbol)
  11195. END;
  11196. procedure := scope.firstProcedure;
  11197. WHILE procedure # NIL DO
  11198. IF (procedure # bodyProcedure) & ~procedure.isInline THEN NProcedure(procedure, prevSymbol) END;
  11199. procedure := procedure.nextProcedure;
  11200. END;
  11201. typeDeclaration := scope.firstTypeDeclaration;
  11202. WHILE typeDeclaration # NIL DO
  11203. NTypeDeclaration(typeDeclaration, prevSymbol);
  11204. typeDeclaration := typeDeclaration.nextTypeDeclaration;
  11205. END;
  11206. Char(section, sfScopeEnd); (* scope ends *)
  11207. END NScope;
  11208. BEGIN
  11209. NEW(indirectTypes, 32);
  11210. ArrayBlock(section,sizePC,"", FALSE);
  11211. IF ~implementationVisitor.backend.cooperative THEN
  11212. startPC := section.pc;
  11213. NModule(module.module, -1);
  11214. PatchArray(section,sizePC,CurrentIndex());
  11215. END;
  11216. END References;
  11217. (*
  11218. Command* = RECORD
  11219. (* Fields exported for initialization by loader/linker only! Consider read-only! *)
  11220. name*: Name; (* name of the procedure *)
  11221. argTdAdr*, retTdAdr* : ADDRESS; (* address of type descriptors of argument and return type, 0 if no type *)
  11222. entryAdr* : ADDRESS; (* entry address of procedure *)
  11223. END;
  11224. *)
  11225. PROCEDURE CommandArray(source: IntermediateCode.Section);
  11226. VAR
  11227. p: Sections.Section; sizePC, numberCommands: LONGINT;
  11228. procedure : SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType;
  11229. name: SyntaxTree.IdentifierString; numberParameters, i: LONGINT;
  11230. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  11231. PROCEDURE GetProcedureAllowed() : BOOLEAN;
  11232. PROCEDURE TypeAllowed(type : SyntaxTree.Type) : BOOLEAN;
  11233. BEGIN
  11234. RETURN
  11235. (type = NIL) OR
  11236. (type.resolved IS SyntaxTree.RecordType) OR
  11237. (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) OR
  11238. (type.resolved IS SyntaxTree.AnyType);
  11239. END TypeAllowed;
  11240. BEGIN
  11241. numberParameters := procedureType.numberParameters;
  11242. RETURN
  11243. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  11244. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  11245. (numberParameters = 1) & (procedureType.firstParameter.type.resolved IS SyntaxTree.AnyType) & (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.AnyType);
  11246. END GetProcedureAllowed;
  11247. PROCEDURE WriteType(type : SyntaxTree.Type);
  11248. VAR typeDeclaration: SyntaxTree.TypeDeclaration; section: Sections.Section;
  11249. name: Basic.SegmentedName; offset: LONGINT;
  11250. BEGIN
  11251. IF type = NIL THEN
  11252. Address(source,0);
  11253. ELSIF (type.resolved IS SyntaxTree.AnyType) OR (type.resolved IS SyntaxTree.ObjectType) THEN
  11254. Address(source,1);
  11255. ELSE
  11256. type := type.resolved;
  11257. IF type IS SyntaxTree.PointerType THEN
  11258. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  11259. END;
  11260. typeDeclaration := type.typeDeclaration; (* must be non-nil *)
  11261. IF (typeDeclaration.scope = NIL) OR (typeDeclaration.scope.ownerModule = module.module) THEN
  11262. name[0] := typeDeclaration.name; name[1] := -1;
  11263. section := module.allSections.FindBySymbol(type.typeDeclaration); (*TODO*)
  11264. ASSERT(section # NIL);
  11265. ELSE
  11266. Global.GetSymbolSegmentedName(typeDeclaration,name);
  11267. (* TODO *)
  11268. section := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,typeDeclaration, source.comments # NIL);
  11269. END;
  11270. IF implementationVisitor.backend.cooperative THEN
  11271. offset := 0;
  11272. ELSE
  11273. offset := 1 + type(SyntaxTree.RecordType).recordScope.numberMethods+16+1;
  11274. END;
  11275. Symbol(source,section, 0, ToMemoryUnits(module.system,offset*module.system.addressSize));
  11276. END;
  11277. END WriteType;
  11278. BEGIN
  11279. Info(source, "command array descriptor");
  11280. ArrayBlock(source,sizePC,"Modules.Command", FALSE);
  11281. numberCommands := 0;
  11282. Info(source, "command array content");
  11283. FOR i := 0 TO module.allSections.Length() - 1 DO
  11284. p := module.allSections.GetSection(i);
  11285. IF (p.symbol # NIL) & (p.symbol IS SyntaxTree.Procedure) THEN
  11286. procedure := p.symbol(SyntaxTree.Procedure);
  11287. procedureType := procedure.type(SyntaxTree.ProcedureType);
  11288. IF (SyntaxTree.PublicWrite IN procedure.access) & ~(procedure.isInline) & ~(procedureType.isDelegate) & GetProcedureAllowed() THEN
  11289. procedure.GetName(name);
  11290. Name(source,name);
  11291. numberParameters := procedureType.numberParameters;
  11292. (* offset of type of first parameter *)
  11293. IF (numberParameters = 0 ) THEN WriteType(NIL)
  11294. ELSE WriteType(procedureType.firstParameter.type)
  11295. END;
  11296. (* offset of type of return parameter *)
  11297. WriteType(procedureType.returnType);
  11298. (* command name *)
  11299. (* command code offset *)
  11300. Symbol(source,p,0,0);
  11301. INC(numberCommands);
  11302. IF Trace THEN
  11303. D.Ln;
  11304. END;
  11305. END;
  11306. END
  11307. END;
  11308. PatchArray(source,sizePC,numberCommands);
  11309. END CommandArray;
  11310. (* to prevent from double import of different module aliases *)
  11311. PROCEDURE IsFirstDirectOccurence(import: SyntaxTree.Import): BOOLEAN; (*! inefficient *)
  11312. VAR i: SyntaxTree.Import;
  11313. BEGIN
  11314. i := module.module.moduleScope.firstImport;
  11315. WHILE (i # NIL) & ((i.module # import.module) OR ~i.direct) DO
  11316. i := i.nextImport;
  11317. END;
  11318. RETURN i = import
  11319. END IsFirstDirectOccurence;
  11320. PROCEDURE ImportsArray(source: IntermediateCode.Section);
  11321. VAR import: SyntaxTree.Import ; pc: LONGINT;name: Basic.SegmentedName; numberImports: LONGINT; offset: LONGINT;
  11322. BEGIN
  11323. (* strictly speaking this needs to be a pointer array but by the construction of module loading, this references are not required *)
  11324. ArrayBlock(source,pc,"", FALSE);
  11325. Info(source, "import module array data");
  11326. IF implementationVisitor.backend.cooperative THEN
  11327. offset := 0;
  11328. ELSE
  11329. IF module.system.addressType.sizeInBits = 64 THEN
  11330. offset := ToMemoryUnits(module.system, 18* module.system.addressSize) (* Module pointer offset -- cf. ModuleSection(), how to encode generically correct? *);
  11331. ELSE
  11332. offset := ToMemoryUnits(module.system, 22* module.system.addressSize) (* Module pointer offset -- cf. ModuleSection(), how to encode generically correct? *);
  11333. END;
  11334. END;
  11335. import := module.module.moduleScope.firstImport;
  11336. numberImports := 0;
  11337. WHILE import # NIL DO
  11338. IF import.direct & ~Global.IsSystemModule(import.module) & IsFirstDirectOccurence(import) THEN
  11339. Global.GetModuleSegmentedName(import.module,name);
  11340. Basic.SuffixSegmentedName(name, StringPool.GetIndex1("@Module"));
  11341. NamedSymbol(source, name, NIL, 0, offset);
  11342. INC(numberImports);
  11343. END;
  11344. import := import.nextImport
  11345. END;
  11346. PatchArray(source,pc,numberImports);
  11347. END ImportsArray;
  11348. PROCEDURE TypeInfoSection(source: IntermediateCode.Section);
  11349. VAR
  11350. p: Sections.Section; sizePC, size, i: LONGINT;
  11351. BEGIN
  11352. Info(source, "Type info section");
  11353. size := 0;
  11354. ArrayBlock(source,sizePC,"Modules.TypeDesc", FALSE);
  11355. FOR i := 0 TO module.allSections.Length() - 1 DO
  11356. p := module.allSections.GetSection(i);
  11357. WITH p: IntermediateCode.Section DO
  11358. IF Basic.SegmentedNameEndsWith(p.name,"@Info") THEN
  11359. Symbol(source,p,0,0);
  11360. INC(size);
  11361. END;
  11362. END
  11363. END;
  11364. PatchArray(source,sizePC,size);
  11365. END TypeInfoSection;
  11366. (*
  11367. ProcTableEntry* = RECORD
  11368. pcFrom*, pcLimit*, pcStatementBegin*, pcStatementEnd*: ADDRESS;
  11369. noPtr*: LONGINT;
  11370. END;
  11371. ProcTable* = POINTER TO ARRAY OF ProcTableEntry;
  11372. PtrTable* = POINTER TO ARRAY OF ADDRESS;
  11373. *)
  11374. PROCEDURE ProcedureDescriptor(section: IntermediateCode.Section; procedureSection: IntermediateCode.Section);
  11375. VAR
  11376. numberPointers: SIZE;
  11377. procedure: SyntaxTree.Procedure;
  11378. BEGIN
  11379. Info(section,"pcFrom");
  11380. Symbol(section,procedureSection,0,0);
  11381. Info(section,"pcTo");
  11382. Symbol(section, procedureSection, procedureSection.pc, 0);
  11383. Info(section,"pointer to offsets array");
  11384. Symbol(section, section,section.pc+1,0);
  11385. Info(section,"offsets array");
  11386. procedure := procedureSection.symbol(SyntaxTree.Procedure);
  11387. PointerArray(section, procedure.procedureScope, numberPointers);
  11388. END ProcedureDescriptor;
  11389. PROCEDURE ProcedureDescriptorPointer(section: IntermediateCode.Section; procedureSection: IntermediateCode.Section);
  11390. VAR dest: IntermediateCode.Section; name: Basic.SegmentedName; offset: LONGINT;
  11391. BEGIN
  11392. name := procedureSection.name;
  11393. Basic.SuffixSegmentedName(name, Basic.MakeString("@Descriptor"));
  11394. dest := NamedBlock("Heaps","SystemBlock",name,offset);
  11395. ProcedureDescriptor(dest, procedureSection);
  11396. Symbol(section, dest, offset, 0);
  11397. END ProcedureDescriptorPointer;
  11398. PROCEDURE ProcedureDescriptorArray(section: IntermediateCode.Section; VAR numberProcs: LONGINT);
  11399. VAR sizePC, i: LONGINT; destination: Sections.Section;
  11400. BEGIN
  11401. ArrayBlock(section, sizePC,"Modules.ProcedureDescPointer",FALSE);
  11402. numberProcs := 0;
  11403. FOR i := 0 TO module.allSections.Length() - 1 DO
  11404. destination := module.allSections.GetSection(i);
  11405. IF (destination.type IN {Sections.CodeSection, Sections.BodyCodeSection}) & (destination.symbol # NIL) & (destination.symbol IS SyntaxTree.Procedure) & ~destination.symbol(SyntaxTree.Procedure).isInline THEN
  11406. ProcedureDescriptorPointer(section, destination(IntermediateCode.Section));
  11407. INC(numberProcs);
  11408. END
  11409. END;
  11410. PatchArray(section, sizePC, numberProcs);
  11411. END ProcedureDescriptorArray;
  11412. (*
  11413. Module* = OBJECT (Heaps.RootObject) (* cf. Linker0 & Heaps.WriteType *)
  11414. VAR
  11415. next*: Module; (** once a module is published, all fields are read-only *)
  11416. name*: Name;
  11417. init, published: BOOLEAN;
  11418. refcnt*: LONGINT; (* counts loaded modules that import this module *)
  11419. sb*: ADDRESS; <- should be zero as the static base in generic object file is indeed 0 !
  11420. entry*: POINTER TO ARRAY OF ADDRESS; <- not needed in new loader
  11421. command*: POINTER TO ARRAY OF Command;
  11422. ptrAdr*: POINTER TO ARRAY OF ADDRESS;
  11423. typeInfo*: POINTER TO ARRAY OF TypeDesc;
  11424. module*: POINTER TO ARRAY OF Module; <---- currently done by loader
  11425. procTable*: ProcTable; (* information inserted by loader, removed after use in Publish *)
  11426. ptrTable*: PtrTable; (* information inserted by loader, removed after use in Publish *)
  11427. data*, code*: Bytes;
  11428. staticTypeDescs* (* ug *), refs*: Bytes; <- staticTypeDescs in data section, refs currently unsupported
  11429. export*: ExportDesc;
  11430. term*: TerminationHandler;
  11431. exTable*: ExceptionTable;
  11432. noProcs*: LONGINT;
  11433. firstProc*: ADDRESS; <- done by loader
  11434. maxPtrs*: LONGINT;
  11435. crc*: LONGINT;
  11436. *)
  11437. PROCEDURE BasePointer (section: IntermediateCode.Section);
  11438. BEGIN
  11439. Info(section, "cycle");
  11440. Size(section,0);
  11441. Info(section, "references");
  11442. Size(section,0);
  11443. Info(section, "nextMarked");
  11444. Address(section,0);
  11445. Info(section, "nextWatched");
  11446. Address(section,0);
  11447. END BasePointer;
  11448. PROCEDURE BaseObject (section: IntermediateCode.Section);
  11449. BEGIN
  11450. BasePointer(section);
  11451. Info(section, "action");
  11452. Address(section,0);
  11453. Info(section, "monitor");
  11454. Address(section,0);
  11455. END BaseObject;
  11456. PROCEDURE ModuleDescriptor(section: IntermediateCode.Section);
  11457. VAR descriptorSection: IntermediateCode.Section; name: ARRAY 128 OF CHAR;
  11458. pooledName: Basic.SegmentedName;
  11459. symbol: SyntaxTree.Symbol;
  11460. BEGIN
  11461. Global.GetModuleName(module.module,name);
  11462. Strings.Append(name,".@Module.@Descriptor");
  11463. Basic.ToSegmentedName(name, pooledName);
  11464. descriptorSection := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, pooledName,NIL,declarationVisitor.dump);
  11465. Symbol(section,descriptorSection,0,0);
  11466. Info(descriptorSection, "descriptor");
  11467. symbol := implementationVisitor.GetTypeDescriptor("Modules","Module", pooledName);
  11468. NamedSymbol(descriptorSection, pooledName,symbol, 0, 0);
  11469. Address(descriptorSection,0);
  11470. Global.GetModuleName(module.module,name);
  11471. Strings.Append(name,".@Trace");
  11472. Basic.ToSegmentedName(name, pooledName);
  11473. NamedSymbol(descriptorSection, pooledName,NIL, 0, 0);
  11474. Basic.ToSegmentedName ("BaseTypes.Object.Finalize",pooledName);
  11475. NamedSymbol(descriptorSection, pooledName,NIL, 0, 0);
  11476. END ModuleDescriptor;
  11477. PROCEDURE ModuleSection(): IntermediateCode.Section;
  11478. VAR name: ARRAY 128 OF CHAR;
  11479. moduleSection: IntermediateCode.Section; offset: LONGINT; pooledName: Basic.SegmentedName;
  11480. symbol: SyntaxTree.Symbol;
  11481. BEGIN
  11482. Global.GetModuleName(module.module,name);
  11483. Strings.Append(name,".@Module");
  11484. Basic.ToSegmentedName(name, pooledName);
  11485. moduleSection := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, pooledName,NIL,declarationVisitor.dump);
  11486. moduleSection.SetExported(TRUE);
  11487. IF moduleSection.pc = 0 THEN
  11488. IF implementationVisitor.backend.cooperative THEN
  11489. Info(moduleSection, "descriptor");
  11490. ModuleDescriptor(moduleSection);
  11491. BaseObject(moduleSection);
  11492. implementationVisitor.CreateTraceModuleMethod(module.module);
  11493. ELSE
  11494. ProtectedHeapBlock("Heaps","ProtRecBlockDesc",moduleSection,2);
  11495. Info(moduleSection, "HeapBlock");
  11496. Symbol(moduleSection,moduleSection,2,0);
  11497. Info(moduleSection, "TypeDescriptor");
  11498. symbol := implementationVisitor.GetTypeDescriptor("Modules","Module", pooledName);
  11499. offset := ToMemoryUnits(module.system,(TypeRecordBaseOffset + 1 (*= numberMethods*))*module.system.addressSize);
  11500. NamedSymbol(moduleSection, pooledName,symbol, 0, offset);
  11501. END;
  11502. END;
  11503. RETURN moduleSection;
  11504. END ModuleSection;
  11505. PROCEDURE NewModuleInfo();
  11506. VAR name: Basic.SegmentedName;source: IntermediateCode.Section;
  11507. moduleSection: IntermediateCode.Section; i: LONGINT; flags: SET;
  11508. sectionName: Basic.SectionName;
  11509. CONST MPO=-40000000H;
  11510. BEGIN
  11511. (*
  11512. TypeDesc* = POINTER TO RECORD
  11513. descSize: LONGINT;
  11514. sentinel: LONGINT; (* = MPO-4 *)
  11515. tag*: ADDRESS; (* pointer to static type descriptor, only used by linker and loader *)
  11516. flags*: SET;
  11517. mod*: Module; (* hint only, because module may have been freed (at Heaps.ModOfs) *)
  11518. name*: Name;
  11519. END;
  11520. *)
  11521. Global.GetSymbolSegmentedName(module.module,name);
  11522. Basic.AppendToSegmentedName(name,".@Info");
  11523. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11524. Info(source, "type info size"); Address(source, 3*ToMemoryUnits(module.system,module.system.addressSize)+32);
  11525. Address(source,MPO-4);
  11526. Info(source, "type tag pointer");
  11527. Address( source,0);
  11528. Info(source, "type flags");
  11529. flags := {};
  11530. Set( source, flags);
  11531. Info(source, "pointer to module");
  11532. moduleSection := ModuleSection();
  11533. Symbol( source, moduleSection, moduleSection.pc,0);
  11534. Info(source, "type name");
  11535. i := 0;
  11536. sectionName := "@Self";
  11537. (*
  11538. Global.GetSymbolSegmentedName(td,name);
  11539. Basic.SegmentedNameToString(name, sectionName);
  11540. *)
  11541. Name(source,sectionName);
  11542. source.SetReferenced(FALSE);
  11543. patchInfoPC := source.pc;
  11544. Size(source, 0);
  11545. END NewModuleInfo;
  11546. PROCEDURE Module(bodyProc: IntermediateCode.Section);
  11547. VAR
  11548. moduleSection, pointerSection, importSection, emptyArraySection, exceptionSection, commandsSection,
  11549. typeInfoSection, procTableSection, referenceSection : IntermediateCode.Section;
  11550. emptyArraySectionOffset, pointerSectionOffset, importSectionOffset, numberPointers,
  11551. exceptionSectionOffset, commandsSectionOffset, typeInfoSectionOffset, procTableSectionOffset, maxPointers, numberProcs,temp,
  11552. referenceSectionOffset : LONGINT;
  11553. name: Basic.SegmentedName; offset: LONGINT;
  11554. BEGIN
  11555. NewModuleInfo();
  11556. pointerSection := Block("Heaps","SystemBlockDesc",".@PointerArray",pointerSectionOffset);
  11557. PointerArray(pointerSection,module.module.moduleScope, numberPointers);
  11558. importSection := Block("Heaps","SystemBlockDesc",".@ImportsArray",importSectionOffset);
  11559. ImportsArray(importSection);
  11560. commandsSection := Block("Heaps","SystemBlockDesc",".@CommandArray",commandsSectionOffset);
  11561. CommandArray(commandsSection);
  11562. exceptionSection := Block("Heaps","SystemBlockDesc",".@ExceptionArray",exceptionSectionOffset);
  11563. ExceptionArray(exceptionSection);
  11564. typeInfoSection := Block("Heaps","SystemBlockDesc",".@TypeInfoArray",typeInfoSectionOffset);
  11565. AddPointer(typeInfoSection, typeInfoSectionOffset);
  11566. TypeInfoSection(typeInfoSection);
  11567. referenceSection := Block("Heaps","SystemBlockDesc",".@References",referenceSectionOffset);
  11568. References(referenceSection);
  11569. procTableSection := Block("Heaps","SystemBlockDesc",".@ProcTable",procTableSectionOffset);
  11570. ProcedureDescriptorArray(procTableSection, numberProcs);
  11571. name := "Heaps.AnyPtr";
  11572. offset := ToMemoryUnits(module.system,TypeRecordBaseOffset*module.system.addressSize);
  11573. (* set base pointer *)
  11574. IF procTableSectionOffset > 0 THEN (*? for coop ? *)
  11575. NamedSymbolAt(procTableSection, procTableSectionOffset -1 , name, NIL, 0, offset);
  11576. END;
  11577. emptyArraySection := Block("Heaps","SystemBlockDesc",".@EmptyArray",emptyArraySectionOffset);
  11578. ArrayBlock(emptyArraySection,temp,"", FALSE);
  11579. moduleSection := ModuleSection();
  11580. Info(moduleSection, "nextRoot*: RootObject");
  11581. Address(moduleSection,0);
  11582. Info(moduleSection, "next*: Module");
  11583. Address(moduleSection,0);
  11584. Info(moduleSection, "name*: Name");
  11585. Name(moduleSection,moduleName);
  11586. Info(moduleSection, "init, published: BOOLEAN");
  11587. Boolean(moduleSection,FALSE);
  11588. Boolean(moduleSection,FALSE);
  11589. Info(moduleSection,"filler"); (*! introduce alignment! *)
  11590. Boolean(moduleSection,FALSE);
  11591. Boolean(moduleSection,FALSE);
  11592. Info(moduleSection, "refcnt*: LONGINT");
  11593. Longint(moduleSection,0);
  11594. Info(moduleSection, "sb*: ADDRESS");
  11595. Address(moduleSection,0);
  11596. Info(moduleSection, "entry*: POINTER TO ARRAY OF ADDRESS");
  11597. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  11598. Info(moduleSection, "command*: POINTER TO ARRAY OF Command");
  11599. Symbol(moduleSection,commandsSection,commandsSectionOffset,0);
  11600. Info(moduleSection, "ptrAdr*: POINTER TO ARRAY OF ADDRESS");
  11601. Symbol(moduleSection,pointerSection,pointerSectionOffset,0);
  11602. Info(moduleSection, "typeInfo*: POINTER TO ARRAY OF TypeDesc");
  11603. Symbol(moduleSection,typeInfoSection,typeInfoSectionOffset,0);
  11604. Info(moduleSection, "module*: POINTER TO ARRAY OF Module");
  11605. Symbol(moduleSection,importSection,emptyArraySectionOffset,0);
  11606. Info(moduleSection, "procTable*: ProcTable");
  11607. Symbol(moduleSection,procTableSection,procTableSectionOffset,0);
  11608. Info(moduleSection, "data*, code*, staticTypeDescs*, refs*: Bytes");
  11609. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  11610. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  11611. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  11612. Symbol(moduleSection,referenceSection,referenceSectionOffset,0);
  11613. Info(moduleSection, "export*: ExportDesc");
  11614. ExportDesc(moduleSection);
  11615. Info(moduleSection, "term*: TerminationHandler");
  11616. Address(moduleSection,0);
  11617. Info(moduleSection, "exTable*: ExceptionTable");
  11618. Symbol(moduleSection,exceptionSection,exceptionSectionOffset,0);
  11619. IF module.system.addressType.sizeInBits = 64 THEN Longint(moduleSection, 0); END; (* padding -> alignment *)
  11620. Info(moduleSection,"internal: POINTER TO ARRAY OF Pointer");
  11621. Symbol(moduleSection, modulePointerSection, modulePointerSectionOffset, 0);
  11622. Info(moduleSection, "crc*: LONGINT");
  11623. Longint(moduleSection, 0); (*! must be implemented *)
  11624. Info(moduleSection, "body*: ADDRESS");
  11625. Symbol(moduleSection, bodyProc, 0,0);
  11626. IF implementationVisitor.backend.cooperative THEN
  11627. PatchSymbol(moduleSection,MonitorOffset,moduleSection.name,NIL,moduleSection.pc,0);
  11628. Info(moduleSection, "monitor.owner");
  11629. Address(moduleSection,0);
  11630. Info(moduleSection, "monitor.nestingLevel");
  11631. Address(moduleSection,0);
  11632. Info(moduleSection, "monitor.blockedQueue");
  11633. Address(moduleSection,0); Address(moduleSection,0);
  11634. Info(moduleSection, "monitor.waitingQueue");
  11635. Address(moduleSection,0); Address(moduleSection,0);
  11636. Info(moduleSection, "monitor.waitingSentinel");
  11637. Address(moduleSection,0);
  11638. END;
  11639. END Module;
  11640. PROCEDURE PointerArray(source: IntermediateCode.Section; scope: SyntaxTree.Scope; VAR numberPointers: LONGINT);
  11641. VAR variable: SyntaxTree.Variable; pc: LONGINT; symbol: Sections.Section; parameter: SyntaxTree.Parameter; parametersSize: SIZE;
  11642. BEGIN
  11643. ArrayBlock(source,pc,"",FALSE);
  11644. Info(source, "pointer offsets array data");
  11645. IF scope IS SyntaxTree.RecordScope THEN
  11646. Pointers(0,symbol, source,scope(SyntaxTree.RecordScope).ownerRecord,numberPointers);
  11647. ELSIF scope IS SyntaxTree.CellScope THEN
  11648. Pointers(0, symbol, source, scope(SyntaxTree.CellScope).ownerCell, numberPointers);
  11649. ELSIF scope IS SyntaxTree.ModuleScope THEN
  11650. variable := scope(SyntaxTree.ModuleScope).firstVariable;
  11651. WHILE variable # NIL DO
  11652. IF ~(variable.untraced) & (variable.externalName = NIL) THEN
  11653. symbol := module.allSections.FindBySymbol(variable);
  11654. ASSERT(symbol # NIL);
  11655. Pointers(0,symbol, source,variable.type,numberPointers);
  11656. END;
  11657. variable := variable.nextVariable;
  11658. END;
  11659. ELSIF scope IS SyntaxTree.ProcedureScope THEN
  11660. parameter := scope(SyntaxTree.ProcedureScope).ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  11661. WHILE parameter # NIL DO
  11662. IF parameter.NeedsTrace() & ~IsVariableParameter(parameter) THEN
  11663. Pointers(ToMemoryUnits(module.system,parameter.offsetInBits), NIL, source, parameter.type, numberPointers);
  11664. END;
  11665. parameter := parameter.nextParameter;
  11666. END;
  11667. IF scope(SyntaxTree.ProcedureScope).ownerProcedure.type(SyntaxTree.ProcedureType).isDelegate THEN
  11668. parametersSize := ProcedureParametersSize(module.system,scope(SyntaxTree.ProcedureScope).ownerProcedure);
  11669. INC(parametersSize,ToMemoryUnits(module.system,module.system.addressSize));
  11670. IF implementationVisitor.backend.preciseGC THEN
  11671. INC(parametersSize,ToMemoryUnits(module.system,module.system.addressSize));
  11672. END;
  11673. Symbol(source, NIL, 0, parametersSize); INC(numberPointers);
  11674. END;
  11675. variable := scope(SyntaxTree.ProcedureScope).firstVariable;
  11676. WHILE(variable # NIL) DO
  11677. IF ~(variable.untraced) & (variable.externalName = NIL) THEN
  11678. Pointers(ToMemoryUnits(module.system,variable.offsetInBits), NIL, source, variable.type, numberPointers);
  11679. END;
  11680. variable := variable.nextVariable
  11681. END;
  11682. END;
  11683. PatchArray(source,pc,numberPointers);
  11684. END PointerArray;
  11685. PROCEDURE SymbolSection(symbol: SyntaxTree.Symbol; CONST suffix: ARRAY OF CHAR; VAR pc: LONGINT): IntermediateCode.Section;
  11686. VAR
  11687. name: Basic.SegmentedName;
  11688. section: IntermediateCode.Section;
  11689. BEGIN
  11690. ASSERT(implementationVisitor.newObjectFile);
  11691. Global.GetSymbolSegmentedName(symbol,name);
  11692. Basic.AppendToSegmentedName(name,suffix);
  11693. section := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name, NIL,declarationVisitor.dump);
  11694. HeapBlock("Heaps","SystemBlockDesc", section, 2);
  11695. Info(section, "HeapBlock");
  11696. Address(section,0); (* empty such that GC does not go on traversing *)
  11697. Info(section, suffix);
  11698. Address(section,0);
  11699. pc := section.pc;
  11700. RETURN section;
  11701. END SymbolSection;
  11702. PROCEDURE CheckTypeDeclaration(x: SyntaxTree.Type);
  11703. VAR recordType: SyntaxTree.RecordType;
  11704. tir: IntermediateCode.Section; op: IntermediateCode.Operand; name: Basic.SegmentedName; td: SyntaxTree.TypeDeclaration;
  11705. section: Sections.Section; cellType: SyntaxTree.CellType;
  11706. PROCEDURE NewTypeDescriptorInfo(tag: Sections.Section; offset: LONGINT; isProtected: BOOLEAN): Sections.Section;
  11707. VAR name: Basic.SegmentedName;source: IntermediateCode.Section;
  11708. moduleSection: IntermediateCode.Section; i: LONGINT; flags: SET;
  11709. sectionName: Basic.SectionName;
  11710. CONST MPO=-40000000H;
  11711. BEGIN
  11712. (*
  11713. TypeDesc* = POINTER TO RECORD
  11714. descSize: LONGINT;
  11715. sentinel: LONGINT; (* = MPO-4 *)
  11716. tag*: ADDRESS; (* pointer to static type descriptor, only used by linker and loader *)
  11717. flags*: SET;
  11718. mod*: Module; (* hint only, because module may have been freed (at Heaps.ModOfs) *)
  11719. name*: Name;
  11720. END;
  11721. *)
  11722. (* source := module.sections.FindByName(...) *)
  11723. Global.GetSymbolSegmentedName(td,name);
  11724. Basic.AppendToSegmentedName(name,".@Info");
  11725. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11726. Info(source, "type info size"); Address(source, 3*ToMemoryUnits(module.system,module.system.addressSize)+32);
  11727. Address(source,MPO-4);
  11728. Info(source, "type tag pointer");
  11729. Symbol( source, tag, offset, 0);
  11730. Info(source, "type flags");
  11731. flags := {};
  11732. IF isProtected THEN INCL(flags,31) END;
  11733. Set( source, flags);
  11734. Info(source, "pointer to module");
  11735. moduleSection := ModuleSection();
  11736. Symbol( source, moduleSection, moduleSection.pc,0);
  11737. Info(source, "type name");
  11738. i := 0;
  11739. Global.GetSymbolNameInScope(td, module.module.moduleScope, sectionName);
  11740. (*
  11741. Global.GetSymbolSegmentedName(td,name);
  11742. Basic.SegmentedNameToString(name, sectionName);
  11743. *)
  11744. Name(source,sectionName);
  11745. source.SetReferenced(FALSE);
  11746. Size(source, 0);
  11747. RETURN source;
  11748. END NewTypeDescriptorInfo;
  11749. PROCEDURE NewTypeDescriptor;
  11750. VAR name: Basic.SegmentedName; op: IntermediateCode.Operand; source, base: IntermediateCode.Section;
  11751. procedure: SyntaxTree.Procedure; baseRecord: SyntaxTree.RecordType;
  11752. baseTD: SyntaxTree.TypeDeclaration; sym: SyntaxTree.Symbol;
  11753. numberPointers: LONGINT; padding, i: LONGINT;
  11754. CONST MPO=-40000000H;
  11755. PROCEDURE TdTable(size: LONGINT; reverse: BOOLEAN);
  11756. VAR i: LONGINT;
  11757. PROCEDURE Td(record: SyntaxTree.RecordType);
  11758. VAR baseTD: SyntaxTree.TypeDeclaration; name: Basic.SegmentedName; offset: LONGINT;
  11759. BEGIN
  11760. IF record # NIL THEN
  11761. IF ~reverse THEN Td(record.GetBaseRecord()) END;
  11762. baseTD := record.typeDeclaration;
  11763. Global.GetSymbolSegmentedName(baseTD,name);
  11764. IF (baseTD.scope = NIL) OR (baseTD.scope.ownerModule = module.module) THEN
  11765. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11766. ELSE
  11767. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11768. END;
  11769. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(record.recordScope.numberMethods)*module.system.addressSize);
  11770. Symbol(source, tir, 0, offset);
  11771. IF reverse THEN Td(record.GetBaseRecord()) END;
  11772. END;
  11773. END Td;
  11774. BEGIN
  11775. Info(source, "tag table");
  11776. baseRecord := recordType;
  11777. i := 0;
  11778. WHILE baseRecord # NIL DO
  11779. INC(i);
  11780. baseRecord := baseRecord.GetBaseRecord();
  11781. END;
  11782. IF i > size THEN implementationVisitor.Error(x.position,"maximal extension level exceeded") END;
  11783. IF ~reverse THEN Td(recordType) END;
  11784. WHILE i < size DO
  11785. Address(source,0);
  11786. INC(i);
  11787. END;
  11788. IF reverse THEN Td(recordType) END;
  11789. END TdTable;
  11790. PROCEDURE MethodTable(reverse: BOOLEAN);
  11791. VAR i,methods: LONGINT;
  11792. BEGIN
  11793. Info(source, "method table");
  11794. IF recordType # NIL THEN
  11795. methods := recordType.recordScope.numberMethods;
  11796. IF reverse THEN
  11797. FOR i := methods-1 TO 0 BY -1 DO
  11798. procedure := recordType.recordScope.FindMethod(i);
  11799. implementationVisitor.GetCodeSectionNameForSymbol(procedure, name);
  11800. NamedSymbol(source, name,procedure, 0,0);
  11801. END;
  11802. ELSE
  11803. FOR i := 0 TO methods-1 DO
  11804. procedure := recordType.recordScope.FindMethod(i);
  11805. implementationVisitor.GetCodeSectionNameForSymbol(procedure, name);
  11806. NamedSymbol(source, name,procedure, 0,0);
  11807. END;
  11808. END;
  11809. END;
  11810. END MethodTable;
  11811. PROCEDURE CooperativeMethodTable(pointer: BOOLEAN);
  11812. VAR baseRecord: SyntaxTree.RecordType; name, stackFrame: Basic.SegmentedName; i,start,methods: LONGINT;
  11813. BEGIN
  11814. Info(source, "method table");
  11815. baseRecord := recordType;
  11816. WHILE baseRecord.baseType # NIL DO
  11817. baseRecord := baseRecord.GetBaseRecord ();
  11818. END;
  11819. GetRecordTypeName (baseRecord, name);
  11820. Basic.ToSegmentedName ("BaseTypes.StackFrame", stackFrame);
  11821. IF name = stackFrame THEN
  11822. start := 0;
  11823. ELSIF ~HasExplicitTraceMethod(recordType) THEN
  11824. baseRecord := recordType;
  11825. WHILE (baseRecord # NIL) & ~baseRecord.hasPointers DO
  11826. baseRecord := baseRecord.GetBaseRecord ();
  11827. END;
  11828. IF baseRecord # NIL THEN
  11829. GetRecordTypeName (baseRecord, name);
  11830. IF pointer & ~baseRecord.isObject THEN
  11831. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  11832. END;
  11833. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  11834. ELSIF recordType.isObject THEN
  11835. Basic.ToSegmentedName ("BaseTypes.Object.@Trace",name);
  11836. ELSIF pointer THEN
  11837. Basic.ToSegmentedName ("BaseTypes.Pointer.Trace",name);
  11838. ELSE
  11839. Basic.ToSegmentedName ("BaseTypes.Record.@Trace",name);
  11840. END;
  11841. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11842. Symbol(source, tir, 0, 0);
  11843. start := 0;
  11844. baseRecord := recordType;
  11845. WHILE (baseRecord # NIL) DO
  11846. IF HasExplicitTraceMethod(baseRecord) THEN start := 1 END;
  11847. baseRecord := baseRecord.GetBaseRecord ();
  11848. END;
  11849. ELSE
  11850. (* explicit trace method: *)
  11851. procedure := recordType.recordScope.FindMethod(0);
  11852. IF ~procedure.isFinalizer THEN
  11853. Global.GetSymbolSegmentedName(procedure, name);
  11854. NamedSymbol(source, name,procedure, 0,0);
  11855. END;
  11856. start := 1;
  11857. END;
  11858. IF (name # stackFrame) & recordType.isObject THEN
  11859. baseRecord := recordType;
  11860. WHILE (baseRecord # NIL) & (baseRecord.recordScope.finalizer = NIL) DO
  11861. baseRecord := baseRecord.GetBaseRecord ();
  11862. END;
  11863. IF (baseRecord = NIL) OR (baseRecord.recordScope.finalizer = NIL) THEN
  11864. Basic.ToSegmentedName ("BaseTypes.Object.Finalize",name);
  11865. ELSE
  11866. Global.GetSymbolSegmentedName(baseRecord.recordScope.finalizer, name);
  11867. END;
  11868. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11869. Symbol(source, tir, 0, 0);
  11870. END;
  11871. methods := recordType.recordScope.numberMethods;
  11872. FOR i := start TO methods-1 DO
  11873. procedure := recordType.recordScope.FindMethod(i);
  11874. IF ~procedure.isFinalizer THEN
  11875. Global.GetSymbolSegmentedName(procedure, name);
  11876. NamedSymbol(source, name,procedure, 0,0);
  11877. END;
  11878. END;
  11879. END CooperativeMethodTable;
  11880. BEGIN
  11881. Global.GetSymbolSegmentedName(td,name);
  11882. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,td,declarationVisitor.dump);
  11883. source.SetExported(IsExported(td));
  11884. IF (cellType # NIL) THEN recordType := cellType.GetBaseRecord() END;
  11885. IF implementationVisitor.backend.cooperative THEN
  11886. base := NIL;
  11887. baseRecord := recordType.GetBaseRecord();
  11888. IF baseRecord # NIL THEN
  11889. baseTD := baseRecord.typeDeclaration;
  11890. END;
  11891. IF ~recordType.isObject THEN
  11892. Info(source, "parent");
  11893. IF baseRecord # NIL THEN
  11894. Global.GetSymbolSegmentedName(baseTD,name);
  11895. IF (baseTD.scope = NIL) OR (baseTD.scope.ownerModule = module.module) THEN
  11896. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11897. ELSE
  11898. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11899. END;
  11900. Symbol(source, tir, 0, 0);
  11901. ELSE
  11902. Address(source,0);
  11903. END;
  11904. Info(source, "record size");
  11905. Address(source, ToMemoryUnits(module.system,module.system.SizeOf(recordType)));
  11906. source.SetReferenced(FALSE);
  11907. CooperativeMethodTable(FALSE);
  11908. base := source;
  11909. Global.GetSymbolSegmentedName(td,name);
  11910. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  11911. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11912. source.SetExported(IsExported(td));
  11913. source.SetReferenced(FALSE);
  11914. END;
  11915. Info(source, "parent");
  11916. IF baseRecord # NIL THEN
  11917. Global.GetSymbolSegmentedName(baseTD,name);
  11918. sym := baseTD;
  11919. IF ~recordType.isObject THEN
  11920. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  11921. sym := NIL;
  11922. END;
  11923. IF (baseTD.scope = NIL) OR (baseTD.scope.ownerModule = module.module) THEN
  11924. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,sym,declarationVisitor.dump);
  11925. ELSE
  11926. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,sym,declarationVisitor.dump);
  11927. END;
  11928. Symbol(source, tir, 0, 0);
  11929. ELSIF (recordType.pointerType # NIL) & recordType.pointerType.isPlain THEN
  11930. Address(source,0);
  11931. ELSE
  11932. IF recordType.isObject THEN
  11933. Basic.ToSegmentedName ("BaseTypes.Object",name);
  11934. ELSE
  11935. Basic.ToSegmentedName ("BaseTypes.Record",name);
  11936. END;
  11937. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11938. Symbol(source, tir, 0, 0);
  11939. END;
  11940. Info(source, "base record descriptor");
  11941. Symbol(source, base, 0, 0);
  11942. CooperativeMethodTable(TRUE);
  11943. source.SetReferenced(FALSE);
  11944. IF recordType.hasPointers THEN
  11945. IF ~HasExplicitTraceMethod (recordType) THEN
  11946. implementationVisitor.CreateTraceMethod(recordType);
  11947. END;
  11948. implementationVisitor.CreateResetProcedure(recordType);
  11949. implementationVisitor.CreateAssignProcedure(recordType);
  11950. END;
  11951. ELSIF ~simple THEN
  11952. (*
  11953. MethodEnd = MPO
  11954. ---
  11955. methods (# methods)
  11956. ---
  11957. tags (16)
  11958. ---
  11959. TypeDesc = TypeInfoAdr
  11960. ---
  11961. td adr ---> rec size
  11962. ----
  11963. pointer offsets
  11964. ----
  11965. (padding)
  11966. -----
  11967. empty [2 addresses aligned]
  11968. empty
  11969. empty
  11970. numPtrs
  11971. ---
  11972. pointer offsets
  11973. ---
  11974. *)
  11975. Info(source, "MethodEnd = MPO");
  11976. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),MPO);
  11977. source(IntermediateCode.Section).Emit(Data(Basic.invalidPosition,op));
  11978. MethodTable(TRUE);
  11979. TdTable(TypeTags, TRUE);
  11980. Info(source, "type descriptor info pointer");
  11981. Symbol(source, NewTypeDescriptorInfo(source,source.pc+1,recordType.IsProtected()),0,0);
  11982. IF (cellType # NIL) THEN
  11983. IF cellType.sizeInBits < 0 THEN
  11984. ASSERT(module.system.GenerateVariableOffsets(cellType.cellScope));
  11985. END;
  11986. Info(source, "cell size");
  11987. Address(source, ToMemoryUnits(module.system,cellType.sizeInBits));
  11988. ELSE
  11989. Info(source, "record size");
  11990. Address(source, ToMemoryUnits(module.system,module.system.SizeOf(recordType)));
  11991. END;
  11992. Info(source, "pointer offsets pointer");
  11993. padding := 1- source.pc MOD 2;
  11994. Symbol(source, source, source.pc+1+padding,0);
  11995. IF padding >0 THEN
  11996. Info(source, "padding");
  11997. FOR i := 1 TO padding DO Address(source,0) END;
  11998. END;
  11999. IF cellType # NIL THEN
  12000. PointerArray(source, cellType.cellScope, numberPointers);
  12001. ELSE
  12002. PointerArray(source, recordType.recordScope, numberPointers);
  12003. END;
  12004. ELSE
  12005. (*
  12006. simple:
  12007. td adr --> size
  12008. tag(1)
  12009. tag(2)
  12010. tag(3)
  12011. methods ->
  12012. *)
  12013. Info(source, "record size");
  12014. Address(source, ToMemoryUnits(module.system,module.system.SizeOf(recordType)));
  12015. TdTable(TypeTags, FALSE);
  12016. MethodTable(FALSE);
  12017. source.SetReferenced(FALSE);
  12018. END;
  12019. END NewTypeDescriptor;
  12020. BEGIN
  12021. x := x.resolved;
  12022. IF (x IS SyntaxTree.PointerType) THEN
  12023. x := x(SyntaxTree.PointerType).pointerBase.resolved;
  12024. END;
  12025. IF (x IS SyntaxTree.RecordType) THEN (* enter: insert only if not already inserted *)
  12026. recordType := x(SyntaxTree.RecordType);
  12027. td := x.typeDeclaration;
  12028. IF td = NIL THEN td := recordType.pointerType.resolved.typeDeclaration END; (* for compatibility with paco *)
  12029. ASSERT(td # NIL);
  12030. section := module.allSections.FindBySymbol(td); (* TODO *)
  12031. IF (section = NIL) OR (section(IntermediateCode.Section).pc = 0) THEN
  12032. IF implementationVisitor.newObjectFile THEN
  12033. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  12034. NewTypeDescriptor
  12035. END;
  12036. ELSE
  12037. (* data section in intermediate code *)
  12038. Global.GetSymbolSegmentedName(td,name);
  12039. Basic.SuffixSegmentedName (name, Basic.MakeString("@"));
  12040. Basic.SuffixSegmentedName (name, module.module.name);
  12041. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,td,implementationVisitor.dump # NIL);
  12042. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),0);
  12043. tir.Emit(Data(Basic.invalidPosition,op));
  12044. END;
  12045. END;
  12046. ELSIF (x IS SyntaxTree.CellType) & implementationVisitor.backend.cellsAreObjects THEN
  12047. cellType := x(SyntaxTree.CellType);
  12048. td := x.typeDeclaration;
  12049. section := module.allSections.FindBySymbol(td); (* TODO *)
  12050. IF (section = NIL) OR (section(IntermediateCode.Section).pc = 0) THEN
  12051. IF implementationVisitor.newObjectFile THEN
  12052. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  12053. NewTypeDescriptor
  12054. END;
  12055. END;
  12056. END;
  12057. END
  12058. END CheckTypeDeclaration
  12059. END MetaDataGenerator;
  12060. IntermediateBackend*= OBJECT (IntermediateCode.IntermediateBackend)
  12061. VAR
  12062. trace-: BOOLEAN;
  12063. traceString-: SyntaxTree.IdentifierString;
  12064. traceModuleName-: SyntaxTree.IdentifierString;
  12065. newObjectFile-: BOOLEAN;
  12066. profile-: BOOLEAN;
  12067. noRuntimeChecks: BOOLEAN;
  12068. simpleMetaData-: BOOLEAN;
  12069. noAsserts: BOOLEAN;
  12070. optimize-: BOOLEAN;
  12071. cooperative-: BOOLEAN;
  12072. preregisterStatic-: BOOLEAN;
  12073. dump-: Basic.Writer;
  12074. cellsAreObjects: BOOLEAN;
  12075. preciseGC: BOOLEAN;
  12076. PROCEDURE &InitIntermediateBackend*;
  12077. BEGIN
  12078. simpleMetaData := FALSE;
  12079. newObjectFile := FALSE;
  12080. InitBackend;
  12081. SetRuntimeModuleName(DefaultRuntimeModuleName);
  12082. SetTraceModuleName(DefaultTraceModuleName);
  12083. END InitIntermediateBackend;
  12084. PROCEDURE GenerateIntermediate*(x: SyntaxTree.Module; supportedInstruction: SupportedInstructionProcedure; supportedImmediate: SupportedImmediateProcedure): Sections.Module;
  12085. VAR
  12086. declarationVisitor: DeclarationVisitor;
  12087. implementationVisitor: ImplementationVisitor;
  12088. module: Sections.Module;
  12089. name, platformName: SyntaxTree.IdentifierString;
  12090. meta: MetaDataGenerator;
  12091. BEGIN
  12092. ResetError;
  12093. Global.GetSymbolName(x,name);
  12094. NEW(module,x,system); (* backend structures *)
  12095. Global.GetModuleName(x, name);
  12096. module.SetModuleName(name);
  12097. NEW(implementationVisitor,system,checker,supportedInstruction, supportedImmediate, Compiler.FindPC IN flags, runtimeModuleName, SELF, newObjectFile);
  12098. NEW(declarationVisitor,system,implementationVisitor,SELF,Compiler.ForceModuleBodies IN flags,trace & (Compiler.Info IN flags));
  12099. NEW(meta, implementationVisitor, declarationVisitor,simpleMetaData);
  12100. declarationVisitor.Module(x,module);
  12101. IF newObjectFile & ~meta.simple THEN
  12102. meta.Module(implementationVisitor.moduleBodySection);
  12103. END;
  12104. GetDescription(platformName);
  12105. module.SetPlatformName(platformName);
  12106. RETURN module
  12107. END GenerateIntermediate;
  12108. PROCEDURE SupportedImmediate*(CONST op: IntermediateCode.Operand): BOOLEAN;
  12109. BEGIN RETURN TRUE
  12110. END SupportedImmediate;
  12111. PROCEDURE ProcessSyntaxTreeModule(syntaxTreeModule: SyntaxTree.Module): Formats.GeneratedModule;
  12112. BEGIN RETURN ProcessIntermediateCodeModule(GenerateIntermediate(syntaxTreeModule, SupportedInstruction, SupportedImmediate))
  12113. END ProcessSyntaxTreeModule;
  12114. PROCEDURE ProcessIntermediateCodeModule(intermediateCodeModule: Formats.GeneratedModule): Formats.GeneratedModule;
  12115. VAR
  12116. result: Sections.Module;
  12117. traceName: Basic.MessageString;
  12118. BEGIN
  12119. ASSERT(intermediateCodeModule IS Sections.Module);
  12120. result := intermediateCodeModule(Sections.Module);
  12121. IF trace THEN
  12122. traceName := "intermediate code trace: ";
  12123. Strings.Append(traceName,traceString);
  12124. dump := Basic.GetWriter(Basic.GetDebugWriter(traceName));
  12125. IF (traceString="") OR (traceString="*") THEN
  12126. result.Dump(dump);
  12127. dump.Update
  12128. ELSE
  12129. Sections.DumpFiltered(dump, result, traceString);
  12130. END
  12131. END;
  12132. RETURN result
  12133. END ProcessIntermediateCodeModule;
  12134. PROCEDURE GetDescription*(VAR instructionSet: ARRAY OF CHAR);
  12135. BEGIN instructionSet := "Intermediate";
  12136. END GetDescription;
  12137. PROCEDURE SetNewObjectFile*(newObjectFile: BOOLEAN; simpleMetaData: BOOLEAN);
  12138. BEGIN
  12139. SELF.newObjectFile := newObjectFile;
  12140. SELF.simpleMetaData := simpleMetaData;
  12141. END SetNewObjectFile;
  12142. PROCEDURE SetTraceModuleName(CONST name: ARRAY OF CHAR);
  12143. BEGIN COPY(name, traceModuleName)
  12144. END SetTraceModuleName;
  12145. PROCEDURE DefineOptions(options: Options.Options);
  12146. BEGIN
  12147. DefineOptions^(options);
  12148. options.Add(0X,"trace",Options.String);
  12149. options.Add(0X,"runtime",Options.String);
  12150. options.Add(0X,"newObjectFile",Options.Flag);
  12151. options.Add(0X,"traceModule",Options.String);
  12152. options.Add(0X,"profile",Options.Flag);
  12153. options.Add(0X,"noRuntimeChecks",Options.Flag);
  12154. options.Add(0X,"noAsserts",Options.Flag);
  12155. options.Add(0X,"metaData",Options.String);
  12156. options.Add('o',"optimize", Options.Flag);
  12157. options.Add(0X,"preregisterStatic", Options.Flag);
  12158. options.Add(0X,"cellsAreObjects", Options.Flag);
  12159. options.Add(0X,"preciseGC", Options.Flag);
  12160. END DefineOptions;
  12161. PROCEDURE GetOptions(options: Options.Options);
  12162. VAR name,string: SyntaxTree.IdentifierString;
  12163. BEGIN
  12164. GetOptions^(options);
  12165. trace := options.GetString("trace",traceString);
  12166. profile := options.GetFlag("profile");
  12167. noRuntimeChecks := options.GetFlag("noRuntimeChecks");
  12168. noAsserts := options.GetFlag("noAsserts");
  12169. cooperative := options.GetFlag("cooperative");
  12170. IF options.GetFlag("newObjectFile") THEN newObjectFile := TRUE;
  12171. IF cooperative THEN
  12172. SetRuntimeModuleName("CPU") END
  12173. END;
  12174. IF options.GetString("objectFile",string) & (string = "Minos") THEN
  12175. simpleMetaData := TRUE
  12176. END;
  12177. IF options.GetString("metaData",string) THEN
  12178. IF string = "simple" THEN simpleMetaData := TRUE
  12179. ELSIF string ="full" THEN simpleMetaData := FALSE
  12180. END;
  12181. END;
  12182. IF options.GetString("runtime",name) THEN SetRuntimeModuleName(name) END;
  12183. IF options.GetString("traceModule",name) THEN SetTraceModuleName(name) END;
  12184. optimize := options.GetFlag("optimize");
  12185. preregisterStatic := options.GetFlag("preregisterStatic");
  12186. cellsAreObjects := options.GetFlag("cellsAreObjects");
  12187. preciseGC := options.GetFlag("preciseGC");
  12188. END GetOptions;
  12189. PROCEDURE DefaultSymbolFileFormat(): Formats.SymbolFileFormat;
  12190. BEGIN RETURN SymbolFileFormat.Get()
  12191. END DefaultSymbolFileFormat;
  12192. END IntermediateBackend;
  12193. (* ----------------------------------- register allocation ------------------------------------- *)
  12194. (* register mapping scheme
  12195. virtual register number --> register mapping = part(0) --> ticket <--> physical register
  12196. spill offset
  12197. part(n) --> ticket <--> physical register
  12198. spill offset
  12199. *)
  12200. VAR int8-, int16-, int32-, int64-, uint8-, uint16-, uint32-, uint64-, float32-, float64-: IntermediateCode.Type;
  12201. emptyOperand: IntermediateCode.Operand;
  12202. systemCalls: ARRAY NumberSystemCalls OF SyntaxTree.Symbol;
  12203. statCoopResetVariables: LONGINT;
  12204. statCoopModifyAssignments: LONGINT;
  12205. modifyAssignmentsPC : LONGINT;
  12206. statCoopNilCheck: LONGINT;
  12207. statCoopSwitch: LONGINT;
  12208. statCoopAssignProcedure: LONGINT;
  12209. statCoopTraceMethod: LONGINT;
  12210. statCoopResetProcedure: LONGINT;
  12211. statCoopTraceModule: LONGINT;
  12212. PROCEDURE ResetStatistics*;
  12213. BEGIN
  12214. statCoopResetVariables := 0;
  12215. statCoopModifyAssignments := 0;
  12216. statCoopNilCheck:= 0;
  12217. statCoopSwitch:= 0;
  12218. statCoopAssignProcedure:= 0;
  12219. statCoopTraceMethod:= 0;
  12220. statCoopResetProcedure:= 0;
  12221. statCoopTraceModule:= 0;
  12222. END ResetStatistics;
  12223. PROCEDURE Statistics*;
  12224. BEGIN
  12225. TRACE(statCoopResetVariables, statCoopModifyAssignments);
  12226. TRACE(statCoopNilCheck, statCoopSwitch);
  12227. TRACE(statCoopAssignProcedure,
  12228. statCoopTraceMethod,
  12229. statCoopResetProcedure,
  12230. statCoopTraceModule)
  12231. END Statistics;
  12232. PROCEDURE GCD(a,b: LONGINT): LONGINT;
  12233. VAR h: LONGINT;
  12234. BEGIN
  12235. WHILE b # 0 DO
  12236. h := a MOD b;
  12237. a := b;
  12238. b := h;
  12239. END;
  12240. RETURN a
  12241. END GCD;
  12242. PROCEDURE SCM(a,b: LONGINT): LONGINT;
  12243. BEGIN
  12244. RETURN a*b DIV GCD(a,b)
  12245. END SCM;
  12246. PROCEDURE CommonAlignment(a,b: LONGINT): LONGINT;
  12247. BEGIN
  12248. (*TRACE(a,b);*)
  12249. IF a = 0 THEN RETURN b
  12250. ELSIF b = 0 THEN RETURN a
  12251. ELSE RETURN SCM(a,b)
  12252. END;
  12253. END CommonAlignment;
  12254. PROCEDURE PassBySingleReference(parameter: SyntaxTree.Parameter): BOOLEAN;
  12255. BEGIN
  12256. IF parameter.kind = SyntaxTree.ValueParameter THEN RETURN FALSE
  12257. ELSIF parameter.kind = SyntaxTree.ConstParameter THEN
  12258. RETURN (parameter.type.resolved IS SyntaxTree.RecordType) OR (parameter.type.resolved IS SyntaxTree.ArrayType) & SysvABIorWINAPI(parameter.ownerType(SyntaxTree.ProcedureType).callingConvention)
  12259. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  12260. RETURN ~(parameter.type.resolved IS SyntaxTree.ArrayType) & ~(parameter.type.resolved IS SyntaxTree.MathArrayType) OR (parameter.type.resolved IS SyntaxTree.ArrayType) & SysvABIorWINAPI(parameter.ownerType(SyntaxTree.ProcedureType).callingConvention)
  12261. END
  12262. END PassBySingleReference;
  12263. PROCEDURE PassInRegister(parameter: SyntaxTree.Parameter): BOOLEAN;
  12264. BEGIN
  12265. RETURN ~parameter.type.IsComposite() OR PassBySingleReference(parameter)
  12266. END PassInRegister;
  12267. PROCEDURE AddRegisterEntry(VAR queue: RegisterEntry; register: LONGINT; class: IntermediateCode.RegisterClass; type: IntermediateCode.Type);
  12268. VAR new: RegisterEntry;
  12269. BEGIN
  12270. NEW(new); new.register := register; new.registerClass := class; new.type := type; new.next := NIL; new.prev := NIL;
  12271. IF queue = NIL THEN
  12272. queue := new
  12273. ELSE
  12274. new.next := queue;
  12275. IF queue#NIL THEN queue.prev := new END;
  12276. queue := new
  12277. END;
  12278. END AddRegisterEntry;
  12279. PROCEDURE RemoveRegisterEntry(VAR queue: RegisterEntry; register: LONGINT): BOOLEAN;
  12280. VAR this: RegisterEntry;
  12281. BEGIN
  12282. this := queue;
  12283. WHILE (this # NIL) & (this.register # register) DO
  12284. this := this.next;
  12285. END;
  12286. IF this = NIL THEN
  12287. RETURN FALSE
  12288. END;
  12289. ASSERT(this # NIL);
  12290. IF this = queue THEN queue := queue.next END;
  12291. IF this.prev # NIL THEN this.prev.next := this.next END;
  12292. IF this.next # NIL THEN this.next.prev := this.prev END;
  12293. RETURN TRUE
  12294. END RemoveRegisterEntry;
  12295. PROCEDURE Assert(cond: BOOLEAN; CONST reason: ARRAY OF CHAR);
  12296. BEGIN ASSERT(cond);
  12297. END Assert;
  12298. PROCEDURE ReusableRegister(op: IntermediateCode.Operand): BOOLEAN;
  12299. BEGIN
  12300. RETURN (op.mode = IntermediateCode.ModeRegister) & (op.register > 0) & (op.offset = 0);
  12301. END ReusableRegister;
  12302. PROCEDURE EnsureBodyProcedure(moduleScope: SyntaxTree.ModuleScope);
  12303. VAR procedure: SyntaxTree.Procedure; procedureScope: SyntaxTree.ProcedureScope;
  12304. BEGIN
  12305. procedure := moduleScope.bodyProcedure;
  12306. IF procedure = NIL THEN (* artificially add body procedure if not existing. Really needed? *)
  12307. procedureScope := SyntaxTree.NewProcedureScope(moduleScope);
  12308. procedure := SyntaxTree.NewProcedure(Basic.invalidPosition,Global.ModuleBodyName, procedureScope);
  12309. procedure.SetScope(moduleScope);
  12310. procedure.SetType(SyntaxTree.NewProcedureType(Basic.invalidPosition,moduleScope));
  12311. procedure.SetAccess(SyntaxTree.Hidden);
  12312. moduleScope.SetBodyProcedure(procedure);
  12313. moduleScope.AddProcedure(procedure);
  12314. procedureScope.SetBody(SyntaxTree.NewBody(Basic.invalidPosition,procedureScope)); (* empty body *)
  12315. END;
  12316. END EnsureBodyProcedure;
  12317. PROCEDURE GetSymbol*(scope: SyntaxTree.ModuleScope; CONST moduleName, symbolName: ARRAY OF CHAR): SyntaxTree.Symbol;
  12318. VAR import: SyntaxTree.Import;
  12319. selfName: SyntaxTree.IdentifierString;
  12320. module: SyntaxTree.Module;
  12321. BEGIN
  12322. scope.ownerModule.GetName(selfName);
  12323. IF (moduleName = selfName) & (scope.ownerModule.context = Global.A2Name) THEN
  12324. module := scope.ownerModule
  12325. ELSE
  12326. import := scope.ImportByModuleName(SyntaxTree.NewIdentifier(moduleName),SyntaxTree.NewIdentifier("A2"));
  12327. IF import = NIL THEN
  12328. RETURN NIL
  12329. ELSIF import.module = NIL THEN
  12330. RETURN NIL
  12331. ELSE module := import.module
  12332. END;
  12333. END;
  12334. RETURN module.moduleScope.FindSymbol(SyntaxTree.NewIdentifier(symbolName));
  12335. END GetSymbol;
  12336. PROCEDURE InitOperand(VAR op: Operand; mode: SHORTINT);
  12337. BEGIN
  12338. op.mode := mode;
  12339. IntermediateCode.InitOperand(op.op);
  12340. IntermediateCode.InitOperand(op.tag);
  12341. IntermediateCode.InitOperand(op.extra);
  12342. op.dimOffset := 0;
  12343. END InitOperand;
  12344. (* TODO: remove this, and redirect calls to 'IntermediateCode.GetType' directly *)
  12345. PROCEDURE GetType*(system: Global.System; type: SyntaxTree.Type): IntermediateCode.Type;
  12346. BEGIN RETURN IntermediateCode.GetType(system, type)
  12347. END GetType;
  12348. PROCEDURE BuildConstant(module: SyntaxTree.Module; value: SyntaxTree.Value; VAR adr: LONGINT): SyntaxTree.Constant;
  12349. VAR name: SyntaxTree.IdentifierString; constant: SyntaxTree.Constant;
  12350. BEGIN
  12351. name := "@const"; Basic.AppendNumber(name, adr); INC(adr);
  12352. (*
  12353. UniqueId(name,module,name,adr);
  12354. *)
  12355. constant := SyntaxTree.NewConstant(Basic.invalidPosition,SyntaxTree.NewIdentifier(name));
  12356. constant.SetValue(value);
  12357. constant.SetAccess(SyntaxTree.Hidden);
  12358. module.moduleScope.AddConstant(constant);
  12359. constant.SetScope(module.moduleScope);
  12360. RETURN constant
  12361. END BuildConstant;
  12362. PROCEDURE HasPointers (scope: SyntaxTree.ProcedureScope): BOOLEAN;
  12363. VAR variable: SyntaxTree.Variable; parameter: SyntaxTree.Parameter;
  12364. BEGIN
  12365. variable := scope.firstVariable;
  12366. WHILE variable # NIL DO
  12367. IF variable.NeedsTrace() THEN
  12368. RETURN TRUE;
  12369. END;
  12370. variable := variable.nextVariable;
  12371. END;
  12372. parameter := scope.ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  12373. WHILE parameter # NIL DO
  12374. IF parameter.NeedsTrace() & ~IsVariableParameter(parameter) THEN
  12375. RETURN TRUE;
  12376. END;
  12377. parameter := parameter.nextParameter;
  12378. END;
  12379. RETURN FALSE;
  12380. END HasPointers;
  12381. PROCEDURE IsVariableParameter (parameter: SyntaxTree.Parameter): BOOLEAN;
  12382. BEGIN RETURN (parameter.kind = SyntaxTree.VarParameter) OR (parameter.kind = SyntaxTree.ConstParameter) & ((parameter.type.resolved IS SyntaxTree.RecordType) OR (parameter.type.resolved IS SyntaxTree.ArrayType));
  12383. END IsVariableParameter;
  12384. PROCEDURE HasVariableParameters(scope: SyntaxTree.ProcedureScope): BOOLEAN;
  12385. VAR parameter: SyntaxTree.Parameter;
  12386. BEGIN
  12387. parameter := scope.ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  12388. WHILE parameter # NIL DO
  12389. IF IsVariableParameter (parameter) THEN RETURN TRUE END;
  12390. IF parameter.movable THEN RETURN TRUE END;
  12391. parameter := parameter.nextParameter;
  12392. END;
  12393. RETURN scope.ownerProcedure.type(SyntaxTree.ProcedureType).returnParameter # NIL;
  12394. END HasVariableParameters;
  12395. PROCEDURE HasExplicitTraceMethod(recordType: SyntaxTree.RecordType): BOOLEAN;
  12396. BEGIN
  12397. IF (recordType.pointerType # NIL) & ~recordType.pointerType.isPlain THEN RETURN FALSE END;
  12398. RETURN (recordType.recordScope.firstProcedure # NIL) & Basic.StringEqual (Basic.MakeString ("Trace"), recordType.recordScope.firstProcedure.name);
  12399. END HasExplicitTraceMethod;
  12400. PROCEDURE IsIntegerConstant(expression: SyntaxTree.Expression; VAR val: HUGEINT): BOOLEAN;
  12401. BEGIN
  12402. IF expression.resolved # NIL THEN expression := expression.resolved END;
  12403. IF (expression IS SyntaxTree.IntegerValue) THEN
  12404. val := expression(SyntaxTree.IntegerValue).value;
  12405. RETURN TRUE
  12406. ELSE
  12407. RETURN FALSE
  12408. END;
  12409. END IsIntegerConstant;
  12410. PROCEDURE PowerOf2*(val: HUGEINT; VAR exp: LONGINT): BOOLEAN;
  12411. BEGIN
  12412. IF val <= 0 THEN RETURN FALSE END;
  12413. exp := 0;
  12414. WHILE ~ODD(val) DO
  12415. val := val DIV 2;
  12416. INC(exp)
  12417. END;
  12418. RETURN val = 1
  12419. END PowerOf2;
  12420. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  12421. VAR procedure: SyntaxTree.Procedure;
  12422. BEGIN
  12423. procedure := record.recordScope.constructor;
  12424. IF procedure = NIL THEN
  12425. record := record.GetBaseRecord();
  12426. IF record # NIL THEN
  12427. procedure := GetConstructor(record)
  12428. END;
  12429. END;
  12430. RETURN procedure;
  12431. END GetConstructor;
  12432. PROCEDURE IsIntegerImmediate(CONST op: IntermediateCode.Operand; VAR value: LONGINT): BOOLEAN;
  12433. BEGIN
  12434. value := SHORT(op.intValue);
  12435. RETURN op.mode = IntermediateCode.ModeImmediate;
  12436. END IsIntegerImmediate;
  12437. (** whether a type strictily is a pointer to record or object type
  12438. (however, the basic type <<OBJECT>> is explicitly excluded) **)
  12439. PROCEDURE IsStrictlyPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  12440. BEGIN
  12441. IF type = NIL THEN
  12442. RETURN FALSE
  12443. ELSIF type.resolved IS SyntaxTree.PointerType THEN
  12444. RETURN type.resolved(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType
  12445. ELSE
  12446. RETURN FALSE
  12447. END
  12448. END IsStrictlyPointerToRecord;
  12449. PROCEDURE IsUnsafePointer(type: SyntaxTree.Type): BOOLEAN;
  12450. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & type.resolved(SyntaxTree.PointerType).isUnsafe
  12451. END IsUnsafePointer;
  12452. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  12453. BEGIN type := type.resolved;
  12454. IF type IS SyntaxTree.PointerType THEN
  12455. type := type(SyntaxTree.PointerType).pointerBase;
  12456. type := type.resolved;
  12457. IF type IS SyntaxTree.RecordType THEN
  12458. recordType := type(SyntaxTree.RecordType);
  12459. RETURN TRUE
  12460. ELSE
  12461. RETURN FALSE
  12462. END
  12463. ELSIF type IS SyntaxTree.RecordType THEN
  12464. recordType := type(SyntaxTree.RecordType);
  12465. RETURN type(SyntaxTree.RecordType).pointerType # NIL
  12466. ELSIF type IS SyntaxTree.ObjectType THEN
  12467. RETURN TRUE
  12468. ELSIF type IS SyntaxTree.AnyType THEN
  12469. RETURN TRUE (*! potentially is a pointer to record, treat it this way?? *)
  12470. ELSE
  12471. RETURN FALSE
  12472. END;
  12473. END IsPointerToRecord;
  12474. PROCEDURE IsArrayOfSystemByte(type: SyntaxTree.Type): BOOLEAN;
  12475. BEGIN
  12476. type := type.resolved;
  12477. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open)
  12478. & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType);
  12479. END IsArrayOfSystemByte;
  12480. PROCEDURE IsOpenArray(type: SyntaxTree.Type): BOOLEAN;
  12481. BEGIN
  12482. IF type = NIL THEN RETURN FALSE END;
  12483. type := type.resolved;
  12484. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open);
  12485. END IsOpenArray;
  12486. PROCEDURE IsSemiDynamicArray(type: SyntaxTree.Type): BOOLEAN;
  12487. BEGIN
  12488. IF type = NIL THEN RETURN FALSE END;
  12489. type := type.resolved;
  12490. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic);
  12491. END IsSemiDynamicArray;
  12492. PROCEDURE IsStaticArray(type: SyntaxTree.Type): BOOLEAN;
  12493. BEGIN
  12494. IF type = NIL THEN RETURN FALSE END;
  12495. type := type.resolved;
  12496. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static);
  12497. END IsStaticArray;
  12498. PROCEDURE StaticArrayNumElements(type: SyntaxTree.Type): LONGINT;
  12499. VAR size: LONGINT;
  12500. BEGIN
  12501. size := 1;
  12502. WHILE (IsStaticArray(type)) DO
  12503. size := size * type.resolved(SyntaxTree.ArrayType).staticLength;
  12504. type := type.resolved(SyntaxTree.ArrayType).arrayBase;
  12505. END;
  12506. RETURN size;
  12507. END StaticArrayNumElements;
  12508. PROCEDURE StaticArrayBaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  12509. BEGIN
  12510. WHILE (IsStaticArray(type)) DO
  12511. type := type.resolved(SyntaxTree.ArrayType).arrayBase;
  12512. END;
  12513. RETURN type;
  12514. END StaticArrayBaseType;
  12515. PROCEDURE ArrayBaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  12516. BEGIN
  12517. WHILE (type.resolved IS SyntaxTree.ArrayType) DO
  12518. type := type.resolved(SyntaxTree.ArrayType).arrayBase;
  12519. END;
  12520. RETURN type;
  12521. END ArrayBaseType;
  12522. PROCEDURE IsDelegate(type: SyntaxTree.Type): BOOLEAN;
  12523. BEGIN
  12524. IF type = NIL THEN RETURN FALSE END;
  12525. type := type.resolved;
  12526. RETURN (type IS SyntaxTree.ProcedureType) & (type(SyntaxTree.ProcedureType).isDelegate)
  12527. END IsDelegate;
  12528. PROCEDURE DynamicDim(type:SyntaxTree.Type): LONGINT;
  12529. VAR i: LONGINT;
  12530. BEGIN
  12531. i := 0; type := type.resolved;
  12532. WHILE(type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form # SyntaxTree.Static) DO
  12533. INC(i);
  12534. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  12535. END;
  12536. WHILE(type # NIL) & (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) DO
  12537. INC(i);
  12538. type := type(SyntaxTree.MathArrayType).arrayBase;
  12539. IF type # NIL THEN type := type.resolved END;
  12540. END;
  12541. RETURN i
  12542. END DynamicDim;
  12543. PROCEDURE StaticSize(system: Global.System; type: SyntaxTree.Type): LONGINT;
  12544. BEGIN
  12545. WHILE (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form # SyntaxTree.Static) DO
  12546. type := type(SyntaxTree.ArrayType).arrayBase;
  12547. END;
  12548. WHILE (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) DO
  12549. type := type(SyntaxTree.MathArrayType).arrayBase;
  12550. END;
  12551. RETURN ToMemoryUnits(system,system.AlignedSizeOf(type));
  12552. END StaticSize;
  12553. PROCEDURE IsImmediate(x: IntermediateCode.Operand): BOOLEAN;
  12554. BEGIN
  12555. RETURN (x.mode = IntermediateCode.ModeImmediate) & (x.symbol.name = "");
  12556. END IsImmediate;
  12557. PROCEDURE IsAddress(x: IntermediateCode.Operand): BOOLEAN;
  12558. BEGIN
  12559. RETURN (x.mode = IntermediateCode.ModeImmediate) & (x.symbol.name # "")
  12560. END IsAddress;
  12561. PROCEDURE IsRegister(x: IntermediateCode.Operand): BOOLEAN;
  12562. BEGIN
  12563. RETURN (x.mode = IntermediateCode.ModeRegister);
  12564. END IsRegister;
  12565. PROCEDURE GetRecordTypeName(recordType: SyntaxTree.RecordType; VAR name: Basic.SegmentedName);
  12566. VAR typeDeclaration: SyntaxTree.TypeDeclaration;
  12567. BEGIN
  12568. typeDeclaration := recordType.typeDeclaration;
  12569. IF typeDeclaration = NIL THEN typeDeclaration := recordType.pointerType.resolved.typeDeclaration END; (* for compatibility with paco *)
  12570. Global.GetSymbolSegmentedName(typeDeclaration,name);
  12571. END GetRecordTypeName;
  12572. PROCEDURE ParametersSize(system: Global.System; procedureType: SyntaxTree.ProcedureType; isNested: BOOLEAN): LONGINT;
  12573. VAR parSize: LONGINT; parameter: SyntaxTree.Parameter;
  12574. BEGIN
  12575. parSize := 0;
  12576. IF SemanticChecker.StructuredReturnType(procedureType) THEN
  12577. parameter := procedureType.returnParameter;
  12578. INC(parSize,system.SizeOfParameter(parameter));
  12579. parSize := parSize + (-parSize) MOD system.addressSize;
  12580. END;
  12581. parameter :=procedureType.lastParameter;
  12582. WHILE (parameter # NIL) DO
  12583. INC(parSize,system.SizeOfParameter(parameter));
  12584. parSize := parSize + (-parSize) MOD system.addressSize;
  12585. parameter := parameter.prevParameter;
  12586. END;
  12587. IF procedureType.selfParameter # NIL THEN
  12588. parameter := procedureType.selfParameter;
  12589. INC(parSize,system.SizeOfParameter(parameter));
  12590. parSize := parSize + (-parSize) MOD system.addressSize;
  12591. ELSIF procedureType.isDelegate THEN INC(parSize,system.addressSize)
  12592. END; (* method => self pointer *)
  12593. IF isNested THEN INC(parSize,system.addressSize) END; (* nested procedure => static base *)
  12594. RETURN ToMemoryUnits(system,parSize)
  12595. END ParametersSize;
  12596. PROCEDURE IsNested(procedure: SyntaxTree.Procedure): BOOLEAN;
  12597. BEGIN
  12598. RETURN (procedure.scope IS SyntaxTree.ProcedureScope) & (procedure.externalName = NIL);
  12599. END IsNested;
  12600. PROCEDURE InCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  12601. BEGIN
  12602. WHILE (scope # NIL) & ~(scope IS SyntaxTree.CellScope) DO
  12603. scope := scope.outerScope;
  12604. END;
  12605. RETURN scope # NIL;
  12606. END InCellScope;
  12607. PROCEDURE ProcedureParametersSize*(system: Global.System; procedure: SyntaxTree.Procedure): LONGINT;
  12608. BEGIN
  12609. (*IF (procedure.scope IS SyntaxTree.CellScope) & (procedure = procedure.scope(SyntaxTree.CellScope).constructor) & ~backend.cellsAreObjects THEN
  12610. RETURN 0
  12611. ELSE
  12612. *)
  12613. RETURN ParametersSize(system,procedure.type(SyntaxTree.ProcedureType),IsNested(procedure));
  12614. (*END;*)
  12615. END ProcedureParametersSize;
  12616. PROCEDURE ToMemoryUnits*(system: Global.System; size: LONGINT): LONGINT;
  12617. VAR dataUnit: LONGINT;
  12618. BEGIN dataUnit := system.dataUnit;
  12619. ASSERT(size MOD system.dataUnit = 0);
  12620. RETURN size DIV system.dataUnit
  12621. END ToMemoryUnits;
  12622. PROCEDURE Get*(): Backend.Backend;
  12623. VAR backend: IntermediateBackend;
  12624. BEGIN NEW(backend); RETURN backend
  12625. END Get;
  12626. PROCEDURE Nop(position: Basic.Position):IntermediateCode.Instruction;
  12627. VAR instruction: IntermediateCode.Instruction;
  12628. BEGIN
  12629. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.nop,emptyOperand,emptyOperand,emptyOperand);
  12630. RETURN instruction
  12631. END Nop;
  12632. PROCEDURE Mov(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12633. VAR instruction: IntermediateCode.Instruction;
  12634. BEGIN
  12635. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.mov,dest,src,emptyOperand);
  12636. RETURN instruction
  12637. END Mov;
  12638. (* like Mov but ensures that no new register will be allocated for dest *)
  12639. PROCEDURE MovReplace(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12640. VAR instruction: IntermediateCode.Instruction;
  12641. BEGIN
  12642. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.mov,dest,src,dest);
  12643. RETURN instruction
  12644. END MovReplace;
  12645. PROCEDURE Conv(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12646. VAR instruction: IntermediateCode.Instruction;
  12647. BEGIN
  12648. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.conv,dest,src,emptyOperand);
  12649. RETURN instruction
  12650. END Conv;
  12651. PROCEDURE SysvABI( cc: LONGINT ): BOOLEAN;
  12652. BEGIN
  12653. RETURN (cc IN {SyntaxTree.CCallingConvention, SyntaxTree.DarwinCCallingConvention})
  12654. END SysvABI;
  12655. PROCEDURE SysvABIorWINAPI( cc: LONGINT ): BOOLEAN;
  12656. BEGIN
  12657. RETURN (cc IN {SyntaxTree.CCallingConvention, SyntaxTree.DarwinCCallingConvention, SyntaxTree.WinAPICallingConvention})
  12658. END SysvABIorWINAPI;
  12659. PROCEDURE Call*(position: Basic.Position;op: IntermediateCode.Operand; parSize: LONGINT): IntermediateCode.Instruction;
  12660. VAR instruction: IntermediateCode.Instruction;
  12661. BEGIN
  12662. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.call,op,IntermediateCode.Number(parSize),emptyOperand);
  12663. RETURN instruction
  12664. END Call;
  12665. PROCEDURE Exit(position: Basic.Position;pcOffset: LONGINT; callingConvention, unwind: LONGINT): IntermediateCode.Instruction;
  12666. VAR op1, op2, op3: IntermediateCode.Operand;
  12667. VAR instruction: IntermediateCode.Instruction;
  12668. BEGIN
  12669. IntermediateCode.InitNumber(op1,pcOffset);
  12670. IntermediateCode.InitNumber(op2,callingConvention);
  12671. IntermediateCode.InitNumber(op3,unwind);
  12672. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.exit,op1,op2,op3);
  12673. RETURN instruction
  12674. END Exit;
  12675. PROCEDURE Return(position: Basic.Position;res: IntermediateCode.Operand): IntermediateCode.Instruction;
  12676. VAR instruction: IntermediateCode.Instruction;
  12677. BEGIN
  12678. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.return,res,emptyOperand,emptyOperand);
  12679. RETURN instruction
  12680. END Return;
  12681. PROCEDURE Result*(position: Basic.Position;res: IntermediateCode.Operand): IntermediateCode.Instruction;
  12682. VAR instruction: IntermediateCode.Instruction;
  12683. BEGIN
  12684. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.result,res,emptyOperand,emptyOperand);
  12685. RETURN instruction
  12686. END Result;
  12687. PROCEDURE Trap(position: Basic.Position;nr: LONGINT): IntermediateCode.Instruction;
  12688. VAR op1: IntermediateCode.Operand;
  12689. VAR instruction: IntermediateCode.Instruction;
  12690. BEGIN
  12691. IntermediateCode.InitNumber(op1,nr);
  12692. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.trap,op1,emptyOperand,emptyOperand);
  12693. RETURN instruction
  12694. END Trap;
  12695. PROCEDURE Br(position: Basic.Position;dest: IntermediateCode.Operand): IntermediateCode.Instruction;
  12696. VAR instruction: IntermediateCode.Instruction;
  12697. BEGIN
  12698. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.br,dest,emptyOperand,emptyOperand);
  12699. RETURN instruction
  12700. END Br;
  12701. PROCEDURE Breq(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12702. VAR instruction: IntermediateCode.Instruction;
  12703. BEGIN
  12704. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.breq,dest,left,right);
  12705. RETURN instruction
  12706. END Breq;
  12707. PROCEDURE Brne(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12708. VAR instruction: IntermediateCode.Instruction;
  12709. BEGIN
  12710. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.brne,dest,left,right);
  12711. RETURN instruction
  12712. END Brne;
  12713. PROCEDURE Brge(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12714. VAR instruction: IntermediateCode.Instruction;
  12715. BEGIN
  12716. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.brge,dest,left,right);
  12717. RETURN instruction
  12718. END Brge;
  12719. PROCEDURE Brlt(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12720. VAR instruction: IntermediateCode.Instruction;
  12721. BEGIN
  12722. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.brlt,dest,left,right);
  12723. RETURN instruction
  12724. END Brlt;
  12725. PROCEDURE Pop*(position: Basic.Position;op:IntermediateCode.Operand): IntermediateCode.Instruction;
  12726. VAR instruction: IntermediateCode.Instruction;
  12727. BEGIN
  12728. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.pop,op,emptyOperand,emptyOperand);
  12729. RETURN instruction
  12730. END Pop;
  12731. PROCEDURE Push*(position: Basic.Position;op: IntermediateCode.Operand): IntermediateCode.Instruction;
  12732. VAR instruction: IntermediateCode.Instruction;
  12733. BEGIN
  12734. ASSERT(op.mode # IntermediateCode.Undefined);
  12735. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.push,op,emptyOperand,emptyOperand);
  12736. RETURN instruction
  12737. END Push;
  12738. PROCEDURE Neg(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12739. VAR instruction: IntermediateCode.Instruction;
  12740. BEGIN
  12741. IntermediateCode.InitInstruction(instruction,position, IntermediateCode.neg,dest,src,emptyOperand);
  12742. RETURN instruction
  12743. END Neg;
  12744. PROCEDURE Not(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12745. VAR instruction: IntermediateCode.Instruction;
  12746. BEGIN
  12747. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.not,dest,src,emptyOperand);
  12748. RETURN instruction
  12749. END Not;
  12750. PROCEDURE Abs(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12751. VAR instruction: IntermediateCode.Instruction;
  12752. BEGIN
  12753. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.abs,dest,src,emptyOperand);
  12754. RETURN instruction
  12755. END Abs;
  12756. PROCEDURE Mul(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12757. VAR instruction: IntermediateCode.Instruction;
  12758. BEGIN
  12759. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.mul,dest,left,right);
  12760. ASSERT(~IsImmediate(instruction.op1));
  12761. RETURN instruction
  12762. END Mul;
  12763. PROCEDURE Div(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12764. VAR instruction: IntermediateCode.Instruction;
  12765. BEGIN
  12766. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.div,dest,left,right);
  12767. RETURN instruction
  12768. END Div;
  12769. PROCEDURE Mod(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12770. VAR instruction: IntermediateCode.Instruction;
  12771. BEGIN
  12772. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.mod,dest,left,right);
  12773. RETURN instruction
  12774. END Mod;
  12775. PROCEDURE Sub(position: Basic.Position;dest: IntermediateCode.Operand; left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12776. VAR instruction: IntermediateCode.Instruction;
  12777. BEGIN
  12778. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.sub,dest,left,right);
  12779. RETURN instruction
  12780. END Sub;
  12781. PROCEDURE Add(position: Basic.Position;dest: IntermediateCode.Operand; left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12782. VAR instruction: IntermediateCode.Instruction;
  12783. BEGIN
  12784. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.add,dest,left,right);
  12785. RETURN instruction
  12786. END Add;
  12787. PROCEDURE And(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12788. VAR instruction: IntermediateCode.Instruction;
  12789. BEGIN
  12790. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.and,dest,left,right);
  12791. RETURN instruction
  12792. END And;
  12793. PROCEDURE Or(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12794. VAR instruction: IntermediateCode.Instruction;
  12795. BEGIN
  12796. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.or,dest,left,right);
  12797. RETURN instruction
  12798. END Or;
  12799. PROCEDURE Xor(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12800. VAR instruction: IntermediateCode.Instruction;
  12801. BEGIN
  12802. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.xor,dest,left,right);
  12803. RETURN instruction
  12804. END Xor;
  12805. PROCEDURE Shl(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12806. VAR instruction: IntermediateCode.Instruction;
  12807. BEGIN
  12808. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.shl,dest,left, IntermediateCode.ToUnsigned(right));
  12809. RETURN instruction
  12810. END Shl;
  12811. PROCEDURE Shr(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12812. VAR instruction: IntermediateCode.Instruction;
  12813. BEGIN
  12814. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.shr,dest,left, IntermediateCode.ToUnsigned(right));
  12815. RETURN instruction
  12816. END Shr;
  12817. PROCEDURE Rol(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12818. VAR instruction: IntermediateCode.Instruction;
  12819. BEGIN
  12820. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.rol,dest,left, IntermediateCode.ToUnsigned(right));
  12821. RETURN instruction
  12822. END Rol;
  12823. PROCEDURE Ror(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12824. VAR instruction: IntermediateCode.Instruction;
  12825. BEGIN
  12826. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.ror,dest,left, IntermediateCode.ToUnsigned(right));
  12827. RETURN instruction
  12828. END Ror;
  12829. PROCEDURE Cas(position: Basic.Position;dest,src,size: IntermediateCode.Operand): IntermediateCode.Instruction;
  12830. VAR instruction: IntermediateCode.Instruction;
  12831. BEGIN
  12832. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.cas,dest,src,size);
  12833. RETURN instruction
  12834. END Cas;
  12835. PROCEDURE Copy(position: Basic.Position;dest,src,size: IntermediateCode.Operand): IntermediateCode.Instruction;
  12836. VAR instruction: IntermediateCode.Instruction;
  12837. BEGIN
  12838. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.copy,dest,src,size);
  12839. RETURN instruction
  12840. END Copy;
  12841. PROCEDURE Fill(position: Basic.Position;dest,size, value: IntermediateCode.Operand): IntermediateCode.Instruction;
  12842. VAR instruction: IntermediateCode.Instruction;
  12843. BEGIN
  12844. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.fill,dest,size,value);
  12845. RETURN instruction
  12846. END Fill;
  12847. PROCEDURE Asm(position: Basic.Position;s: SyntaxTree.SourceCode; inRules, outRules: IntermediateCode.Rules): IntermediateCode.Instruction;
  12848. VAR instruction: IntermediateCode.Instruction; string, o1, o2: IntermediateCode.Operand;
  12849. BEGIN
  12850. string := IntermediateCode.String(s);
  12851. (*IntermediateCode.SetIntValue(string,position); (* for error reporting *)*)
  12852. IF inRules # NIL THEN IntermediateCode.InitRule(o1, inRules) ELSE o1 := emptyOperand END;
  12853. IF outRules # NIL THEN IntermediateCode.InitRule(o2, outRules) ELSE o2 := emptyOperand END;
  12854. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.asm,string,o1,o2);
  12855. RETURN instruction
  12856. END Asm;
  12857. PROCEDURE Data*(position: Basic.Position;op: IntermediateCode.Operand): IntermediateCode.Instruction;
  12858. VAR instruction: IntermediateCode.Instruction;
  12859. BEGIN
  12860. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.data,op,emptyOperand,emptyOperand);
  12861. RETURN instruction
  12862. END Data;
  12863. PROCEDURE SpecialInstruction(position: Basic.Position;subtype: SHORTINT; op1,op2,op3: IntermediateCode.Operand): IntermediateCode.Instruction;
  12864. VAR instruction: IntermediateCode.Instruction;
  12865. BEGIN
  12866. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.special,op1,op2,op3);
  12867. IntermediateCode.SetSubType(instruction, subtype);
  12868. RETURN instruction
  12869. END SpecialInstruction;
  12870. PROCEDURE Reserve(position: Basic.Position;units: LONGINT): IntermediateCode.Instruction;
  12871. VAR op1: IntermediateCode.Operand;
  12872. VAR instruction: IntermediateCode.Instruction;
  12873. BEGIN
  12874. (*! generate a warning if size exceeds a certain limit *)
  12875. (*
  12876. ASSERT(bytes < 1000000); (* sanity check *)
  12877. *)
  12878. ASSERT(0 <= units); (* sanity check *)
  12879. IntermediateCode.InitNumber(op1,units);
  12880. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.reserve,op1,emptyOperand,emptyOperand);
  12881. RETURN instruction
  12882. END Reserve;
  12883. PROCEDURE LabelInstruction(position: Basic.Position): IntermediateCode.Instruction;
  12884. VAR op1: IntermediateCode.Operand;
  12885. VAR instruction: IntermediateCode.Instruction;
  12886. BEGIN
  12887. IntermediateCode.InitNumber(op1,position.start);
  12888. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.label,op1,emptyOperand,emptyOperand);
  12889. RETURN instruction
  12890. END LabelInstruction;
  12891. PROCEDURE EnterImmediate*(data: IntermediateCode.Section; CONST vop: IntermediateCode.Operand): LONGINT;
  12892. VAR pc: LONGINT;
  12893. PROCEDURE ProvidesValue(CONST instr: IntermediateCode.Instruction; op: IntermediateCode.Operand): BOOLEAN;
  12894. BEGIN
  12895. IF instr.opcode # IntermediateCode.data THEN RETURN FALSE END;
  12896. ASSERT(instr.op1.mode = IntermediateCode.ModeImmediate);
  12897. IF instr.op1.type.sizeInBits # op.type.sizeInBits THEN RETURN FALSE END;
  12898. IF instr.op1.type.form # op.type.form THEN RETURN FALSE END;
  12899. IF instr.op1.type.form = IntermediateCode.Float THEN
  12900. RETURN instr.op1.floatValue = op.floatValue
  12901. ELSE
  12902. RETURN instr.op1.intValue = op.intValue
  12903. END;
  12904. END ProvidesValue;
  12905. BEGIN
  12906. ASSERT(vop.mode = IntermediateCode.ModeImmediate);
  12907. pc := 0;
  12908. WHILE (pc<data.pc) & ~ProvidesValue(data.instructions[pc],vop) DO
  12909. INC(pc);
  12910. END;
  12911. IF pc = data.pc THEN
  12912. data.Emit(Data(Basic.invalidPosition,vop));
  12913. END;
  12914. RETURN pc
  12915. END EnterImmediate;
  12916. PROCEDURE Init;
  12917. VAR i: LONGINT; name: SyntaxTree.IdentifierString;
  12918. BEGIN
  12919. int8 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits8);
  12920. int16 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits16);
  12921. int32 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits32);
  12922. int64 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits64);
  12923. uint8 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits8);
  12924. uint16 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits16);
  12925. uint32 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits32);
  12926. uint64 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits64);
  12927. float32 := IntermediateCode.NewType(IntermediateCode.Float,IntermediateCode.Bits32);
  12928. float64 := IntermediateCode.NewType(IntermediateCode.Float,IntermediateCode.Bits64);
  12929. IntermediateCode.InitOperand(emptyOperand);
  12930. FOR i := 0 TO NumberSystemCalls-1 DO
  12931. name := "@SystemCall";
  12932. Basic.AppendNumber(name,i);
  12933. systemCalls[i] := SyntaxTree.NewSymbol(SyntaxTree.NewIdentifier(name));
  12934. END;
  12935. END Init;
  12936. PROCEDURE IsExported(symbol: SyntaxTree.Symbol): BOOLEAN;
  12937. BEGIN
  12938. RETURN (symbol # NIL) & (symbol.access * SyntaxTree.Public # {})
  12939. END IsExported;
  12940. BEGIN
  12941. Init;
  12942. END FoxIntermediateBackend.
  12943. Compiler.Compile FoxIntermediateBackend.Mod ~
  12944. # Release.Build --path="/temp/obg/" WinAosNewObjectFile ~
  12945. # StaticLinker.Link --fileFormat=PE32 --fileName=A2Z.exe --extension=GofW --displacement=401000H --path="/temp/obg/" Runtime Trace Kernel32 Machine Heaps Modules Objects Kernel KernelLog Streams Commands Files WinFS Clock Dates Reals Strings Diagnostics BitSets StringPool ObjectFile GenericLinker Reflection GenericLoader BootConsole ~
  12946. # Release.Build --path="/temp/obg/" WinAosNewObjectFile ~
  12947. # StaticLinker.Link --fileFormat=PE32 --fileName=A2H.exe --extension=GofW --displacement=401000H Runtime Trace Kernel32 Machine Heaps Modules Objects Kernel KernelLog Streams Commands Files WinFS Clock Dates Reals Strings Diagnostics BitSets StringPool ObjectFile GenericLinker Reflection GenericLoader BootConsole ~
  12948. FSTools.CloseFiles A2Z.exe ~
  12949. SystemTools.FreeDownTo FoxIntermediateBackend ~
  12950. Compiler.Compile -p=Win32G --destPath=/temp/obg/ --traceModule=Trace
  12951. Runtime.Mod Trace.Mod Generic.Win32.Kernel32.Mod Win32.Machine.Mod Heaps.Mod
  12952. Generic.Modules.Mod Win32.Objects.Mod Win32.Kernel.Mod KernelLog.Mod Plugins.Mod Streams.Mod Pipes.Mod
  12953. Commands.Mod I386.Reals.Mod Generic.Reflection.Mod TrapWriters.Mod CRC.Mod SystemVersion.Mod
  12954. Win32.Traps.Mod Locks.Mod Win32.Clock.Mod Disks.Mod Files.Mod Dates.Mod Strings.Mod UTF8Strings.Mod
  12955. FileTrapWriter.Mod Caches.Mod DiskVolumes.Mod OldDiskVolumes.Mod RAMVolumes.Mod DiskFS.Mod OldDiskFS.Mod
  12956. OberonFS.Mod FATVolumes.Mod FATFiles.Mod ISO9660Volumes.Mod ISO9660Files.Mod Win32.User32.Mod
  12957. Win32.WinTrace.Mod Win32.ODBC.Mod Win32.Shell32.Mod Win32.SQL.Mod Win32.WinFS.Mod
  12958. RelativeFileSystem.Mod BitSets.Mod Diagnostics.Mod StringPool.Mod ObjectFile.Mod
  12959. GenericLinker.Mod GenericLoader.Mod BootConsole.Mod