FoxIntermediateBackend.Mod 519 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354
  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, ActiveCells := FoxActiveCells;
  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. suppressModuleRegistration=FALSE;
  53. NonPointer = -1; (* special pointer values *)
  54. NoType = 0; (* special type info values *)
  55. LhsIsPointer = 0; (* for the operator kind *)
  56. RhsIsPointer = 1;
  57. (* priority values, lower means higher priority *)
  58. EntryPriority=-4;
  59. FirstPriority=-3;
  60. InitPriority=-2;
  61. ExitPriority=-1;
  62. StrictChecks = FALSE;
  63. BasePointerTypeSize = 5;
  64. BaseArrayTypeSize = BasePointerTypeSize + 3;
  65. LengthOffset = BasePointerTypeSize + 0;
  66. DataOffset = BasePointerTypeSize + 1;
  67. DescriptorOffset = BasePointerTypeSize + 2;
  68. BaseRecordTypeSize = BasePointerTypeSize + 2;
  69. ActionOffset = BasePointerTypeSize + 0;
  70. MonitorOffset = BasePointerTypeSize + 1;
  71. BaseObjectTypeSize = BaseRecordTypeSize;
  72. ActionTypeSize = 3;
  73. MonitorTypeSize = 7;
  74. ProcessorOffset = BaseObjectTypeSize + 1;
  75. StackLimitOffset* = BaseObjectTypeSize + 3;
  76. QuantumOffset = BaseObjectTypeSize + 4;
  77. TYPE
  78. SupportedInstructionProcedure* = PROCEDURE {DELEGATE} (CONST instr: IntermediateCode.Instruction; VAR moduleName,procedureName: ARRAY OF CHAR): BOOLEAN;
  79. SupportedImmediateProcedure* = PROCEDURE {DELEGATE} (CONST op: IntermediateCode.Operand): BOOLEAN;
  80. Operand = RECORD
  81. mode: SHORTINT;
  82. op: IntermediateCode.Operand;
  83. tag: IntermediateCode.Operand;
  84. extra: IntermediateCode.Operand; (* stores the step size of an array range *)
  85. dimOffset: LONGINT;
  86. END;
  87. Fixup= POINTER TO RECORD
  88. pc: LONGINT;
  89. nextFixup: Fixup;
  90. END;
  91. WriteBackCall = POINTER TO RECORD
  92. call: SyntaxTree.ProcedureCallDesignator;
  93. next: WriteBackCall;
  94. END;
  95. Label= OBJECT
  96. VAR
  97. fixups: Fixup;
  98. section: IntermediateCode.Section;
  99. pc: LONGINT;
  100. PROCEDURE &InitLabel(section: IntermediateCode.Section);
  101. BEGIN
  102. SELF.section := section; pc := -1;
  103. END InitLabel;
  104. PROCEDURE Resolve(pc: LONGINT);
  105. VAR at: LONGINT;
  106. BEGIN
  107. SELF.pc := pc;
  108. WHILE(fixups # NIL) DO
  109. at := fixups.pc;
  110. section.PatchAddress(at,pc);
  111. fixups := fixups.nextFixup;
  112. END;
  113. END Resolve;
  114. PROCEDURE AddFixup(at: LONGINT);
  115. VAR fixup: Fixup;
  116. BEGIN
  117. ASSERT(pc=-1);
  118. NEW(fixup); fixup.pc := at; fixup.nextFixup := fixups; fixups := fixup;
  119. END AddFixup;
  120. END Label;
  121. ConditionalBranch = PROCEDURE {DELEGATE}(label: Label; op1,op2: IntermediateCode.Operand);
  122. DeclarationVisitor =OBJECT(SyntaxTree.Visitor)
  123. VAR
  124. backend: IntermediateBackend;
  125. implementationVisitor: ImplementationVisitor;
  126. meta: MetaDataGenerator;
  127. system: Global.System;
  128. currentScope: SyntaxTree.Scope;
  129. module: Sections.Module;
  130. moduleSelf: SyntaxTree.Variable;
  131. dump: BOOLEAN;
  132. forceModuleBody: BOOLEAN;
  133. addressType: IntermediateCode.Type;
  134. PROCEDURE & Init(system: Global.System; implementationVisitor: ImplementationVisitor; backend: IntermediateBackend; forceModuleBody, dump: BOOLEAN);
  135. BEGIN
  136. currentScope := NIL; module := NIL; moduleSelf := NIL;
  137. SELF.system := system; SELF.implementationVisitor := implementationVisitor;
  138. SELF.dump := dump;
  139. SELF.backend := backend;
  140. SELF.forceModuleBody := forceModuleBody;
  141. addressType := IntermediateCode.GetType(system,system.addressType)
  142. END Init;
  143. PROCEDURE Error(position: LONGINT; CONST s: ARRAY OF CHAR);
  144. BEGIN
  145. backend.Error(module.module.sourceName, position, Diagnostics.Invalid, s);
  146. END Error;
  147. PROCEDURE Type(x: SyntaxTree.Type);
  148. BEGIN
  149. x.Accept(SELF);
  150. END Type;
  151. (** types **)
  152. PROCEDURE VisitBasicType(x: SyntaxTree.BasicType);
  153. BEGIN (* no code emission *) END VisitBasicType;
  154. PROCEDURE VisitCharacterType(x: SyntaxTree.CharacterType);
  155. BEGIN (* no code emission *) END VisitCharacterType;
  156. PROCEDURE VisitIntegerType(x: SyntaxTree.IntegerType);
  157. BEGIN (* no code emission *) END VisitIntegerType;
  158. PROCEDURE VisitFloatType(x: SyntaxTree.FloatType);
  159. BEGIN (* no code emission *) END VisitFloatType;
  160. PROCEDURE VisitComplexType(x: SyntaxTree.ComplexType);
  161. BEGIN (* no code emission *) END VisitComplexType;
  162. PROCEDURE VisitQualifiedType(x: SyntaxTree.QualifiedType);
  163. VAR type: SyntaxTree.Type;
  164. BEGIN (* no further traversal to x.resolved necessary since type descriptor and code will be inserted at "original" position ? *)
  165. type := x.resolved;
  166. IF (type.typeDeclaration # NIL) & (type.typeDeclaration.scope.ownerModule # module.module) THEN
  167. meta.CheckTypeDeclaration(type);
  168. END;
  169. END VisitQualifiedType;
  170. PROCEDURE VisitStringType(x: SyntaxTree.StringType);
  171. BEGIN (* no code emission *) END VisitStringType;
  172. PROCEDURE VisitArrayRangeType(x: SyntaxTree.RangeType);
  173. BEGIN (* no code emission *)
  174. END VisitArrayRangeType;
  175. PROCEDURE VisitArrayType(x: SyntaxTree.ArrayType);
  176. BEGIN (* no code emission *) END VisitArrayType;
  177. PROCEDURE VisitMathArrayType(x: SyntaxTree.MathArrayType);
  178. BEGIN
  179. meta.CheckTypeDeclaration(x);
  180. END VisitMathArrayType;
  181. PROCEDURE VisitPointerType(x: SyntaxTree.PointerType);
  182. BEGIN
  183. meta.CheckTypeDeclaration(x);
  184. (* base type must not be visited => will be done via record type declaration, otherwise is done twice ! *)
  185. END VisitPointerType;
  186. PROCEDURE VisitRecordType(x: SyntaxTree.RecordType);
  187. VAR name: ARRAY 256 OF CHAR; td: SyntaxTree.TypeDeclaration;
  188. BEGIN (* no code emission *)
  189. meta.CheckTypeDeclaration(x);
  190. IF (x.recordScope.ownerModule = module.module) & (x.isObject) THEN
  191. IF x.pointerType.typeDeclaration # NIL THEN
  192. td := x.pointerType.typeDeclaration
  193. ELSE
  194. td := x.typeDeclaration
  195. END;
  196. Global.GetSymbolName(td,name);
  197. (* code section for object *)
  198. END;
  199. Scope(x.recordScope);
  200. END VisitRecordType;
  201. PROCEDURE HasFlag(modifiers: SyntaxTree.Modifier; CONST name: ARRAY OF CHAR): BOOLEAN;
  202. VAR this: SyntaxTree.Modifier; id: SyntaxTree.Identifier;
  203. BEGIN
  204. this := modifiers; id := SyntaxTree.NewIdentifier(name);
  205. WHILE (this # NIL) & (this.identifier# id) DO
  206. this := this.nextModifier;
  207. END;
  208. RETURN this # NIL
  209. END HasFlag;
  210. PROCEDURE VisitCellType(x: SyntaxTree.CellType);
  211. VAR name:Basic.SegmentedName; td: SyntaxTree.TypeDeclaration; type: SyntaxTree.Type; len,port,adr: LONGINT;
  212. parameter: SyntaxTree.Parameter; symbol: IntermediateCode.Section; op: IntermediateCode.Operand; capabilities: SET;
  213. PROCEDURE CreatePortArray(type: SyntaxTree.Type; len: LONGINT);
  214. VAR i,len2: LONGINT; baseType: SyntaxTree.Type;
  215. BEGIN
  216. FOR i := 0 TO len-1 DO
  217. IF SemanticChecker.IsStaticArray(type, baseType, len2) THEN
  218. CreatePortArray(baseType, len2);
  219. ELSE
  220. IF backend.cellsAreObjects THEN
  221. adr := port
  222. ELSE
  223. (*! add check from ActiveCells2 *)
  224. adr := backend.activeCellsSpecification.GetPortAddress(port);
  225. END;
  226. IntermediateCode.InitImmediate(op,addressType,adr);
  227. symbol.Emit(Data(-1,op));
  228. INC(port);
  229. END;
  230. END;
  231. END CreatePortArray;
  232. BEGIN
  233. IF backend.cellsAreObjects THEN meta.CheckTypeDeclaration(x) END;
  234. IF (x.cellScope.ownerModule = module.module) THEN
  235. td := x.typeDeclaration;
  236. Global.GetSymbolSegmentedName(td,name);
  237. (* code section for object *)
  238. END;
  239. port := 0;
  240. parameter := x.firstParameter;
  241. WHILE parameter # NIL DO
  242. type := parameter.type.resolved;
  243. IF type IS SyntaxTree.PortType THEN
  244. len := 1;
  245. INC(port);
  246. ELSIF SemanticChecker.IsStaticArray(type,type,len) OR SemanticChecker.IsDynamicArray(type, type) THEN
  247. IF backend.cellsAreObjects THEN
  248. IF IsStaticArray(parameter.type.resolved) THEN
  249. Error(parameter.position, "static arrays of ports are currently not implemented, please use a property (array property of port)");
  250. END;
  251. (* do nothing *)
  252. ELSE
  253. Global.GetSymbolSegmentedName(parameter,name);
  254. symbol := implementationVisitor.NewSection(module.allSections, Sections.ConstSection, name,parameter,dump);
  255. CreatePortArray(type, len);
  256. (*
  257. WHILE len > 0 DO
  258. adr := backend.activeCellsSpecification.GetPortAddress(port);
  259. IntermediateCode.InitImmediate(op,addressType,adr);
  260. symbol.Emit(Data(-1,op));
  261. DEC(len); INC(port);
  262. END;
  263. *)
  264. END;
  265. ELSE
  266. Error(parameter.position,"should never happen, check semantic checker!");
  267. END;
  268. parameter := parameter.nextParameter;
  269. END;
  270. capabilities := {};
  271. IF HasFlag(x.modifiers, Global.StringFloatingPoint) THEN INCL(capabilities, Global.FloatingPointCapability) END;
  272. IF HasFlag(x.modifiers, Global.StringVector) THEN INCL(capabilities, Global.VectorCapability) END;
  273. backend.SetCapabilities(capabilities);
  274. Scope(x.cellScope);
  275. END VisitCellType;
  276. PROCEDURE VisitProcedureType(x: SyntaxTree.ProcedureType);
  277. BEGIN (* no code emission *) END VisitProcedureType;
  278. PROCEDURE VisitEnumerationType(x: SyntaxTree.EnumerationType);
  279. BEGIN (* no code emission, exported enumeration type values should be included in symbol file *)
  280. END VisitEnumerationType;
  281. (* symbols *)
  282. PROCEDURE VisitProcedure(x: SyntaxTree.Procedure);
  283. BEGIN
  284. Procedure(x);
  285. END VisitProcedure;
  286. PROCEDURE VisitOperator(x: SyntaxTree.Operator);
  287. BEGIN
  288. Procedure(x);
  289. END VisitOperator;
  290. PROCEDURE VisitVariable(x: SyntaxTree.Variable);
  291. VAR name: Basic.SegmentedName; irv: IntermediateCode.Section; align: LONGINT;
  292. BEGIN
  293. IF x.externalName # NIL THEN RETURN END;
  294. IF (currentScope IS SyntaxTree.ModuleScope) OR (currentScope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN
  295. (* code section for variable *)
  296. Global.GetSymbolSegmentedName(x,name);
  297. irv := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,x,dump);
  298. irv.SetExported(IsExported(x));
  299. irv.SetOffset(ToMemoryUnits(system,x.offsetInBits));
  300. irv.Emit(Reserve(x.position,ToMemoryUnits(system,system.SizeOf(x.type))));
  301. IF ~x.fixed THEN
  302. align := CommonAlignment(x.alignment, ToMemoryUnits(system, system.AlignmentOf(system.variableAlignment, x.type)));
  303. ELSE
  304. align := x.alignment;
  305. END;
  306. irv.SetPositionOrAlignment(x.fixed, align);
  307. meta.CheckTypeDeclaration(x.type);
  308. ELSIF currentScope IS SyntaxTree.RecordScope THEN
  309. ELSIF currentScope IS SyntaxTree.ProcedureScope THEN
  310. END;
  311. (* do not call Type(x.type) here as this must already performed in the type declaration section ! *)
  312. END VisitVariable;
  313. PROCEDURE VisitParameter(x: SyntaxTree.Parameter);
  314. VAR name: Basic.SegmentedName; irv: IntermediateCode.Section; op: Operand;
  315. BEGIN
  316. ASSERT(currentScope.outerScope IS SyntaxTree.CellScope);
  317. (* code section for variable *)
  318. Global.GetSymbolSegmentedName(x,name);
  319. irv := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,x,dump);
  320. irv.SetPositionOrAlignment(x.fixed, x.alignment);
  321. (*
  322. irv.SetOffset(ToMemoryUnits(system,x.offsetInBits));
  323. *)
  324. IF x.defaultValue = NIL THEN
  325. irv.Emit(Reserve(x.position,ToMemoryUnits(system,system.SizeOf(x.type))))
  326. ELSE
  327. implementationVisitor.inData := TRUE;
  328. implementationVisitor.Evaluate(x.defaultValue, op);
  329. irv.Emit(Data(x.position,op.op));
  330. implementationVisitor.inData := FALSE;
  331. END;
  332. meta.CheckTypeDeclaration(x.type);
  333. END VisitParameter;
  334. PROCEDURE VisitTypeDeclaration(x: SyntaxTree.TypeDeclaration);
  335. BEGIN
  336. Type(x.declaredType); (* => code in objects *)
  337. IF ~(x.declaredType IS SyntaxTree.QualifiedType) & (x.declaredType.resolved IS SyntaxTree.PointerType) THEN
  338. Type(x.declaredType.resolved(SyntaxTree.PointerType).pointerBase);
  339. END;
  340. END VisitTypeDeclaration;
  341. PROCEDURE VisitConstant(x: SyntaxTree.Constant);
  342. BEGIN
  343. IF (SyntaxTree.Public * x.access # {}) THEN
  344. implementationVisitor.VisitConstant(x);
  345. END;
  346. END VisitConstant;
  347. PROCEDURE Scope(x: SyntaxTree.Scope);
  348. VAR procedure: SyntaxTree.Procedure;
  349. constant: SyntaxTree.Constant;
  350. variable: SyntaxTree.Variable;
  351. prevScope: SyntaxTree.Scope; typeDeclaration: SyntaxTree.TypeDeclaration;
  352. BEGIN
  353. prevScope := currentScope;
  354. currentScope := x;
  355. (* constants treated in implementation visitor *)
  356. typeDeclaration := x.firstTypeDeclaration;
  357. WHILE typeDeclaration # NIL DO
  358. VisitTypeDeclaration(typeDeclaration);
  359. typeDeclaration := typeDeclaration.nextTypeDeclaration;
  360. END;
  361. variable := x.firstVariable;
  362. WHILE variable # NIL DO
  363. VisitVariable(variable);
  364. variable := variable.nextVariable;
  365. END;
  366. procedure := x.firstProcedure;
  367. WHILE procedure # NIL DO
  368. VisitProcedure(procedure);
  369. procedure := procedure.nextProcedure;
  370. END;
  371. constant := x.firstConstant;
  372. WHILE constant # NIL DO
  373. VisitConstant(constant);
  374. constant := constant.nextConstant;
  375. END;
  376. currentScope := prevScope;
  377. END Scope;
  378. PROCEDURE Parameters(first: SyntaxTree.Parameter);
  379. VAR parameter: SyntaxTree.Parameter;
  380. BEGIN
  381. parameter := first;
  382. WHILE parameter # NIL DO
  383. VisitParameter(parameter);
  384. parameter := parameter.nextParameter;
  385. END;
  386. END Parameters;
  387. PROCEDURE Procedure(x: SyntaxTree.Procedure);
  388. VAR scope: SyntaxTree.ProcedureScope;
  389. prevScope: SyntaxTree.Scope;
  390. inline, finalizer: BOOLEAN;
  391. procedureType: SyntaxTree.ProcedureType;
  392. pc: LONGINT;
  393. stackSize: LONGINT;
  394. name,baseObject: Basic.SegmentedName; ir: IntermediateCode.Section;
  395. null,size,src,dest,fp,res: IntermediateCode.Operand;
  396. cc: LONGINT;
  397. cellType: SyntaxTree.CellType;
  398. registerNumber: LONGINT;
  399. registerClass: IntermediateCode.RegisterClass;
  400. type: IntermediateCode.Type;
  401. formalParameter: SyntaxTree.Parameter;
  402. recordType: SyntaxTree.RecordType;
  403. isModuleBody: BOOLEAN;
  404. PROCEDURE Signature;
  405. VAR parameter: SyntaxTree.Parameter; procedureType: SyntaxTree.ProcedureType; returnType : SyntaxTree.Type;
  406. BEGIN
  407. procedureType := x.type(SyntaxTree.ProcedureType);
  408. returnType := procedureType.returnType;
  409. IF returnType # NIL THEN
  410. meta.CheckTypeDeclaration(returnType)
  411. END;
  412. parameter := procedureType.firstParameter;
  413. WHILE parameter # NIL DO
  414. meta.CheckTypeDeclaration(parameter.type); (* we have to re-export a type, i.e. it has to be present in the list of symbols *)
  415. parameter := parameter.nextParameter;
  416. END;
  417. END Signature;
  418. PROCEDURE CheckIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  419. VAR result: BOOLEAN;
  420. BEGIN
  421. result := FALSE;
  422. IF x = SyntaxTree.invalidExpression THEN
  423. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  424. result := TRUE;
  425. value := x.resolved(SyntaxTree.IntegerValue).value;
  426. ELSE
  427. Error(x.position,"expression is not an integer constant");
  428. END;
  429. RETURN result;
  430. END CheckIntegerValue;
  431. PROCEDURE HasValue(modifiers: SyntaxTree.Modifier; CONST name: ARRAY OF CHAR; VAR value: LONGINT): BOOLEAN;
  432. VAR this: SyntaxTree.Modifier; id: SyntaxTree.Identifier;
  433. BEGIN
  434. this := modifiers; id := SyntaxTree.NewIdentifier(name);
  435. WHILE (this # NIL) & (this.identifier # id) DO
  436. this := this.nextModifier;
  437. END;
  438. IF this # NIL THEN
  439. IF this.expression = NIL THEN
  440. Error(this.position,"expected expression value");
  441. ELSIF CheckIntegerValue(this.expression,value) THEN
  442. END;
  443. RETURN TRUE
  444. ELSE RETURN FALSE
  445. END;
  446. END HasValue;
  447. BEGIN
  448. IF x.externalName # NIL THEN RETURN END;
  449. (*
  450. IF Trace & (dump # NIL) THEN dump.String("DeclarationVisitor:Procedure"); dump.Ln END;
  451. *)
  452. (* code section for this procedure *)
  453. scope := x.procedureScope;
  454. prevScope := currentScope;
  455. currentScope := scope;
  456. procedureType := x.type(SyntaxTree.ProcedureType);
  457. isModuleBody := x = module.module.moduleScope.bodyProcedure;
  458. implementationVisitor.temporaries.Init;
  459. implementationVisitor.usedRegisters := NIL;
  460. implementationVisitor.registerUsageCount.Init;
  461. implementationVisitor.GetCodeSectionNameForSymbol(x, name);
  462. IF (scope.body # NIL) & (x.isInline) THEN
  463. inline := TRUE;
  464. ir := implementationVisitor.NewSection(module.allSections, Sections.InlineCodeSection, name,x,dump);
  465. ir.SetExported(IsExported(x));
  466. ELSIF (x.scope # NIL) & (x.scope IS SyntaxTree.CellScope) & (x.scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  467. OR (x.scope # NIL) & (x.scope IS SyntaxTree.ModuleScope) & (x.scope(SyntaxTree.ModuleScope).ownerModule.isCellNet) THEN
  468. IF backend.cellsAreObjects THEN
  469. ir := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name, x, dump);
  470. ir.SetExported(IsExported(x));
  471. ELSE
  472. RETURN; (* cellnet cannot be compiled for final static hardware *)
  473. END;
  474. ELSIF x = module.module.moduleScope.bodyProcedure THEN
  475. inline := FALSE;
  476. AddBodyCallStub(x);
  477. ir := implementationVisitor.NewSection(module.allSections, Sections.BodyCodeSection, name,x,dump);
  478. ir.SetExported(IsExported(x));
  479. ELSIF (scope.outerScope IS SyntaxTree.CellScope) & (x = scope.outerScope(SyntaxTree.CellScope).bodyProcedure) THEN
  480. inline := FALSE;
  481. cellType := scope.outerScope(SyntaxTree.CellScope).ownerCell;
  482. IF ~HasValue(cellType.modifiers,Global.StringDataMemorySize,stackSize) THEN stackSize := ActiveCells.defaultDataMemorySize END;
  483. AddBodyCallStub(x);
  484. AddStackAllocation(x,stackSize);
  485. ir := implementationVisitor.NewSection(module.allSections,Sections.BodyCodeSection, name,x,dump);
  486. ir.SetExported(IsExported(x));
  487. ELSIF (scope.outerScope IS SyntaxTree.CellScope) & (x.isConstructor) THEN
  488. inline := FALSE;
  489. Parameters(procedureType.firstParameter);
  490. ir := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name,x,dump);
  491. ir.SetExported(IsExported(x));
  492. ELSE
  493. inline := FALSE;
  494. IF x.isEntry OR x.isExit THEN
  495. ir := implementationVisitor.NewSection(module.allSections, Sections.InitCodeSection, name,x,dump);
  496. ir.SetExported(TRUE);
  497. IF x.isEntry THEN ir.SetPriority(EntryPriority) ELSE ir.SetPriority(ExitPriority) END;
  498. ELSE
  499. ir := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name,x,dump);
  500. ir.SetExported(IsExported(x) OR SemanticChecker.InMethodTable(x));
  501. END;
  502. END;
  503. cc := procedureType.callingConvention;
  504. IF scope.body # NIL THEN
  505. IF implementationVisitor.emitLabels THEN ir.Emit(LabelInstruction(scope.body.position)) END;
  506. registerNumber := 0;
  507. IF ~inline THEN
  508. IF scope.lastVariable = NIL THEN
  509. stackSize := 0
  510. ELSE
  511. stackSize := scope.lastVariable.offsetInBits;
  512. IF stackSize <0 THEN stackSize := -stackSize END;
  513. Basic.Align(stackSize,system.AlignmentOf(system.parameterAlignment,system.byteType)); (* round up to parameter alignment *)
  514. END;
  515. (*
  516. ir.Emit(Nop(position)); (* placeholder for stack frame check *)
  517. ir.Emit(Nop(position)); (* placeholder for stack frame check (2) *)
  518. *)
  519. IF ~procedureType.noPAF THEN (* no procedure activation frame ! *)
  520. implementationVisitor.EmitEnter(ir,x.position,x,cc,ToMemoryUnits(system,stackSize),registerNumber);
  521. END;
  522. pc := ir.pc-1;
  523. (*
  524. ir.Emit(Nop(position)); (* placeholder for fill *)
  525. *)
  526. IF procedureType.callingConvention # SyntaxTree.OberonCallingConvention THEN
  527. formalParameter := procedureType.lastParameter;
  528. WHILE (formalParameter # NIL) & (registerNumber < system.registerParameters) DO
  529. IF ~PassInRegister(formalParameter) THEN
  530. Error(formalParameter.position,"Calling convention error: cannot be passed as register");
  531. ELSE
  532. IntermediateCode.InitRegisterClass(registerClass, IntermediateCode.Parameter, SHORT(registerNumber));
  533. type := GetType(system, formalParameter.type);
  534. src := IntermediateCode.Register(type, registerClass, implementationVisitor.AcquireRegister(type, registerClass));
  535. IntermediateCode.InitMemory(dest,GetType(system,formalParameter.type),implementationVisitor.fp,ToMemoryUnits(system,formalParameter.offsetInBits));
  536. ir.Emit(Mov(-1,dest, src));
  537. implementationVisitor.ReleaseIntermediateOperand(src);
  538. INC(registerNumber);
  539. formalParameter := formalParameter.prevParameter;
  540. END;
  541. END;
  542. END;
  543. ir.EnterValidPAF;
  544. END;
  545. implementationVisitor.tagsAvailable := procedureType.callingConvention = SyntaxTree.OberonCallingConvention;
  546. implementationVisitor.Body(scope.body,currentScope,ir,isModuleBody);
  547. IF ~inline & ~(procedureType.noPAF) & ~x.isEntry & ~x.isExit THEN
  548. IF scope.lastVariable # NIL THEN
  549. stackSize := scope.lastVariable.offsetInBits;
  550. IF stackSize <0 THEN stackSize := -stackSize END;
  551. Basic.Align(stackSize,system.AlignmentOf(system.parameterAlignment,system.byteType)); (* round up to parameter alignment *)
  552. END;
  553. END;
  554. IF ~inline & ~(procedureType.noPAF) & ~x.isEntry & ~x.isExit THEN
  555. IF ToMemoryUnits(system,stackSize) > 4*1024-256 THEN (* stack frame potentially larger than page size *) (*! get page size from backend *)
  556. (*! unnecessary with new implementation of ENTER -- should potentially be called by backend
  557. IF implementationVisitor.GetRuntimeProcedure(implementationVisitor.runtimeModuleName,"EnsureAllocatedStack",procedure,TRUE) THEN
  558. size := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,stackSize+256));
  559. ir.EmitAt(pc,Push(size));
  560. implementationVisitor.StaticCallOperand(result,procedure);
  561. ir.EmitAt(pc+1,Call(result.op,ProcedureParametersSize(system,procedure)));
  562. END;
  563. *)
  564. END;
  565. ir.EmitAt(pc(*+2*),implementationVisitor.Enter(x.position,cc,ToMemoryUnits(system,stackSize))); (*!!*)
  566. IF stackSize > 0 THEN
  567. IF (stackSize MOD system.addressSize = 0) THEN
  568. null := IntermediateCode.Immediate(addressType,0);
  569. fp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.FP);
  570. IntermediateCode.AddOffset(fp,ToMemoryUnits(system,-system.addressSize));
  571. size := IntermediateCode.Immediate(addressType,stackSize DIV system.addressSize);
  572. ELSE
  573. null := IntermediateCode.Immediate(int8,0);
  574. fp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.FP);
  575. IntermediateCode.AddOffset(fp,ToMemoryUnits(system,-null.type.sizeInBits));
  576. size := IntermediateCode.Immediate(addressType,stackSize DIV null.type.sizeInBits);
  577. END;
  578. (*! should potentially be called by backend -- enter might initialize
  579. ir.EmitAt(pc+3,Fill(fp,null,size,TRUE));
  580. *)
  581. END;
  582. ir.ExitValidPAF;
  583. IF (procedureType.returnType = NIL) OR (scope.body.code # NIL) THEN
  584. finalizer := FALSE;
  585. IF backend.cooperative & x.isFinalizer THEN
  586. recordType := x.scope(SyntaxTree.RecordScope).ownerRecord;
  587. Basic.ToSegmentedName("BaseTypes.Object", baseObject);
  588. GetRecordTypeName(recordType,name);
  589. finalizer := (name # baseObject) & (recordType.baseType = NIL);
  590. END;
  591. implementationVisitor.SetLabel(implementationVisitor.exitLabel);
  592. IF backend.cooperative THEN
  593. IF HasPointers (scope) THEN implementationVisitor.ResetVariables(scope); END;
  594. IF implementationVisitor.profile & ~isModuleBody THEN
  595. implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE)
  596. END;
  597. END;
  598. implementationVisitor.EmitLeave(ir, x.position,cc);
  599. IF finalizer THEN
  600. Basic.ToSegmentedName("BaseTypes.Object.Finalize", name);
  601. IntermediateCode.InitAddress(dest, addressType, name , 0, 0);
  602. ir.Emit(Br(x.position,dest));
  603. ELSE
  604. ir.Emit(Exit(x.position,procedureType.pcOffset,cc));
  605. END;
  606. ELSE
  607. IF ~scope.body.isUnchecked & ~backend.noRuntimeChecks THEN
  608. implementationVisitor.EmitTrap(x.position,ReturnTrap);
  609. END;
  610. implementationVisitor.SetLabel(implementationVisitor.exitLabel);
  611. IF backend.cooperative THEN
  612. IF HasPointers (scope) THEN
  613. IF ~ReturnedAsParameter(procedureType.returnType) THEN
  614. res := implementationVisitor.NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  615. ir.Emit(Result(x.position, res));
  616. ir.Emit(Push(x.position, res));
  617. implementationVisitor.ResetVariables(scope);
  618. IF implementationVisitor.profile & ~isModuleBody THEN implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE) END;
  619. ir.Emit(Pop(x.position, res));
  620. ir.Emit(Return(x.position, res));
  621. ELSE
  622. implementationVisitor.ResetVariables(scope);
  623. IF implementationVisitor.profile & ~isModuleBody THEN implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE) END;
  624. END;
  625. ELSIF implementationVisitor.profile & ~isModuleBody THEN
  626. IF ~ReturnedAsParameter(procedureType.returnType) THEN
  627. res := implementationVisitor.NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  628. ir.Emit(Result(x.position, res));
  629. ir.Emit(Push(x.position, res));
  630. implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE);
  631. ir.Emit(Pop(x.position, res));
  632. ir.Emit(Return(x.position, res));
  633. ELSE
  634. implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE);
  635. END;
  636. END;
  637. implementationVisitor.EmitLeave(ir,x.position,cc);
  638. ir.Emit(Exit(x.position,procedureType.pcOffset,cc));
  639. ELSE
  640. ir.Emit(Nop(x.position));
  641. IF scope.body.isUnchecked OR backend.noRuntimeChecks THEN (* return from procedure in any case *)
  642. implementationVisitor.EmitLeave(ir,x.position,cc);
  643. ir.Emit(Exit(x.position,procedureType.pcOffset,cc));
  644. END;
  645. END;
  646. END
  647. END;
  648. ELSE (* force body for procedures *)
  649. implementationVisitor.EmitEnter(ir, x.position,x,cc,0,0);
  650. ir.EnterValidPAF;
  651. implementationVisitor.Body(scope.body,currentScope,ir,x = module.module.moduleScope.bodyProcedure);
  652. IF implementationVisitor.usedRegisters # NIL THEN D.TraceBack END;
  653. ir.ExitValidPAF;
  654. implementationVisitor.EmitLeave(ir,x.position,cc);
  655. ir.Emit(Exit(x.position,procedureType.pcOffset,cc));
  656. END;
  657. Scope(scope);
  658. Signature;
  659. IF (x IS SyntaxTree.Operator) & x(SyntaxTree.Operator).isDynamic THEN implementationVisitor.RegisterDynamicOperator(x(SyntaxTree.Operator)) END;
  660. currentScope := prevScope;
  661. END Procedure;
  662. PROCEDURE AddBodyCallStub(bodyProcedure: SyntaxTree.Procedure); (* code that is only necessary for static linkers *)
  663. VAR procedure: SyntaxTree.Procedure; procedureScope: SyntaxTree.ProcedureScope; name: Basic.SegmentedName;
  664. ir: IntermediateCode.Section; op: IntermediateCode.Operand;
  665. BEGIN
  666. ASSERT (bodyProcedure # NIL);
  667. procedureScope := SyntaxTree.NewProcedureScope(bodyProcedure.scope);
  668. procedure := SyntaxTree.NewProcedure(-1,SyntaxTree.NewIdentifier("@BodyStub"), procedureScope);
  669. procedure.SetScope(bodyProcedure.scope);
  670. procedure.SetType(SyntaxTree.NewProcedureType(-1,bodyProcedure.scope));
  671. procedure.SetAccess(SyntaxTree.Hidden);
  672. Global.GetSymbolSegmentedName (procedure,name);
  673. ir := implementationVisitor.NewSection(module.allSections, Sections.InitCodeSection, name,procedure,dump);
  674. ir.SetExported(TRUE);
  675. ir.SetPriority(InitPriority);
  676. Global.GetSymbolSegmentedName (bodyProcedure,name);
  677. IF (backend.newObjectFile OR backend.cooperative) & ~meta.simple THEN
  678. implementationVisitor.currentScope := module.module.moduleScope;
  679. implementationVisitor.section := ir;
  680. implementationVisitor.PushSelfPointer();
  681. implementationVisitor.CallThis(bodyProcedure.position,"Modules","Register",1);
  682. ELSIF backend.preregisterStatic THEN
  683. implementationVisitor.currentScope := module.module.moduleScope;
  684. implementationVisitor.section := ir;
  685. implementationVisitor.PushSelfPointer();
  686. implementationVisitor.CallThis(bodyProcedure.position,"Modules","Preregister",1);
  687. ELSE
  688. IntermediateCode.InitAddress(op, addressType, name, implementationVisitor.GetFingerprint(bodyProcedure), 0);
  689. ir.Emit(Call(bodyProcedure.position,op, 0));
  690. END;
  691. END AddBodyCallStub;
  692. PROCEDURE AddStackAllocation(symbol: SyntaxTree.Symbol; initStack: LONGINT); (* code that is only necessary for static linkers *)
  693. VAR name: Basic.SegmentedName;
  694. ir: IntermediateCode.Section; op: IntermediateCode.Operand;
  695. BEGIN
  696. Global.GetSymbolSegmentedName (symbol,name);
  697. Basic.RemoveSuffix(name);
  698. Basic.SuffixSegmentedName(name, Basic.MakeString("@StackAllocation"));
  699. ir := implementationVisitor.NewSection(module.allSections,Sections.InitCodeSection,name,NIL,dump);
  700. ir.SetExported(TRUE);
  701. ir.SetPriority(FirstPriority);
  702. IntermediateCode.InitImmediate(op,addressType,initStack);
  703. ir.Emit(Mov(-1,implementationVisitor.sp,op));
  704. END AddStackAllocation;
  705. (** entry function to visit a complete module *)
  706. PROCEDURE Module(x: SyntaxTree.Module; module: Sections.Module);
  707. VAR
  708. ir: IntermediateCode.Section; op: IntermediateCode.Operand; name: Basic.SegmentedName; idstr: SyntaxTree.IdentifierString;
  709. hasDynamicOperatorDeclarations: BOOLEAN;
  710. operator: SyntaxTree.Operator;
  711. import: SyntaxTree.Import;
  712. PROCEDURE TypeNeedsInitialization(type: SyntaxTree.Type): BOOLEAN;
  713. BEGIN
  714. type := type.resolved;
  715. IF type IS SyntaxTree.RecordType THEN
  716. IF ScopeNeedsInitialization(type(SyntaxTree.RecordType).recordScope) THEN RETURN TRUE END;
  717. ELSIF (type IS SyntaxTree.ArrayType) THEN
  718. IF type(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  719. IF TypeNeedsInitialization(type(SyntaxTree.ArrayType).arrayBase) THEN RETURN TRUE END;
  720. END;
  721. ELSIF type IS SyntaxTree.MathArrayType THEN
  722. WITH type: SyntaxTree.MathArrayType DO
  723. IF type.form = SyntaxTree.Open THEN
  724. RETURN TRUE
  725. ELSIF type.form = SyntaxTree.Static THEN
  726. IF TypeNeedsInitialization(type.arrayBase) THEN RETURN TRUE END;
  727. END;
  728. END;
  729. END;
  730. RETURN FALSE
  731. END TypeNeedsInitialization;
  732. PROCEDURE ScopeNeedsInitialization(scope: SyntaxTree.Scope): BOOLEAN;
  733. VAR variable: SyntaxTree.Variable;
  734. BEGIN
  735. variable := scope.firstVariable;
  736. WHILE variable # NIL DO
  737. IF TypeNeedsInitialization(variable.type) THEN RETURN TRUE END;
  738. IF variable.initializer # NIL THEN RETURN TRUE END;
  739. variable := variable.nextVariable;
  740. END;
  741. RETURN FALSE
  742. END ScopeNeedsInitialization;
  743. BEGIN
  744. ASSERT(x # NIL); ASSERT(module # NIL);
  745. SELF.module := module;
  746. (* add import names to the generated Sections.Module *)
  747. import := x.moduleScope.firstImport;
  748. WHILE import # NIL DO
  749. import.module.GetName(idstr);
  750. module.imports.AddName(idstr);
  751. import := import.nextImport
  752. END;
  753. IF ~implementationVisitor.newObjectFile & ~meta.simple THEN
  754. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@moduleSelf"));
  755. moduleSelf := SyntaxTree.NewVariable(0,SyntaxTree.NewIdentifier("@moduleSelf"));
  756. moduleSelf.SetType(system.anyType);
  757. moduleSelf.SetScope(x.moduleScope);
  758. moduleSelf.SetUntraced(TRUE);
  759. ir := implementationVisitor.NewSection(module.allSections, Sections.ConstSection, name,moduleSelf,dump); ir.SetOffset(0);
  760. ir.SetExported(TRUE);
  761. IntermediateCode.InitImmediate(op,addressType,0);
  762. ir.Emit(Data(-1,op));
  763. END;
  764. implementationVisitor.module := module;
  765. implementationVisitor.moduleScope := x.moduleScope;
  766. implementationVisitor.moduleSelf := moduleSelf;
  767. implementationVisitor.canBeLoaded := TRUE;
  768. meta.SetModule(module);
  769. IF (forceModuleBody OR implementationVisitor.newObjectFile & ~meta.simple OR ScopeNeedsInitialization(x.moduleScope)) THEN
  770. EnsureBodyProcedure(x.moduleScope); (* currently needed in Oberon, remove ? *)
  771. END;
  772. IF backend.profile THEN
  773. EnsureBodyProcedure(x.moduleScope);
  774. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@ModuleId"));
  775. implementationVisitor.profileId := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,NIL,dump);
  776. implementationVisitor.profileId.Emit(Reserve(-1,ToMemoryUnits(system,system.SizeOf(system.longintType))));
  777. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@InitProfiler"));
  778. implementationVisitor.profileInit := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump);
  779. implementationVisitor.EmitEnter(implementationVisitor.profileInit,-1,NIL,0,0,0);
  780. Global.GetModuleName(module.module,idstr);
  781. implementationVisitor.ProfilerAddModule(idstr);
  782. implementationVisitor.numberProcedures := 0;
  783. END;
  784. implementationVisitor.profile := backend.profile;
  785. (* check if there is at least one dynamic operator locally defined *)
  786. hasDynamicOperatorDeclarations := FALSE;
  787. operator := x.moduleScope.firstOperator;
  788. WHILE operator # NIL DO
  789. IF operator.isDynamic THEN hasDynamicOperatorDeclarations := TRUE END;
  790. operator := operator.nextOperator
  791. END;
  792. (* add operator initialization code section *)
  793. IF hasDynamicOperatorDeclarations THEN
  794. EnsureBodyProcedure(x.moduleScope);
  795. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@OperatorInitialization"));
  796. implementationVisitor.operatorInitializationCodeSection := implementationVisitor.NewSection(module.allSections, Sections.CodeSection,name, NIL, dump);
  797. implementationVisitor.EmitEnter(implementationVisitor.operatorInitializationCodeSection,-1,NIL,0,0,0);
  798. END;
  799. Scope(x.moduleScope);
  800. IF hasDynamicOperatorDeclarations THEN
  801. implementationVisitor.EmitLeave(implementationVisitor.operatorInitializationCodeSection,-1,0);
  802. implementationVisitor.operatorInitializationCodeSection.Emit(Exit(-1,0,0));
  803. END;
  804. IF backend.profile THEN
  805. implementationVisitor.ProfilerPatchInit;
  806. END;
  807. END Module;
  808. END DeclarationVisitor;
  809. UsedArray*=POINTER TO ARRAY OF RECORD count: LONGINT; map: LONGINT; type: IntermediateCode.Type; class: IntermediateCode.RegisterClass END;
  810. RegisterUsageCount*=OBJECT
  811. VAR used: UsedArray; count: LONGINT;
  812. PROCEDURE &Init;
  813. VAR i: LONGINT;
  814. BEGIN
  815. count := 0;
  816. IF used = NIL THEN NEW(used,64); END;
  817. FOR i := 0 TO LEN(used)-1 DO used[i].count := 0; used[i].map := i END;
  818. END Init;
  819. PROCEDURE Grow;
  820. VAR new: UsedArray; size,i: LONGINT;
  821. BEGIN
  822. size := LEN(used)*2;
  823. NEW(new,size);
  824. FOR i := 0 TO LEN(used)-1 DO
  825. new[i].count := used[i].count;
  826. new[i].type := used[i].type;
  827. new[i].map := used[i].map
  828. END;
  829. FOR i := LEN(used) TO LEN(new)-1 DO new[i].count := 0 END;
  830. used := new
  831. END Grow;
  832. PROCEDURE Next(type: IntermediateCode.Type; class: IntermediateCode.RegisterClass): LONGINT;
  833. BEGIN
  834. INC(count);
  835. IF count = LEN(used) THEN Grow END;
  836. used[count].type := type;
  837. used[count].class := class;
  838. used[count].map := count;
  839. RETURN count;
  840. END Next;
  841. PROCEDURE IncUse(register: LONGINT);
  842. BEGIN
  843. INC(used[register].count);
  844. (*
  845. IF (register = 1) & (count > 30) THEN
  846. D.TraceBack;
  847. END;
  848. *)
  849. END IncUse;
  850. PROCEDURE DecUse(register: LONGINT);
  851. BEGIN
  852. DEC(used[register].count);
  853. END DecUse;
  854. PROCEDURE Map(register: LONGINT): LONGINT;
  855. VAR map : LONGINT;
  856. BEGIN
  857. IF register > 0 THEN
  858. map := used[register].map;
  859. WHILE register # map DO register := map; map := used[register].map END;
  860. END;
  861. RETURN register
  862. END Map;
  863. PROCEDURE Remap(register: LONGINT; to: LONGINT);
  864. BEGIN
  865. used[register].map:= to;
  866. used[to].count := used[register].count;
  867. used[register].count := 0;
  868. END Remap;
  869. PROCEDURE Use(register: LONGINT): LONGINT;
  870. BEGIN
  871. IF register< LEN(used) THEN
  872. RETURN used[register].count
  873. ELSE
  874. RETURN 0
  875. END
  876. END Use;
  877. END RegisterUsageCount;
  878. RegisterEntry = POINTER TO RECORD
  879. prev,next: RegisterEntry;
  880. register: LONGINT;
  881. registerClass: IntermediateCode.RegisterClass;
  882. type: IntermediateCode.Type;
  883. END;
  884. VariableUse= ARRAY 32 OF SET; (* upper bound of 1024 temporary variables in a procedure .. should be enough for all times *)
  885. Variables = OBJECT (Basic.List)
  886. VAR
  887. inUse: VariableUse;
  888. registerIndex: LONGINT;
  889. PROCEDURE & Init;
  890. VAR i: LONGINT;
  891. BEGIN
  892. InitList(16);
  893. FOR i := 0 TO LEN(inUse)-1 DO inUse[i] := {} END;
  894. registerIndex := 1024;
  895. END Init;
  896. PROCEDURE GetUsage(VAR use: VariableUse);
  897. BEGIN
  898. use := inUse;
  899. END GetUsage;
  900. PROCEDURE SetUsage(CONST use: VariableUse);
  901. BEGIN
  902. inUse := use;
  903. END SetUsage;
  904. PROCEDURE GetVariable(i: LONGINT): SyntaxTree.Variable;
  905. VAR any: ANY;
  906. BEGIN
  907. any := Get(i);;
  908. IF any = NIL THEN RETURN NIL ELSE RETURN any(SyntaxTree.Variable) END;
  909. END GetVariable;
  910. PROCEDURE Occupy(pos: LONGINT);
  911. BEGIN
  912. INCL(inUse[pos DIV 32], pos MOD 32);
  913. END Occupy;
  914. PROCEDURE AddVariable(v: SyntaxTree.Variable);
  915. BEGIN
  916. Occupy(Length());
  917. Add(v);
  918. END AddVariable;
  919. PROCEDURE GetFreeVariable(type: SyntaxTree.Type; VAR pos: LONGINT): SyntaxTree.Variable;
  920. VAR var : SyntaxTree.Variable;
  921. BEGIN
  922. FOR pos := 0 TO Length()-1 DO
  923. IF ~((pos MOD 32) IN inUse[pos DIV 32]) THEN
  924. var := GetVariable(pos);
  925. IF type.SameType(var.type) THEN
  926. Occupy(pos); RETURN var
  927. END;
  928. END;
  929. END;
  930. pos := Length();
  931. RETURN NIL
  932. END GetFreeVariable;
  933. END Variables;
  934. SymbolMap = POINTER TO RECORD this: SyntaxTree.Symbol; to, tag: SyntaxTree.Expression; next: SymbolMap; END;
  935. SymbolMapper = OBJECT
  936. VAR
  937. first: SymbolMap;
  938. PROCEDURE & Init;
  939. BEGIN
  940. first := NIL;
  941. END Init;
  942. PROCEDURE Add(this: SyntaxTree.Symbol; to, tag: SyntaxTree.Expression);
  943. VAR new: SymbolMap;
  944. BEGIN
  945. NEW(new); new.this := this; new.to := to; new.tag := tag;
  946. new.next := first; first := new;
  947. END Add;
  948. PROCEDURE Get(this: SyntaxTree.Symbol): SymbolMap;
  949. VAR s: SymbolMap;
  950. BEGIN
  951. s := first;
  952. WHILE (s # NIL) & (s.this # this) DO
  953. s := s.next
  954. END;
  955. RETURN s
  956. END Get;
  957. END SymbolMapper;
  958. ImplementationVisitor =OBJECT(SyntaxTree.Visitor)
  959. VAR
  960. system: Global.System;
  961. section: IntermediateCode.Section;
  962. module: Sections.Module;
  963. moduleScope : SyntaxTree.ModuleScope; (* shortcut for module.module.moduleScope *)
  964. awaitProcCounter, labelId, constId, caseId: LONGINT;
  965. hiddenPointerType: SyntaxTree.RecordType; (* used as hidden pointer, for example for ARRAY OF ANY *)
  966. delegatePointerType: SyntaxTree.RecordType; (* used for delegates, for example in ARRAY OF PROCEDURE{DELEGATE} *)
  967. checker: SemanticChecker.Checker;
  968. backend: IntermediateBackend;
  969. meta: MetaDataGenerator;
  970. position: LONGINT;
  971. moduleSelf: SyntaxTree.Variable;
  972. (* variables for hand over of variables / temporary state *)
  973. currentScope: SyntaxTree.Scope;
  974. constantDeclaration : SyntaxTree.Symbol;
  975. result: Operand; (* result of the most recent expression / statement *)
  976. destination: IntermediateCode.Operand;
  977. arrayDestinationTag: IntermediateCode.Operand;
  978. arrayDestinationDimension:LONGINT;
  979. currentLoop: Label; (* variable to hand over loop exit jump list *)
  980. conditional: BOOLEAN;
  981. trueLabel, falseLabel, exitLabel: Label;
  982. locked: BOOLEAN;
  983. (*
  984. usedRegisters: Registers;
  985. *)
  986. registerUsageCount: RegisterUsageCount;
  987. usedRegisters: RegisterEntry;
  988. (* useful operands and types *)
  989. nil,fp,sp,ap,lr,true,false: IntermediateCode.Operand;
  990. bool,addressType,setType, sizeType, byteType: IntermediateCode.Type;
  991. commentPrintout: Printout.Printer;
  992. dump: Streams.Writer;
  993. tagsAvailable : BOOLEAN;
  994. supportedInstruction: SupportedInstructionProcedure;
  995. supportedImmediate: SupportedImmediateProcedure;
  996. inData: BOOLEAN; (* to prevent indirect reference to data within data sections, cf. VisitIntegerValue *)
  997. emitLabels: BOOLEAN;
  998. runtimeModuleName : SyntaxTree.IdentifierString;
  999. newObjectFile: BOOLEAN;
  1000. indexCounter: LONGINT;
  1001. profile: BOOLEAN;
  1002. profileId, profileInit: IntermediateCode.Section;
  1003. profileInitPatchPosition: LONGINT;
  1004. numberProcedures: LONGINT;
  1005. procedureResultDesignator : SyntaxTree.Designator;
  1006. operatorInitializationCodeSection: IntermediateCode.Section;
  1007. fingerPrinter: FingerPrinter.FingerPrinter;
  1008. temporaries: Variables;
  1009. canBeLoaded : BOOLEAN;
  1010. currentIsInline: BOOLEAN;
  1011. currentMapper: SymbolMapper;
  1012. currentInlineExit: Label;
  1013. moduleBodySection: IntermediateCode.Section;
  1014. NeedDescriptor : BOOLEAN;
  1015. cooperativeSwitches: BOOLEAN;
  1016. lastSwitchPC: LONGINT;
  1017. isUnchecked: BOOLEAN;
  1018. PROCEDURE & Init(system: Global.System; checker: SemanticChecker.Checker; supportedInstructionProcedure: SupportedInstructionProcedure; supportedImmediateProcedure: SupportedImmediateProcedure; emitLabels: BOOLEAN; CONST runtime: SyntaxTree.IdentifierString; backend: IntermediateBackend;
  1019. newObjectFile: BOOLEAN);
  1020. BEGIN
  1021. SELF.system := system;
  1022. SELF.runtimeModuleName := runtime;
  1023. currentScope := NIL;
  1024. hiddenPointerType := NIL;
  1025. delegatePointerType := NIL;
  1026. awaitProcCounter := 0;
  1027. labelId := 0; constId := 0; labelId := 0;
  1028. SELF.checker := checker;
  1029. SELF.backend := backend;
  1030. position := Diagnostics.Invalid;
  1031. conditional := FALSE;
  1032. locked := FALSE;
  1033. InitOperand(result,ModeUndefined);
  1034. addressType := IntermediateCode.GetType(system,system.addressType);
  1035. setType := IntermediateCode.GetType(system,system.setType);
  1036. sizeType := IntermediateCode.GetType(system, system.sizeType);
  1037. byteType := IntermediateCode.GetType(system, system.byteType);
  1038. fp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.FP);
  1039. sp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.SP);
  1040. ap := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.AP);
  1041. lr := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.LR);
  1042. nil := IntermediateCode.Immediate(addressType,0);
  1043. IntermediateCode.InitOperand(destination);
  1044. tagsAvailable := TRUE;
  1045. supportedInstruction := supportedInstructionProcedure;
  1046. supportedImmediate := supportedImmediateProcedure;
  1047. inData := FALSE;
  1048. SELF.emitLabels := emitLabels;
  1049. IntermediateCode.InitOperand(arrayDestinationTag);
  1050. bool := IntermediateCode.GetType(system,system.booleanType);
  1051. IntermediateCode.InitImmediate(false,bool,0);
  1052. IntermediateCode.InitImmediate(true,bool,1);
  1053. SELF.newObjectFile := newObjectFile;
  1054. indexCounter := 0;
  1055. NEW(registerUsageCount);
  1056. usedRegisters := NIL;
  1057. procedureResultDesignator := NIL;
  1058. NEW(fingerPrinter, system);
  1059. NEW(temporaries);
  1060. currentIsInline := FALSE;
  1061. NeedDescriptor := FALSE;
  1062. isUnchecked := backend.noRuntimeChecks;
  1063. END Init;
  1064. TYPE Context = RECORD
  1065. section: IntermediateCode.Section;
  1066. registerUsageCount: RegisterUsageCount;
  1067. usedRegisters: RegisterEntry;
  1068. END;
  1069. PROCEDURE SwitchContext(new: IntermediateCode.Section): Context;
  1070. VAR context: Context;
  1071. BEGIN
  1072. context.section := section;
  1073. context.registerUsageCount := registerUsageCount;
  1074. context.usedRegisters := usedRegisters;
  1075. section := new;
  1076. NEW(registerUsageCount);
  1077. usedRegisters := NIL;
  1078. RETURN context;
  1079. END SwitchContext;
  1080. PROCEDURE ReturnToContext(context: Context);
  1081. BEGIN
  1082. section := context.section;
  1083. registerUsageCount := context.registerUsageCount;
  1084. usedRegisters := context.usedRegisters;
  1085. END ReturnToContext;
  1086. PROCEDURE NewSection(list: Sections.SectionList; type: SHORTINT; CONST name: Basic.SegmentedName; syntaxTreeSymbol: SyntaxTree.Symbol; dump: BOOLEAN): IntermediateCode.Section;
  1087. VAR fp: SyntaxTree.FingerPrint; section: IntermediateCode.Section;
  1088. BEGIN
  1089. IF (syntaxTreeSymbol # NIL) & ~((syntaxTreeSymbol IS SyntaxTree.Procedure) & (syntaxTreeSymbol(SyntaxTree.Procedure).isInline)) THEN
  1090. fp := fingerPrinter.SymbolFP(syntaxTreeSymbol)
  1091. END;
  1092. section := IntermediateCode.NewSection(list, type, name, syntaxTreeSymbol, dump);
  1093. section.SetExported(IsExported(syntaxTreeSymbol));
  1094. RETURN section
  1095. END NewSection;
  1096. PROCEDURE AcquireRegister(CONST type: IntermediateCode.Type; class: IntermediateCode.RegisterClass): LONGINT;
  1097. VAR new: LONGINT;
  1098. BEGIN
  1099. new := registerUsageCount.Next(type,class);
  1100. UseRegister(new);
  1101. RETURN new
  1102. END AcquireRegister;
  1103. (** get the name for the code section that represens a certain symbol
  1104. (essentially the same as Global.GetSymbolName, apart from operators) **)
  1105. PROCEDURE GetCodeSectionNameForSymbol(symbol: SyntaxTree.Symbol; VAR name: Basic.SegmentedName);
  1106. VAR
  1107. operatorFingerPrint: SyntaxTree.FingerPrint;
  1108. operatorFingerPrintString,string: ARRAY 32 OF CHAR;
  1109. BEGIN
  1110. Global.GetSymbolSegmentedName(symbol, name);
  1111. (* if the symbol is an operator, then append the fingerprint to the name *)
  1112. IF symbol IS SyntaxTree.Operator THEN
  1113. operatorFingerPrint := fingerPrinter.SymbolFP(symbol);
  1114. string := "[";
  1115. Strings.IntToHexStr(operatorFingerPrint.shallow, 8, operatorFingerPrintString);
  1116. Strings.Append(string, operatorFingerPrintString);
  1117. Strings.Append(string, "]");
  1118. Basic.AppendToSegmentedName(name,string);
  1119. END
  1120. END GetCodeSectionNameForSymbol;
  1121. PROCEDURE TraceEnter(CONST s: ARRAY OF CHAR);
  1122. BEGIN
  1123. IF dump # NIL THEN
  1124. dump.String("enter "); dump.String(s); dump.Ln;
  1125. END;
  1126. END TraceEnter;
  1127. PROCEDURE TraceExit(CONST s: ARRAY OF CHAR);
  1128. BEGIN
  1129. IF dump # NIL THEN
  1130. dump.String("exit "); dump.String(s); dump.Ln;
  1131. END;
  1132. END TraceExit;
  1133. PROCEDURE Emit(instruction: IntermediateCode.Instruction);
  1134. VAR moduleName, procedureName: SyntaxTree.IdentifierString;
  1135. PROCEDURE CheckRegister(VAR op: IntermediateCode.Operand);
  1136. VAR i: LONGINT;
  1137. BEGIN
  1138. IF op.register >0 THEN IntermediateCode.SetRegister(op,registerUsageCount.Map(op.register)) END;
  1139. IF op.rule # NIL THEN
  1140. FOR i := 0 TO LEN(op.rule)-1 DO
  1141. CheckRegister(op.rule[i])
  1142. END;
  1143. END;
  1144. END CheckRegister;
  1145. BEGIN
  1146. CheckRegister(instruction.op1);
  1147. CheckRegister(instruction.op2);
  1148. CheckRegister(instruction.op3);
  1149. IF supportedInstruction(instruction,moduleName,procedureName) THEN section.Emit(instruction)
  1150. ELSE section.Emit(instruction);
  1151. EnsureSymbol(moduleName,procedureName); (* remainder for binary object file *)
  1152. END;
  1153. END Emit;
  1154. PROCEDURE EmitTrap (position, trapNo: LONGINT);
  1155. BEGIN
  1156. IF backend.cooperative THEN
  1157. Emit(Push(position,IntermediateCode.Immediate(sizeType,trapNo)));
  1158. CallThis(position,DefaultRuntimeModuleName,"Trap",1);
  1159. ELSE
  1160. Emit(Trap(position,trapNo));
  1161. END;
  1162. END EmitTrap;
  1163. PROCEDURE EmitEnter (section: IntermediateCode.Section; position: LONGINT; procedure: SyntaxTree.Procedure; callconv: LONGINT; varSize: LONGINT; numRegs: LONGINT);
  1164. VAR name: Basic.SegmentedName;
  1165. VAR op1, op2, reg: IntermediateCode.Operand;
  1166. VAR call, nocall: Label;
  1167. VAR parametersSize: LONGINT;
  1168. VAR prevSection: IntermediateCode.Section;
  1169. VAR prevDump: Streams.Writer;
  1170. BEGIN
  1171. ASSERT(~procedure.type(SyntaxTree.ProcedureType).noPAF);
  1172. prevSection := SELF.section;
  1173. SELF.section := section;
  1174. prevDump := dump;
  1175. dump := section.comments;
  1176. IF backend.hasLinkRegister THEN
  1177. Emit(Push(-1, lr));
  1178. END;
  1179. Emit(Push(-1,fp));
  1180. IF backend.cooperative & (callconv = SyntaxTree.OberonCallingConvention) THEN
  1181. IF (procedure # NIL) & (HasPointers (procedure.procedureScope) OR HasVariableParameters (procedure.procedureScope) OR IsNested (procedure)) THEN
  1182. GetCodeSectionNameForSymbol(procedure, name);
  1183. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Descriptor"));
  1184. ELSE
  1185. Basic.ToSegmentedName ("BaseTypes.StackFrame",name);
  1186. END;
  1187. IntermediateCode.InitAddress(op1, addressType, name , 0, 0);
  1188. Emit(Push(-1,op1));
  1189. Emit(Mov(-1,fp, sp));
  1190. IF (procedure.procedureScope.body = NIL) OR ~procedure.procedureScope.body.isUnchecked THEN
  1191. NEW(call, section);
  1192. NEW(nocall, section);
  1193. reg := NewRegisterOperand(addressType);
  1194. IntermediateCode.InitImmediate(op1,addressType, varSize);
  1195. Emit(Sub(-1,reg, sp, op1));
  1196. BrltL(call, sp, reg);
  1197. IntermediateCode.InitMemory(op2, addressType,ap,ToMemoryUnits(system,system.addressSize*10));
  1198. BrgeL(nocall, sp, op2);
  1199. call.Resolve(section.pc);
  1200. parametersSize := ProcedureParametersSize(backend.system,procedure);
  1201. IntermediateCode.InitImmediate(op2,addressType, parametersSize);
  1202. Emit(Push(-1, op2));
  1203. Emit(Push(-1, reg));
  1204. ReleaseIntermediateOperand(reg);
  1205. CallThis(position, "Activities","ExpandStack",2);
  1206. Emit(Result(-1, sp));
  1207. nocall.Resolve(section.pc);
  1208. END;
  1209. ELSE
  1210. Emit(Mov(-1, fp, sp));
  1211. END;
  1212. Emit(Enter(-1, callconv, varSize));
  1213. SELF.section := prevSection;
  1214. dump := prevDump;
  1215. END EmitEnter;
  1216. PROCEDURE Enter(position: LONGINT; callconv: LONGINT; varSize: LONGINT): IntermediateCode.Instruction;
  1217. VAR op1,op2: IntermediateCode.Operand;
  1218. VAR instruction: IntermediateCode.Instruction;
  1219. BEGIN
  1220. IntermediateCode.InitNumber(op1,callconv);
  1221. IntermediateCode.InitNumber(op2,varSize);
  1222. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.enter,op1,op2,emptyOperand);
  1223. RETURN instruction
  1224. END Enter;
  1225. PROCEDURE Leave(position: LONGINT; callconv: LONGINT): IntermediateCode.Instruction;
  1226. VAR op1: IntermediateCode.Operand;
  1227. VAR instruction: IntermediateCode.Instruction;
  1228. BEGIN
  1229. IntermediateCode.InitNumber(op1,callconv);
  1230. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.leave,op1,emptyOperand,emptyOperand);
  1231. RETURN instruction
  1232. END Leave;
  1233. PROCEDURE EmitLeave(section: IntermediateCode.Section; position: LONGINT; callconv: LONGINT);
  1234. VAR prevSection: IntermediateCode.Section;
  1235. VAR op2: IntermediateCode.Operand;
  1236. BEGIN
  1237. prevSection := SELF.section;
  1238. SELF.section := section;
  1239. Emit(Leave(position, callconv));
  1240. IF backend.cooperative THEN
  1241. IntermediateCode.InitImmediate(op2,addressType, ToMemoryUnits(system, system.addressSize));
  1242. Emit(Add(position, sp, fp, op2));
  1243. ELSE
  1244. Emit(Mov(position, sp, fp));
  1245. END;
  1246. Emit(Pop(position, fp));
  1247. SELF.section := prevSection;
  1248. END EmitLeave;
  1249. PROCEDURE Symbol(x: SyntaxTree.Symbol; VAR op: Operand);
  1250. VAR m: SymbolMap;
  1251. BEGIN
  1252. position := x.position;
  1253. IF currentIsInline THEN
  1254. m := currentMapper.Get(x);
  1255. IF m # NIL THEN
  1256. (*
  1257. Printout.Info("mapping from", x);
  1258. Printout.Info("mapping to ", m.to);
  1259. *)
  1260. m.to.Accept(SELF);
  1261. op := result;
  1262. IF m.tag # NIL THEN
  1263. ReleaseIntermediateOperand(result.tag);
  1264. m.tag.Accept(SELF);
  1265. op.tag := result.op;
  1266. ReleaseIntermediateOperand(result.tag);
  1267. END;
  1268. RETURN
  1269. END;
  1270. END;
  1271. x.Accept(SELF);
  1272. op := result;
  1273. END Symbol;
  1274. PROCEDURE Expression(x: SyntaxTree.Expression);
  1275. BEGIN
  1276. position := x.position;
  1277. constantDeclaration := NIL;
  1278. IF (x IS SyntaxTree.SymbolDesignator) & (x(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Constant) THEN
  1279. constantDeclaration := x(SyntaxTree.SymbolDesignator).symbol;
  1280. END;
  1281. IF x.resolved # NIL THEN
  1282. x.resolved.Accept(SELF)
  1283. ELSE
  1284. x.Accept(SELF)
  1285. END;
  1286. (* check this, was commented out in ActiveCells3 *)
  1287. IF (x IS SyntaxTree.Designator) & (x(SyntaxTree.Designator).modifiers # NIL) & ~backend.cellsAreObjects THEN
  1288. Error(x.position, "unsupported modifier");
  1289. END;
  1290. END Expression;
  1291. (*
  1292. PROCEDURE ResetUsedTemporaries(previous: VariableUse);
  1293. VAR current: VariableUse; set: SET; i,j: LONGINT; variable: SyntaxTree.Variable; op: Operand; tmp: IntermediateCode.Operand;
  1294. BEGIN
  1295. temporaries.GetUsage(current);
  1296. FOR i := 0 TO LEN(current)-1 DO
  1297. set := current[i] - previous[i];
  1298. IF set # {} THEN
  1299. FOR j := 0 TO MAX(SET)-1 DO
  1300. IF j IN set THEN
  1301. variable := temporaries.GetVariable(i*MAX(SET)+j);
  1302. IF (variable.type.resolved IS SyntaxTree.MathArrayType) & (variable.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  1303. Symbol(variable, op);
  1304. MakeMemory(tmp,op.op,addressType,0);
  1305. ReleaseOperand(op);
  1306. Emit(Mov(position,tmp, nil ) );
  1307. ReleaseIntermediateOperand(tmp);
  1308. END;
  1309. END;
  1310. END;
  1311. END;
  1312. END;
  1313. END ResetUsedTemporaries;
  1314. *)
  1315. PROCEDURE Statement(x: SyntaxTree.Statement);
  1316. VAR use: VariableUse;
  1317. BEGIN
  1318. temporaries.GetUsage(use);
  1319. position := x.position;
  1320. IF emitLabels THEN Emit(LabelInstruction(x.position)) END;
  1321. IF commentPrintout # NIL THEN
  1322. commentPrintout.Statement(x);
  1323. dump.Ln;
  1324. (*dump.Update;*)
  1325. END;
  1326. x.Accept(SELF);
  1327. (*
  1328. CheckRegistersFree();
  1329. *)
  1330. (*ResetUsedTemporaries(use);*)
  1331. temporaries.SetUsage(use);
  1332. END Statement;
  1333. (* dereference op. If op is already a memory operand then use auxiliary register to dereference
  1334. result will be registered as a new use of operand, op is not released (op must be released by caller)
  1335. *)
  1336. PROCEDURE MakeMemory(VAR res: IntermediateCode.Operand; op: IntermediateCode.Operand; type: IntermediateCode.Type; offset: LONGINT);
  1337. BEGIN
  1338. IF op.mode = IntermediateCode.ModeMemory THEN
  1339. ReuseCopy(res,op);
  1340. ELSE
  1341. res := op;
  1342. UseIntermediateOperand(res);
  1343. END;
  1344. IntermediateCode.AddOffset(res,offset);
  1345. IntermediateCode.MakeMemory(res,type);
  1346. END MakeMemory;
  1347. PROCEDURE ToMemory(VAR res: IntermediateCode.Operand; type: IntermediateCode.Type; offset: LONGINT);
  1348. VAR mem: IntermediateCode.Operand;
  1349. BEGIN
  1350. MakeMemory(mem,res,type,offset);
  1351. ReleaseIntermediateOperand(res);
  1352. res := mem;
  1353. END ToMemory;
  1354. PROCEDURE LoadValue(VAR operand: Operand; type: SyntaxTree.Type);
  1355. VAR mem: IntermediateCode.Operand;
  1356. firstOp, lastOp, stepOp: IntermediateCode.Operand;
  1357. componentType: SyntaxTree.Type;
  1358. BEGIN
  1359. type := type.resolved;
  1360. IF operand.mode = ModeReference THEN
  1361. IF type IS SyntaxTree.RangeType THEN
  1362. MakeMemory(firstOp, operand.op, IntermediateCode.GetType(system, system.longintType), 0);
  1363. MakeMemory(lastOp, operand.op, IntermediateCode.GetType(system, system.longintType), ToMemoryUnits(system, system.SizeOf(system.longintType)));
  1364. MakeMemory(stepOp, operand.op, IntermediateCode.GetType(system, system.longintType), 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)));
  1365. ReleaseOperand(operand);
  1366. operand.op := firstOp;
  1367. operand.tag := lastOp;
  1368. operand.extra := stepOp;
  1369. ELSIF type IS SyntaxTree.ComplexType THEN
  1370. componentType := type(SyntaxTree.ComplexType).componentType;
  1371. ASSERT((componentType.SameType(system.realType)) OR (componentType.SameType(system.longrealType)));
  1372. MakeMemory(firstOp, operand.op, IntermediateCode.GetType(system, componentType), 0);
  1373. MakeMemory(lastOp, operand.op, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  1374. ReleaseOperand(operand);
  1375. operand.op := firstOp;
  1376. operand.tag := lastOp
  1377. ELSE
  1378. MakeMemory(mem,operand.op,IntermediateCode.GetType(system,type),0);
  1379. ReleaseIntermediateOperand(operand.op);
  1380. operand.op := mem;
  1381. END;
  1382. operand.mode := ModeValue;
  1383. END;
  1384. ASSERT(operand.mode = ModeValue);
  1385. END LoadValue;
  1386. PROCEDURE Evaluate(x: SyntaxTree.Expression; VAR op: Operand);
  1387. VAR prevConditional: BOOLEAN;
  1388. BEGIN
  1389. prevConditional := conditional;
  1390. conditional := FALSE;
  1391. InitOperand(result, ModeUndefined);
  1392. Expression(x);
  1393. op := result;
  1394. LoadValue(op,x.type.resolved);
  1395. conditional := prevConditional;
  1396. END Evaluate;
  1397. PROCEDURE Designate(x: SyntaxTree.Expression; VAR op: Operand);
  1398. VAR prevConditional: BOOLEAN;
  1399. BEGIN
  1400. prevConditional := conditional;
  1401. conditional := FALSE;
  1402. InitOperand(result,ModeUndefined);
  1403. Expression(x);
  1404. op := result;
  1405. (*
  1406. ASSERT((op.mode = ModeReference) OR (x.type.resolved IS SyntaxTree.NilType)); (* special case: winapi NIL parameter on references *)
  1407. *)
  1408. conditional := prevConditional;
  1409. END Designate;
  1410. PROCEDURE Condition(x: SyntaxTree.Expression; trueL,falseL: Label);
  1411. VAR prevTrue, prevFalse: Label; prevConditional: BOOLEAN;
  1412. BEGIN
  1413. ASSERT(trueL # NIL); ASSERT(falseL # NIL);
  1414. prevTrue := trueLabel; prevFalse := falseLabel; prevConditional := conditional;
  1415. conditional := TRUE;
  1416. trueLabel := trueL; falseLabel := falseL;
  1417. Expression(x);
  1418. trueL := trueLabel; falseL := falseLabel;
  1419. trueLabel := prevTrue;falseLabel := prevFalse;conditional := prevConditional;
  1420. END Condition;
  1421. PROCEDURE NewRegisterOperand(type: IntermediateCode.Type): IntermediateCode.Operand;
  1422. VAR op: IntermediateCode.Operand; reg: LONGINT;
  1423. BEGIN
  1424. reg := AcquireRegister(type,IntermediateCode.GeneralPurposeRegister);
  1425. IntermediateCode.InitRegister(op, type, IntermediateCode.GeneralPurposeRegister,reg);
  1426. RETURN op
  1427. END NewRegisterOperand;
  1428. PROCEDURE UnuseRegister(register: LONGINT);
  1429. BEGIN
  1430. IF (register > 0) THEN
  1431. register := registerUsageCount.Map(register);
  1432. registerUsageCount.DecUse(register);
  1433. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1434. dump.String("unuse register "); dump.Int(register,1); dump.String(": ");dump.Int(registerUsageCount.Use(register),1); dump.Ln; dump.Update;
  1435. END;
  1436. IF registerUsageCount.Use(register)=0 THEN
  1437. IF ~RemoveRegisterEntry(usedRegisters,register) THEN
  1438. Warning(position, "register cannot be removed");
  1439. END;
  1440. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1441. dump.String("remove register from usedRegisters"); dump.Ln; dump.Update;
  1442. END;
  1443. ELSIF registerUsageCount.Use(register)<0 THEN
  1444. Warning(position, "register removed too often");
  1445. IF dump # NIL THEN
  1446. dump.String("register removed too often"); dump.Ln; dump.Update;
  1447. END;
  1448. D.TraceBack;
  1449. END;
  1450. END;
  1451. END UnuseRegister;
  1452. PROCEDURE UseRegister(register: LONGINT);
  1453. BEGIN
  1454. IF (register > 0) THEN
  1455. register := registerUsageCount.Map(register);
  1456. registerUsageCount.IncUse(register);
  1457. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1458. dump.String("use register "); dump.Int(register,1); dump.String(": ");dump.Int(registerUsageCount.Use(register),1); dump.Ln; dump.Update;
  1459. END;
  1460. IF registerUsageCount.Use(register)=1 THEN
  1461. AddRegisterEntry(usedRegisters,register, registerUsageCount.used[register].class, registerUsageCount.used[register].type);
  1462. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1463. dump.String("add register to usedRegisters"); dump.Ln; dump.Update;
  1464. END;
  1465. END;
  1466. END;
  1467. END UseRegister;
  1468. PROCEDURE ReleaseIntermediateOperand(CONST op: IntermediateCode.Operand);
  1469. BEGIN
  1470. UnuseRegister(op.register)
  1471. END ReleaseIntermediateOperand;
  1472. PROCEDURE UseIntermediateOperand(CONST op: IntermediateCode.Operand);
  1473. BEGIN
  1474. UseRegister(op.register)
  1475. END UseIntermediateOperand;
  1476. PROCEDURE ReleaseOperand(CONST op: Operand);
  1477. BEGIN
  1478. UnuseRegister(op.op.register);
  1479. UnuseRegister(op.tag.register);
  1480. UnuseRegister(op.extra.register);
  1481. END ReleaseOperand;
  1482. (* save registers marked in array "markedRegisters" to the stack
  1483. remove entries from array "markedRegisters" and save to array "saved" (=> recursion possible)
  1484. *)
  1485. PROCEDURE SaveRegisters();
  1486. VAR op: IntermediateCode.Operand; entry: RegisterEntry; type: IntermediateCode.Type;
  1487. BEGIN
  1488. entry := usedRegisters;
  1489. WHILE entry # NIL DO
  1490. type := registerUsageCount.used[entry.register].type;
  1491. IntermediateCode.InitRegister(op,entry.type,entry.registerClass, entry.register);
  1492. Emit(Push(position,op));
  1493. entry := entry.next;
  1494. END;
  1495. END SaveRegisters;
  1496. PROCEDURE ReleaseUsedRegisters(VAR saved: RegisterEntry);
  1497. BEGIN
  1498. saved := usedRegisters;
  1499. usedRegisters := NIL;
  1500. END ReleaseUsedRegisters;
  1501. (** remove parameter registers from used queue *)
  1502. PROCEDURE ReleaseParameterRegisters;
  1503. VAR entry,prev,next: RegisterEntry;
  1504. BEGIN
  1505. entry := usedRegisters; prev := NIL; usedRegisters := NIL;
  1506. WHILE entry # NIL DO
  1507. next := entry.next;
  1508. IF entry.registerClass.class = IntermediateCode.Parameter THEN
  1509. registerUsageCount.DecUse(entry.register);
  1510. ASSERT(registerUsageCount.Use(entry.register)=0);
  1511. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1512. dump.String("unuse register "); dump.Int(entry.register,1); dump.Ln; dump.Update;
  1513. END;
  1514. ELSIF prev = NIL THEN
  1515. usedRegisters := entry; entry.prev := NIL; entry.next := NIL; prev := entry;
  1516. ELSE
  1517. prev.next := entry; entry.prev := prev; entry.next := NIL; prev:= entry;
  1518. END;
  1519. entry := next;
  1520. END;
  1521. END ReleaseParameterRegisters;
  1522. (* restore registers from array saved and re-enter into array markedRegisters (recursion possible) *)
  1523. PROCEDURE RestoreRegisters(CONST saved: RegisterEntry);
  1524. VAR op: IntermediateCode.Operand; entry,prev: RegisterEntry; type: IntermediateCode.Type; class: IntermediateCode.RegisterClass;
  1525. BEGIN
  1526. entry := saved;
  1527. WHILE (entry # NIL) DO prev := entry; entry := entry.next END;
  1528. entry := prev;
  1529. WHILE entry # NIL DO
  1530. prev := entry.prev;
  1531. type := entry.type;
  1532. class := entry.registerClass;
  1533. IntermediateCode.InitRegister(op,type,class,entry.register);
  1534. (*
  1535. new := registerUsageCount.Next(type,class);
  1536. registerUsageCount.Remap(entry.register,new);
  1537. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1538. dump.String("remap register "); dump.Int(entry.register,1);
  1539. dump.String("to "); dump.Int(new,1);
  1540. dump.String("with count "); dump.Int(registerUsageCount.Use(new),1); dump.Ln; dump.Update;
  1541. END;
  1542. entry.register := new;
  1543. *)
  1544. Emit(Pop(position,op));
  1545. AddRegisterEntry(usedRegisters,entry.register,entry.registerClass, entry.type);
  1546. entry := prev;
  1547. END;
  1548. (*
  1549. usedRegisters := saved;
  1550. *)
  1551. END RestoreRegisters;
  1552. PROCEDURE CheckRegistersFree;
  1553. VAR r: RegisterEntry; warning: ARRAY 128 OF CHAR; i: LONGINT;
  1554. BEGIN
  1555. IF usedRegisters # NIL THEN
  1556. r := usedRegisters;
  1557. WHILE r # NIL DO
  1558. warning := "register ";
  1559. Strings.AppendInt(warning, r.register);
  1560. Strings.Append(warning, " not released.");
  1561. Warning(position,warning);
  1562. r := r .next;
  1563. END;
  1564. END;
  1565. FOR i := 0 TO registerUsageCount.count-1 DO
  1566. IF registerUsageCount.used[i].count < 0 THEN
  1567. warning := "register ";
  1568. Strings.AppendInt(warning, i);
  1569. Strings.Append(warning, " unused too often.");
  1570. Warning(position,warning);
  1571. ELSIF registerUsageCount.used[i].count > 0 THEN (* should always coincide with cases above *)
  1572. warning := "register ";
  1573. Strings.AppendInt(warning, i);
  1574. Strings.Append(warning, " not unused often enough.");
  1575. Warning(position,warning);
  1576. END;
  1577. END;
  1578. END CheckRegistersFree;
  1579. (* Reuse2: reuse src1 or src2 for ongoing computation if src1 or src2, respectively, is a register.
  1580. Otherwise allocate a new register.
  1581. Does NOT necessarily keep the content of src1 or src2 in result! *)
  1582. PROCEDURE Reuse2(VAR result: IntermediateCode.Operand; src1,src2: IntermediateCode.Operand);
  1583. BEGIN
  1584. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1585. UseIntermediateOperand(result);
  1586. ELSIF ReusableRegister(src2) THEN IntermediateCode.InitRegister(result,src2.type,src2.registerClass, src2.register);
  1587. UseIntermediateOperand(result);
  1588. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass,AcquireRegister(src1.type, src1.registerClass));
  1589. END;
  1590. END Reuse2;
  1591. (* Reuse2a: reuse src1 or src2 for ongoing computation if src1 or src2, respectively, is a register.
  1592. Otherwise check if an alternative destination is available. If so, then take the alternative (which is not necessarily a register).
  1593. If not then allocate a new register.
  1594. Does NOT necessarily keep the content of src1 or src2 in result!
  1595. *)
  1596. PROCEDURE Reuse2a(VAR result: IntermediateCode.Operand; src1,src2: IntermediateCode.Operand; VAR alternative: IntermediateCode.Operand);
  1597. BEGIN
  1598. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1599. UseIntermediateOperand(result);
  1600. ELSIF ReusableRegister(src2) THEN IntermediateCode.InitRegister(result,src2.type,src2.registerClass, src2.register);
  1601. UseIntermediateOperand(result);
  1602. ELSIF alternative.mode # IntermediateCode.Undefined THEN
  1603. result := alternative; alternative := emptyOperand;
  1604. UseIntermediateOperand(result);
  1605. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1606. END;
  1607. END Reuse2a;
  1608. (* like reuse2 but only one source *)
  1609. PROCEDURE Reuse1(VAR result: IntermediateCode.Operand; src1: IntermediateCode.Operand);
  1610. BEGIN
  1611. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1612. UseIntermediateOperand(result);
  1613. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1614. END;
  1615. END Reuse1;
  1616. (* like reuse2a but only one source *)
  1617. PROCEDURE Reuse1a(VAR result: IntermediateCode.Operand; src1: IntermediateCode.Operand; VAR alternative: IntermediateCode.Operand);
  1618. BEGIN
  1619. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1620. UseIntermediateOperand(result);
  1621. ELSIF alternative.mode # IntermediateCode.Undefined THEN result := alternative; alternative := emptyOperand;
  1622. UseIntermediateOperand(result);
  1623. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1624. END;
  1625. END Reuse1a;
  1626. (* like reuse1 but guarantees that content of src1 is in result *)
  1627. PROCEDURE ReuseCopy(VAR result: IntermediateCode.Operand; src1: IntermediateCode.Operand);
  1628. BEGIN
  1629. IF ReusableRegister(src1) THEN
  1630. IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1631. ASSERT((src1.mode = IntermediateCode.ModeRegister) & (src1.offset = 0));
  1632. UseIntermediateOperand(result);
  1633. ELSE
  1634. IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1635. Emit(Mov(position,result,src1));
  1636. END
  1637. END ReuseCopy;
  1638. PROCEDURE TransferToRegister(VAR result: IntermediateCode.Operand; src: IntermediateCode.Operand);
  1639. BEGIN
  1640. IF ReusableRegister(src) THEN
  1641. IntermediateCode.InitRegister(result,src.type,src.registerClass, src.register);
  1642. ELSE
  1643. IntermediateCode.InitRegister(result,src.type,src.registerClass, AcquireRegister(src.type, src.registerClass));
  1644. Emit(Mov(position,result,src));
  1645. ReleaseIntermediateOperand(src);
  1646. END
  1647. END TransferToRegister;
  1648. (** labels and branches **)
  1649. PROCEDURE NewLabel(): Label;
  1650. VAR label: Label;
  1651. BEGIN
  1652. NEW(label,section); RETURN label;
  1653. END NewLabel;
  1654. PROCEDURE SetLabel(label: Label);
  1655. BEGIN label.Resolve(section.pc);
  1656. END SetLabel;
  1657. PROCEDURE LabelOperand(label: Label): IntermediateCode.Operand;
  1658. BEGIN
  1659. ASSERT(label # NIL);
  1660. IF label.pc < 0 THEN (* label not yet set *)
  1661. label.AddFixup(section.pc);
  1662. END;
  1663. RETURN IntermediateCode.Address(addressType,label.section.name,GetFingerprint(label.section.symbol), label.pc);
  1664. END LabelOperand;
  1665. PROCEDURE BrL(label: Label);
  1666. BEGIN
  1667. Emit(Br(position,LabelOperand(label)));
  1668. END BrL;
  1669. PROCEDURE BrgeL(label: Label; left,right: IntermediateCode.Operand);
  1670. BEGIN
  1671. Emit(Brge(position,LabelOperand(label),left,right));
  1672. END BrgeL;
  1673. PROCEDURE BrltL(label: Label; left,right: IntermediateCode.Operand);
  1674. BEGIN
  1675. Emit(Brlt(position,LabelOperand(label),left,right));
  1676. END BrltL;
  1677. PROCEDURE BreqL(label: Label; left,right: IntermediateCode.Operand);
  1678. BEGIN
  1679. Emit(Breq(position,LabelOperand(label),left,right));
  1680. END BreqL;
  1681. PROCEDURE BrneL(label: Label; left,right: IntermediateCode.Operand);
  1682. BEGIN
  1683. Emit(Brne(position,LabelOperand(label),left,right));
  1684. END BrneL;
  1685. PROCEDURE Convert(VAR operand: IntermediateCode.Operand; type: IntermediateCode.Type);
  1686. VAR new: IntermediateCode.Operand;
  1687. BEGIN
  1688. IF Trace THEN TraceEnter("Convert") END;
  1689. IF IntermediateCode.TypeEquals(type,operand.type) THEN (* nothing to be done *)
  1690. ELSIF (operand.mode = IntermediateCode.ModeRegister) THEN
  1691. IF (type.sizeInBits = operand.type.sizeInBits) & (type.form IN IntermediateCode.Integer) & (operand.type.form IN IntermediateCode.Integer)
  1692. & (operand.offset = 0)
  1693. THEN
  1694. IntermediateCode.InitRegister(new,type,IntermediateCode.GeneralPurposeRegister,operand.register);
  1695. Emit(Conv(position,new,operand));
  1696. ELSE
  1697. IntermediateCode.InitRegister(new,type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(type,IntermediateCode.GeneralPurposeRegister));
  1698. Emit(Conv(position,new,operand));
  1699. ReleaseIntermediateOperand(operand);
  1700. END;
  1701. operand := new;
  1702. 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
  1703. IntermediateCode.InitImmediate(operand,type,operand.intValue);
  1704. ELSE
  1705. IntermediateCode.InitRegister(new,type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(type,IntermediateCode.GeneralPurposeRegister));
  1706. Emit(Conv(position,new,operand));
  1707. ReleaseIntermediateOperand(operand);
  1708. operand := new;
  1709. END;
  1710. IF Trace THEN TraceExit("Convert") END;
  1711. END Convert;
  1712. PROCEDURE TrapC(br: ConditionalBranch; left,right:IntermediateCode.Operand; trapNo: LONGINT);
  1713. VAR exit: Label;
  1714. BEGIN
  1715. Assert((left.mode # IntermediateCode.ModeImmediate) OR (right.mode # IntermediateCode.ModeImmediate),"trap emission with two immediates");
  1716. exit := NewLabel();
  1717. br(exit,left,right);
  1718. EmitTrap(position,trapNo);
  1719. SetLabel(exit);
  1720. END TrapC;
  1721. (** expressions *)
  1722. (** emit necessary runtime check for set elements **)
  1723. PROCEDURE CheckSetElement(o: IntermediateCode.Operand);
  1724. VAR max: IntermediateCode.Operand;
  1725. BEGIN
  1726. IF isUnchecked THEN RETURN END;
  1727. IF o.mode # IntermediateCode.ModeImmediate THEN (* otherwise it's the job of the checker *)
  1728. IntermediateCode.InitImmediate(max, setType, setType.sizeInBits (* number of bits in set *) -1);
  1729. TrapC(BrgeL, max, o, SetElementTrap);
  1730. END;
  1731. END CheckSetElement;
  1732. (** the set that a range represents **)
  1733. PROCEDURE SetFromRange(x: SyntaxTree.RangeExpression): IntermediateCode.Operand;
  1734. VAR
  1735. operand: Operand;
  1736. resultingSet, temp, size, allBits, noBits, one: IntermediateCode.Operand;
  1737. BEGIN
  1738. ASSERT((x.first # NIL) & (x.last # NIL)); (* ensured by the checker *)
  1739. allBits := IntermediateCode.Immediate(setType, -1); (* bit mask 111...11111 *)
  1740. noBits := IntermediateCode.Immediate(setType, 0); (* bit mask 0...0 *)
  1741. one := IntermediateCode.Immediate(setType, 1);
  1742. Evaluate(x, operand);
  1743. Convert(operand.op, setType);
  1744. Convert(operand.tag, setType);
  1745. CheckSetElement(operand.op);
  1746. CheckSetElement(operand.tag);
  1747. (* create mask for lower bound
  1748. i.e. shift 11111111 to the left by the value of the lower bound
  1749. *)
  1750. Reuse1(temp, operand.op);
  1751. Emit(Shl(position,temp, allBits, operand.op));
  1752. ReleaseIntermediateOperand(operand.op);
  1753. operand.op := temp;
  1754. (* create mask for upper bound
  1755. i.e. shift 11111111 to the right by the difference between the
  1756. upper bound and the maximum number of set elements
  1757. *)
  1758. IF (operand.tag.mode = IntermediateCode.ModeImmediate) & (operand.tag.symbol.name = "") THEN
  1759. IntermediateCode.InitImmediate(operand.tag, operand.tag.type, operand.op.type.sizeInBits - 1- operand.tag.intValue);
  1760. Reuse1(temp, operand.tag);
  1761. ELSE
  1762. Reuse1(temp, operand.tag);
  1763. IntermediateCode.InitImmediate(size, operand.tag.type, operand.op.type.sizeInBits - 1);
  1764. Emit(Sub(position,temp, size, operand.tag));
  1765. END;
  1766. Emit(Shr(position,temp, allBits, operand.tag));
  1767. ReleaseIntermediateOperand(operand.tag);
  1768. operand.tag := temp;
  1769. Reuse2(resultingSet, operand.op, operand.tag);
  1770. (* intersect the two masks *)
  1771. Emit(And(position,resultingSet, operand.op, operand.tag));
  1772. ReleaseOperand(operand);
  1773. RETURN resultingSet
  1774. END SetFromRange;
  1775. PROCEDURE VisitSet(x: SyntaxTree.Set);
  1776. VAR
  1777. res, operand: Operand;
  1778. temp, one, noBits, dest: IntermediateCode.Operand;
  1779. expression: SyntaxTree.Expression;
  1780. i: LONGINT;
  1781. BEGIN
  1782. IF Trace THEN TraceEnter("VisitSet") END;
  1783. dest := destination;
  1784. destination := emptyOperand;
  1785. noBits := IntermediateCode.Immediate(setType, 0);
  1786. one := IntermediateCode.Immediate(setType, 1);
  1787. (* start off with the empty set *)
  1788. InitOperand(res, ModeValue);
  1789. IntermediateCode.InitRegister(res.op, setType, IntermediateCode.GeneralPurposeRegister, AcquireRegister(setType, IntermediateCode.GeneralPurposeRegister));
  1790. Emit(Mov(position,res.op, noBits));
  1791. FOR i := 0 TO x.elements.Length() - 1 DO
  1792. expression := x.elements.GetExpression(i);
  1793. IF expression IS SyntaxTree.RangeExpression THEN
  1794. (* range of set elements *)
  1795. temp := SetFromRange(expression(SyntaxTree.RangeExpression));
  1796. ASSERT(IntermediateCode.TypeEquals(setType, temp.type));
  1797. Emit(Or(position,res.op, res.op, temp)); (* unify subset with current set *)
  1798. ReleaseIntermediateOperand(temp)
  1799. ELSE
  1800. (* singelton element *)
  1801. Evaluate(expression, operand);
  1802. Convert(operand.op, setType);
  1803. CheckSetElement(operand.op);
  1804. (* create subset containing single element *)
  1805. Reuse1(temp, operand.op);
  1806. Emit(Shl(position,temp, one, operand.op));
  1807. ReleaseOperand(operand);
  1808. Emit(Or(position,res.op, res.op, temp)); (* unify subset with current set *)
  1809. ReleaseIntermediateOperand(temp);
  1810. END
  1811. END;
  1812. result := res;
  1813. destination := dest;
  1814. IF Trace THEN TraceExit("VisitSet") END;
  1815. END VisitSet;
  1816. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1817. VAR variable: SyntaxTree.Variable; index: SyntaxTree.IndexDesignator; dim: LONGINT;
  1818. designator: SyntaxTree.Designator; i: LONGINT; element: SyntaxTree.IntegerValue;
  1819. PROCEDURE RecursiveAssignment(x: SyntaxTree.MathArrayExpression; dim: LONGINT);
  1820. VAR numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1821. element: SyntaxTree.IntegerValue;
  1822. BEGIN
  1823. numberElements := x.elements.Length();
  1824. expression := index.parameters.GetExpression(dim);
  1825. element := expression(SyntaxTree.IntegerValue);
  1826. FOR i := 0 TO numberElements-1 DO
  1827. expression := x.elements.GetExpression(i);
  1828. element.SetValue(i);
  1829. IF expression IS SyntaxTree.MathArrayExpression THEN
  1830. RecursiveAssignment(expression(SyntaxTree.MathArrayExpression),dim+1);
  1831. ELSE
  1832. Assign(index,expression);
  1833. END;
  1834. END;
  1835. END RecursiveAssignment;
  1836. BEGIN
  1837. variable := GetTemporaryVariable(x.type, FALSE);
  1838. designator := SyntaxTree.NewSymbolDesignator(-1,NIL,variable);
  1839. designator.SetType(variable.type);
  1840. dim := SemanticChecker.Dimension(x.type,{SyntaxTree.Static});
  1841. index := SyntaxTree.NewIndexDesignator(x.position,designator);
  1842. FOR i := 0 TO dim-1 DO
  1843. element := SyntaxTree.NewIntegerValue(x.position,0);
  1844. element.SetType(system.longintType);
  1845. index.parameters.AddExpression(element);
  1846. END;
  1847. index.SetType(SemanticChecker.ArrayBase(x.type,dim));
  1848. RecursiveAssignment(x,0);
  1849. Expression(designator);
  1850. END VisitMathArrayExpression;
  1851. PROCEDURE VisitUnaryExpression(x: SyntaxTree.UnaryExpression);
  1852. VAR type,t0: SyntaxTree.Type; operand: Operand; dest: IntermediateCode.Operand;
  1853. BEGIN
  1854. IF Trace THEN TraceEnter("VisitUnaryExpression") END;
  1855. dest := destination; destination := emptyOperand;
  1856. IF x.operator = Scanner.Not THEN
  1857. IF conditional THEN
  1858. Condition(x.left,falseLabel,trueLabel)
  1859. ELSE
  1860. Evaluate(x.left,operand);
  1861. InitOperand(result,ModeValue);
  1862. Reuse1a(result.op,operand.op,dest);
  1863. Emit(Xor(position,result.op,operand.op,true));
  1864. ReleaseOperand(operand);
  1865. END;
  1866. ELSIF x.operator = Scanner.Minus THEN
  1867. Evaluate(x.left,operand);
  1868. InitOperand(result,ModeValue);
  1869. Reuse1a(result.op,operand.op,dest);
  1870. type := x.left.type.resolved;
  1871. IF type IS SyntaxTree.SetType THEN
  1872. Emit(Not(position,result.op,operand.op));
  1873. ELSIF (type IS SyntaxTree.ComplexType) THEN
  1874. Reuse1(result.tag,operand.tag);
  1875. Emit(Neg(position,result.op,operand.op)); (* real part *)
  1876. Emit(Neg(position,result.tag,operand.tag)) (* imaginary part *)
  1877. ELSIF (type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.SizeType) OR (type IS SyntaxTree.AddressType) THEN
  1878. Emit(Neg(position,result.op,operand.op));
  1879. ELSE HALT(200)
  1880. END;
  1881. ReleaseOperand(operand);
  1882. ELSIF x.operator = Scanner.Address THEN
  1883. Designate(x.left,operand);
  1884. operand.mode := ModeValue;
  1885. t0 := x.left.type.resolved;
  1886. IF (t0 IS SyntaxTree.MathArrayType) & (t0(SyntaxTree.MathArrayType).form = SyntaxTree.Open) THEN
  1887. ReleaseIntermediateOperand(operand.op);
  1888. operand.op := operand.tag;
  1889. IntermediateCode.InitOperand(operand.tag);
  1890. END;
  1891. Convert(operand.op,IntermediateCode.GetType(system,x.type));
  1892. result := operand;
  1893. ELSE HALT(100)
  1894. END;
  1895. destination := dest;
  1896. IF Trace THEN TraceExit("VisitUnaryExpression") END;
  1897. END VisitUnaryExpression;
  1898. (* test if e is of type type, side effect: result of evaluation of e stays in the operand *)
  1899. PROCEDURE TypeTest(tag: IntermediateCode.Operand; type: SyntaxTree.Type; trueL,falseL: Label);
  1900. VAR left,right: IntermediateCode.Operand; level,offset: LONGINT; repeatL: Label; originalType: SyntaxTree.Type;
  1901. BEGIN
  1902. type := type.resolved;
  1903. originalType := type;
  1904. IF type IS SyntaxTree.PointerType THEN
  1905. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  1906. END;
  1907. IF type IS SyntaxTree.ObjectType THEN
  1908. BrL(trueL);
  1909. ELSE
  1910. ASSERT(type IS SyntaxTree.RecordType);
  1911. (*
  1912. IntermediateCode.MakeMemory(tag,addressType); (*! already done during generation *)
  1913. *)
  1914. ReuseCopy(left,tag);
  1915. right := TypeDescriptorAdr(type);
  1916. IF ~newObjectFile THEN
  1917. IntermediateCode.MakeMemory(right,addressType);
  1918. END;
  1919. IF backend.cooperative THEN
  1920. repeatL := NewLabel();
  1921. IF (originalType IS SyntaxTree.PointerType) & ~type(SyntaxTree.RecordType).isObject THEN
  1922. Emit(Mov(position,left,IntermediateCode.Memory(addressType,left,ToMemoryUnits(system,addressType.sizeInBits))));
  1923. END;
  1924. SetLabel(repeatL);
  1925. BreqL(trueL,left,right);
  1926. Emit(Mov(position,left,IntermediateCode.Memory(addressType,left,0)));
  1927. BrneL(repeatL,left,nil);
  1928. ELSE
  1929. level := type(SyntaxTree.RecordType).Level();
  1930. (* get type desc tag of level relative to base tag *)
  1931. offset := (meta.BaseTypesTableOffset - level) * addressType.sizeInBits;
  1932. IntermediateCode.AddOffset(left,ToMemoryUnits(system,offset));
  1933. IntermediateCode.MakeMemory(left,addressType);
  1934. BreqL(trueL,left,right);
  1935. END;
  1936. ReleaseIntermediateOperand(left); ReleaseIntermediateOperand(right);
  1937. BrL(falseL);
  1938. END;
  1939. END TypeTest;
  1940. PROCEDURE Error(position: LONGINT; CONST s: ARRAY OF CHAR);
  1941. BEGIN
  1942. backend.Error(module.module.sourceName,position,Diagnostics.Invalid,s);
  1943. IF dump # NIL THEN
  1944. dump.String(s); dump.Ln;
  1945. END;
  1946. END Error;
  1947. PROCEDURE Warning(position: LONGINT; CONST s: ARRAY OF CHAR);
  1948. BEGIN
  1949. backend.diagnostics.Warning(module.module.sourceName,position,Diagnostics.Invalid,s);
  1950. IF dump # NIL THEN
  1951. dump.String(s); dump.Ln; dump.Update;
  1952. END;
  1953. END Warning;
  1954. PROCEDURE CreateTraceModuleMethod(mod: SyntaxTree.Module);
  1955. VAR name: Basic.SectionName; pooledName: Basic.SegmentedName; previousSection: IntermediateCode.Section;
  1956. VAR variable: SyntaxTree.Variable; register,op: IntermediateCode.Operand;
  1957. operand:Operand;
  1958. BEGIN
  1959. previousSection := section;
  1960. Global.GetModuleName(mod,name);
  1961. Strings.Append(name,".@Trace");
  1962. Basic.ToSegmentedName(name, pooledName);
  1963. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, pooledName,NIL,TRUE);
  1964. IF dump # NIL THEN dump := section.comments END;
  1965. variable := mod.moduleScope.firstVariable;
  1966. WHILE variable # NIL DO
  1967. IF ~variable.untraced & variable.type.resolved.hasPointers THEN
  1968. Symbol(variable, operand);
  1969. register := operand.op;
  1970. CallTraceMethod(register, variable.type);
  1971. ReleaseIntermediateOperand(register);
  1972. END;
  1973. variable := variable.nextVariable;
  1974. END;
  1975. Basic.ToSegmentedName ("Modules.Module.@Trace",pooledName);
  1976. IntermediateCode.InitAddress(op, addressType, pooledName , 0, 0);
  1977. Emit(Br(position,op));
  1978. INC(statCoopTraceModule, section.pc);
  1979. section := previousSection;
  1980. IF dump # NIL THEN dump := section.comments END;
  1981. END CreateTraceModuleMethod;
  1982. PROCEDURE CallAssignPointer(CONST dst (* address *) , src (* value *): IntermediateCode.Operand);
  1983. BEGIN
  1984. Emit (Push(position, dst));
  1985. Emit (Push(position, src));
  1986. CallThis(position,"GarbageCollector","Assign", 2);
  1987. END CallAssignPointer;
  1988. PROCEDURE CallAssignMethod(CONST dst (* address *) , src (* address *) : IntermediateCode.Operand; type: SyntaxTree.Type);
  1989. VAR name: Basic.SegmentedName; size: LONGINT; base: SyntaxTree.Type; op: IntermediateCode.Operand;
  1990. BEGIN
  1991. IF SemanticChecker.IsPointerType (type) THEN
  1992. CallAssignPointer(dst, IntermediateCode.Memory (addressType,src,0));
  1993. ELSIF type.IsRecordType() THEN
  1994. Emit (Push(position,dst));
  1995. Emit (Push(position,src));
  1996. GetRecordTypeName (type.resolved(SyntaxTree.RecordType),name);
  1997. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  1998. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  1999. Emit(Call(position,op, ToMemoryUnits(system, 2*system.addressSize)));
  2000. ELSIF IsStaticArray(type) THEN
  2001. size := StaticArrayNumElements(type);
  2002. base := StaticArrayBaseType(type);
  2003. IF base.IsRecordType() THEN
  2004. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2005. Emit (Push(position, dst));
  2006. Emit (Push(position, src));
  2007. GetRecordTypeName (base.resolved(SyntaxTree.RecordType),name);
  2008. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2009. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2010. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2011. Emit(Call(position,op,ToMemoryUnits(system, 3*system.addressSize)));
  2012. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  2013. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2014. Emit (Push(position, dst));
  2015. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2016. Emit (Push(position, src));
  2017. CallThis(position,"GarbageCollector","AssignDelegateArray", 4);
  2018. ELSE
  2019. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2020. Emit (Push(position, dst));
  2021. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2022. Emit (Push(position, src));
  2023. CallThis(position,"GarbageCollector","AssignPointerArray", 4);
  2024. ASSERT(StaticArrayBaseType(type).IsPointer());
  2025. END;
  2026. ELSIF type.resolved IS SyntaxTree.ProcedureType THEN
  2027. ASSERT(type.resolved(SyntaxTree.ProcedureType).isDelegate);
  2028. Emit (Push(position, dst));
  2029. Emit (Push(position, src));
  2030. CallThis(position,"GarbageCollector","AssignDelegate", 2);
  2031. ELSE HALT(100); (* missing ? *)
  2032. END;
  2033. END CallAssignMethod;
  2034. PROCEDURE CreateAssignProcedure (recordType: SyntaxTree.RecordType);
  2035. VAR name: Basic.SegmentedName;
  2036. VAR variable: SyntaxTree.Variable; src, dst, op, ofs: IntermediateCode.Operand; recordBase: SyntaxTree.RecordType;
  2037. parameter1, parameter2, parameter0: IntermediateCode.Operand; label: Label;
  2038. context: Context;
  2039. BEGIN
  2040. parameter0 (* len *) := IntermediateCode.Memory(sizeType,sp,ToMemoryUnits(system,3*addressType.sizeInBits));
  2041. parameter1 (* dest *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,2*addressType.sizeInBits));
  2042. parameter2 (* src *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,1*addressType.sizeInBits));
  2043. GetRecordTypeName (recordType,name);
  2044. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2045. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE));
  2046. IF dump # NIL THEN dump := section.comments END;
  2047. variable := recordType.recordScope.firstVariable;
  2048. WHILE variable # NIL DO
  2049. IF variable.NeedsTrace() THEN
  2050. dst := NewRegisterOperand (addressType);
  2051. src := NewRegisterOperand (addressType);
  2052. Emit (Mov(position, dst, parameter1));
  2053. Emit (Mov(position, src, parameter2));
  2054. IntermediateCode.AddOffset(dst,ToMemoryUnits(system,variable.offsetInBits));
  2055. IntermediateCode.AddOffset(src,ToMemoryUnits(system,variable.offsetInBits));
  2056. CallAssignMethod(dst, src, variable.type);
  2057. ReleaseIntermediateOperand(src);
  2058. ReleaseIntermediateOperand(dst);
  2059. END;
  2060. variable := variable.nextVariable;
  2061. END;
  2062. recordBase := recordType.GetBaseRecord();
  2063. IF (recordBase # NIL) & recordBase.NeedsTrace() THEN
  2064. GetRecordTypeName (recordBase,name);
  2065. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2066. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2067. Emit(Br(position,op));
  2068. ELSE
  2069. Emit(Exit(position,0,0));
  2070. END;
  2071. IF ~recordType.isObject THEN
  2072. GetRecordTypeName (recordType,name);
  2073. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2074. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2075. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE);
  2076. NEW(registerUsageCount);
  2077. usedRegisters := NIL;
  2078. dst := NewRegisterOperand (addressType);
  2079. src := NewRegisterOperand (addressType);
  2080. ofs := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(recordType)));
  2081. Emit(Mov(position, dst, parameter1));
  2082. Emit(Mov(position, src, parameter2));
  2083. label := NewLabel();
  2084. SetLabel(label);
  2085. Emit(Push(position, dst));
  2086. Emit(Push(position, src));
  2087. GetRecordTypeName (recordType,name);
  2088. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2089. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2090. Emit(Call(position, op, 0));
  2091. Emit(Pop(position, src));
  2092. Emit(Pop(position, dst));
  2093. Emit(Add(position, dst, dst, ofs));
  2094. Emit(Add(position, src, src, ofs));
  2095. Emit(Sub(position, parameter0, parameter0, IntermediateCode.Immediate(sizeType, 1)));
  2096. BrneL(label, parameter0, IntermediateCode.Immediate(sizeType, 0));
  2097. Emit(Exit(position,0,0));
  2098. END;
  2099. INC(statCoopAssignProcedure, section.pc);
  2100. ReturnToContext(context);
  2101. IF dump # NIL THEN dump := section.comments END;
  2102. END CreateAssignProcedure;
  2103. PROCEDURE CallTraceMethod(CONST register: IntermediateCode.Operand; type: SyntaxTree.Type);
  2104. VAR name: Basic.SegmentedName; op: IntermediateCode.Operand; size: LONGINT; base: SyntaxTree.Type; skip: Label;
  2105. BEGIN
  2106. IF IsUnsafePointer (type) THEN
  2107. skip := NewLabel();
  2108. IntermediateCode.InitRegister(op, addressType, IntermediateCode.GeneralPurposeRegister, register.register);
  2109. Emit (Mov (position, op, IntermediateCode.Memory (addressType,register,0)));
  2110. BreqL (skip, op, nil);
  2111. CallTraceMethod (op,type.resolved(SyntaxTree.PointerType).pointerBase);
  2112. SetLabel (skip);
  2113. ELSIF SemanticChecker.IsPointerType (type) THEN
  2114. Emit (Push(position, IntermediateCode.Memory (addressType,register,0)));
  2115. CallThis(position,"GarbageCollector","Mark", 1);
  2116. ELSIF type.IsRecordType() THEN
  2117. Emit (Push(position,register));
  2118. GetRecordTypeName (type.resolved(SyntaxTree.RecordType),name);
  2119. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2120. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2121. Emit(Call(position,op, ToMemoryUnits(system, system.addressSize)));
  2122. ELSIF IsStaticArray(type) THEN
  2123. size := StaticArrayNumElements(type);
  2124. base := StaticArrayBaseType(type);
  2125. IF base.IsRecordType() THEN
  2126. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2127. Emit (Push(position, register));
  2128. GetRecordTypeName (base.resolved(SyntaxTree.RecordType), name);
  2129. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2130. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2131. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2132. Emit(Call(position, op, ToMemoryUnits(system, system.addressSize*2)));
  2133. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  2134. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2135. Emit (Push(position, register));
  2136. CallThis(position,"GarbageCollector","MarkDelegateArray", 2);
  2137. ELSE
  2138. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2139. Emit (Push(position, register));
  2140. CallThis(position,"GarbageCollector","MarkPointerArray", 2);
  2141. ASSERT(base.IsPointer());
  2142. END;
  2143. ELSIF type.resolved IS SyntaxTree.ProcedureType THEN
  2144. Emit (Push(position, IntermediateCode.Memory (addressType,register,ToMemoryUnits(system,addressType.sizeInBits))));
  2145. CallThis(position,"GarbageCollector","Mark", 1);
  2146. ELSE HALT(100); (* missing ? *)
  2147. END;
  2148. END CallTraceMethod;
  2149. PROCEDURE CreateTraceMethod (recordType: SyntaxTree.RecordType);
  2150. VAR name: Basic.SegmentedName; previousSection: IntermediateCode.Section;
  2151. VAR variable: SyntaxTree.Variable; register,op,ofs: IntermediateCode.Operand; recordBase: SyntaxTree.RecordType;
  2152. parameter0, parameter1: IntermediateCode.Operand; label: Label; context: Context;
  2153. BEGIN
  2154. previousSection := section;
  2155. parameter0 (* size *) := IntermediateCode.Memory(sizeType,sp,ToMemoryUnits(system,2*addressType.sizeInBits));
  2156. parameter1 (* address *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,1*addressType.sizeInBits));
  2157. GetRecordTypeName (recordType,name);
  2158. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2159. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE));
  2160. IF dump # NIL THEN dump := section.comments END;
  2161. variable := recordType.recordScope.firstVariable;
  2162. WHILE variable # NIL DO
  2163. IF variable.NeedsTrace() THEN
  2164. register := NewRegisterOperand (addressType);
  2165. Emit (Mov(position,register,parameter1));
  2166. IntermediateCode.AddOffset(register,ToMemoryUnits(system,variable.offsetInBits));
  2167. IF recordType.isObject & ((recordType.pointerType = NIL) OR ~recordType.pointerType.isPlain) THEN
  2168. IntermediateCode.AddOffset(register,BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  2169. END;
  2170. CallTraceMethod(register, variable.type);
  2171. ReleaseIntermediateOperand(register);
  2172. END;
  2173. variable := variable.nextVariable;
  2174. END;
  2175. recordBase := recordType.GetBaseRecord();
  2176. WHILE (recordBase # NIL) & ~recordBase.hasPointers DO
  2177. recordBase := recordBase.GetBaseRecord();
  2178. END;
  2179. IF recordBase # NIL THEN
  2180. GetRecordTypeName (recordBase,name);
  2181. IF HasExplicitTraceMethod (recordBase) THEN
  2182. Basic.SuffixSegmentedName (name, Basic.MakeString ("Trace"));
  2183. ELSE
  2184. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2185. END;
  2186. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2187. Emit(Br(position,op));
  2188. ELSIF (recordType.pointerType # NIL) & recordType.pointerType.isPlain THEN
  2189. Emit(Exit(position,0,0));
  2190. ELSE
  2191. IF recordType.isObject THEN
  2192. Basic.ToSegmentedName ("BaseTypes.Object",name);
  2193. ELSE
  2194. Basic.ToSegmentedName ("BaseTypes.Record",name);
  2195. END;
  2196. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2197. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2198. Emit(Br(position,op));
  2199. END;
  2200. IF ~recordType.isObject THEN
  2201. GetRecordTypeName (recordType,name);
  2202. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  2203. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2204. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE);
  2205. NEW(registerUsageCount);
  2206. usedRegisters := NIL;
  2207. IF dump # NIL THEN dump := section.comments END;
  2208. register := NewRegisterOperand (addressType);
  2209. Emit (Mov(position,register,IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,addressType.sizeInBits))));
  2210. IF (recordType.pointerType = NIL) OR ~recordType.pointerType.isPlain THEN
  2211. IntermediateCode.AddOffset(register,BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  2212. END;
  2213. Emit (Push(position,register));
  2214. GetRecordTypeName (recordType,name);
  2215. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2216. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2217. Emit(Call(position,op, ToMemoryUnits(system, system.addressSize)));
  2218. ReleaseIntermediateOperand(register);
  2219. Emit(Exit(position,0,0));
  2220. GetRecordTypeName (recordType,name);
  2221. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2222. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2223. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE);
  2224. NEW(registerUsageCount);
  2225. usedRegisters := NIL;
  2226. register := NewRegisterOperand (addressType);
  2227. ofs := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(recordType)));
  2228. Emit(Mov(position, register, parameter1));
  2229. label := NewLabel();
  2230. SetLabel(label);
  2231. Emit(Push(position, register));
  2232. GetRecordTypeName (recordType,name);
  2233. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2234. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2235. Emit(Call(position, op, 0));
  2236. Emit(Pop(position, register));
  2237. Emit(Add(position, register, register, ofs));
  2238. Emit(Sub(position, parameter0, parameter0, IntermediateCode.Immediate(sizeType, 1)));
  2239. BrneL(label, parameter0, IntermediateCode.Immediate(sizeType, 0));
  2240. Emit(Exit(position,0,0));
  2241. END;
  2242. INC(statCoopTraceMethod, section.pc);
  2243. ReturnToContext(context);
  2244. IF dump # NIL THEN dump := section.comments END;
  2245. END CreateTraceMethod;
  2246. PROCEDURE CreateResetProcedure (recordType: SyntaxTree.RecordType);
  2247. VAR name: Basic.SegmentedName;
  2248. VAR variable: SyntaxTree.Variable; dst, op, ofs: IntermediateCode.Operand; recordBase: SyntaxTree.RecordType;
  2249. parameter1, parameter0: IntermediateCode.Operand; label: Label;
  2250. context: Context;
  2251. BEGIN
  2252. IF recordType.isObject THEN RETURN END;
  2253. parameter0 (* len *) := IntermediateCode.Memory(sizeType,sp,ToMemoryUnits(system,2*addressType.sizeInBits));
  2254. parameter1 (* dest *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,1*addressType.sizeInBits));
  2255. GetRecordTypeName (recordType,name);
  2256. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2257. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE));
  2258. IF dump # NIL THEN dump := section.comments END;
  2259. variable := recordType.recordScope.firstVariable;
  2260. WHILE variable # NIL DO
  2261. IF variable.NeedsTrace() THEN
  2262. dst := NewRegisterOperand (addressType);
  2263. Emit (Mov(position, dst, parameter1));
  2264. IntermediateCode.AddOffset(dst,ToMemoryUnits(system,variable.offsetInBits));
  2265. IF recordType.isObject & ((recordType.pointerType = NIL) OR ~recordType.pointerType.isPlain) THEN
  2266. IntermediateCode.AddOffset(dst,BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  2267. END;
  2268. CallResetProcedure(dst, nil, variable.type);
  2269. ReleaseIntermediateOperand(dst);
  2270. END;
  2271. variable := variable.nextVariable;
  2272. END;
  2273. recordBase := recordType.GetBaseRecord();
  2274. IF (recordBase # NIL) & recordBase.NeedsTrace() THEN
  2275. GetRecordTypeName (recordBase,name);
  2276. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2277. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2278. Emit(Br(position,op));
  2279. ELSE
  2280. Emit(Exit(position,0,0));
  2281. END;
  2282. GetRecordTypeName (recordType,name);
  2283. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2284. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2285. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE);
  2286. NEW(registerUsageCount);
  2287. usedRegisters := NIL;
  2288. dst := NewRegisterOperand (addressType);
  2289. ofs := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(recordType)));
  2290. Emit(Mov(position, dst, parameter1));
  2291. label := NewLabel();
  2292. SetLabel(label);
  2293. Emit(Push(position, dst));
  2294. GetRecordTypeName (recordType,name);
  2295. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2296. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2297. Emit(Call(position, op, 0));
  2298. Emit(Pop(position, dst));
  2299. Emit(Add(position, dst, dst, ofs));
  2300. Emit(Sub(position, parameter0, parameter0, IntermediateCode.Immediate(sizeType, 1)));
  2301. BrneL(label, parameter0, IntermediateCode.Immediate(sizeType, 0));
  2302. Emit(Exit(position,0,0));
  2303. INC(statCoopResetProcedure, section.pc);
  2304. ReturnToContext(context);
  2305. IF dump # NIL THEN dump := section.comments END;
  2306. END CreateResetProcedure;
  2307. PROCEDURE CreateResetMethod (scope: SyntaxTree.ProcedureScope);
  2308. VAR name: Basic.SegmentedName; context: Context;
  2309. BEGIN
  2310. GetCodeSectionNameForSymbol(scope.ownerProcedure, name);
  2311. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2312. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE));
  2313. IF dump # NIL THEN dump := section.comments END;
  2314. Emit(Push(position,fp));
  2315. Emit(Mov(position,fp, IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,addressType.sizeInBits * 2))));
  2316. ResetVariables(scope);
  2317. Emit(Pop(position,fp));
  2318. Emit(Exit(position,0,0));
  2319. ReturnToContext(context);
  2320. IF dump # NIL THEN dump := section.comments END;
  2321. END CreateResetMethod;
  2322. PROCEDURE CallResetProcedure(dest, tag: IntermediateCode.Operand; type: SyntaxTree.Type);
  2323. VAR base: SyntaxTree.Type; op, size: IntermediateCode.Operand; name: Basic.SegmentedName;
  2324. BEGIN
  2325. IF SemanticChecker.IsPointerType (type) THEN
  2326. Emit (Push(position, dest));
  2327. CallThis(position,"GarbageCollector","Reset", 1);
  2328. ELSIF type.IsRecordType() THEN
  2329. Emit (Push(position, dest));
  2330. GetRecordTypeName (type.resolved(SyntaxTree.RecordType),name);
  2331. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2332. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2333. Emit(Call(position,op, ToMemoryUnits(system, system.addressSize)));
  2334. ELSIF type.resolved IS SyntaxTree.ArrayType THEN
  2335. size := GetArrayLength(type, tag);
  2336. base := ArrayBaseType(type);
  2337. IF base.IsRecordType() THEN
  2338. Emit (Push(position, size));
  2339. Emit (Push(position, dest));
  2340. GetRecordTypeName (base.resolved(SyntaxTree.RecordType),name);
  2341. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2342. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2343. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2344. Emit(Call(position,op,ToMemoryUnits(system, 2*system.addressSize)));
  2345. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  2346. Emit (Push(position, size));
  2347. Emit (Push(position, dest));
  2348. CallThis(position,"GarbageCollector","ResetDelegateArray", 2);
  2349. ELSE
  2350. Emit (Push(position, size));
  2351. Emit (Push(position, dest));
  2352. CallThis(position,"GarbageCollector","ResetArray", 2);
  2353. ASSERT(ArrayBaseType(type).IsPointer());
  2354. END;
  2355. ReleaseIntermediateOperand(size);
  2356. ELSIF type.resolved IS SyntaxTree.ProcedureType THEN
  2357. ASSERT(type.resolved(SyntaxTree.ProcedureType).isDelegate);
  2358. Emit (Push(position, dest));
  2359. CallThis(position,"GarbageCollector","ResetDelegate", 1);
  2360. ELSE HALT(100); (* missing ? *)
  2361. END;
  2362. END CallResetProcedure;
  2363. PROCEDURE ResetVariables (scope: SyntaxTree.ProcedureScope);
  2364. VAR variable: SyntaxTree.Variable; parameter: SyntaxTree.Parameter; previousScope: SyntaxTree.Scope; pc: LONGINT;
  2365. PROCEDURE Reset (symbol: SyntaxTree.Symbol);
  2366. VAR operand: Operand;
  2367. BEGIN
  2368. Symbol (symbol, operand);
  2369. CallResetProcedure(operand.op, operand.tag, symbol.type.resolved);
  2370. ReleaseOperand(operand);
  2371. END Reset;
  2372. BEGIN
  2373. previousScope := currentScope;
  2374. currentScope := scope;
  2375. pc := section.pc;
  2376. variable := scope.firstVariable;
  2377. WHILE variable # NIL DO
  2378. IF variable.NeedsTrace() THEN
  2379. Reset (variable);
  2380. END;
  2381. variable := variable.nextVariable;
  2382. END;
  2383. parameter := scope.ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  2384. WHILE parameter # NIL DO
  2385. IF parameter.NeedsTrace() & ~IsVariableParameter(parameter) THEN
  2386. Reset (parameter);
  2387. END;
  2388. parameter := parameter.nextParameter;
  2389. END;
  2390. INC(statCoopResetVariables, section.pc - pc);
  2391. currentScope := previousScope;
  2392. END ResetVariables;
  2393. PROCEDURE CreateProcedureDescriptor (procedure: SyntaxTree.Procedure);
  2394. VAR previousSection: IntermediateCode.Section; name: Basic.SegmentedName;
  2395. VAR op: IntermediateCode.Operand; context: Context;
  2396. BEGIN
  2397. previousSection := section;
  2398. GetCodeSectionNameForSymbol(procedure, name);
  2399. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Descriptor"));
  2400. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,TRUE));
  2401. IF dump # NIL THEN dump := section.comments END;
  2402. Basic.ToSegmentedName ("BaseTypes.StackFrame",name);
  2403. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2404. Emit(Data(position,op));
  2405. Emit(Data(position,nil));
  2406. IF HasPointers (procedure.procedureScope) THEN
  2407. GetCodeSectionNameForSymbol(procedure, name);
  2408. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2409. ELSE
  2410. Basic.ToSegmentedName ("BaseTypes.StackFrame.Reset",name);
  2411. END;
  2412. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2413. Emit(Data(position,op));
  2414. ReturnToContext(context);
  2415. IF dump # NIL THEN dump := section.comments END;
  2416. END CreateProcedureDescriptor;
  2417. PROCEDURE AddImport(CONST moduleName: ARRAY OF CHAR; VAR module: SyntaxTree.Module; force: BOOLEAN): BOOLEAN;
  2418. VAR import: SyntaxTree.Import;
  2419. s: Basic.MessageString;
  2420. selfName: SyntaxTree.IdentifierString;
  2421. BEGIN
  2422. moduleScope.ownerModule.GetName(selfName);
  2423. IF (moduleName = selfName) & (moduleScope.ownerModule.context = Global.A2Name) THEN
  2424. module := moduleScope.ownerModule
  2425. ELSE
  2426. import := moduleScope.ImportByModuleName(SyntaxTree.NewIdentifier(moduleName),SyntaxTree.NewIdentifier("A2"));
  2427. IF import = NIL THEN
  2428. import := SyntaxTree.NewImport(-1,SyntaxTree.NewIdentifier(moduleName),SyntaxTree.NewIdentifier(moduleName),TRUE);
  2429. import.SetContext(SyntaxTree.NewIdentifier("A2"));
  2430. IF ~checker.AddImport(moduleScope.ownerModule,import) THEN
  2431. s := "Module ";
  2432. Strings.Append(s,moduleName);
  2433. Strings.Append(s," cannot be imported.");
  2434. IF force THEN
  2435. Error(position,s);
  2436. ELSIF canBeLoaded THEN
  2437. Strings.Append(s, "=> no dynamic linking.");
  2438. Warning(position, s);
  2439. canBeLoaded := FALSE;
  2440. END;
  2441. RETURN FALSE
  2442. ELSE
  2443. SELF.module.imports.AddName(moduleName)
  2444. END;
  2445. ELSIF import.module = NIL THEN (* already tried *)
  2446. RETURN FALSE
  2447. END;
  2448. module := import.module;
  2449. END;
  2450. RETURN TRUE
  2451. END AddImport;
  2452. (* needed for old binary object file format*)
  2453. PROCEDURE EnsureSymbol(CONST moduleName,procedureName: SyntaxTree.IdentifierString);
  2454. VAR r: Operand; procedure: SyntaxTree.Procedure; module: SyntaxTree.Module; s: ARRAY 128 OF CHAR; fp: LONGINT;
  2455. BEGIN
  2456. IF AddImport(moduleName,module,TRUE) THEN
  2457. procedure := module.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  2458. IF procedure = NIL THEN
  2459. s := "Instruction not supported on target, emulation procedure ";
  2460. Strings.Append(s,moduleName);
  2461. Strings.Append(s,".");
  2462. Strings.Append(s,procedureName);
  2463. Strings.Append(s," not present");
  2464. Error(position,s);
  2465. ELSE
  2466. StaticCallOperand(r,procedure);
  2467. ReleaseOperand(r);
  2468. fp := GetFingerprint(procedure);
  2469. END;
  2470. END;
  2471. END EnsureSymbol;
  2472. PROCEDURE ConditionToValue(x: SyntaxTree.Expression);
  2473. VAR exit: Label; trueL,falseL: Label;
  2474. BEGIN
  2475. trueL := NewLabel();
  2476. falseL := NewLabel();
  2477. exit := NewLabel();
  2478. Condition(x,trueL,falseL);
  2479. InitOperand(result,ModeValue);
  2480. SetLabel(trueL);
  2481. IntermediateCode.InitRegister(result.op,IntermediateCode.GetType(system,x.type),IntermediateCode.GeneralPurposeRegister,AcquireRegister(IntermediateCode.GetType(system,x.type),IntermediateCode.GeneralPurposeRegister));
  2482. Emit(Mov(position,result.op,true));
  2483. BrL(exit);
  2484. SetLabel(falseL);
  2485. Emit(MovReplace(position,result.op,false));
  2486. SetLabel(exit);
  2487. END ConditionToValue;
  2488. PROCEDURE ValueToCondition(VAR op: Operand);
  2489. BEGIN
  2490. LoadValue(op,system.booleanType);
  2491. BrneL(trueLabel,op.op, false);
  2492. ReleaseOperand(op);
  2493. BrL(falseLabel);
  2494. END ValueToCondition;
  2495. PROCEDURE GetDynamicSize(type: SyntaxTree.Type; tag: IntermediateCode.Operand):IntermediateCode.Operand;
  2496. VAR size: LONGINT;
  2497. PROCEDURE GetArraySize(type: SyntaxTree.ArrayType; offset: LONGINT):IntermediateCode.Operand;
  2498. VAR baseType: SyntaxTree.Type; size: LONGINT; sizeOperand,len,res: IntermediateCode.Operand;
  2499. BEGIN
  2500. ASSERT(type.form = SyntaxTree.Open);
  2501. baseType := type.arrayBase.resolved;
  2502. IF IsOpenArray(baseType) THEN
  2503. sizeOperand := GetArraySize(baseType(SyntaxTree.ArrayType),offset+system.addressSize);
  2504. ELSE
  2505. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  2506. sizeOperand := IntermediateCode.Immediate(addressType,size);
  2507. END;
  2508. len := tag;
  2509. IntermediateCode.AddOffset(len,ToMemoryUnits(system,offset));
  2510. IntermediateCode.MakeMemory(len,addressType);
  2511. UseIntermediateOperand(len);
  2512. Reuse2(res,sizeOperand,len);
  2513. Emit(Mul(position,res,sizeOperand,len));
  2514. ReleaseIntermediateOperand(sizeOperand); ReleaseIntermediateOperand(len);
  2515. RETURN res
  2516. END GetArraySize;
  2517. BEGIN
  2518. type := type.resolved;
  2519. IF IsOpenArray(type) THEN
  2520. IF tag.mode = IntermediateCode.ModeImmediate THEN (* special rule for winapi/c arrays *)
  2521. RETURN tag
  2522. ELSE
  2523. RETURN GetArraySize(type.resolved(SyntaxTree.ArrayType),0)
  2524. END;
  2525. ELSE
  2526. size := ToMemoryUnits(system,system.SizeOf(type));
  2527. RETURN IntermediateCode.Immediate(addressType,size)
  2528. END;
  2529. END GetDynamicSize;
  2530. PROCEDURE GetArrayLength(type: SyntaxTree.Type; tag: IntermediateCode.Operand):IntermediateCode.Operand;
  2531. PROCEDURE GetLength(type: SyntaxTree.ArrayType; offset: LONGINT):IntermediateCode.Operand;
  2532. VAR baseType: SyntaxTree.Type; sizeOperand,len,res: IntermediateCode.Operand;
  2533. BEGIN
  2534. ASSERT(type.form = SyntaxTree.Open);
  2535. baseType := type.arrayBase.resolved;
  2536. IF IsOpenArray(baseType) THEN
  2537. sizeOperand := GetLength(baseType(SyntaxTree.ArrayType),offset+system.addressSize);
  2538. ELSE
  2539. sizeOperand := IntermediateCode.Immediate(addressType,StaticArrayNumElements(baseType));
  2540. END;
  2541. len := tag;
  2542. IntermediateCode.AddOffset(len,ToMemoryUnits(system,offset));
  2543. IntermediateCode.MakeMemory(len,addressType);
  2544. UseIntermediateOperand(len);
  2545. Reuse2(res,sizeOperand,len);
  2546. Emit(Mul(position,res,sizeOperand,len));
  2547. ReleaseIntermediateOperand(sizeOperand); ReleaseIntermediateOperand(len);
  2548. RETURN res
  2549. END GetLength;
  2550. BEGIN
  2551. type := type.resolved;
  2552. IF IsOpenArray(type) THEN
  2553. ASSERT(tag.mode # IntermediateCode.ModeImmediate);
  2554. RETURN GetLength(type.resolved(SyntaxTree.ArrayType),0)
  2555. ELSE
  2556. RETURN IntermediateCode.Immediate(addressType,1)
  2557. END;
  2558. END GetArrayLength;
  2559. PROCEDURE GetSizeFromTag(tag: IntermediateCode.Operand): IntermediateCode.Operand;
  2560. VAR result: IntermediateCode.Operand;
  2561. BEGIN
  2562. IF backend.cooperative THEN
  2563. MakeMemory(result, tag, addressType, ToMemoryUnits(system,system.addressSize));
  2564. ELSE
  2565. MakeMemory(result, tag, addressType, 0);
  2566. END;
  2567. RETURN result
  2568. END GetSizeFromTag;
  2569. PROCEDURE GetArrayOfBytesSize(e: SyntaxTree.Expression; tag: IntermediateCode.Operand): IntermediateCode.Operand;
  2570. VAR parameter: SyntaxTree.Parameter;
  2571. BEGIN
  2572. IF (e IS SyntaxTree.SymbolDesignator) & (e(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  2573. parameter := e(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter);
  2574. IF (parameter.kind IN {SyntaxTree.VarParameter,SyntaxTree.ConstParameter}) & (parameter.type.resolved IS SyntaxTree.RecordType) THEN RETURN GetSizeFromTag(tag) END;
  2575. ELSIF e IS SyntaxTree.DereferenceDesignator THEN
  2576. IF (e.type.resolved IS SyntaxTree.RecordType) THEN RETURN GetSizeFromTag(tag) END;
  2577. END;
  2578. RETURN GetDynamicSize(e.type, tag);
  2579. END GetArrayOfBytesSize;
  2580. (*
  2581. to find imported symbol. not needed ?
  2582. PROCEDURE SymbolByName(CONST moduleName, symbolName: ARRAY OF CHAR): SyntaxTree.Symbol;
  2583. VAR importedModule: SyntaxTree.Module; symbol: SyntaxTree.Symbol;
  2584. BEGIN
  2585. IF AddImport(moduleName,importedModule,FALSE) THEN
  2586. symbol := importedModule.moduleScope.FindSymbol(SyntaxTree.NewIdentifier(symbolName));
  2587. RETURN symbol
  2588. ELSE
  2589. RETURN NIL
  2590. END
  2591. END SymbolByName;
  2592. *)
  2593. PROCEDURE GetRuntimeProcedure(CONST moduleName, procedureName: ARRAY OF CHAR; VAR procedure: SyntaxTree.Procedure; force: BOOLEAN): BOOLEAN;
  2594. VAR runtimeModule: SyntaxTree.Module; s: Basic.MessageString;
  2595. BEGIN
  2596. IF AddImport(moduleName,runtimeModule,force) THEN
  2597. procedure := runtimeModule.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  2598. IF procedure = NIL THEN
  2599. s := "Procedure ";
  2600. Strings.Append(s,moduleName);
  2601. Strings.Append(s,".");
  2602. Strings.Append(s,procedureName);
  2603. Strings.Append(s," not present");
  2604. Error(position,s);
  2605. RETURN FALSE
  2606. ELSE
  2607. RETURN TRUE
  2608. END;
  2609. ELSE RETURN FALSE
  2610. END;
  2611. END GetRuntimeProcedure;
  2612. PROCEDURE GetTypeDescriptor(CONST moduleName, typeName: ARRAY OF CHAR; VAR name: Basic.SegmentedName): SyntaxTree.Symbol;
  2613. VAR importedModule: SyntaxTree.Module; source: IntermediateCode.Section; symbol: SyntaxTree.Symbol;
  2614. s: Basic.MessageString;
  2615. BEGIN
  2616. Basic.InitSegmentedName(name);
  2617. name[0] := Basic.MakeString(moduleName);
  2618. name[1] := Basic.MakeString(typeName);
  2619. name[2] := -1;
  2620. IF AddImport(moduleName,importedModule, FALSE) THEN
  2621. symbol := importedModule.moduleScope.FindTypeDeclaration(SyntaxTree.NewIdentifier(typeName));
  2622. IF symbol = NIL THEN
  2623. s := "type ";
  2624. Strings.Append(s,moduleName);
  2625. Strings.Append(s,".");
  2626. Strings.Append(s,typeName);
  2627. Strings.Append(s," not present");
  2628. Error(position,s);
  2629. END;
  2630. ELSE symbol := NIL;
  2631. END;
  2632. IF importedModule = moduleScope.ownerModule THEN
  2633. source := NewSection(module.allSections, Sections.ConstSection, name, symbol, commentPrintout # NIL);
  2634. ELSE
  2635. source := NewSection(module.importedSections, Sections.ConstSection, name, symbol, commentPrintout # NIL);
  2636. END;
  2637. RETURN symbol
  2638. END GetTypeDescriptor;
  2639. (* 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. *)
  2640. PROCEDURE CallThis(position: LONGINT; CONST moduleName, procedureName: ARRAY OF CHAR; numberParameters: LONGINT);
  2641. VAR procedure: SyntaxTree.Procedure; result: Operand; reg: IntermediateCode.Operand; source: IntermediateCode.Section;
  2642. pooledName: Basic.SegmentedName; size: LONGINT;
  2643. BEGIN
  2644. IF GetRuntimeProcedure(moduleName,procedureName,procedure,numberParameters < 0) THEN (* ready for dynamic linking *)
  2645. StaticCallOperand(result,procedure);
  2646. IF numberParameters < 0 THEN
  2647. size := ProcedureParametersSize(system,procedure);
  2648. ELSE
  2649. size := ToMemoryUnits(system,numberParameters * system.addressSize);
  2650. IF size # ProcedureParametersSize(system,procedure) THEN
  2651. Error(position,"runtime call parameter count mismatch");
  2652. END;
  2653. END;
  2654. Emit(Call(position, result.op, size));
  2655. ReleaseOperand(result);
  2656. ELSE (* only static linking possible *)
  2657. Basic.InitSegmentedName(pooledName);
  2658. pooledName[0] := Basic.MakeString(moduleName);
  2659. pooledName[1] := Basic.MakeString(procedureName);
  2660. pooledName[2] := -1;
  2661. source := NewSection(module.importedSections, Sections.CodeSection, pooledName, NIL,commentPrintout # NIL);
  2662. IntermediateCode.InitAddress(reg, addressType, pooledName , 0, 0);
  2663. Emit(Call(position,reg, ToMemoryUnits(system,numberParameters * system.addressSize)));
  2664. END;
  2665. END CallThis;
  2666. PROCEDURE CompareString(br: ConditionalBranch; leftExpression,rightExpression: SyntaxTree.Expression);
  2667. VAR
  2668. left,right: Operand;
  2669. leftSize, rightSize: IntermediateCode.Operand;
  2670. saved: RegisterEntry;
  2671. reg: IntermediateCode.Operand;
  2672. procedureName: SyntaxTree.IdentifierString;
  2673. BEGIN
  2674. procedureName := "CompareString";
  2675. SaveRegisters();ReleaseUsedRegisters(saved);
  2676. Designate(leftExpression,left);
  2677. leftSize := GetDynamicSize(leftExpression.type,left.tag);
  2678. Emit(Push(position,leftSize));
  2679. ReleaseIntermediateOperand(leftSize);
  2680. Emit(Push(position,left.op));
  2681. ReleaseOperand(left);
  2682. Designate(rightExpression,right);
  2683. rightSize := GetDynamicSize(rightExpression.type,right.tag);
  2684. Emit(Push(position,rightSize));
  2685. ReleaseIntermediateOperand(rightSize);
  2686. Emit(Push(position,right.op));
  2687. ReleaseOperand(right);
  2688. IF backend.cooperative THEN
  2689. CallThis(position,DefaultRuntimeModuleName,procedureName, 4);
  2690. ELSE
  2691. CallThis(position,runtimeModuleName,procedureName, 4);
  2692. END;
  2693. IntermediateCode.InitRegister(reg,int8,IntermediateCode.GeneralPurposeRegister,AcquireRegister(int8,IntermediateCode.GeneralPurposeRegister));
  2694. Emit(Result(position,reg));
  2695. (*
  2696. AcquireThisRegister(int8,IntermediateCode.Result);
  2697. *)
  2698. RestoreRegisters(saved); (* must be done before use of result, might be jumped over otherwise *)
  2699. (*
  2700. IntermediateCode.InitRegister(reg,int8,IntermediateCode.Result);
  2701. *)
  2702. br(trueLabel,reg,IntermediateCode.Immediate(int8,0));
  2703. ReleaseIntermediateOperand(reg);
  2704. BrL(falseLabel);
  2705. END CompareString;
  2706. PROCEDURE CopyString(leftExpression,rightExpression: SyntaxTree.Expression);
  2707. VAR
  2708. left,right: Operand;
  2709. leftSize, rightSize: IntermediateCode.Operand;
  2710. saved: RegisterEntry;
  2711. procedureName: SyntaxTree.IdentifierString;
  2712. BEGIN
  2713. procedureName := "CopyString";
  2714. SaveRegisters();ReleaseUsedRegisters(saved);
  2715. Designate(leftExpression,left);
  2716. leftSize := GetDynamicSize(leftExpression.type,left.tag);
  2717. Emit(Push(position,leftSize));
  2718. ReleaseIntermediateOperand(leftSize);
  2719. Emit(Push(position,left.op));
  2720. ReleaseOperand(left);
  2721. Designate(rightExpression,right);
  2722. rightSize := GetDynamicSize(rightExpression.type,right.tag);
  2723. Emit(Push(position,rightSize));
  2724. ReleaseIntermediateOperand(rightSize);
  2725. Emit(Push(position,right.op));
  2726. ReleaseOperand(right);
  2727. IF backend.cooperative THEN
  2728. CallThis(position,DefaultRuntimeModuleName,procedureName, 4);
  2729. ELSE
  2730. CallThis(position,runtimeModuleName,procedureName,4);
  2731. END;
  2732. RestoreRegisters(saved);
  2733. END CopyString;
  2734. PROCEDURE VisitBinaryExpression(x: SyntaxTree.BinaryExpression);
  2735. VAR left,right: Operand; temp: Operand; zero, one, tempReg, tempReg2: IntermediateCode.Operand;
  2736. leftType,rightType: SyntaxTree.Type;
  2737. leftExpression,rightExpression : SyntaxTree.Expression;
  2738. componentType: IntermediateCode.Type;
  2739. value: HUGEINT; exp: LONGINT;next,exit: Label; recordType: SyntaxTree.RecordType; dest: IntermediateCode.Operand;
  2740. size: LONGINT;
  2741. BEGIN
  2742. IF Trace THEN TraceEnter("VisitBinaryExpression") END;
  2743. dest := destination; destination := emptyOperand;
  2744. leftType := x.left.type.resolved;
  2745. rightType := x.right.type.resolved;
  2746. (* for "OR" and "&" the left and right expressions may not be emitted first <= shortcut evaluation *)
  2747. CASE x.operator OF
  2748. Scanner.Or:
  2749. (* shortcut evaluation of left OR right *)
  2750. IF ~conditional THEN ConditionToValue(x);
  2751. ELSE
  2752. next := NewLabel();
  2753. Condition(x.left,trueLabel,next);
  2754. SetLabel(next);
  2755. Condition(x.right,trueLabel,falseLabel);
  2756. END;
  2757. |Scanner.And:
  2758. (* shortcut evaluation of left & right *)
  2759. IF ~conditional THEN ConditionToValue(x);
  2760. ELSE
  2761. next := NewLabel();
  2762. Condition(x.left,next,falseLabel);
  2763. SetLabel(next);
  2764. Condition(x.right,trueLabel,falseLabel);
  2765. END;
  2766. |Scanner.Is:
  2767. IF ~conditional THEN ConditionToValue(x);
  2768. ELSE
  2769. (* get type desc tag *)
  2770. IF IsPointerToRecord(leftType,recordType) THEN
  2771. Evaluate(x.left,left);
  2772. Dereference(left,recordType,IsUnsafePointer(leftType))
  2773. ELSE
  2774. Designate(x.left,left);
  2775. END;
  2776. TypeTest(left.tag,x.right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved,trueLabel,falseLabel);
  2777. ReleaseOperand(left);
  2778. END;
  2779. |Scanner.Plus:
  2780. Evaluate(x.left,left);
  2781. Evaluate(x.right,right);
  2782. IF leftType IS SyntaxTree.SetType THEN
  2783. InitOperand(result,ModeValue);
  2784. Reuse2a(result.op,left.op,right.op,dest);
  2785. Emit(Or(position,result.op,left.op,right.op));
  2786. ELSIF leftType IS SyntaxTree.ComplexType THEN
  2787. InitOperand(result,ModeValue);
  2788. Reuse2a(result.op,left.op,right.op,dest); (* TODO: review this *)
  2789. Reuse2(result.tag,left.tag,right.tag);
  2790. Emit(Add(position,result.op,left.op,right.op));
  2791. Emit(Add(position,result.tag,left.tag,right.tag))
  2792. ELSE
  2793. InitOperand(result,ModeValue);
  2794. Reuse2a(result.op,left.op,right.op,dest);
  2795. Emit(Add(position,result.op,left.op,right.op));
  2796. END;
  2797. ReleaseOperand(left); ReleaseOperand(right);
  2798. |Scanner.Minus:
  2799. Evaluate(x.left,left);
  2800. Evaluate(x.right,right);
  2801. IF leftType IS SyntaxTree.SetType THEN
  2802. InitOperand(result,ModeValue);
  2803. Reuse1(result.op,right.op);
  2804. Emit(Not(position,result.op,right.op));
  2805. ReleaseOperand(right);
  2806. Emit(And(position,result.op,result.op,left.op));
  2807. ReleaseOperand(left);
  2808. ELSIF leftType IS SyntaxTree.ComplexType THEN
  2809. InitOperand(result,ModeValue);
  2810. Reuse2a(result.op,left.op,right.op,dest); (* TODO: review this *)
  2811. Reuse2(result.tag,left.tag,right.tag);
  2812. Emit(Sub(position,result.op,left.op,right.op));
  2813. Emit(Sub(position,result.tag,left.tag,right.tag));
  2814. ReleaseOperand(left); ReleaseOperand(right)
  2815. ELSE
  2816. InitOperand(result,ModeValue);
  2817. Reuse2a(result.op,left.op,right.op,dest);
  2818. Emit(Sub(position,result.op,left.op,right.op));
  2819. ReleaseOperand(left); ReleaseOperand(right);
  2820. END;
  2821. |Scanner.Times:
  2822. Evaluate(x.left,left);
  2823. Evaluate(x.right,right);
  2824. IF leftType IS SyntaxTree.SetType THEN
  2825. InitOperand(result,ModeValue);
  2826. Reuse2a(result.op,left.op,right.op,dest);
  2827. Emit(And(position,result.op,left.op,right.op));
  2828. ELSIF (x.type.resolved IS SyntaxTree.IntegerType) & IsIntegerConstant(x.right,value) & PowerOf2(value,exp) THEN
  2829. InitOperand(result,ModeValue);
  2830. Reuse1a(result.op,left.op,dest);
  2831. IntermediateCode.InitImmediate(right.op,IntermediateCode.GetType(system, system.longintType),exp);
  2832. Emit(Shl(position,result.op,left.op,right.op));
  2833. ELSIF leftType IS SyntaxTree.ComplexType THEN
  2834. InitOperand(result, ModeValue);
  2835. componentType := left.op.type;
  2836. (* TODO: review this *)
  2837. (*
  2838. result.op = left.op * right.op - left.tag * right.tag
  2839. result.tag = left.tag * right.op + left.op * right.tag
  2840. *)
  2841. result.op := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  2842. Emit(Mul(position,result.op, left.op, right.op));
  2843. tempReg := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  2844. Emit(Mul(position,tempReg, left.tag, right.tag));
  2845. Emit(Sub(position,result.op, result.op, tempReg));
  2846. Reuse2(result.tag, left.tag, right.op);
  2847. Emit(Mul(position,result.tag, left.tag, right.op));
  2848. Emit(Mul(position,tempReg, left.op, right.tag));
  2849. Emit(Add(position,result.tag, result.tag, tempReg));
  2850. ReleaseIntermediateOperand(tempReg)
  2851. ELSE
  2852. InitOperand(result,ModeValue);
  2853. Reuse2a(result.op,left.op,right.op,dest);
  2854. Emit(Mul(position,result.op,left.op,right.op));
  2855. END;
  2856. ReleaseOperand(left); ReleaseOperand(right);
  2857. |Scanner.Div:
  2858. Evaluate(x.left,left);
  2859. Evaluate(x.right,right);
  2860. IF ((x.type.resolved IS SyntaxTree.IntegerType) OR (x.type.resolved IS SyntaxTree.AddressType)) & IsIntegerConstant(x.right,value) & PowerOf2(value,exp) THEN
  2861. InitOperand(result,ModeValue);
  2862. Reuse1a(result.op,left.op,dest);
  2863. IntermediateCode.InitImmediate(right.op,IntermediateCode.GetType(system, system.longintType),exp);
  2864. Emit(Shr(position,result.op,left.op,right.op));
  2865. ELSE
  2866. IF (x.type.resolved IS SyntaxTree.IntegerType) & (x.right.resolved = NIL) THEN (* divisor negative check *)
  2867. IntermediateCode.InitImmediate(zero,IntermediateCode.GetType(system,rightType),0);
  2868. IF ~isUnchecked THEN
  2869. exit := NewLabel();
  2870. BrltL(exit,zero,right.op);
  2871. EmitTrap(position,NegativeDivisorTrap);
  2872. SetLabel(exit);
  2873. END;
  2874. END;
  2875. InitOperand(result,ModeValue);
  2876. Reuse2a(result.op,left.op,right.op,dest);
  2877. Emit(Div(position,result.op,left.op,right.op));
  2878. END;
  2879. ReleaseOperand(left); ReleaseOperand(right);
  2880. |Scanner.Mod:
  2881. Evaluate(x.left,left);
  2882. Evaluate(x.right,right);
  2883. IF ((x.type.resolved IS SyntaxTree.IntegerType) OR (x.type.resolved IS SyntaxTree.AddressType)) & IsIntegerConstant(x.right,value) & PowerOf2(value,exp) THEN
  2884. IntermediateCode.InitImmediate(right.op,IntermediateCode.GetType(system,x.type),value-1);
  2885. InitOperand(result,ModeValue);
  2886. Reuse1a(result.op,left.op,dest);
  2887. Emit(And(position,result.op,left.op,right.op));
  2888. ELSE
  2889. IF (x.type.resolved IS SyntaxTree.IntegerType) & (x.right.resolved = NIL) THEN (* divisor negative check *)
  2890. IntermediateCode.InitImmediate(zero,IntermediateCode.GetType(system,rightType),0);
  2891. IF ~isUnchecked THEN
  2892. exit := NewLabel();
  2893. BrltL(exit,zero,right.op);
  2894. EmitTrap(position,NegativeDivisorTrap);
  2895. SetLabel(exit);
  2896. END;
  2897. END;
  2898. InitOperand(result,ModeValue);
  2899. Reuse2a(result.op,left.op,right.op,dest);
  2900. Emit(Mod(position,result.op,left.op,right.op));
  2901. END;
  2902. ReleaseOperand(left); ReleaseOperand(right);
  2903. |Scanner.Slash:
  2904. Evaluate(x.left,left);
  2905. Evaluate(x.right,right);
  2906. IF leftType IS SyntaxTree.SetType THEN
  2907. InitOperand(result,ModeValue);
  2908. Reuse2a(result.op,left.op,right.op,dest);
  2909. Emit(Xor(position,result.op,left.op,right.op));
  2910. ELSIF leftType IS SyntaxTree.ComplexType THEN
  2911. InitOperand(result,ModeValue);
  2912. componentType := left.op.type;
  2913. (* review this *)
  2914. (*
  2915. divisor = right.op * right.op + right.tag * right.tag
  2916. result.op = (left.op * right.op + left.tag * right.tag) / divisor
  2917. result.tag = (left.tag * right.op - left.op * right.tag) / divisor
  2918. *)
  2919. tempReg := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  2920. tempReg2 := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  2921. Emit(Mul(position,tempReg, right.op, right.op));
  2922. Emit(Mul(position,tempReg2, right.tag, right.tag));
  2923. Emit(Add(position,tempReg, tempReg, tempReg2));
  2924. result.op := tempReg2;
  2925. Emit(Mul(position,result.op, left.op, right.op));
  2926. tempReg2 := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister, AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  2927. Emit(Mul(position,tempReg2, left.tag, right.tag));
  2928. Emit(Add(position,result.op, result.op, tempReg2));
  2929. Emit(Div(position,result.op, result.op, tempReg));
  2930. Reuse2(result.tag, left.tag, right.op);
  2931. Emit(Mul(position,result.tag, left.tag, right.op));
  2932. Emit(Mul(position,tempReg2, left.op, right.tag));
  2933. Emit(Sub(position,result.tag, result.tag, tempReg2));
  2934. Emit(Div(position,result.tag, result.tag, tempReg));
  2935. ReleaseIntermediateOperand(tempReg);
  2936. ReleaseIntermediateOperand(tempReg2)
  2937. ELSE
  2938. InitOperand(result,ModeValue);
  2939. Reuse2a(result.op,left.op,right.op,dest);
  2940. Emit(Div(position,result.op,left.op,right.op));
  2941. END;
  2942. ReleaseOperand(left); ReleaseOperand(right);
  2943. |Scanner.Equal:
  2944. IF ~conditional THEN ConditionToValue(x);
  2945. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  2946. CompareString(BreqL,x.left,x.right);
  2947. ELSE
  2948. Evaluate(x.left,left);
  2949. Evaluate(x.right,right);
  2950. IF leftType IS SyntaxTree.RangeType THEN
  2951. ASSERT(rightType IS SyntaxTree.RangeType);
  2952. BrneL(falseLabel, left.op, right.op); (* first *)
  2953. BrneL(falseLabel, left.tag, right.tag); (* last *)
  2954. BrneL(falseLabel, left.extra, right.extra); (* step *)
  2955. ReleaseOperand(left); ReleaseOperand(right);
  2956. BrL(trueLabel)
  2957. ELSIF IsDelegate(leftType) THEN (* delegate comparison *)
  2958. BrneL(falseLabel, left.op, right.op); (* first *)
  2959. BrneL(falseLabel, left.tag, right.tag); (* last *)
  2960. ReleaseOperand(left); ReleaseOperand(right);
  2961. BrL(trueLabel)
  2962. ELSIF leftType IS SyntaxTree.ComplexType THEN
  2963. (* TODO: review this *)
  2964. BrneL(falseLabel, left.op, right.op); (* real part *)
  2965. BrneL(falseLabel, left.tag, right.tag); (* imaginary part *)
  2966. ReleaseOperand(left); ReleaseOperand(right);
  2967. BrL(trueLabel)
  2968. ELSE
  2969. BrneL(falseLabel,left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  2970. ReleaseOperand(left); ReleaseOperand(right);
  2971. BrL(trueLabel);
  2972. END;
  2973. END;
  2974. |Scanner.LessEqual:
  2975. IF ~conditional THEN ConditionToValue(x);
  2976. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  2977. CompareString(BrgeL,x.right,x.left);
  2978. ELSE
  2979. Evaluate(x.left,left);
  2980. Evaluate(x.right,right);
  2981. IF leftType IS SyntaxTree.SetType THEN (* left subsetequal right: left \cap right = left *)
  2982. Reuse1(temp.op,right.op);
  2983. Emit(And(position,temp.op,left.op,right.op));
  2984. ReleaseOperand(right);
  2985. BreqL(trueLabel,temp.op,left.op);
  2986. BrL(falseLabel);
  2987. ReleaseOperand(temp);ReleaseOperand(left);
  2988. ELSE
  2989. BrltL(falseLabel,right.op,left.op); (* inverse evaluation to optimize jumps for true case *)
  2990. ReleaseOperand(left); ReleaseOperand(right);
  2991. BrL(trueLabel);
  2992. END;
  2993. END;
  2994. |Scanner.Less:
  2995. IF leftType IS SyntaxTree.SetType THEN (* left < right <=> left <= right & left # right *)
  2996. leftExpression := SyntaxTree.NewBinaryExpression(-1,x.left,x.right,Scanner.LessEqual);
  2997. leftExpression.SetType(system.booleanType);
  2998. rightExpression := SyntaxTree.NewBinaryExpression(-1,x.left,x.right,Scanner.Unequal);
  2999. rightExpression.SetType(system.booleanType);
  3000. leftExpression := SyntaxTree.NewBinaryExpression(-1,leftExpression,rightExpression,Scanner.And);
  3001. leftExpression.SetType(system.booleanType);
  3002. Expression(leftExpression);
  3003. ELSIF ~conditional THEN ConditionToValue(x);
  3004. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3005. CompareString(BrltL,x.left,x.right);
  3006. ELSE
  3007. Evaluate(x.left,left);
  3008. Evaluate(x.right,right);
  3009. BrgeL(falseLabel,left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3010. ReleaseOperand(left); ReleaseOperand(right);
  3011. BrL(trueLabel);
  3012. END;
  3013. |Scanner.Greater:
  3014. IF leftType IS SyntaxTree.SetType THEN (* left > right <=> left >= right & left # right *)
  3015. leftExpression := SyntaxTree.NewBinaryExpression(-1,x.left,x.right,Scanner.GreaterEqual);
  3016. leftExpression.SetType(system.booleanType);
  3017. rightExpression := SyntaxTree.NewBinaryExpression(-1,x.left,x.right,Scanner.Unequal);
  3018. rightExpression.SetType(system.booleanType);
  3019. leftExpression := SyntaxTree.NewBinaryExpression(-1,leftExpression,rightExpression,Scanner.And);
  3020. leftExpression.SetType(system.booleanType);
  3021. Expression(leftExpression);
  3022. ELSIF ~conditional THEN ConditionToValue(x);
  3023. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3024. CompareString(BrltL,x.right,x.left);
  3025. ELSE
  3026. Evaluate(x.left,left);
  3027. Evaluate(x.right,right);
  3028. BrgeL(falseLabel, right.op,left.op); (* inverse evaluation to optimize jumps for true case *)
  3029. ReleaseOperand(left); ReleaseOperand(right);
  3030. BrL(trueLabel);
  3031. END;
  3032. |Scanner.GreaterEqual:
  3033. IF ~conditional THEN ConditionToValue(x);
  3034. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3035. CompareString(BrgeL,x.left,x.right);
  3036. ELSE
  3037. Evaluate(x.left,left);
  3038. Evaluate(x.right,right);
  3039. IF leftType IS SyntaxTree.SetType THEN (* left supsetequal right: left \cap right = right *)
  3040. Reuse1(temp.op,left.op);
  3041. Emit(And(position,temp.op,left.op,right.op));
  3042. ReleaseOperand(left);
  3043. BreqL(trueLabel, temp.op,right.op);
  3044. ReleaseOperand(temp); ReleaseOperand(right);
  3045. BrL(falseLabel);
  3046. ELSE
  3047. BrltL(falseLabel, left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3048. ReleaseOperand(left); ReleaseOperand(right);
  3049. BrL(trueLabel);
  3050. END;
  3051. END;
  3052. |Scanner.Unequal:
  3053. IF ~conditional THEN ConditionToValue(x);
  3054. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3055. CompareString(BrneL,x.left,x.right);
  3056. ELSE
  3057. Evaluate(x.left,left);
  3058. Evaluate(x.right,right);
  3059. IF leftType IS SyntaxTree.RangeType THEN
  3060. ASSERT(rightType IS SyntaxTree.RangeType);
  3061. BrneL(trueLabel, left.op, right.op); (* first *)
  3062. BrneL(trueLabel, left.tag, right.tag); (* last *)
  3063. BrneL(trueLabel, left.extra, right.extra); (* step *)
  3064. ReleaseOperand(left); ReleaseOperand(right);
  3065. BrL(falseLabel)
  3066. ELSIF IsDelegate(leftType) THEN (* delegate comparison *)
  3067. BrneL(trueLabel, left.op, right.op); (* first *)
  3068. BrneL(trueLabel, left.tag, right.tag); (* last *)
  3069. ReleaseOperand(left); ReleaseOperand(right);
  3070. BrL(falseLabel)
  3071. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3072. (* TODO: review this *)
  3073. BrneL(trueLabel, left.op, right.op); (* real part *)
  3074. BrneL(trueLabel, left.tag, right.tag); (* imaginary part *)
  3075. ReleaseOperand(left); ReleaseOperand(right);
  3076. BrL(falseLabel)
  3077. ELSE
  3078. BreqL(falseLabel,left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3079. ReleaseOperand(left); ReleaseOperand(right);
  3080. BrL(trueLabel);
  3081. END;
  3082. END;
  3083. |Scanner.In:
  3084. ASSERT(rightType.resolved IS SyntaxTree.SetType);
  3085. Evaluate(x.left,left);
  3086. Evaluate(x.right,right);
  3087. Convert(left.op,setType);
  3088. ReuseCopy(temp.op,right.op);
  3089. Emit(Shr(position,temp.op,temp.op,left.op));
  3090. ReleaseOperand(right); ReleaseOperand(left);
  3091. IntermediateCode.InitImmediate(one,setType,1);
  3092. Emit(And(position,temp.op,temp.op,one));
  3093. IF conditional THEN
  3094. IntermediateCode.InitImmediate(zero,setType,0);
  3095. BrneL(trueLabel,temp.op,zero);
  3096. ReleaseOperand(temp);
  3097. BrL(falseLabel);
  3098. ELSE
  3099. Convert(temp.op,bool);
  3100. result.mode := ModeValue;
  3101. result.op := temp.op;
  3102. result.tag := nil; (* may be left over from calls to evaluate *)
  3103. END;
  3104. ELSE
  3105. IF (x.operator = Scanner.Questionmarks) OR (x.operator = Scanner.LessLessQ) & (x.right.type.resolved IS SyntaxTree.PortType) THEN
  3106. IF x.operator = Scanner.Questionmarks THEN
  3107. leftExpression := x.left;
  3108. rightExpression := x.right;
  3109. ELSE
  3110. leftExpression := x.right;
  3111. rightExpression := x.left;
  3112. END;
  3113. Evaluate(leftExpression, left);
  3114. Emit(Push(position,left.op));
  3115. ReleaseOperand(left);
  3116. Designate(rightExpression, right);
  3117. size := ToMemoryUnits(system,system.SizeOf(x.right.type));
  3118. IF ~backend.cellsAreObjects THEN
  3119. IF size # 1 THEN Error(x.right.position,"receive not implemented for complex data types") END;
  3120. END;
  3121. Emit(Push(position,right.op));
  3122. ReleaseOperand(right);
  3123. IF backend.cellsAreObjects THEN
  3124. CallThis(position,"ActiveCellsRuntime","ReceiveNonBlocking",2);
  3125. ELSE
  3126. CallThis(position,ChannelModuleName,"ReceiveNonBlocking",2);
  3127. END;
  3128. InitOperand(result, ModeValue);
  3129. result.op := NewRegisterOperand(bool);
  3130. Emit(Result(position,result.op));
  3131. IF conditional THEN
  3132. IntermediateCode.InitImmediate(zero,setType,0);
  3133. BrneL(trueLabel,result.op,zero);
  3134. ReleaseOperand(result);
  3135. BrL(falseLabel);
  3136. END;
  3137. ELSIF (x.operator = Scanner.ExclamationMarks) OR (x.operator = Scanner.LessLessQ) & (x.left.type.resolved IS SyntaxTree.PortType) THEN
  3138. leftExpression := x.left;
  3139. rightExpression := x.right;
  3140. Evaluate(leftExpression, left);
  3141. Emit(Push(position,left.op));
  3142. ReleaseOperand(left);
  3143. Evaluate(rightExpression, right);
  3144. size := ToMemoryUnits(system,system.SizeOf(x.right.type));
  3145. IF ~backend.cellsAreObjects THEN
  3146. IF size # 1 THEN Error(x.right.position,"send not implemented for complex data types") END;
  3147. END;
  3148. Emit(Push(position,right.op));
  3149. ReleaseOperand(right);
  3150. IF backend.cellsAreObjects THEN
  3151. CallThis(position,"ActiveCellsRuntime","SendNonBlocking",2);
  3152. ELSE
  3153. CallThis(position,ChannelModuleName,"SendNonBlocking",2);
  3154. END;
  3155. InitOperand(result, ModeValue);
  3156. result.op := NewRegisterOperand(bool);
  3157. Emit(Result(position,result.op));
  3158. IF conditional THEN
  3159. IntermediateCode.InitImmediate(zero,setType,0);
  3160. BrneL(trueLabel,result.op,zero);
  3161. ReleaseOperand(result);
  3162. BrL(falseLabel);
  3163. END;
  3164. ELSE
  3165. HALT(100);
  3166. END;
  3167. END;
  3168. destination := dest;
  3169. IF Trace THEN TraceExit("VisitBinaryExpression") END;
  3170. END VisitBinaryExpression;
  3171. PROCEDURE VisitRangeExpression(x: SyntaxTree.RangeExpression);
  3172. VAR localResult, operand: Operand;
  3173. BEGIN
  3174. IF Trace THEN TraceEnter("VisitRangeExpression") END;
  3175. InitOperand(localResult, ModeValue);
  3176. ASSERT(x.first # NIL);
  3177. Evaluate(x.first, operand);
  3178. localResult.op := operand.op;
  3179. ReleaseOperand(operand);
  3180. UseIntermediateOperand(localResult.op);
  3181. ASSERT(x.last # NIL);
  3182. Evaluate(x.last, operand);
  3183. localResult.tag := operand.op;
  3184. ReleaseOperand(operand);
  3185. UseIntermediateOperand(localResult.tag);
  3186. IF x.step # NIL THEN
  3187. Evaluate(x.step, operand);
  3188. localResult.extra := operand.op;
  3189. ReleaseOperand(operand);
  3190. UseIntermediateOperand(localResult.extra);
  3191. END;
  3192. result := localResult;
  3193. IF Trace THEN TraceExit("VisitRangeExpression") END
  3194. END VisitRangeExpression;
  3195. PROCEDURE VisitTensorRangeExpression*(x: SyntaxTree.TensorRangeExpression);
  3196. BEGIN
  3197. HALT(100); (* should never be evaluated *)
  3198. END VisitTensorRangeExpression;
  3199. PROCEDURE VisitConversion(x: SyntaxTree.Conversion);
  3200. VAR old: Operand; dest: IntermediateCode.Operand; componentType: SyntaxTree.Type;
  3201. BEGIN
  3202. IF Trace THEN TraceEnter("VisitConversion") END;
  3203. ASSERT(~(x.expression.type.resolved IS SyntaxTree.RangeType));
  3204. dest := destination; destination := emptyOperand;
  3205. Evaluate(x.expression,old);
  3206. InitOperand(result,ModeValue);
  3207. result.op := old.op;
  3208. ASSERT(result.op.mode # 0);
  3209. IF x.type.resolved IS SyntaxTree.ComplexType THEN
  3210. (* convert TO a complex number *)
  3211. componentType := x.type.resolved(SyntaxTree.ComplexType).componentType;
  3212. Convert(result.op,IntermediateCode.GetType(system, componentType));
  3213. ASSERT(result.op.mode # 0);
  3214. IF x.expression.type.resolved IS SyntaxTree.ComplexType THEN
  3215. (* convert FROM a complex number TO a complex number*)
  3216. result.tag := old.tag;
  3217. ASSERT(result.tag.mode # 0);
  3218. Convert(result.tag,IntermediateCode.GetType(system, componentType));
  3219. ASSERT(result.tag.mode # 0)
  3220. ELSE
  3221. ASSERT(componentType IS SyntaxTree.FloatType); (* this excludes complex types based on integer types *)
  3222. result.tag := IntermediateCode.FloatImmediate(IntermediateCode.GetType(system, componentType), 0); (* the imaginary part is set to 0 *)
  3223. END
  3224. ELSE
  3225. Convert(result.op,IntermediateCode.GetType(system,x.type));
  3226. ASSERT(result.op.mode # 0);
  3227. result.tag := old.tag; (*! probably never used *)
  3228. END;
  3229. destination := dest;
  3230. IF Trace THEN TraceExit("VisitConversion") END;
  3231. END VisitConversion;
  3232. PROCEDURE VisitTypeDeclaration(x: SyntaxTree.TypeDeclaration);
  3233. BEGIN
  3234. IF Trace THEN TraceEnter("VisitTypeDeclaration") END;
  3235. ASSERT((x.declaredType.resolved IS SyntaxTree.EnumerationType) OR (x.declaredType.resolved IS SyntaxTree.RecordType)
  3236. OR (x.declaredType.resolved IS SyntaxTree.PointerType) & (x.declaredType.resolved(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType));
  3237. IF Trace THEN TraceExit("VisitTypeDeclaration") END;
  3238. END VisitTypeDeclaration;
  3239. (** designators (expressions) *)
  3240. PROCEDURE VisitSymbolDesignator(x: SyntaxTree.SymbolDesignator);
  3241. VAR ownerType, designatorType: SyntaxTree.RecordType;
  3242. BEGIN
  3243. IF Trace THEN TraceEnter("VisitSymbolDesignator") END;
  3244. IF x.left # NIL THEN Expression(x.left) END;
  3245. Symbol(x.symbol,result);
  3246. IF backend.cooperative & (x.symbol IS SyntaxTree.Variable) & (x.symbol.scope IS SyntaxTree.RecordScope) THEN
  3247. ASSERT ((x.left # NIL) & (x.left.type.resolved IS SyntaxTree.RecordType));
  3248. ownerType := x.symbol.scope(SyntaxTree.RecordScope).ownerRecord;
  3249. designatorType := x.left.type.resolved(SyntaxTree.RecordType);
  3250. IF ~ownerType.isObject & designatorType.isObject & ~designatorType.pointerType.isPlain THEN
  3251. IntermediateCode.AddOffset(result.op,BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  3252. END;
  3253. END;
  3254. IF Trace THEN TraceExit("VisitSymbolDesignator") END;
  3255. END VisitSymbolDesignator;
  3256. PROCEDURE BoundCheck(index,length: IntermediateCode.Operand);
  3257. BEGIN
  3258. IF isUnchecked THEN RETURN END;
  3259. IF tagsAvailable THEN
  3260. TrapC(BrltL,index,length,IndexCheckTrap);
  3261. END;
  3262. END BoundCheck;
  3263. PROCEDURE DimensionCheck(base,dim: IntermediateCode.Operand; op: ConditionalBranch );
  3264. VAR d: IntermediateCode.Operand;
  3265. BEGIN
  3266. IF isUnchecked THEN RETURN END;
  3267. MakeMemory(d,base,dim.type,ToMemoryUnits(system,MathDimOffset * addressType.sizeInBits));
  3268. TrapC(op,dim,d,ArraySizeTrap);
  3269. ReleaseIntermediateOperand(d);
  3270. END DimensionCheck;
  3271. PROCEDURE MathIndexDesignator(x: SyntaxTree.IndexDesignator);
  3272. VAR
  3273. index, range, array, sourceLength, sourceIncrement, localResult: Operand;
  3274. firstIndex, lastIndex, stepSize, summand, targetLength, targetIncrement, tmp, srcDim, destDim: IntermediateCode.Operand;
  3275. expression: SyntaxTree.Expression;
  3276. resultingType, leftType, baseType: SyntaxTree.Type;
  3277. skipLabel1: Label;
  3278. i, indexListSize, indexDim, rangeCount, indexCount, tensorRangeCount, srcDimOffset, destDimOffset, targetArrayDimensionality: LONGINT;
  3279. staticSourceLength, staticSourceIncrement, staticIndex, staticFirstIndex, staticLastIndex, staticStepSize, staticTargetLength: LONGINT;
  3280. variableOp: Operand;
  3281. variable: SyntaxTree.Variable;
  3282. PROCEDURE CountIndices(parameters: SyntaxTree.ExpressionList; VAR indexCount: LONGINT; VAR rangeCount: LONGINT; VAR tensorRangeCount: LONGINT);
  3283. VAR expression: SyntaxTree.Expression;
  3284. BEGIN
  3285. (* count the number of indices, ranges and tensorRanges in the index list *)
  3286. indexCount := 0; rangeCount := 0; tensorRangeCount := 0;
  3287. FOR i := 0 TO parameters.Length() - 1 DO
  3288. expression := parameters.GetExpression(i);
  3289. IF expression IS SyntaxTree.TensorRangeExpression THEN INC(tensorRangeCount)
  3290. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.RangeType) THEN INC(indexCount)
  3291. ELSE INC(indexCount)
  3292. END
  3293. END;
  3294. END CountIndices;
  3295. BEGIN
  3296. ASSERT(tagsAvailable);
  3297. resultingType := x.type.resolved; (* resulting type *)
  3298. leftType := x.left.type.resolved; (* type of array to be indexed over *)
  3299. InitOperand(localResult, ModeReference);
  3300. IF (resultingType IS SyntaxTree.MathArrayType) & ( (resultingType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) OR NeedDescriptor) THEN
  3301. targetArrayDimensionality := resultingType(SyntaxTree.MathArrayType).Dimensionality();
  3302. IF arrayDestinationTag.mode # IntermediateCode.Undefined THEN
  3303. (* a globally defined array destination tag is available -> use and invalidate it*)
  3304. localResult.tag := arrayDestinationTag;
  3305. IntermediateCode.InitOperand(arrayDestinationTag)
  3306. ELSE
  3307. (* otherwise, create a temporary variable and use it to store the array destination tag *)
  3308. variable := GetTemporaryVariable(GetMathArrayDescriptorType(targetArrayDimensionality), FALSE);
  3309. Symbol(variable, variableOp);
  3310. ReuseCopy(localResult.tag, variableOp.op);
  3311. ReleaseOperand(variableOp);
  3312. END
  3313. END;
  3314. indexListSize := x.parameters.Length();
  3315. CountIndices(x.parameters, indexCount, rangeCount, tensorRangeCount);
  3316. ASSERT(tensorRangeCount <= 1);
  3317. (* designate the array to be indexed over, perform tensor range check if known *)
  3318. Designate(x.left, array);
  3319. IF leftType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  3320. Dereference(array, leftType,FALSE);
  3321. IF tensorRangeCount=0 THEN
  3322. DimensionCheck(array.tag, IntermediateCode.Immediate(int32, rangeCount+indexCount), BreqL)
  3323. END
  3324. END;
  3325. (* determine source and destination dimension offsets; this depends on if the list starts with a '?' *)
  3326. IF x.parameters.GetExpression(0) IS SyntaxTree.TensorRangeExpression THEN
  3327. srcDimOffset := -indexListSize;
  3328. destDimOffset := -rangeCount
  3329. ELSE
  3330. srcDimOffset := 0;
  3331. destDimOffset := 0
  3332. END;
  3333. indexDim := 0;
  3334. (* use address of source array as basis *)
  3335. (*
  3336. ReuseCopy(localResult.op, array.op);
  3337. *)
  3338. localResult.op := array.op;
  3339. UseIntermediateOperand(localResult.op);
  3340. (* go through the index list *)
  3341. FOR i := 0 TO indexListSize - 1 DO
  3342. expression := x.parameters.GetExpression(i);
  3343. IF expression IS SyntaxTree.TensorRangeExpression THEN
  3344. (* nothing to do *)
  3345. ELSE
  3346. (* determine which dimension of source array is currently looked at *)
  3347. IF srcDimOffset < 0 THEN (* tensor expression or the form a[?,i,j] *)
  3348. (* get the memory operand pointing to array descriptor dimension *)
  3349. GetMathArrayField(tmp, array.tag, MathDimOffset);
  3350. (* make a reusable register from it *)
  3351. ReuseCopy(srcDim, tmp);
  3352. ReleaseIntermediateOperand(tmp);
  3353. AddInt(srcDim, srcDim, IntermediateCode.Immediate(addressType, i + srcDimOffset));
  3354. ELSE
  3355. srcDim := IntermediateCode.Immediate(int32, i)
  3356. END;
  3357. (* get length and increment of source array for current dimension *)
  3358. GetMathArrayLength(leftType(SyntaxTree.MathArrayType), array, srcDim, FALSE, sourceLength);
  3359. Convert(sourceLength.op, sizeType);
  3360. GetMathArrayIncrement(leftType(SyntaxTree.MathArrayType), array, srcDim, FALSE, sourceIncrement);
  3361. Convert(sourceIncrement.op, sizeType);
  3362. (* release the dim operand, if dynamic. No register reuse to decrease register pressure *)
  3363. ReleaseIntermediateOperand(srcDim);
  3364. IF SemanticChecker.IsIntegerType(expression.type.resolved) THEN
  3365. (* SINGLE INDEX *)
  3366. Evaluate(expression, index);
  3367. ReleaseIntermediateOperand(index.tag);
  3368. index.tag := emptyOperand;
  3369. Convert(index.op, sizeType);
  3370. (* lower bound check *)
  3371. IF IsIntegerImmediate(index.op, staticIndex) THEN
  3372. ASSERT(staticIndex >= 0) (* ensured by the checker *)
  3373. ELSIF isUnchecked THEN (* do nothing *)
  3374. ELSE
  3375. TrapC(BrgeL, index.op, IntermediateCode.Immediate(sizeType, 0), IndexCheckTrap)
  3376. END;
  3377. (* upper bound check *)
  3378. IF IsIntegerImmediate(index.op, staticIndex) & IsIntegerImmediate(sourceLength.op, staticSourceLength) THEN
  3379. ASSERT(staticIndex < staticSourceLength) (* ensured by checker *)
  3380. ELSIF isUnchecked THEN (* do nothing *)
  3381. ELSE
  3382. TrapC(BrltL, index.op, sourceLength.op, IndexCheckTrap)
  3383. END;
  3384. ReleaseOperand(sourceLength);
  3385. Convert(index.op, addressType);
  3386. summand := index.op;
  3387. ELSIF expression.type.resolved IS SyntaxTree.RangeType THEN
  3388. (* RANGE OF INDICES *)
  3389. Evaluate(expression, range);
  3390. firstIndex := range.op; UseIntermediateOperand(firstIndex);
  3391. lastIndex := range.tag; UseIntermediateOperand(lastIndex);
  3392. stepSize := range.extra; UseIntermediateOperand(stepSize);
  3393. ReleaseOperand(range);
  3394. Convert(firstIndex, sizeType);
  3395. Convert(lastIndex, sizeType);
  3396. Convert(stepSize, sizeType);
  3397. (* for dynamic upper bounds: add a runtime check, which repaces the upper bound with the largest valid index
  3398. if it is 'MAX(LONGINT)' *)
  3399. IF ~IsIntegerImmediate(lastIndex, staticLastIndex) THEN
  3400. TransferToRegister(lastIndex, lastIndex);
  3401. skipLabel1 := NewLabel();
  3402. BrneL(skipLabel1, lastIndex, IntermediateCode.Immediate(sizeType, MAX(LONGINT)));
  3403. Emit(MovReplace(position,lastIndex, sourceLength.op)); (* make sure that no new register is allocated *)
  3404. Emit(Sub(position,lastIndex, lastIndex, IntermediateCode.Immediate(sizeType, 1)));
  3405. SetLabel(skipLabel1)
  3406. END;
  3407. (* check if step size is valid *)
  3408. IF IsIntegerImmediate(stepSize, staticStepSize) THEN
  3409. ASSERT(staticStepSize >= 1) (* ensured by the checker *)
  3410. ELSIF isUnchecked THEN (* do nothing *)
  3411. ELSE
  3412. TrapC(BrgeL, stepSize, IntermediateCode.Immediate(sizeType, 1), IndexCheckTrap)
  3413. END;
  3414. (* check lower bound check *)
  3415. IF IsIntegerImmediate(firstIndex, staticFirstIndex) THEN
  3416. ASSERT(staticFirstIndex >= 0) (* ensured by the checker *)
  3417. ELSIF isUnchecked THEN (* do nothing *)
  3418. ELSE
  3419. TrapC(BrgeL, firstIndex, IntermediateCode.Immediate(sizeType, 0), IndexCheckTrap)
  3420. END;
  3421. (* check upper bound check *)
  3422. IF IsIntegerImmediate(lastIndex, staticLastIndex) & (staticLastIndex = MAX(LONGINT)) THEN
  3423. (* statically open range: nothing to do *)
  3424. ELSIF IsIntegerImmediate(lastIndex, staticLastIndex) & IsIntegerImmediate(sourceLength.op, staticSourceLength) THEN
  3425. ASSERT(staticLastIndex < staticSourceLength)
  3426. ELSIF isUnchecked THEN (* do nothing *)
  3427. ELSE
  3428. TrapC(BrltL, lastIndex, sourceLength.op, IndexCheckTrap)
  3429. END;
  3430. (* determine length of target array for current dimension *)
  3431. (* 1. incorporate last index: *)
  3432. IF IsIntegerImmediate(lastIndex, staticLastIndex) THEN
  3433. (* last index is static *)
  3434. IF IsIntegerImmediate(lastIndex, staticLastIndex) & (staticLastIndex = MAX(LONGINT)) THEN
  3435. targetLength := sourceLength.op
  3436. ELSE
  3437. targetLength := IntermediateCode.Immediate(sizeType, staticLastIndex + 1)
  3438. END;
  3439. UseIntermediateOperand(targetLength);
  3440. ELSE
  3441. (* targetLength := lastIndex + 1
  3442. Reuse1(targetLength, lastIndex);
  3443. *)
  3444. AddInt(targetLength, lastIndex, IntermediateCode.Immediate(sizeType, 1));
  3445. END;
  3446. ReleaseOperand(sourceLength);
  3447. ReleaseIntermediateOperand(lastIndex);
  3448. (* 2. incorporate first index: *)
  3449. IF IsIntegerImmediate(firstIndex, staticFirstIndex) & IsIntegerImmediate(targetLength, staticTargetLength) THEN
  3450. (* first index and current target length are static *)
  3451. targetLength := IntermediateCode.Immediate(sizeType, staticTargetLength - staticFirstIndex)
  3452. ELSIF IsIntegerImmediate(firstIndex, staticFirstIndex) & (staticFirstIndex = 0) THEN
  3453. (* first index = 0: nothing to do *)
  3454. ELSE
  3455. (* targetLength := targetLength - firstIndex *)
  3456. TransferToRegister(targetLength, targetLength);
  3457. Emit(Sub(position,targetLength, targetLength, firstIndex))
  3458. END;
  3459. (* clip negative lengths to 0 *)
  3460. IF IsIntegerImmediate(targetLength, staticTargetLength) THEN
  3461. IF staticTargetLength < 0 THEN
  3462. targetLength := IntermediateCode.Immediate(sizeType, 0)
  3463. END
  3464. ELSE
  3465. skipLabel1 := NewLabel();
  3466. BrgeL(skipLabel1, targetLength, IntermediateCode.Immediate(sizeType, 0));
  3467. TransferToRegister(targetLength, targetLength);
  3468. Emit(Mov(position,targetLength, IntermediateCode.Immediate(sizeType, 0)));
  3469. SetLabel(skipLabel1)
  3470. END;
  3471. (* 3. incorporate index step size: *)
  3472. IF IsIntegerImmediate(stepSize, staticStepSize) & IsIntegerImmediate(targetLength, staticTargetLength) THEN
  3473. (*step size and current target length are static *)
  3474. staticTargetLength := (staticTargetLength-1) DIV staticStepSize + 1;
  3475. targetLength := IntermediateCode.Immediate(sizeType, staticTargetLength)
  3476. ELSIF IsIntegerImmediate(stepSize, staticStepSize) & (staticStepSize = 1) THEN
  3477. (* step size = 1: nothing to do *)
  3478. ELSE
  3479. (* emit code for this:
  3480. targetLength := (targetLength-1) DIV stepSize +1;
  3481. *)
  3482. AddInt(targetLength, targetLength, IntermediateCode.Immediate(sizeType, -1));
  3483. DivInt(targetLength, targetLength, stepSize);
  3484. AddInt(targetLength, targetLength, IntermediateCode.Immediate(sizeType, 1));
  3485. END;
  3486. (* determine increment of target array for current dimension *)
  3487. IF IsIntegerImmediate(sourceIncrement.op, staticSourceIncrement) & IsIntegerImmediate(stepSize, staticStepSize) THEN
  3488. targetIncrement := IntermediateCode.Immediate(sizeType, staticSourceIncrement * staticStepSize);
  3489. ELSIF IsIntegerImmediate(stepSize, staticStepSize) & (staticStepSize = 1) THEN
  3490. (* step size = 1 *)
  3491. targetIncrement := sourceIncrement.op;
  3492. UseIntermediateOperand(targetIncrement)
  3493. ELSE
  3494. (* targetIncrement := sourceIncrement * stepSize *)
  3495. Reuse1(targetIncrement, stepSize);
  3496. ASSERT((sourceIncrement.op.mode # IntermediateCode.ModeImmediate) OR (stepSize.mode # IntermediateCode.ModeImmediate));
  3497. MulInt(targetIncrement, sourceIncrement.op, stepSize);
  3498. END;
  3499. ReleaseIntermediateOperand(stepSize);
  3500. (* write length and increment of target array to descriptor *)
  3501. IF destDimOffset < 0 THEN
  3502. (* determine which dimension of target array is currently looked at *)
  3503. GetMathArrayField(tmp, array.tag, MathDimOffset);
  3504. TransferToRegister(destDim, tmp);
  3505. AddInt(destDim, destDim, IntermediateCode.Immediate(sizeType, indexDim + destDimOffset));
  3506. PutMathArrayLenOrIncr(localResult.tag, targetLength, destDim, FALSE);
  3507. PutMathArrayLenOrIncr(localResult.tag, targetIncrement, destDim, TRUE);
  3508. ReleaseIntermediateOperand(destDim)
  3509. ELSE
  3510. PutMathArrayLength(localResult.tag, targetLength, indexDim);
  3511. PutMathArrayIncrement(localResult.tag , targetIncrement, indexDim)
  3512. END;
  3513. ReleaseIntermediateOperand(targetLength); targetLength := nil;
  3514. ReleaseIntermediateOperand(targetIncrement); targetIncrement := nil;
  3515. INC(indexDim);
  3516. Convert(firstIndex, addressType);
  3517. TransferToRegister(summand, firstIndex);
  3518. ELSE HALT(100);
  3519. END;
  3520. (*
  3521. ASSERT((summand.mode # IntermediateCode.ModeImmediate) OR (sourceIncrement.op.mode # IntermediateCode.ModeImmediate));
  3522. *)
  3523. Convert(sourceIncrement.op, addressType);
  3524. Convert(summand, addressType);
  3525. MulInt(summand, summand, sourceIncrement.op);
  3526. ReleaseIntermediateOperand(sourceIncrement.op);
  3527. AddInt(localResult.op, localResult.op, summand);
  3528. ReleaseIntermediateOperand(summand);
  3529. END
  3530. END;
  3531. result := localResult;
  3532. IF (resultingType IS SyntaxTree.RecordType) & (resultingType(SyntaxTree.RecordType).pointerType = NIL) THEN
  3533. ReleaseIntermediateOperand(result.tag);
  3534. result.tag := TypeDescriptorAdr(resultingType);
  3535. IF ~newObjectFile THEN
  3536. IntermediateCode.MakeMemory(result.tag,addressType);
  3537. END;
  3538. ELSIF IsDelegate(resultingType) THEN
  3539. ReleaseIntermediateOperand(result.tag);
  3540. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  3541. UseIntermediateOperand(result.tag);
  3542. ELSIF (resultingType IS SyntaxTree.ArrayType) & (resultingType(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  3543. ReleaseIntermediateOperand(result.tag);
  3544. IntermediateCode.InitImmediate(result.tag,addressType,resultingType(SyntaxTree.ArrayType).staticLength);
  3545. ELSIF (resultingType IS SyntaxTree.ArrayType) THEN
  3546. result.tag := array.tag; UseIntermediateOperand(result.tag); result.dimOffset := array.dimOffset+indexListSize-1;
  3547. ELSIF (resultingType IS SyntaxTree.MathArrayType) & ( (resultingType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) OR NeedDescriptor) THEN
  3548. (* finalize target array descriptor *)
  3549. ASSERT(result.tag.mode # IntermediateCode.Undefined); (* tag has been already set in the beginning *)
  3550. (* write lengths and increments of target array for remaining dimensions *)
  3551. i := indexListSize;
  3552. WHILE indexDim < targetArrayDimensionality DO
  3553. GetMathArrayLengthAt(leftType(SyntaxTree.MathArrayType),array,i,FALSE, sourceLength);
  3554. PutMathArrayLength(result.tag, sourceLength.op,indexDim);
  3555. ReleaseOperand(sourceLength);
  3556. GetMathArrayIncrementAt(leftType(SyntaxTree.MathArrayType),array,i,FALSE,sourceIncrement);
  3557. PutMathArrayIncrement(result.tag, sourceIncrement.op,indexDim);
  3558. ReleaseOperand(sourceIncrement);
  3559. INC(i); INC(indexDim);
  3560. END;
  3561. IF leftType(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  3562. tmp := nil;
  3563. ELSE
  3564. GetMathArrayField(tmp,array.tag,MathPtrOffset);
  3565. END;
  3566. PutMathArrayField(result.tag, tmp, MathPtrOffset);
  3567. ReleaseIntermediateOperand(tmp);
  3568. IF leftType(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  3569. baseType := SemanticChecker.ArrayBase(resultingType, indexDim);
  3570. tmp := IntermediateCode.Immediate(addressType, ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  3571. ELSE
  3572. GetMathArrayField(tmp,array.tag, MathElementSizeOffset);
  3573. END;
  3574. PutMathArrayField(result.tag, tmp, MathElementSizeOffset);
  3575. ReleaseIntermediateOperand(tmp);
  3576. PutMathArrayField(result.tag, result.op, MathAdrOffset);
  3577. (* write dimensionality *)
  3578. IF targetArrayDimensionality # 0 THEN
  3579. PutMathArrayField(result.tag, IntermediateCode.Immediate(addressType, targetArrayDimensionality),MathDimOffset);
  3580. END;
  3581. PutMathArrayField(result.tag, IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{RangeFlag})),MathFlagsOffset);
  3582. END;
  3583. ReleaseOperand(array);
  3584. IF conditional & (resultingType.resolved IS SyntaxTree.BooleanType) THEN
  3585. ValueToCondition(result); (*! wrong as the result of an index designator is always an address *)
  3586. END;
  3587. END MathIndexDesignator;
  3588. (* TENTATIVE *)
  3589. PROCEDURE DumpOperand(operand: Operand);
  3590. BEGIN
  3591. D.Log.String(" op = ");
  3592. IntermediateCode.DumpOperand(D.Log, operand.op );
  3593. D.Log.Ln;
  3594. D.Log.String(" tag = ");
  3595. IntermediateCode.DumpOperand(D.Log, operand.tag );
  3596. D.Log.Ln;
  3597. D.Log.String(" extra = ");
  3598. IntermediateCode.DumpOperand(D.Log, operand.extra );
  3599. D.Log.Ln;
  3600. D.Log.Update
  3601. END DumpOperand;
  3602. (* get the length of an array , trying to make use of static information *)
  3603. PROCEDURE ArrayLength(type: SyntaxTree.Type; dim: LONGINT; tag: IntermediateCode.Operand): IntermediateCode.Operand;
  3604. VAR res: IntermediateCode.Operand; size: LONGINT;
  3605. BEGIN
  3606. type := type.resolved;
  3607. IF type IS SyntaxTree.ArrayType THEN
  3608. WITH type: SyntaxTree.ArrayType DO
  3609. IF type.form = SyntaxTree.Static THEN
  3610. RETURN IntermediateCode.Immediate(addressType,type.staticLength);
  3611. (*ELSIF (type.form = SyntaxTree.SemiDynamic) & backend.cellsAreObjects THEN
  3612. Evaluate(type.length, op);
  3613. ReleaseIntermediateOperand(op.tag);
  3614. RETURN op.op;*)
  3615. ELSE
  3616. res := tag;
  3617. IntermediateCode.AddOffset(res,ToMemoryUnits(system,addressType.sizeInBits*(DynamicDim(type)-1)));
  3618. IntermediateCode.MakeMemory(res,addressType);
  3619. UseIntermediateOperand(res);
  3620. RETURN res
  3621. END
  3622. END;
  3623. ELSE
  3624. size := ToMemoryUnits(system,system.AlignedSizeOf(type));
  3625. RETURN IntermediateCode.Immediate(addressType,size);
  3626. END;
  3627. END ArrayLength;
  3628. PROCEDURE AddInt(VAR res: IntermediateCode.Operand; x,y: IntermediateCode.Operand);
  3629. BEGIN
  3630. ReleaseIntermediateOperand(res);
  3631. IF IsImmediate(x) & IsImmediate(y) THEN
  3632. IntermediateCode.InitImmediate(res,x.type,x.intValue+y.intValue);
  3633. ELSIF IsAddress(x) & IsImmediate(y) THEN
  3634. IntermediateCode.InitAddress(res,x.type,x.symbol.name, x.symbol.fingerprint, x.symbolOffset);
  3635. IntermediateCode.AddOffset(res, LONGINT(y.intValue)+x.offset);
  3636. ELSIF IsAddress(y) & IsImmediate(x) THEN
  3637. IntermediateCode.InitAddress(res,y.type,y.symbol.name, y.symbol.fingerprint, y.symbolOffset);
  3638. IntermediateCode.AddOffset(res, LONGINT(x.intValue)+y.offset);
  3639. ELSIF IsRegister(x) & IsImmediate(y) THEN
  3640. IntermediateCode.InitRegister(res, x.type, x.registerClass, x.register);
  3641. IntermediateCode.AddOffset(res, x.offset + LONGINT(y.intValue));
  3642. UseIntermediateOperand(res);
  3643. ELSIF IsRegister(y) & IsImmediate(x) THEN
  3644. IntermediateCode.InitRegister(res, y.type, y.registerClass, y.register);
  3645. IntermediateCode.AddOffset(res, y.offset + LONGINT(x.intValue));
  3646. UseIntermediateOperand(res);
  3647. ELSE
  3648. IF ~ReusableRegister(res) THEN
  3649. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3650. ELSE
  3651. UseIntermediateOperand(res);
  3652. END;
  3653. IF IsImmediate(x) & (x.intValue = 0) THEN
  3654. Emit(Mov(position,res,y))
  3655. ELSIF IsImmediate(y) & (y.intValue=0) THEN
  3656. Emit(Mov(position,res,x))
  3657. ELSE
  3658. Emit(Add(position,res, x, y));
  3659. END;
  3660. END;
  3661. END AddInt;
  3662. PROCEDURE MulInt(VAR res: IntermediateCode.Operand; x,y: IntermediateCode.Operand);
  3663. BEGIN
  3664. ReleaseIntermediateOperand(res);
  3665. IF IsImmediate(x) & IsImmediate(y) THEN
  3666. IntermediateCode.InitImmediate(res,x.type,x.intValue*y.intValue);
  3667. ELSE
  3668. IF ~ReusableRegister(res) THEN
  3669. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3670. ELSE UseIntermediateOperand(res);
  3671. END;
  3672. IF IsImmediate(x) & (x.intValue = 1) THEN
  3673. Emit(Mov(position,res,y))
  3674. ELSIF IsImmediate(y) & (y.intValue=1) THEN
  3675. Emit(Mov(position,res,x))
  3676. ELSE
  3677. Emit(Mul(position,res, x, y));
  3678. END;
  3679. END;
  3680. END MulInt;
  3681. PROCEDURE DivInt(VAR res: IntermediateCode.Operand; x,y: IntermediateCode.Operand);
  3682. BEGIN
  3683. ReleaseIntermediateOperand(res);
  3684. IF IsImmediate(x) & IsImmediate(y) THEN
  3685. IntermediateCode.InitImmediate(res,x.type,x.intValue DIV y.intValue);
  3686. ELSE
  3687. IF ~ReusableRegister(res) THEN
  3688. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3689. ELSE UseIntermediateOperand(res);
  3690. END;
  3691. IF IsImmediate(x) & (x.intValue = 1) THEN
  3692. Emit(Mov(position,res,y))
  3693. ELSIF IsImmediate(y) & (y.intValue=1) THEN
  3694. Emit(Mov(position,res,x))
  3695. ELSE
  3696. Emit(Div(position,res, x, y));
  3697. END;
  3698. END;
  3699. END DivInt;
  3700. PROCEDURE IndexDesignator(x: SyntaxTree.IndexDesignator);
  3701. VAR length,res: IntermediateCode.Operand; type: SyntaxTree.Type; maxDim: LONGINT; array:Operand;
  3702. index: Operand; e: SyntaxTree.Expression;i: LONGINT; size: LONGINT; atype: SyntaxTree.ArrayType;
  3703. BEGIN
  3704. type := x.left.type.resolved;
  3705. IF type IS SyntaxTree.StringType THEN
  3706. atype := SyntaxTree.NewArrayType(-1, NIL, SyntaxTree.Static);
  3707. atype.SetArrayBase(type(SyntaxTree.StringType).baseType);
  3708. atype.SetLength(Global.NewIntegerValue(system,-1, type(SyntaxTree.StringType).length));
  3709. type := atype;
  3710. x.left.SetType(type);
  3711. END;
  3712. IntermediateCode.InitImmediate(res,addressType,0);
  3713. maxDim := x.parameters.Length()-1;
  3714. FOR i := 0 TO maxDim DO
  3715. e := x.parameters.GetExpression(i);
  3716. Evaluate(e,index);
  3717. Convert(index.op,addressType);
  3718. AddInt(res, res, index.op);
  3719. IF i = 0 THEN
  3720. (*
  3721. ReuseCopy(res, index.op);
  3722. *)
  3723. Designate(x.left,array);
  3724. type := x.left.type.resolved;
  3725. IF (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) & backend.cellsAreObjects THEN
  3726. Dereference(array, type, FALSE);
  3727. END;
  3728. (*
  3729. ELSE AddInt(res, res, index.op);
  3730. *)
  3731. END;
  3732. IF (array.tag.mode # IntermediateCode.Undefined ) THEN
  3733. length := ArrayLength(type(SyntaxTree.ArrayType),array.dimOffset+i,array.tag);
  3734. IF ((length.mode # IntermediateCode.ModeImmediate) OR (index.op.mode # IntermediateCode.ModeImmediate)) & tagsAvailable THEN
  3735. BoundCheck(index.op, length);
  3736. END;
  3737. ReleaseIntermediateOperand(length);
  3738. END;
  3739. ReleaseOperand(index);
  3740. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  3741. length := ArrayLength(type,array.dimOffset+i-1,array.tag);
  3742. IF (length.mode # IntermediateCode.ModeImmediate) OR (length.intValue # 1) THEN
  3743. MulInt(res,res,length);
  3744. END;
  3745. ReleaseIntermediateOperand(length);
  3746. END;
  3747. IF (type IS SyntaxTree.ArrayType) THEN
  3748. IF (type(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  3749. size := StaticSize(system, type);
  3750. IF size # 1 THEN
  3751. length := IntermediateCode.Immediate(addressType,size);
  3752. MulInt(res,res,length);
  3753. END;
  3754. ELSE
  3755. size := StaticSize(system, type(SyntaxTree.ArrayType).arrayBase);
  3756. IF size # 1 THEN
  3757. length := IntermediateCode.Immediate(addressType,size);
  3758. MulInt(res,res,length);
  3759. END;
  3760. END;
  3761. END;
  3762. AddInt(res,res,array.op);
  3763. InitOperand(result,ModeReference);
  3764. result.op := res;
  3765. IF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType = NIL) THEN
  3766. ReleaseIntermediateOperand(result.tag);
  3767. result.tag := TypeDescriptorAdr(type);
  3768. IF ~newObjectFile THEN
  3769. IntermediateCode.MakeMemory(result.tag,addressType);
  3770. END
  3771. ELSIF IsDelegate(type) THEN
  3772. ReleaseIntermediateOperand(result.tag);
  3773. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  3774. UseIntermediateOperand(result.tag);
  3775. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  3776. ReleaseIntermediateOperand(result.tag);
  3777. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  3778. ELSIF (type IS SyntaxTree.ArrayType) THEN
  3779. result.tag := array.tag; UseIntermediateOperand(result.tag); result.dimOffset := array.dimOffset+maxDim;
  3780. END;
  3781. ReleaseOperand(array);
  3782. IF (conditional) & (type.resolved IS SyntaxTree.BooleanType) THEN
  3783. ValueToCondition(result); (*! wrong as the result of an index designator is always an address *)
  3784. END;
  3785. END IndexDesignator;
  3786. PROCEDURE VisitIndexDesignator(x: SyntaxTree.IndexDesignator);
  3787. VAR type: SyntaxTree.Type; dest: IntermediateCode.Operand;
  3788. BEGIN
  3789. IF Trace THEN TraceEnter("VisitIndexDesignator") END;
  3790. dest := destination; destination := emptyOperand;
  3791. type := x.left.type.resolved;
  3792. IF type IS SyntaxTree.MathArrayType THEN
  3793. MathIndexDesignator(x);
  3794. ELSE ASSERT((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.StringType));
  3795. IndexDesignator(x);
  3796. END;
  3797. destination := dest;
  3798. IF Trace THEN TraceExit("VisitIndexDesignator") END;
  3799. END VisitIndexDesignator;
  3800. PROCEDURE PrepareTensorDescriptor(expression: SyntaxTree.IndexDesignator): SyntaxTree.Variable;
  3801. VAR variable: SyntaxTree.Variable; srcOperand,destOperand,procOp: Operand;
  3802. moduleName, procedureName: SyntaxTree.IdentifierString; arrayBase: SyntaxTree.Module; saved: RegisterEntry; s: Basic.MessageString;
  3803. procedure: SyntaxTree.Procedure;
  3804. parameters: SyntaxTree.ExpressionList; e: SyntaxTree.Expression;
  3805. prefixIndices, prefixRanges, suffixIndices, suffixRanges,i : LONGINT; tensorFound: BOOLEAN;
  3806. BEGIN
  3807. variable := GetTemporaryVariable(expression.left.type, FALSE);
  3808. parameters := expression.parameters;
  3809. moduleName := "FoxArrayBase";
  3810. procedureName := "CopyDescriptor";
  3811. IF AddImport(moduleName,arrayBase,TRUE) THEN
  3812. SaveRegisters();ReleaseUsedRegisters(saved);
  3813. procedure := arrayBase.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  3814. IF procedure = NIL THEN
  3815. s := "procedure ";
  3816. Strings.Append(s,moduleName);
  3817. Strings.Append(s,".");
  3818. Strings.Append(s,procedureName);
  3819. Strings.Append(s," not present");
  3820. Error(position,s);
  3821. ELSE
  3822. (* push address of temporary variable *)
  3823. Symbol(variable,destOperand);
  3824. Emit(Push(position,destOperand.op));
  3825. ReleaseOperand(destOperand);
  3826. (* push src *)
  3827. Evaluate(expression.left,srcOperand);
  3828. (*
  3829. Dereference(srcOperand,expression.type.resolved);
  3830. Emit(Push(position,srcOperand.tag));
  3831. *)
  3832. Emit(Push(position,srcOperand.op));
  3833. ReleaseOperand(srcOperand);
  3834. tensorFound := FALSE;
  3835. FOR i := 0 TO parameters.Length()-1 DO
  3836. e := parameters.GetExpression(i);
  3837. IF e IS SyntaxTree.TensorRangeExpression THEN
  3838. tensorFound := TRUE;
  3839. ELSIF e IS SyntaxTree.RangeExpression THEN
  3840. IF tensorFound THEN INC(suffixRanges) ELSE INC(prefixRanges) END;
  3841. ELSE
  3842. IF tensorFound THEN INC(suffixIndices) ELSE INC(prefixIndices) END;
  3843. END;
  3844. END;
  3845. Emit(Push(position,IntermediateCode.Immediate(int32,prefixIndices)));
  3846. Emit(Push(position,IntermediateCode.Immediate(int32,prefixRanges)));
  3847. Emit(Push(position,IntermediateCode.Immediate(int32,suffixIndices)));
  3848. Emit(Push(position,IntermediateCode.Immediate(int32,suffixRanges)));
  3849. StaticCallOperand(procOp,procedure);
  3850. Emit(Call(position,procOp.op,ProcedureParametersSize(system,procedure)));
  3851. ReleaseOperand(procOp);
  3852. END;
  3853. RestoreRegisters(saved);
  3854. END;
  3855. RETURN variable
  3856. END PrepareTensorDescriptor;
  3857. PROCEDURE PushParameter(expression: SyntaxTree.Expression; parameter: SyntaxTree.Parameter; callingConvention: LONGINT; needsParameterBackup: BOOLEAN; VAR parameterBackup: IntermediateCode.Operand; numberRegister: LONGINT);
  3858. VAR
  3859. type, descriptorType, baseType: SyntaxTree.Type;
  3860. operand, tmpOperand, variableOp, variable2Op: Operand;
  3861. baseReg, tmp, dimOp, null, dst: IntermediateCode.Operand;
  3862. variable, variable2: SyntaxTree.Variable;
  3863. dim, i, size: LONGINT;
  3864. (* TODO: needed? *)
  3865. oldArrayDestinationTag: IntermediateCode.Operand;
  3866. oldArrayDestinationDimension: LONGINT;
  3867. position: LONGINT;
  3868. saved: RegisterEntry;
  3869. PROCEDURE Pass(op: IntermediateCode.Operand);
  3870. VAR registerClass: IntermediateCode.RegisterClass; parameterRegister: IntermediateCode.Operand;
  3871. BEGIN
  3872. IF numberRegister >= 0 THEN
  3873. IntermediateCode.InitRegisterClass(registerClass, IntermediateCode.Parameter, SHORT(numberRegister));
  3874. IntermediateCode.InitRegister(parameterRegister, op.type, registerClass, AcquireRegister(op.type, registerClass));
  3875. Emit(Mov(position,parameterRegister, op));
  3876. ELSE
  3877. Emit(Push(position,op))
  3878. END
  3879. END Pass;
  3880. PROCEDURE PushArrayLens(formalType,actualType: SyntaxTree.Type; dim: LONGINT);
  3881. VAR tmp: IntermediateCode.Operand; actualArrayBase: SyntaxTree.Type;
  3882. BEGIN
  3883. formalType := formalType.resolved; actualType := actualType.resolved;
  3884. IF IsOpenArray(formalType)THEN
  3885. IF actualType IS SyntaxTree.StringType THEN
  3886. Pass((IntermediateCode.Immediate(addressType,actualType(SyntaxTree.StringType).length)));
  3887. RETURN;
  3888. ELSIF (actualType IS SyntaxTree.MathArrayType) & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  3889. Pass((IntermediateCode.Immediate(addressType,actualType(SyntaxTree.MathArrayType).staticLength)));
  3890. actualArrayBase := actualType(SyntaxTree.MathArrayType).arrayBase.resolved;
  3891. ELSIF actualType(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  3892. Pass((IntermediateCode.Immediate(addressType,actualType(SyntaxTree.ArrayType).staticLength)));
  3893. actualArrayBase := actualType(SyntaxTree.ArrayType).arrayBase.resolved;
  3894. ELSE
  3895. tmp := baseReg;
  3896. IntermediateCode.AddOffset(tmp,ToMemoryUnits(system,dim*system.addressSize));
  3897. IntermediateCode.MakeMemory(tmp,addressType);
  3898. Pass((tmp));
  3899. actualArrayBase := actualType(SyntaxTree.ArrayType).arrayBase.resolved;
  3900. END;
  3901. PushArrayLens(formalType(SyntaxTree.ArrayType).arrayBase.resolved, actualArrayBase,dim-1);
  3902. END;
  3903. END PushArrayLens;
  3904. BEGIN
  3905. IF Trace THEN TraceEnter("PushParameter") END;
  3906. position := expression.position;
  3907. IF expression.resolved # NIL THEN expression := expression.resolved END;
  3908. type := expression.type.resolved;
  3909. ASSERT( ((type IS SyntaxTree.MathArrayType) = (parameter.type.resolved IS SyntaxTree.MathArrayType))
  3910. OR (type IS SyntaxTree.MathArrayType) & (parameter.type.resolved IS SyntaxTree.ArrayType)
  3911. & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  3912. & (parameter.type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.Open)
  3913. );
  3914. (* TODO: needed? *)
  3915. oldArrayDestinationTag := arrayDestinationTag;
  3916. oldArrayDestinationDimension := arrayDestinationDimension;
  3917. IF IsArrayOfSystemByte(parameter.type) THEN
  3918. Designate(expression,operand);
  3919. tmp := GetArrayOfBytesSize(expression,operand.tag);
  3920. ReleaseIntermediateOperand(operand.tag);
  3921. operand.tag := tmp;
  3922. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  3923. Pass((operand.tag));
  3924. END;
  3925. Pass((operand.op));
  3926. ELSIF IsOpenArray(parameter.type) THEN
  3927. Designate(expression,operand);
  3928. baseReg := operand.tag;
  3929. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  3930. PushArrayLens(parameter.type,type,operand.dimOffset+DynamicDim(parameter.type)-1);
  3931. END;
  3932. Pass((operand.op)); (* address of the array *)
  3933. ELSIF parameter.type.resolved IS SyntaxTree.MathArrayType THEN
  3934. (* case 1 *)
  3935. IF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) &
  3936. (parameter.kind IN {SyntaxTree.ValueParameter, SyntaxTree.ConstParameter}) THEN
  3937. size := MathLenOffset + 2*SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  3938. size := ToMemoryUnits(system,size*addressType.sizeInBits);
  3939. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,size)));
  3940. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  3941. arrayDestinationTag := sp;
  3942. (* case 1b *)
  3943. IF expression IS SyntaxTree.IndexDesignator THEN
  3944. (*
  3945. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  3946. descriptorType := GetMathArrayDescriptorType(dim);
  3947. variable := GetTemporaryVariable(descriptorType,expression.position);
  3948. Symbol(variable,variableOp);
  3949. arrayDestinationTag := variableOp.op;
  3950. *)
  3951. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  3952. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  3953. arrayDestinationDimension := dim;
  3954. Designate(expression,operand);
  3955. (* case 1a *)
  3956. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  3957. Designate(expression,operand);
  3958. Emit(Copy(position,arrayDestinationTag,operand.tag,IntermediateCode.Immediate(addressType,size)));
  3959. i := 0;
  3960. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  3961. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  3962. INC(i);
  3963. END;
  3964. type := expression.type.resolved;
  3965. WHILE (i<dim) DO (* remaining static dimensions *)
  3966. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  3967. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  3968. ReleaseOperand(tmpOperand);
  3969. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  3970. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  3971. ReleaseOperand(tmpOperand);
  3972. INC(i);
  3973. END;
  3974. dimOp := IntermediateCode.Immediate(addressType,dim);
  3975. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  3976. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset);
  3977. (* case 1d *)
  3978. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  3979. Designate(expression,operand);
  3980. Dereference(operand,type.resolved,FALSE);
  3981. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  3982. Emit(Copy(position,sp(*arrayDestinationTag*),operand.tag,IntermediateCode.Immediate(addressType,size)));
  3983. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset);
  3984. (* case 1f *)
  3985. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  3986. Designate(expression,operand);
  3987. FOR i := 0 TO dim-1 DO
  3988. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  3989. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  3990. ReleaseOperand(tmpOperand);
  3991. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  3992. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  3993. ReleaseOperand(tmpOperand);
  3994. END;
  3995. dimOp := IntermediateCode.Immediate(addressType,dim);
  3996. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  3997. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  3998. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  3999. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4000. baseType := SemanticChecker.ArrayBase(type,dim);
  4001. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4002. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4003. ELSE HALT(100);
  4004. END;
  4005. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) & (parameter.kind = SyntaxTree.VarParameter) THEN
  4006. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4007. (* case 2b *)
  4008. IF expression IS SyntaxTree.IndexDesignator THEN
  4009. descriptorType := GetMathArrayDescriptorType(dim);
  4010. variable := GetTemporaryVariable(descriptorType, FALSE);
  4011. Symbol(variable,variableOp);
  4012. arrayDestinationTag := variableOp.op;
  4013. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4014. arrayDestinationDimension := dim;
  4015. NeedDescriptor := TRUE;
  4016. Designate(expression,operand);
  4017. Pass((operand.tag));
  4018. NeedDescriptor := FALSE;
  4019. (* case 2a *)
  4020. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4021. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4022. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4023. INC(i);
  4024. END;
  4025. IF i = dim THEN
  4026. Designate(expression,operand);
  4027. Pass((operand.tag));
  4028. ELSE (* open-static *)
  4029. type := expression.type.resolved;
  4030. descriptorType := GetMathArrayDescriptorType(dim);
  4031. variable := GetTemporaryVariable(descriptorType, FALSE);
  4032. Symbol(variable,variableOp);
  4033. arrayDestinationTag := variableOp.op;
  4034. Designate(expression,operand);
  4035. FOR i := 0 TO dim-1 DO
  4036. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4037. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4038. ReleaseOperand(tmpOperand);
  4039. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4040. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4041. ReleaseOperand(tmpOperand);
  4042. END;
  4043. dimOp := IntermediateCode.Immediate(addressType,dim);
  4044. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4045. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4046. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4047. PutMathArrayField(arrayDestinationTag, IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4048. baseType := SemanticChecker.ArrayBase(type,dim);
  4049. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4050. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4051. Pass((arrayDestinationTag));
  4052. END;
  4053. (* case 2d *)
  4054. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4055. Designate(expression,operand);
  4056. Dereference(operand,type.resolved,FALSE);
  4057. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4058. Pass((operand.tag));
  4059. (* case 2f *)
  4060. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4061. descriptorType := GetMathArrayDescriptorType(dim);
  4062. variable := GetTemporaryVariable(descriptorType, FALSE);
  4063. Symbol(variable,variableOp);
  4064. arrayDestinationTag := variableOp.op;
  4065. Designate(expression,operand);
  4066. FOR i := 0 TO dim-1 DO
  4067. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4068. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4069. ReleaseOperand(tmpOperand);
  4070. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4071. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4072. ReleaseOperand(tmpOperand);
  4073. END;
  4074. dimOp := IntermediateCode.Immediate(addressType,dim);
  4075. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4076. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4077. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4078. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4079. baseType := SemanticChecker.ArrayBase(type,dim);
  4080. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4081. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4082. Pass((arrayDestinationTag));
  4083. ELSE HALT(100);
  4084. END;
  4085. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (parameter.kind IN {SyntaxTree.ConstParameter,SyntaxTree.ValueParameter}) THEN
  4086. dim := SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static});
  4087. (* case 3b *)
  4088. IF (expression IS SyntaxTree.IndexDesignator) & (type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  4089. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN (* indexer of form a[e,....,?] *)
  4090. variable := PrepareTensorDescriptor(expression(SyntaxTree.IndexDesignator));
  4091. Symbol(variable,variableOp);
  4092. LoadValue(variableOp,system.addressType);
  4093. ELSE
  4094. descriptorType := GetMathArrayDescriptorType(dim);
  4095. variable := GetTemporaryVariable(descriptorType, FALSE);
  4096. Symbol(variable,variableOp);
  4097. END;
  4098. arrayDestinationTag := variableOp.op;
  4099. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4100. arrayDestinationDimension := 0;
  4101. Designate(expression,operand);
  4102. Pass((operand.tag));
  4103. (* case 3a *)
  4104. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4105. i := 0;
  4106. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4107. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4108. INC(i);
  4109. END;
  4110. IF i = dim THEN
  4111. Designate(expression,operand);
  4112. Pass((operand.tag));
  4113. ELSE (* open-static *)
  4114. type := expression.type.resolved;
  4115. descriptorType := GetMathArrayDescriptorType(dim);
  4116. variable := GetTemporaryVariable(descriptorType, FALSE);
  4117. Symbol(variable,variableOp);
  4118. arrayDestinationTag := variableOp.op;
  4119. Designate(expression,operand);
  4120. FOR i := 0 TO dim-1 DO
  4121. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4122. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4123. ReleaseOperand(tmpOperand);
  4124. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4125. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4126. ReleaseOperand(tmpOperand);
  4127. END;
  4128. dimOp := IntermediateCode.Immediate(addressType,dim);
  4129. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4130. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4131. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4132. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset); (* static flag ? *)
  4133. baseType := SemanticChecker.ArrayBase(type,dim);
  4134. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4135. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4136. Pass((arrayDestinationTag));
  4137. END;
  4138. (* case 3d *)
  4139. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4140. Designate(expression,operand);
  4141. Dereference(operand,type.resolved,FALSE);
  4142. (*
  4143. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4144. *)
  4145. Pass((operand.tag));
  4146. (* case 3f *)
  4147. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4148. descriptorType := GetMathArrayDescriptorType(dim);
  4149. variable := GetTemporaryVariable(descriptorType, FALSE);
  4150. Symbol(variable,variableOp);
  4151. arrayDestinationTag := variableOp.op;
  4152. Designate(expression,operand);
  4153. IF operand.op.type.length >1 THEN (* vector register *)
  4154. variable2 := GetTemporaryVariable(type, FALSE);
  4155. Symbol(variable2, variable2Op);
  4156. MakeMemory(tmp,variable2Op.op,operand.op.type,0);
  4157. Emit(Mov(position,tmp, operand.op));
  4158. ReleaseOperand(operand);
  4159. Symbol(variable2, operand);
  4160. END;
  4161. FOR i := 0 TO dim-1 DO
  4162. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4163. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4164. ReleaseOperand(tmpOperand);
  4165. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4166. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4167. ReleaseOperand(tmpOperand);
  4168. END;
  4169. dimOp := IntermediateCode.Immediate(addressType,dim);
  4170. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4171. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4172. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4173. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4174. baseType := SemanticChecker.ArrayBase(type,dim);
  4175. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4176. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4177. Pass((arrayDestinationTag));
  4178. ELSE HALT(100);
  4179. END;
  4180. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (parameter.kind = SyntaxTree.VarParameter) THEN
  4181. dim := SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static});
  4182. (* case 4b *)
  4183. IF (expression IS SyntaxTree.IndexDesignator) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  4184. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN (* indexer of form a[e,....,?] *)
  4185. variable := PrepareTensorDescriptor(expression(SyntaxTree.IndexDesignator));
  4186. Symbol(variable,variableOp);
  4187. LoadValue(variableOp,system.addressType);
  4188. ELSE
  4189. descriptorType := GetMathArrayDescriptorType(dim);
  4190. variable := GetTemporaryVariable(descriptorType, FALSE);
  4191. Symbol(variable,variableOp);
  4192. END;
  4193. arrayDestinationTag := variableOp.op;
  4194. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4195. arrayDestinationDimension := 0;
  4196. Designate(expression,operand);
  4197. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4198. Symbol(variable,variableOp);
  4199. ELSE
  4200. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4201. Symbol(variable,variableOp);
  4202. MakeMemory(tmp,variableOp.op,addressType,0);
  4203. Emit(Mov(position,tmp,operand.tag));
  4204. ReleaseIntermediateOperand(tmp);
  4205. END;
  4206. Pass((variableOp.op));
  4207. ReleaseOperand(variableOp);
  4208. (* case 4a *)
  4209. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4210. i := 0;
  4211. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4212. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4213. INC(i);
  4214. END;
  4215. IF i = dim THEN
  4216. Designate(expression,operand);
  4217. arrayDestinationTag := operand.tag;
  4218. ELSE (* open-static *)
  4219. type := expression.type.resolved;
  4220. descriptorType := GetMathArrayDescriptorType(dim);
  4221. variable := GetTemporaryVariable(descriptorType, FALSE);
  4222. Symbol(variable,variableOp);
  4223. arrayDestinationTag := variableOp.op;
  4224. Designate(expression,operand);
  4225. FOR i := 0 TO dim-1 DO
  4226. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4227. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4228. ReleaseOperand(tmpOperand);
  4229. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4230. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4231. ReleaseOperand(tmpOperand);
  4232. END;
  4233. dimOp := IntermediateCode.Immediate(addressType,dim);
  4234. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4235. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4236. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4237. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset);
  4238. baseType := SemanticChecker.ArrayBase(type,dim);
  4239. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4240. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4241. END;
  4242. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4243. Symbol(variable,variableOp);
  4244. MakeMemory(tmp,variableOp.op,addressType,0);
  4245. Emit(Mov(position,tmp,arrayDestinationTag));
  4246. ReleaseIntermediateOperand(tmp);
  4247. Pass((variableOp.op));
  4248. ReleaseOperand(variableOp);
  4249. (* case 4d *)
  4250. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4251. Designate(expression,operand);
  4252. (*
  4253. Dereference(operand,type.resolved,FALSE);
  4254. *)
  4255. (*
  4256. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4257. *)
  4258. Pass((operand.op));
  4259. (* case 4f *)
  4260. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4261. descriptorType := GetMathArrayDescriptorType(dim);
  4262. variable := GetTemporaryVariable(descriptorType, FALSE);
  4263. Symbol(variable,variableOp);
  4264. arrayDestinationTag := variableOp.op;
  4265. Designate(expression,operand);
  4266. FOR i := 0 TO dim-1 DO
  4267. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4268. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4269. ReleaseOperand(tmpOperand);
  4270. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4271. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4272. ReleaseOperand(tmpOperand);
  4273. END;
  4274. dimOp := IntermediateCode.Immediate(addressType,dim);
  4275. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4276. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4277. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4278. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4279. baseType := SemanticChecker.ArrayBase(type,dim);
  4280. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4281. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4282. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4283. Symbol(variable,variableOp);
  4284. MakeMemory(tmp,variableOp.op,addressType,0);
  4285. Emit(Mov(position,tmp,arrayDestinationTag));
  4286. ReleaseIntermediateOperand(tmp);
  4287. Pass((variableOp.op));
  4288. ReleaseOperand(variableOp);
  4289. ELSE HALT(100);
  4290. END;
  4291. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (parameter.kind = SyntaxTree.ValueParameter) THEN
  4292. ASSERT(type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static);
  4293. Designate(expression,operand);
  4294. IF operand.op.type.length > 1 THEN
  4295. Emit(Push(position, operand.op));
  4296. ReleaseOperand(operand);
  4297. ELSE
  4298. size := system.SizeOf(type);
  4299. Basic.Align(size,system.AlignmentOf(system.parameterAlignment,type));
  4300. size := ToMemoryUnits(system,size);
  4301. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,size)));
  4302. arrayDestinationTag := sp;
  4303. Emit(Copy(position,arrayDestinationTag,operand.op,IntermediateCode.Immediate(addressType,size)));
  4304. END;
  4305. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (parameter.kind IN {SyntaxTree.VarParameter,SyntaxTree.ConstParameter}) THEN
  4306. Designate(expression,operand);
  4307. IF operand.op.type.length > 1 THEN (* need temporary to pass register *)
  4308. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4309. Symbol(variable,variableOp);
  4310. MakeMemory(tmp,variableOp.op,operand.op.type,0);
  4311. Emit(Mov(position,tmp,operand.op));
  4312. Emit(Push(position,variableOp.op));
  4313. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4314. Pass((operand.op));
  4315. ELSE Error(position,"Forbidden non-static actual type. Conversion involved?");
  4316. END;
  4317. ELSE HALT(200)
  4318. END;
  4319. ELSIF parameter.type.resolved IS SyntaxTree.RangeType THEN
  4320. IF parameter.kind = SyntaxTree.VarParameter THEN
  4321. ASSERT(~(expression IS SyntaxTree.RangeExpression));
  4322. Designate(expression, operand);
  4323. Pass((operand.op));
  4324. ELSE
  4325. ASSERT((parameter.kind = SyntaxTree.ValueParameter) OR (parameter.kind = SyntaxTree.ConstParameter));
  4326. Evaluate(expression, operand);
  4327. Pass((operand.extra)); (* step *)
  4328. Pass((operand.tag)); (* last *)
  4329. Pass((operand.op)) (* first *)
  4330. END
  4331. ELSIF parameter.type.resolved IS SyntaxTree.ComplexType THEN
  4332. IF parameter.kind = SyntaxTree.VarParameter THEN
  4333. Designate(expression, operand);
  4334. Pass((operand.op));
  4335. ELSE
  4336. ASSERT((parameter.kind = SyntaxTree.ValueParameter) OR (parameter.kind = SyntaxTree.ConstParameter));
  4337. Evaluate(expression, operand);
  4338. Pass((operand.tag)); (* real part *)
  4339. Pass((operand.op)) (* imaginary part *)
  4340. END
  4341. ELSE
  4342. IF (parameter.kind = SyntaxTree.ValueParameter) OR (parameter.kind = SyntaxTree.ConstParameter) & ~(parameter.type.resolved IS SyntaxTree.RecordType) & ~(parameter.type.resolved IS SyntaxTree.ArrayType) THEN
  4343. IF (type IS SyntaxTree.RecordType) OR IsStaticArray(parameter.type) THEN
  4344. Designate(expression,operand);
  4345. size := ToMemoryUnits(system,system.SizeOf(parameter.type));
  4346. (* stack allocation *)
  4347. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,size + (-size) MOD (system.addressSize DIV system.dataUnit))));
  4348. (*! parameter alignment to be discussed ... *)
  4349. IF type IS SyntaxTree.StringType THEN (* source potentially shorter than destination *)
  4350. size := type(SyntaxTree.StringType).length;
  4351. END;
  4352. IF backend.cooperative & parameter.NeedsTrace() THEN
  4353. dst := NewRegisterOperand (addressType);
  4354. Emit(Mov(position,dst, sp));
  4355. IntermediateCode.InitImmediate(null, byteType, 0);
  4356. Emit(Fill(position, dst, IntermediateCode.Immediate(addressType,size), null));
  4357. ReleaseIntermediateOperand(dst);
  4358. SaveRegisters();ReleaseUsedRegisters(saved);
  4359. (* register dst has been freed before SaveRegisters already *)
  4360. CallAssignMethod(dst, operand.op, parameter.type);
  4361. RestoreRegisters(saved);
  4362. END;
  4363. Emit(Copy(position,sp,operand.op,IntermediateCode.Immediate(addressType,size)));
  4364. ELSIF IsOpenArray(parameter.type) THEN
  4365. Designate(expression,operand);
  4366. baseReg := operand.tag;
  4367. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4368. PushArrayLens(parameter.type,type,operand.dimOffset+DynamicDim(parameter.type)-1);
  4369. END;
  4370. Pass((operand.op)); (* address of the array *)
  4371. ELSIF IsDelegate(parameter.type) THEN
  4372. Evaluate(expression,operand);
  4373. IF backend.cooperative & parameter.NeedsTrace() THEN
  4374. Emit(Push(position, nil));
  4375. dst := NewRegisterOperand (addressType);
  4376. Emit(Mov(position,dst, sp));
  4377. ReleaseIntermediateOperand(dst);
  4378. SaveRegisters();ReleaseUsedRegisters(saved);
  4379. Emit(Push(position, dst));
  4380. (* register dst has been freed before SaveRegisters already *)
  4381. Emit(Push(position, operand.tag));
  4382. CallThis(position,"GarbageCollector","Assign",2);
  4383. RestoreRegisters(saved);
  4384. ELSE
  4385. Pass((operand.tag));
  4386. END;
  4387. Pass((operand.op));
  4388. ELSE
  4389. Evaluate(expression,operand);
  4390. IF backend.cooperative & parameter.NeedsTrace() & (operand.op.mode # IntermediateCode.ModeImmediate) THEN
  4391. Emit(Push(position, nil));
  4392. dst := NewRegisterOperand (addressType);
  4393. Emit(Mov(position,dst, sp));
  4394. ReleaseIntermediateOperand(dst);
  4395. SaveRegisters();ReleaseUsedRegisters(saved);
  4396. Emit(Push(position, dst));
  4397. (* register dst has been freed before SaveRegisters already *)
  4398. Emit(Push(position, operand.op));
  4399. CallThis(position,"GarbageCollector","Assign",2);
  4400. RestoreRegisters(saved);
  4401. ELSE
  4402. Pass((operand.op));
  4403. END;
  4404. END;
  4405. ELSIF expression IS SyntaxTree.NilValue THEN (* for special WinAPI rule *)
  4406. Evaluate(expression,operand);
  4407. Pass((operand.op));
  4408. ELSE (* var parameter *)
  4409. Designate(expression,operand);
  4410. IF (type IS SyntaxTree.RecordType) & (parameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.VarParameter}) THEN
  4411. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4412. Pass((operand.tag));
  4413. END;
  4414. END;
  4415. Pass((operand.op));
  4416. END;
  4417. END;
  4418. (* TODO: needed? *)
  4419. arrayDestinationTag := oldArrayDestinationTag;
  4420. arrayDestinationDimension := oldArrayDestinationDimension;
  4421. IF needsParameterBackup THEN
  4422. (* IF dump # NIL THEN dump.String("backup parameter"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4423. ReuseCopy(parameterBackup, operand.op)
  4424. END;
  4425. ReleaseOperand(operand);
  4426. IF Trace THEN TraceExit("PushParameter") END;
  4427. END PushParameter;
  4428. PROCEDURE VisitStatementDesignator(x: SyntaxTree.StatementDesignator);
  4429. VAR prevConditional: BOOLEAN;
  4430. BEGIN
  4431. prevConditional := conditional;
  4432. conditional := FALSE;
  4433. 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
  4434. Expression(x.result); (* use register *)
  4435. END;
  4436. Statement(x.statement);
  4437. conditional := prevConditional;
  4438. IF x.result # NIL THEN Expression(x.result) END;
  4439. 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
  4440. ReleaseIntermediateOperand(result.op);
  4441. END;
  4442. END VisitStatementDesignator;
  4443. PROCEDURE InlineProcedureCall(x: SyntaxTree.ProcedureCallDesignator): BOOLEAN;
  4444. VAR
  4445. procedure: SyntaxTree.Procedure;
  4446. procedureType: SyntaxTree.ProcedureType;
  4447. wasInline: BOOLEAN;
  4448. actualParameters: SyntaxTree.ExpressionList;
  4449. formalParameter: SyntaxTree.Parameter;
  4450. actualParameter: SyntaxTree.Expression;
  4451. i: LONGINT;
  4452. localVariable: SyntaxTree.Variable;
  4453. variableDesignator, returnDesignator: SyntaxTree.Expression;
  4454. src, dest: Operand;
  4455. prevInlineExit : Label;
  4456. prevMapper: SymbolMapper;
  4457. tooComplex: BOOLEAN;
  4458. PROCEDURE SimpleExpression(e: SyntaxTree.Expression): BOOLEAN;
  4459. BEGIN
  4460. IF e = NIL THEN RETURN TRUE
  4461. ELSIF (e IS SyntaxTree.SymbolDesignator) THEN RETURN SimpleExpression(e(SyntaxTree.SymbolDesignator).left)
  4462. ELSIF (e IS SyntaxTree.Value) THEN RETURN TRUE
  4463. ELSIF (e IS SyntaxTree.SelfDesignator) THEN RETURN TRUE
  4464. ELSIF (e IS SyntaxTree.ResultDesignator) THEN RETURN TRUE
  4465. ELSIF (e IS SyntaxTree.DereferenceDesignator) THEN RETURN SimpleExpression(e(SyntaxTree.DereferenceDesignator).left)
  4466. ELSE RETURN FALSE
  4467. END;
  4468. END SimpleExpression;
  4469. PROCEDURE FitsInRegister(type: SyntaxTree.Type): BOOLEAN;
  4470. BEGIN
  4471. RETURN checker.CanPassInRegister(type)
  4472. END FitsInRegister;
  4473. PROCEDURE GetTemp(type: SyntaxTree.Type; tryRegister: BOOLEAN): SyntaxTree.Expression;
  4474. VAR
  4475. variable: SyntaxTree.Variable;
  4476. variableDesignator: SyntaxTree.Designator;
  4477. BEGIN
  4478. variable := GetTemporaryVariable(type, tryRegister & FitsInRegister(type));
  4479. variableDesignator := SyntaxTree.NewSymbolDesignator(SemanticChecker.InvalidPosition, NIL, variable);
  4480. variableDesignator.SetType(type);
  4481. RETURN variableDesignator
  4482. END GetTemp;
  4483. BEGIN
  4484. wasInline := currentIsInline;
  4485. prevInlineExit := currentInlineExit;
  4486. prevMapper := currentMapper;
  4487. currentInlineExit := NewLabel();
  4488. tooComplex := FALSE;
  4489. NEW(currentMapper);
  4490. currentIsInline := TRUE;
  4491. procedure := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  4492. procedureType := procedure.type(SyntaxTree.ProcedureType);
  4493. formalParameter := procedureType.firstParameter;
  4494. actualParameters := x.parameters;
  4495. i := 0;
  4496. WHILE (i < actualParameters.Length()) & ~tooComplex DO
  4497. actualParameter := actualParameters.GetExpression(i);
  4498. IF actualParameter.resolved # NIL THEN
  4499. actualParameter := actualParameter.resolved
  4500. END;
  4501. (*
  4502. if expression is simple and can be passed immediately
  4503. or if type fits in register then we can proceed
  4504. otherwise we escape to ordinary procedure call.
  4505. *)
  4506. (* cases where the expression can be mapped identically *)
  4507. IF SimpleExpression(actualParameter) & (formalParameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.VarParameter}) THEN
  4508. currentMapper.Add(formalParameter, actualParameter, NIL);
  4509. ELSIF FitsInRegister(actualParameter.type) & (formalParameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.ValueParameter}) THEN
  4510. variableDesignator := GetTemp(formalParameter.type, TRUE);
  4511. (*
  4512. Assign(variableDesignator, actualParameter);
  4513. *)
  4514. Evaluate(actualParameter, src);
  4515. Designate(variableDesignator, dest);
  4516. Emit(Mov(x.position, dest.op, src.op));
  4517. ReleaseOperand(dest);
  4518. ReleaseOperand(src);
  4519. currentMapper.Add(formalParameter, variableDesignator, NIL);
  4520. ELSE tooComplex := TRUE
  4521. END;
  4522. (*
  4523. ELSIF (formalParameter.kind = SyntaxTree.ConstParameter) & IsSimple(actualParameter) THEN
  4524. currentMapper.Add(formalParameter, actualParameter, NIL);
  4525. ELSIF (formalParameter.kind = SyntaxTree.VarParameter) OR formalParameter.type.IsComposite() & (formalParameter.kind = SyntaxTree.ConstParameter) THEN
  4526. variableDesignator := GetTemp(system.addressType, FALSE);
  4527. Designate(actualParameter, src);
  4528. Designate(variableDesignator, dest);
  4529. IntermediateCode.MakeMemory(dest.op,addressType);
  4530. Emit(Mov(x.position, dest.op, src.op));
  4531. ReleaseOperand(dest);
  4532. IF src.tag.mode # IntermediateCode.Undefined THEN
  4533. tagDesignator := GetTemp(system.addressType, FALSE);
  4534. Designate(tagDesignator, dest);
  4535. IntermediateCode.MakeMemory(dest.op,addressType);
  4536. Emit(Mov(x.position, dest.op, src.op));
  4537. END;
  4538. ReleaseOperand(dest); ReleaseOperand(src);
  4539. currentMapper.Add(formalParameter, variableDesignator, tagDesignator);
  4540. END;
  4541. *)
  4542. formalParameter := formalParameter.nextParameter;
  4543. INC(i);
  4544. END;
  4545. IF ~tooComplex & (procedureType.returnType # NIL) THEN
  4546. returnDesignator := GetTemp(procedureType.returnType, TRUE);
  4547. currentMapper.Add(NIL, returnDesignator, NIL);
  4548. END;
  4549. localVariable := procedure.procedureScope.firstVariable;
  4550. WHILE ~tooComplex & (localVariable # NIL) DO
  4551. variableDesignator := GetTemp(localVariable.type, FALSE);
  4552. currentMapper.Add(localVariable, variableDesignator, NIL);
  4553. localVariable := localVariable.nextVariable;
  4554. END;
  4555. IF ~tooComplex THEN
  4556. VisitStatementBlock(procedure.procedureScope.body);
  4557. SetLabel(currentInlineExit);
  4558. IF procedureType.returnType # NIL THEN
  4559. Designate(returnDesignator, result);
  4560. IF conditional THEN
  4561. ASSERT (procedureType.returnType.resolved IS SyntaxTree.BooleanType);
  4562. ValueToCondition(result)
  4563. END;
  4564. END;
  4565. END;
  4566. currentMapper := prevMapper;
  4567. currentInlineExit := prevInlineExit;
  4568. currentIsInline := wasInline;
  4569. RETURN ~tooComplex
  4570. END InlineProcedureCall;
  4571. PROCEDURE VisitProcedureCallDesignator(x: SyntaxTree.ProcedureCallDesignator);
  4572. VAR
  4573. parameters: SyntaxTree.ExpressionList;
  4574. d, resultDesignator, actualParameter: SyntaxTree.Expression;
  4575. designator: SyntaxTree.Designator;
  4576. procedureType: SyntaxTree.ProcedureType;
  4577. formalParameter: SyntaxTree.Parameter;
  4578. operand, returnValue: Operand;
  4579. reg, size, mask, dest: IntermediateCode.Operand;
  4580. saved: RegisterEntry;
  4581. symbol: SyntaxTree.Symbol;
  4582. variable: SyntaxTree.Variable;
  4583. i, parametersSize, returnTypeSize : LONGINT;
  4584. structuredReturnType: BOOLEAN;
  4585. firstWriteBackCall, currentWriteBackCall: WriteBackCall;
  4586. tempVariableDesignator: SyntaxTree.Designator;
  4587. gap, alignment: LONGINT; (*fld*)
  4588. (* TODO: remove unnecessary backup variables *)
  4589. oldResult: Operand;
  4590. oldCurrentScope: SyntaxTree.Scope;
  4591. oldArrayDestinationTag: IntermediateCode.Operand;
  4592. oldArrayDestinationDimension: LONGINT;
  4593. oldConstantDeclaration: SyntaxTree.Symbol;
  4594. oldDestination: IntermediateCode.Operand;
  4595. oldCurrentLoop: Label;
  4596. oldConditional: BOOLEAN;
  4597. oldTrueLabel, oldFalseLabel: Label;
  4598. oldLocked: BOOLEAN;
  4599. usedRegisters,oldUsedRegisters: RegisterEntry;
  4600. return: IntermediateCode.Operand;
  4601. parameterBackups: ARRAY 2 OF IntermediateCode.Operand;
  4602. arg: IntermediateCode.Operand;
  4603. dummy: IntermediateCode.Operand;
  4604. recordType: SyntaxTree.RecordType;
  4605. operatorSelectionProcedureOperand: Operand;
  4606. operatorSelectionProcedure: SyntaxTree.Procedure;
  4607. fingerPrint: SyntaxTree.FingerPrint;
  4608. isCallOfDynamicOperator, hasDynamicOperands: BOOLEAN;
  4609. identifierNumber: LONGINT;
  4610. passByRegister: BOOLEAN; registerNumber,stackSize: LONGINT;
  4611. procedure: SyntaxTree.Procedure;
  4612. PROCEDURE BackupGlobalState;
  4613. BEGIN
  4614. oldResult := result;
  4615. oldCurrentScope := currentScope;
  4616. oldArrayDestinationTag := arrayDestinationTag;
  4617. oldArrayDestinationDimension := arrayDestinationDimension;
  4618. oldConstantDeclaration := constantDeclaration;
  4619. oldDestination := destination;
  4620. oldCurrentLoop := currentLoop;
  4621. oldConditional := conditional;
  4622. oldTrueLabel := trueLabel;
  4623. oldFalseLabel := falseLabel;
  4624. oldLocked := locked;
  4625. oldUsedRegisters := usedRegisters
  4626. END BackupGlobalState;
  4627. PROCEDURE RestoreGlobalState;
  4628. BEGIN
  4629. result := oldResult;
  4630. currentScope := oldCurrentScope;
  4631. arrayDestinationTag := oldArrayDestinationTag;
  4632. arrayDestinationDimension := oldArrayDestinationDimension;
  4633. constantDeclaration := oldConstantDeclaration;
  4634. destination := oldDestination;
  4635. currentLoop := oldCurrentLoop;
  4636. conditional := oldConditional;
  4637. trueLabel := oldTrueLabel;
  4638. falseLabel := oldFalseLabel;
  4639. locked := oldLocked;
  4640. usedRegisters := oldUsedRegisters
  4641. END RestoreGlobalState;
  4642. (** do preparations before parameter push for array-structured object types (ASOTs):
  4643. if ASOT is passed as VAR parameter:
  4644. - allocate temporary variable of math array type
  4645. - copy contents of ASOT to be passed to temporary variable
  4646. - use temporary variable as the actual parameter instead
  4647. - 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)
  4648. **)
  4649. PROCEDURE PrepareParameter(VAR actualParameter: SyntaxTree.Expression; formalParameter: SyntaxTree.Parameter);
  4650. VAR
  4651. expression: SyntaxTree.Expression;
  4652. BEGIN
  4653. IF actualParameter IS SyntaxTree.Designator THEN
  4654. designator := actualParameter(SyntaxTree.Designator);
  4655. IF (formalParameter.kind = SyntaxTree.VarParameter) & (designator.relatedAsot # NIL) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  4656. (* actual parameter is an index read operator call on ASOT and formal parameter is of VAR kind *)
  4657. (* IF dump # NIL THEN dump.String("!!! BEGIN ASOT VAR parameter preparation"); dump.Ln; dump.Update END; *) (* TENTATIVE*)
  4658. ASSERT(checker # NIL);
  4659. checker.SetCurrentScope(currentScope);
  4660. (* allocate temporary variable *)
  4661. ASSERT(actualParameter.type # NIL);
  4662. ASSERT(actualParameter.type.resolved IS SyntaxTree.MathArrayType);
  4663. variable := GetTemporaryVariable(actualParameter.type.resolved, FALSE);
  4664. tempVariableDesignator := SyntaxTree.NewSymbolDesignator(SemanticChecker.InvalidPosition, NIL, variable);
  4665. tempVariableDesignator.SetType(actualParameter.type.resolved);
  4666. ASSERT(tempVariableDesignator IS SyntaxTree.SymbolDesignator);
  4667. ASSERT(tempVariableDesignator.type # NIL);
  4668. ASSERT(tempVariableDesignator.type.resolved IS SyntaxTree.MathArrayType);
  4669. (* copy math array stored in actual parameter to temporary variable *)
  4670. BackupGlobalState;
  4671. AssignMathArray(tempVariableDesignator, actualParameter);
  4672. RestoreGlobalState;
  4673. (* use temporary variable as actual parameter instead of the original one *)
  4674. actualParameter := tempVariableDesignator;
  4675. (* create write-back call and store it in linked list *)
  4676. (* create new list entry *)
  4677. IF firstWriteBackCall = NIL THEN
  4678. NEW(firstWriteBackCall);
  4679. currentWriteBackCall := firstWriteBackCall
  4680. ELSE
  4681. ASSERT(currentWriteBackCall # NIL);
  4682. NEW(currentWriteBackCall.next);
  4683. currentWriteBackCall := currentWriteBackCall.next
  4684. END;
  4685. expression := checker.NewIndexOperatorCall(SemanticChecker.InvalidPosition, designator.relatedAsot, designator.relatedIndexList, tempVariableDesignator);
  4686. ASSERT(expression.type = NIL);
  4687. currentWriteBackCall.call := expression(SyntaxTree.ProcedureCallDesignator);
  4688. ELSIF (formalParameter.kind = SyntaxTree.VarParameter) & (designator.relatedAsot # NIL) THEN
  4689. (* prepare writeback for any other "normal" indexer *)
  4690. variable := GetTemporaryVariable(actualParameter.type.resolved, FALSE);
  4691. tempVariableDesignator := SyntaxTree.NewSymbolDesignator(SemanticChecker.InvalidPosition, NIL, variable);
  4692. tempVariableDesignator.SetType(actualParameter.type.resolved);
  4693. Assign(tempVariableDesignator, actualParameter);
  4694. actualParameter := tempVariableDesignator;
  4695. IF firstWriteBackCall = NIL THEN
  4696. NEW(firstWriteBackCall);
  4697. currentWriteBackCall := firstWriteBackCall
  4698. ELSE
  4699. ASSERT(currentWriteBackCall # NIL);
  4700. NEW(currentWriteBackCall.next);
  4701. currentWriteBackCall := currentWriteBackCall.next
  4702. END;
  4703. expression := checker.NewObjectOperatorCall(SemanticChecker.InvalidPosition, designator.relatedAsot, designator.relatedIndexList, tempVariableDesignator);
  4704. currentWriteBackCall.call := expression(SyntaxTree.ProcedureCallDesignator);
  4705. END
  4706. END
  4707. END PrepareParameter;
  4708. BEGIN
  4709. IF Trace THEN TraceEnter("VisitProcedureCallDesignator") END;
  4710. IF (x.left IS SyntaxTree.SymbolDesignator) & (x.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  4711. procedure := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  4712. IF procedure.isOberonInline & (procedure.scope.ownerModule = module.module) THEN
  4713. IF InlineProcedureCall(x) THEN
  4714. RETURN
  4715. ELSE
  4716. Warning(x.position, "Inline call with complex parameters not yet implemented. Emitting ordinary procedure call.")
  4717. END
  4718. END;
  4719. END;
  4720. resultDesignator := procedureResultDesignator; procedureResultDesignator := NIL;
  4721. procedureType := x.left.type.resolved(SyntaxTree.ProcedureType);
  4722. dest := destination; destination := emptyOperand;
  4723. SaveRegisters();ReleaseUsedRegisters(saved);
  4724. parameters := x.parameters;
  4725. IF (x.left IS SyntaxTree.SymbolDesignator) & (x.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Operator) THEN
  4726. (* an operator is called *)
  4727. (* IF dump # NIL THEN dump.String("*** begin of operator call ***"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4728. ASSERT(procedureType.callingConvention = SyntaxTree.OberonCallingConvention);
  4729. (* check if a dynamic operator call should be performed *)
  4730. isCallOfDynamicOperator := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Operator).isDynamic
  4731. ELSE
  4732. isCallOfDynamicOperator := FALSE
  4733. END;
  4734. IF backend.cooperative & (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) THEN
  4735. Emit(Push(position, ap));
  4736. END;
  4737. alignment := procedureType.stackAlignment;
  4738. IF alignment > 1 THEN
  4739. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  4740. Emit(Mov(position,reg, sp));
  4741. gap := ParametersSize(system, procedureType, FALSE) + ToMemoryUnits(system,system.offsetFirstParameter) + ToMemoryUnits(system,system.addressSize);
  4742. Emit(Sub(position,sp, sp, IntermediateCode.Immediate(addressType,gap)));
  4743. IntermediateCode.InitImmediate(mask,addressType,-alignment);
  4744. Emit(And(position,sp, sp, mask));
  4745. Emit(Add(position, sp, sp, IntermediateCode.Immediate(addressType, gap)));
  4746. Emit(Push(position,reg));
  4747. (*
  4748. IntermediateCode.InitMemory(mem, addressType, sp, 0);
  4749. Emit(Mov(position,mem,reg));
  4750. *)
  4751. ReleaseIntermediateOperand(reg);
  4752. END;
  4753. IF procedureType.callingConvention = SyntaxTree.DarwinCCallingConvention THEN (*fld*)
  4754. IntermediateCode.InitImmediate(mask,addressType,-16);
  4755. Emit(And(position,sp, sp, mask));
  4756. gap := (-ParametersSize( system, procedureType, FALSE )) MOD 16;
  4757. IF gap # 0 THEN
  4758. IntermediateCode.InitImmediate(size,addressType,gap);
  4759. Emit(Sub(position,sp,sp,size))
  4760. END;
  4761. END;
  4762. IF x.left IS SyntaxTree.SupercallDesignator THEN
  4763. symbol := x.left(SyntaxTree.SupercallDesignator).left(SyntaxTree.SymbolDesignator).symbol;
  4764. ELSIF x.left IS SyntaxTree.IndexDesignator THEN
  4765. symbol := x.left(SyntaxTree.IndexDesignator).left(SyntaxTree.SymbolDesignator).symbol;
  4766. ELSE
  4767. symbol := x.left(SyntaxTree.SymbolDesignator).symbol;
  4768. END;
  4769. IF isCallOfDynamicOperator & hasDynamicOperands THEN
  4770. IF dump # NIL THEN dump.String("++++++++++ dynamic operator call ++++++++++"); dump.Ln; dump.Update END; (* TENTATIVE *)
  4771. (* push ID *)
  4772. (* IF dump # NIL THEN dump.String("push ID"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4773. ASSERT(x.left IS SyntaxTree.SymbolDesignator);
  4774. identifierNumber := Global.GetSymbol(module.module.case, x.left(SyntaxTree.SymbolDesignator).symbol.name);
  4775. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), identifierNumber)));
  4776. formalParameter := procedureType.firstParameter;
  4777. FOR i := 0 TO parameters.Length() - 1 DO
  4778. IF formalParameter.access # SyntaxTree.Hidden THEN
  4779. ASSERT(i < 2);
  4780. IF IsStrictlyPointerToRecord(formalParameter.type) THEN
  4781. (* push pointer *)
  4782. (* IF dump # NIL THEN dump.String("push pointer"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4783. IF formalParameter.kind = SyntaxTree.VarParameter THEN
  4784. (* add dereference *)
  4785. (* IF dump # NIL THEN dump.String("dereference pointer"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4786. ReleaseIntermediateOperand(parameterBackups[i]);
  4787. MakeMemory(parameterBackups[i], parameterBackups[i], addressType, 0)
  4788. END;
  4789. Emit(Push(position,parameterBackups[i]));
  4790. ReleaseIntermediateOperand(parameterBackups[i]);
  4791. (* push typetag *)
  4792. (* IF dump # NIL THEN dump.String("push typetag"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4793. recordType := formalParameter.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  4794. arg := TypeDescriptorAdr(recordType);
  4795. IF ~newObjectFile THEN IntermediateCode.MakeMemory(arg, addressType) END;
  4796. Emit(Push(position,arg));
  4797. ELSE
  4798. (* push 'NonPointer' *)
  4799. (* IF dump # NIL THEN dump.String("push 'NonPointer'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4800. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NonPointer)));
  4801. (* push fingerprint *)
  4802. (* IF dump # NIL THEN dump.String("push fingerprint"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4803. fingerPrint := fingerPrinter.TypeFP(formalParameter.type.resolved);
  4804. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), fingerPrint.shallow))) (* TODO: push the type's fingerprint *)
  4805. END
  4806. END;
  4807. formalParameter := formalParameter.nextParameter
  4808. END;
  4809. (* for unary operators: complete the information for the second parameter *)
  4810. IF procedureType.numberParameters < 2 THEN
  4811. (* push 'NonPointer' *)
  4812. (* IF dump # NIL THEN dump.String("push 'NonPointer'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4813. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NonPointer)));
  4814. (* push 'NoType' *)
  4815. (* IF dump # NIL THEN dump.String("push 'NoType'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4816. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NoType)));
  4817. END;
  4818. (* call operator selection procedure *)
  4819. IF GetRuntimeProcedure("FoxOperatorRuntime", "SelectOperator", operatorSelectionProcedure, TRUE) THEN
  4820. StaticCallOperand(operatorSelectionProcedureOperand, operatorSelectionProcedure);
  4821. Emit(Call(position,operatorSelectionProcedureOperand.op, ProcedureParametersSize(system, operatorSelectionProcedure)));
  4822. ReleaseOperand(operatorSelectionProcedureOperand);
  4823. (* use the address that the operator selection procedure returned as the target address of the call *)
  4824. InitOperand(operand, ModeValue);
  4825. operand.op := IntermediateCode.Register(addressType, IntermediateCode.GeneralPurposeRegister, AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  4826. Emit(Result(position,operand.op))
  4827. END
  4828. ELSE
  4829. Evaluate(x.left, operand)
  4830. END;
  4831. IF symbol IS SyntaxTree.Procedure THEN
  4832. IF x.left IS SyntaxTree.SupercallDesignator THEN
  4833. Emit(Push(position,operand.tag));
  4834. ELSIF (procedureType.isDelegate) THEN
  4835. Emit(Push(position,operand.tag));
  4836. END;
  4837. ELSIF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  4838. IF (procedureType.isDelegate) THEN (* push self pointer only if procedure is a method *)
  4839. Emit(Push(position,operand.tag));
  4840. END;
  4841. ELSE HALT(200);
  4842. END;
  4843. ReleaseIntermediateOperand(operand.tag);
  4844. operand.tag := emptyOperand;
  4845. (* determine if a structured return type is needed *)
  4846. structuredReturnType := StructuredReturnType(procedureType);
  4847. IF structuredReturnType THEN
  4848. IF resultDesignator # NIL THEN
  4849. d := resultDesignator;
  4850. ELSE
  4851. variable := GetTemporaryVariable(procedureType.returnType, FALSE);
  4852. variable.SetUntraced(procedureType.hasUntracedReturn);
  4853. d := SyntaxTree.NewSymbolDesignator(-1,NIL,variable);
  4854. d.SetType(variable.type);
  4855. END;
  4856. IF (procedureType.returnType.resolved IS SyntaxTree.RecordType) THEN
  4857. Designate(d,returnValue);
  4858. returnTypeSize := system.SizeOf(procedureType.returnType.resolved);
  4859. size := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,returnTypeSize));
  4860. Emit(Push(position,size));
  4861. Emit(Push(position,returnValue.op));
  4862. ReleaseOperand(returnValue);
  4863. ELSE
  4864. PushParameter(d,procedureType.returnParameter,procedureType.callingConvention, FALSE, dummy,-1)
  4865. END;
  4866. END;
  4867. firstWriteBackCall := NIL; (* reset write-back call list *)
  4868. IF procedureType.callingConvention # SyntaxTree.OberonCallingConvention THEN
  4869. passByRegister := system.registerParameters > 0;
  4870. registerNumber := 0;
  4871. formalParameter := procedureType.lastParameter;
  4872. FOR i := parameters.Length() - 1 TO 0 BY -1 DO
  4873. actualParameter := parameters.GetExpression(i);
  4874. PrepareParameter(actualParameter, formalParameter);
  4875. IF passByRegister & (i < system.registerParameters) THEN
  4876. IF ~PassInRegister(formalParameter) THEN
  4877. Error(actualParameter.position,"cannot be passed by register")
  4878. ELSE
  4879. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, FALSE, dummy,i);
  4880. END;
  4881. INC(registerNumber);
  4882. ELSE
  4883. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  4884. END;
  4885. formalParameter := formalParameter.prevParameter;
  4886. END;
  4887. IF passByRegister & (registerNumber > 0) THEN
  4888. stackSize := ToMemoryUnits(system,system.registerParameters*addressType.sizeInBits);
  4889. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,stackSize)));
  4890. END;
  4891. ELSE
  4892. hasDynamicOperands := FALSE;
  4893. formalParameter := procedureType.firstParameter;
  4894. FOR i := 0 TO parameters.Length() - 1 DO
  4895. actualParameter := parameters.GetExpression(i);
  4896. IF formalParameter # NIL THEN (* TENTATIVE *)
  4897. PrepareParameter(actualParameter, formalParameter);
  4898. IF isCallOfDynamicOperator & IsStrictlyPointerToRecord(formalParameter.type) & (formalParameter.access # SyntaxTree.Hidden) THEN (* TODO: remove hidden parameters *)
  4899. ASSERT(i < 2);
  4900. hasDynamicOperands := TRUE;
  4901. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, TRUE, parameterBackups[i],-1)
  4902. ELSE
  4903. IF passByRegister & (registerNumber > 0) THEN
  4904. stackSize := ToMemoryUnits(system,registerNumber*addressType.sizeInBits);
  4905. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,stackSize)));
  4906. END;
  4907. passByRegister := FALSE;
  4908. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  4909. END;
  4910. formalParameter := formalParameter.nextParameter;
  4911. END;
  4912. END;
  4913. END;
  4914. IF symbol IS SyntaxTree.Procedure THEN
  4915. IF IsNested(symbol(SyntaxTree.Procedure)) THEN
  4916. GetBaseRegister(reg,currentScope,symbol.scope); (* static link, may be fp or [fp+8] (if nested proc calls itself) *)
  4917. Emit(Push(position,reg));
  4918. ReleaseIntermediateOperand(reg);
  4919. END;
  4920. parametersSize := ProcedureParametersSize(system,symbol(SyntaxTree.Procedure));
  4921. ELSIF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  4922. parametersSize := ParametersSize(system,procedureType,FALSE);
  4923. END;
  4924. ReleaseParameterRegisters();
  4925. IF (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) OR (procedureType.callingConvention = SyntaxTree.CCallingConvention) THEN
  4926. Emit(Call(position,operand.op,0));
  4927. ELSE
  4928. Emit(Call(position,operand.op,parametersSize));
  4929. END;
  4930. ReleaseOperand(operand);
  4931. IF procedureType.noReturn THEN
  4932. EmitTrap(position,NoReturnTrap);
  4933. END;
  4934. IF (procedureType.returnType # NIL) & ~structuredReturnType THEN
  4935. return := NewRegisterOperand(IntermediateCode.GetType(system,procedureType.returnType));
  4936. Emit(Result(position,return));
  4937. END;
  4938. IF procedureType.callingConvention = SyntaxTree.CCallingConvention THEN
  4939. IF passByRegister & (registerNumber > 0) & (registerNumber < system.registerParameters) THEN (* allocated space for all registers *)
  4940. parametersSize := ToMemoryUnits(system,system.registerParameters*addressType.sizeInBits);
  4941. END;
  4942. size := IntermediateCode.Immediate(addressType,parametersSize);
  4943. Emit(Add(position,sp,sp,size));
  4944. END;
  4945. IF (resultDesignator = NIL) & (procedureType.returnType # NIL) THEN
  4946. IF structuredReturnType THEN
  4947. (* stack pointer rewinding done by callee
  4948. size := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.SizeOfParameter(procedureType.returnParameter)));
  4949. Emit(Add(position,sp,sp,size));
  4950. *)
  4951. RestoreRegisters(saved);
  4952. InitOperand(result,ModeReference);
  4953. Symbol(variable,result);
  4954. ELSE
  4955. RestoreRegisters(saved);
  4956. InitOperand(result,ModeValue);
  4957. result.op := return;
  4958. END;
  4959. END;
  4960. IF alignment > 1 THEN
  4961. Emit(Pop(position,sp));
  4962. END;
  4963. IF backend.cooperative & (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) THEN
  4964. Emit(Pop(position, ap));
  4965. END;
  4966. IF conditional & (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  4967. ValueToCondition(result);
  4968. END;
  4969. destination := dest;
  4970. (* perform all write-back calls in the list *)
  4971. BackupGlobalState;
  4972. currentWriteBackCall := firstWriteBackCall;
  4973. WHILE currentWriteBackCall # NIL DO
  4974. VisitProcedureCallDesignator(currentWriteBackCall.call);
  4975. currentWriteBackCall := currentWriteBackCall.next
  4976. END;
  4977. RestoreGlobalState;
  4978. (* TENATIVE *)
  4979. (*
  4980. IF isOperatorCall THEN
  4981. IF dump # NIL THEN dump.String("*** end of operator call ***"); dump.Ln; dump.Update END
  4982. END;
  4983. *)
  4984. IF Trace THEN TraceExit("VisitProcedureCallDesignator") END;
  4985. END VisitProcedureCallDesignator;
  4986. PROCEDURE TypeDescriptorAdr(t: SyntaxTree.Type): IntermediateCode.Operand;
  4987. VAR res: IntermediateCode.Operand; offset: LONGINT; name: Basic.SegmentedName;
  4988. td: SyntaxTree.Symbol;
  4989. PROCEDURE GetHiddenPointerType(): SyntaxTree.Type;
  4990. VAR scope: SyntaxTree.RecordScope; variable: SyntaxTree.Variable; typeDeclaration: SyntaxTree.TypeDeclaration;
  4991. BEGIN
  4992. IF (hiddenPointerType = NIL) OR (hiddenPointerType.typeDeclaration.scope.ownerModule # module.module) THEN
  4993. scope := SyntaxTree.NewRecordScope(module.module.moduleScope);
  4994. variable := SyntaxTree.NewVariable(-1,SyntaxTree.NewIdentifier("@Any"));
  4995. variable.SetType(system.anyType);
  4996. scope.AddVariable(variable);
  4997. hiddenPointerType := SyntaxTree.NewRecordType(-1,NIL,scope);
  4998. typeDeclaration := SyntaxTree.NewTypeDeclaration(-1,SyntaxTree.NewIdentifier("@HdPtrDesc"));
  4999. typeDeclaration.SetDeclaredType(hiddenPointerType);
  5000. typeDeclaration.SetScope(module.module.moduleScope);
  5001. hiddenPointerType.SetTypeDeclaration(typeDeclaration);
  5002. hiddenPointerType.SetState(SyntaxTree.Resolved);
  5003. END;
  5004. RETURN hiddenPointerType;
  5005. END GetHiddenPointerType;
  5006. PROCEDURE GetDelegateType(): SyntaxTree.Type;
  5007. VAR scope: SyntaxTree.RecordScope; variable: SyntaxTree.Variable; typeDeclaration: SyntaxTree.TypeDeclaration;
  5008. BEGIN
  5009. IF (delegatePointerType = NIL) OR (delegatePointerType.typeDeclaration.scope.ownerModule # module.module) THEN
  5010. scope := SyntaxTree.NewRecordScope(module.module.moduleScope);
  5011. variable := SyntaxTree.NewVariable(-1,SyntaxTree.NewIdentifier("@Procedure"));
  5012. variable.SetType(SyntaxTree.NewProcedureType(-1,NIL));
  5013. scope.AddVariable(variable);
  5014. variable := SyntaxTree.NewVariable(-1,SyntaxTree.NewIdentifier("@Any"));
  5015. variable.SetType(system.anyType);
  5016. scope.AddVariable(variable);
  5017. delegatePointerType := SyntaxTree.NewRecordType(-1,NIL,scope);
  5018. typeDeclaration := SyntaxTree.NewTypeDeclaration(-1,SyntaxTree.NewIdentifier("@Delegate"));
  5019. typeDeclaration.SetDeclaredType(delegatePointerType);
  5020. typeDeclaration.SetScope(module.module.moduleScope);
  5021. delegatePointerType.SetTypeDeclaration(typeDeclaration);
  5022. delegatePointerType.SetState(SyntaxTree.Resolved);
  5023. END;
  5024. RETURN delegatePointerType
  5025. END GetDelegateType;
  5026. PROCEDURE GetBackendType(x: SyntaxTree.Type; VAR offset: LONGINT; VAR name: Basic.SegmentedName): SyntaxTree.Symbol;
  5027. (* create anonymous type declaration for types that need a type descriptor but have been declared anonymously
  5028. such as in VAR a: RECORD ... END;
  5029. reason: type desciptors in Sections are then accessible via a type declaration symbol and for types
  5030. and variables, constants and procedures the same mechanism can be used for fixups etc.
  5031. *)
  5032. VAR source: Sections.Section;null: HUGEINT; td: SyntaxTree.TypeDeclaration;
  5033. op: IntermediateCode.Operand; baseRecord: SyntaxTree.RecordType;
  5034. BEGIN (* no code emission *)
  5035. source := NIL;
  5036. x := x.resolved;
  5037. IF (x IS SyntaxTree.AnyType) OR (x IS SyntaxTree.PointerType) THEN
  5038. x := GetHiddenPointerType();
  5039. ELSIF IsDelegate(x) THEN
  5040. x := GetDelegateType();
  5041. ELSIF (x IS SyntaxTree.RecordType) OR (x IS SyntaxTree.CellType) THEN
  5042. ELSE HALT(200);
  5043. END;
  5044. td := x.typeDeclaration;
  5045. IF td = NIL THEN
  5046. ASSERT(x(SyntaxTree.RecordType).pointerType # NIL);
  5047. td := x(SyntaxTree.RecordType).pointerType.resolved.typeDeclaration;
  5048. ASSERT(td # NIL);
  5049. END;
  5050. IF newObjectFile THEN
  5051. GetCodeSectionNameForSymbol(td,name);
  5052. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  5053. meta.CheckTypeDeclaration(x);
  5054. source := NewSection(module.allSections, Sections.ConstSection, name,td,commentPrintout # NIL);
  5055. ELSE
  5056. source := NewSection(module.importedSections, Sections.ConstSection,name,td,commentPrintout # NIL);
  5057. END;
  5058. IF backend.cooperative THEN
  5059. offset := 0;
  5060. ELSE
  5061. IF x IS SyntaxTree.CellType THEN
  5062. baseRecord := x(SyntaxTree.CellType).GetBaseRecord();
  5063. IF baseRecord = NIL THEN
  5064. offset := ToMemoryUnits(system, meta.GetTypeRecordBaseOffset(0));
  5065. ELSE
  5066. offset := ToMemoryUnits(system, meta.GetTypeRecordBaseOffset(baseRecord.recordScope.numberMethods)*system.addressSize);
  5067. END;
  5068. ELSE
  5069. offset := ToMemoryUnits(system,meta.GetTypeRecordBaseOffset(x(SyntaxTree.RecordType).recordScope.numberMethods)*system.addressSize);
  5070. END;
  5071. END;
  5072. ELSE
  5073. offset := 0;
  5074. source := module.allSections.FindBySymbol(td); (*TODO*)
  5075. IF source = NIL THEN
  5076. null := 0;
  5077. GetCodeSectionNameForSymbol(td,name);
  5078. source := NewSection(module.allSections, Sections.ConstSection, name,td,commentPrintout # NIL);
  5079. IntermediateCode.InitImmediate(op,addressType,0);
  5080. source(IntermediateCode.Section).Emit(Data(position,op));
  5081. source.SetReferenced(FALSE)
  5082. ELSE
  5083. name := source.name;
  5084. END;
  5085. END;
  5086. RETURN td
  5087. END GetBackendType;
  5088. BEGIN
  5089. (*td := t.typeDeclaration;*)
  5090. td := GetBackendType(t,offset,name); (*! do not dereference a pointer here as the type descriptor for the pointer might be asked for *)
  5091. (*
  5092. IF t IS SyntaxTree.PointerType THEN
  5093. source := GetBackendType(t(SyntaxTree.PointerType).pointerBase.resolved);
  5094. ELSE
  5095. source := GetBackendType(t);
  5096. END;
  5097. *)
  5098. IF newObjectFile THEN
  5099. IntermediateCode.InitAddress(res, addressType, name, GetFingerprint(td), 0 (*
  5100. 1+t(SyntaxTree.RecordType).recordScope.numberMethods+16+1 *));
  5101. IntermediateCode.SetOffset(res,offset);
  5102. ELSE
  5103. IntermediateCode.InitAddress(res, addressType, name, GetFingerprint(td), 0);
  5104. END;
  5105. (*
  5106. IntermediateCode.MakeMemory(res,IntermediateCode.UnsignedInteger,addressType.sizeInBits);
  5107. make memory should be used when tag is used, not earlier
  5108. *)
  5109. RETURN res
  5110. END TypeDescriptorAdr;
  5111. (*
  5112. PROCEDURE MakeTypeTag(VAR operand: Operand);
  5113. VAR result: IntermediateCode.Operand;
  5114. BEGIN
  5115. IF operand.tag.mode = IntermediateCode.Undefined THEN
  5116. operand.tag := TypeDescriptorAdr(operand.type);
  5117. IntermediateCode.MakeMemory(operand.tag,addressType);
  5118. UseIntermediateOperand(operand.tag);
  5119. END;
  5120. END MakeTypeTag;
  5121. *)
  5122. PROCEDURE ProfilerInit;
  5123. VAR reg: IntermediateCode.Operand;
  5124. BEGIN
  5125. IntermediateCode.InitAddress(reg, addressType, profileInit.name , GetFingerprint(profileInit.symbol), 0);
  5126. Emit(Call(position,reg,0));
  5127. END ProfilerInit;
  5128. PROCEDURE ProfilerEnterExit(procedureNumber: LONGINT; enter: BOOLEAN);
  5129. VAR reg: IntermediateCode.Operand; result: Operand; procedure: SyntaxTree.Procedure;
  5130. BEGIN
  5131. IF enter & GetRuntimeProcedure("FoxProfiler","EnterProcedure",procedure,TRUE)
  5132. OR ~enter & GetRuntimeProcedure("FoxProfiler","ExitProcedure",procedure,TRUE)
  5133. THEN
  5134. IntermediateCode.InitAddress(reg, addressType, profileId.name , GetFingerprint(profileId.symbol), 0);
  5135. IntermediateCode.MakeMemory(reg, IntermediateCode.GetType(system,system.longintType));
  5136. Emit(Push(position,reg));
  5137. IntermediateCode.InitImmediate(reg, IntermediateCode.GetType(system,system.longintType), procedureNumber);
  5138. Emit(Push(position,reg));
  5139. StaticCallOperand(result,procedure);
  5140. Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  5141. ReleaseOperand(result);
  5142. END;
  5143. END ProfilerEnterExit;
  5144. PROCEDURE ProfilerAddProcedure(procedureNumber: LONGINT; CONST name: ARRAY OF CHAR);
  5145. VAR string: SyntaxTree.String; reg: IntermediateCode.Operand; result: Operand; procedure: SyntaxTree.Procedure; sv: SyntaxTree.StringValue;type: SyntaxTree.Type;
  5146. BEGIN
  5147. IF GetRuntimeProcedure("FoxProfiler","AddProcedure",procedure,TRUE) THEN
  5148. IntermediateCode.InitAddress(reg, addressType, profileId.name , GetFingerprint(profileId.symbol), 0);
  5149. IntermediateCode.MakeMemory(reg, IntermediateCode.GetType(system,system.longintType));
  5150. profileInit.Emit(Push(position,reg));
  5151. IntermediateCode.InitImmediate(reg, IntermediateCode.GetType(system,system.longintType), procedureNumber);
  5152. profileInit.Emit(Push(position,reg));
  5153. NEW(string, LEN(name)); COPY(name, string^);
  5154. sv := SyntaxTree.NewStringValue(-1,string);
  5155. type := SyntaxTree.NewStringType(-1,system.characterType,Strings.Length(name));
  5156. sv.SetType(type);
  5157. Designate(sv,result);
  5158. profileInit.Emit(Push(position,result.tag));
  5159. profileInit.Emit(Push(position,result.op));
  5160. StaticCallOperand(result,procedure);
  5161. profileInit.Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  5162. ReleaseOperand(result);
  5163. END;
  5164. END ProfilerAddProcedure;
  5165. PROCEDURE ProfilerAddModule(CONST name: ARRAY OF CHAR);
  5166. VAR string: SyntaxTree.String; sv: SyntaxTree.StringValue; type: SyntaxTree.Type; result: Operand; reg: IntermediateCode.Operand; procedure: SyntaxTree.Procedure;
  5167. BEGIN
  5168. IF GetRuntimeProcedure("FoxProfiler","AddModule",procedure,TRUE) THEN
  5169. IntermediateCode.InitAddress(reg, addressType, profileId.name , GetFingerprint(profileId.symbol), 0);
  5170. profileInit.Emit(Push(position,reg));
  5171. profileInitPatchPosition := profileInit.pc;
  5172. profileInit.Emit(Nop(position)); (* placeholder, will be patched by number of procedures *)
  5173. NEW(string, LEN(name)); COPY(name, string^);
  5174. sv := SyntaxTree.NewStringValue(-1,string);
  5175. type := SyntaxTree.NewStringType(-1,system.characterType,Strings.Length(name));
  5176. sv.SetType(type);
  5177. Designate(sv,result);
  5178. profileInit.Emit(Push(position,result.tag));
  5179. profileInit.Emit(Push(position,result.op));
  5180. StaticCallOperand(result,procedure);
  5181. profileInit.Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  5182. ReleaseOperand(result);
  5183. END;
  5184. END ProfilerAddModule;
  5185. PROCEDURE ProfilerPatchInit;
  5186. VAR reg: IntermediateCode.Operand;
  5187. BEGIN
  5188. IntermediateCode.InitImmediate(reg, IntermediateCode.GetType(system,system.longintType), numberProcedures);
  5189. profileInit.EmitAt(profileInitPatchPosition,Push(position,reg));
  5190. EmitLeave(profileInit,position,0);
  5191. profileInit.Emit(Exit(position,0,0));
  5192. END ProfilerPatchInit;
  5193. (** if operator can be overloaded dynamically, emit code that registers it in the runtime **)
  5194. PROCEDURE RegisterDynamicOperator(operator: SyntaxTree.Operator);
  5195. VAR
  5196. id: LONGINT;
  5197. leftType, rightType: SyntaxTree.Type;
  5198. procedureType: SyntaxTree.ProcedureType;
  5199. runtimeProcedure: SyntaxTree.Procedure;
  5200. runtimeProcedureOperand, operatorOperand: Operand;
  5201. kind: SET;
  5202. PROCEDURE PushTypeInfo(type: SyntaxTree.Type);
  5203. VAR
  5204. arg: IntermediateCode.Operand;
  5205. recordType: SyntaxTree.RecordType;
  5206. fingerPrint: SyntaxTree.FingerPrint;
  5207. BEGIN
  5208. IF type = NIL THEN
  5209. (* no type: push 'NoType' *)
  5210. (* IF dump # NIL THEN dump.String("push 'NoType'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5211. arg := IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NoType)
  5212. ELSIF IsStrictlyPointerToRecord(type) THEN
  5213. (* pointer to record type: push typetag *)
  5214. (* IF dump # NIL THEN dump.String("push typetag"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5215. recordType := type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  5216. arg := TypeDescriptorAdr(recordType);
  5217. IF ~newObjectFile THEN IntermediateCode.MakeMemory(arg, addressType) END;
  5218. ELSE
  5219. (* non-pointer to record type: push fingerprint *)
  5220. (* IF dump # NIL THEN dump.String("push fingerprint"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5221. fingerPrint := fingerPrinter.TypeFP(type.resolved);
  5222. arg := IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), fingerPrint.shallow)
  5223. END;
  5224. operatorInitializationCodeSection.Emit(Push(position,arg))
  5225. END PushTypeInfo;
  5226. BEGIN
  5227. ASSERT(operatorInitializationCodeSection # NIL);
  5228. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  5229. procedureType := operator.type(SyntaxTree.ProcedureType);
  5230. (* determine types *)
  5231. leftType := procedureType.firstParameter.type;
  5232. IF procedureType.numberParameters = 2 THEN
  5233. ASSERT(procedureType.firstParameter.nextParameter # NIL);
  5234. rightType := procedureType.firstParameter.nextParameter.type;
  5235. ELSE
  5236. rightType := NIL
  5237. END;
  5238. (* determine operator kind *)
  5239. IF IsStrictlyPointerToRecord(leftType) THEN
  5240. kind := {LhsIsPointer}
  5241. ELSE
  5242. kind := {}
  5243. END;
  5244. IF IsStrictlyPointerToRecord(rightType) THEN
  5245. kind := kind + {RhsIsPointer}
  5246. END;
  5247. IF kind # {} THEN (* TODO: to be removed later on *)
  5248. (* at least one of the types is a pointer to record *)
  5249. (* emit a code that registers this specific operator in the runtime *)
  5250. dump := operatorInitializationCodeSection.comments;
  5251. (* IF dump # NIL THEN dump.String("*** begin of operator registration ***"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5252. IF GetRuntimeProcedure("FoxOperatorRuntime", "RegisterOperator", runtimeProcedure, TRUE) THEN
  5253. (* push ID *)
  5254. (* IF dump # NIL THEN dump.String("push ID"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5255. id := Global.GetSymbol(module.module.case, operator.name);
  5256. operatorInitializationCodeSection.Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), id)));
  5257. (* push kind *)
  5258. (* IF dump # NIL THEN dump.String("push kind"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5259. operatorInitializationCodeSection.Emit(Push(position,IntermediateCode.Immediate(setType, SYSTEM.VAL(LONGINT, kind))));
  5260. (* push type infos *)
  5261. PushTypeInfo(leftType);
  5262. PushTypeInfo(rightType);
  5263. (* push operator address *)
  5264. (* IF dump # NIL THEN dump.String("push operator address"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5265. StaticCallOperand(operatorOperand, operator);
  5266. operatorInitializationCodeSection.Emit(Push(position,operatorOperand.op));
  5267. ReleaseOperand(operatorOperand);
  5268. StaticCallOperand(runtimeProcedureOperand, runtimeProcedure);
  5269. operatorInitializationCodeSection.Emit(Call(position,runtimeProcedureOperand.op, ProcedureParametersSize(system, runtimeProcedure)));
  5270. ReleaseOperand(runtimeProcedureOperand)
  5271. END
  5272. (* IF dump # NIL THEN dump.String("*** end of operator registration ***"); dump.Ln; dump.Update END *) (* TENTATIVE *)
  5273. END
  5274. END RegisterDynamicOperator;
  5275. PROCEDURE SystemTrace(x: SyntaxTree.ExpressionList; pos: LONGINT);
  5276. VAR
  5277. traceModule: SyntaxTree.Module;
  5278. procedure: SyntaxTree.Procedure;
  5279. procedureVariable: SyntaxTree.Variable;
  5280. s,msg: Basic.MessageString;
  5281. res: Operand;
  5282. i: LONGINT;
  5283. sv: SyntaxTree.StringValue;
  5284. type: SyntaxTree.Type;
  5285. recordType: SyntaxTree.RecordType;
  5286. printout: Printout.Printer;
  5287. stringWriter: Streams.StringWriter;
  5288. expression: SyntaxTree.Expression;
  5289. PROCEDURE GetProcedure(CONST procedureName: ARRAY OF CHAR): BOOLEAN;
  5290. BEGIN
  5291. procedure := traceModule.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  5292. IF procedure = NIL THEN
  5293. procedureVariable := traceModule.moduleScope.FindVariable(SyntaxTree.NewIdentifier(procedureName));
  5294. END;
  5295. IF (procedure = NIL) & (procedureVariable = NIL) THEN
  5296. s := "procedure ";
  5297. Strings.Append(s,backend.traceModuleName);
  5298. Strings.Append(s,".");
  5299. Strings.Append(s,procedureName);
  5300. Strings.Append(s," not present");
  5301. Error(position,s);
  5302. RETURN FALSE
  5303. ELSE
  5304. RETURN TRUE
  5305. END;
  5306. END GetProcedure;
  5307. PROCEDURE CallProcedure;
  5308. VAR size: LONGINT;
  5309. BEGIN
  5310. IF procedure # NIL THEN
  5311. StaticCallOperand(result,procedure);
  5312. size := ProcedureParametersSize(system,procedure);
  5313. ELSE
  5314. Symbol(procedureVariable, result);
  5315. LoadValue(result, procedureVariable.type.resolved);
  5316. size := ParametersSize(system, procedureVariable.type.resolved(SyntaxTree.ProcedureType), FALSE);
  5317. END;
  5318. Emit(Call(position,result.op,size));
  5319. END CallProcedure;
  5320. PROCEDURE String(CONST s: ARRAY OF CHAR);
  5321. VAR res: Operand; string: SyntaxTree.String;
  5322. BEGIN
  5323. IF GetProcedure("String") THEN
  5324. NEW(string, LEN(s)); COPY(s, string^);
  5325. sv := SyntaxTree.NewStringValue(-1,string);
  5326. type := SyntaxTree.NewStringType(-1,system.characterType,Strings.Length(s));
  5327. sv.SetType(type);
  5328. Designate(sv,res);
  5329. Emit(Push(position,res.tag));
  5330. Emit(Push(position,res.op));
  5331. ReleaseOperand(res);
  5332. CallProcedure;
  5333. END;
  5334. END String;
  5335. PROCEDURE Integer(op: IntermediateCode.Operand);
  5336. BEGIN
  5337. IF GetProcedure("Int") THEN
  5338. Emit(Push(position,op));
  5339. Emit(Push(position,IntermediateCode.Immediate(int32,1)));
  5340. CallProcedure;
  5341. END;
  5342. END Integer;
  5343. PROCEDURE Float(op: IntermediateCode.Operand);
  5344. BEGIN
  5345. IF GetProcedure("HIntHex") THEN
  5346. Emit(Push(position,op));
  5347. Emit(Push(position,IntermediateCode.Immediate(int32,16)));
  5348. CallProcedure;
  5349. END;
  5350. END Float;
  5351. PROCEDURE Set(op: IntermediateCode.Operand);
  5352. BEGIN
  5353. IF GetProcedure("Set") THEN
  5354. Emit(Push(position,op));
  5355. (*
  5356. Emit(Push(position,IntermediateCode.Immediate(int32,0))); (* ofs *)
  5357. Emit(Push(position,IntermediateCode.Immediate(int32,32))); (* n *)
  5358. *)
  5359. CallProcedure;
  5360. END;
  5361. END Set;
  5362. PROCEDURE Boolean(op: IntermediateCode.Operand);
  5363. BEGIN
  5364. IF GetProcedure("Boolean") THEN
  5365. Emit(Push(position,op));
  5366. CallProcedure;
  5367. END;
  5368. END Boolean;
  5369. PROCEDURE Char(op: IntermediateCode.Operand);
  5370. BEGIN
  5371. IF GetProcedure("Char") THEN
  5372. Emit(Push(position,op));
  5373. CallProcedure;
  5374. END;
  5375. END Char;
  5376. PROCEDURE Address(op: IntermediateCode.Operand);
  5377. BEGIN
  5378. IF GetProcedure("Address") THEN
  5379. Emit(Push(position,op));
  5380. CallProcedure;
  5381. END;
  5382. END Address;
  5383. PROCEDURE StringOperand(op,tag: IntermediateCode.Operand);
  5384. BEGIN
  5385. IF GetProcedure("String") THEN
  5386. Emit(Push(position,tag));
  5387. Emit(Push(position,op));
  5388. CallProcedure;
  5389. END;
  5390. END StringOperand;
  5391. PROCEDURE Ln;
  5392. BEGIN
  5393. IF GetProcedure("Ln") THEN
  5394. CallProcedure;
  5395. END;
  5396. END Ln;
  5397. BEGIN
  5398. IF backend.traceModuleName = "" THEN RETURN END;
  5399. IF AddImport(backend.traceModuleName,traceModule,TRUE) THEN
  5400. IF GetProcedure("Enter") THEN
  5401. CallProcedure
  5402. END;
  5403. NEW(stringWriter,LEN(s));
  5404. FOR i := 0 TO x.Length()-1 DO
  5405. msg := "";
  5406. expression := x.GetExpression(i);
  5407. IF currentScope IS SyntaxTree.ProcedureScope THEN
  5408. Global.GetSymbolName(currentScope(SyntaxTree.ProcedureScope).ownerProcedure, s)
  5409. ELSE
  5410. Global.GetModuleName(module.module, s);
  5411. END;
  5412. IF i = 0 THEN
  5413. stringWriter.String(s); stringWriter.String("@"); stringWriter.Int(pos,1);
  5414. stringWriter.String(":");
  5415. END;
  5416. printout := Printout.NewPrinter(stringWriter,Printout.SourceCode,FALSE);
  5417. IF ~(expression IS SyntaxTree.StringValue) THEN
  5418. printout.Expression(expression);
  5419. stringWriter.Get(s);
  5420. Strings.Append(msg,s);
  5421. Strings.Append(msg,"= ");
  5422. ELSE stringWriter.Get(s); (* remove from string writer *)
  5423. Strings.Append(msg, s);
  5424. END;
  5425. String(msg);
  5426. IF SemanticChecker.IsStringType(expression.type) THEN
  5427. Designate(expression,res);
  5428. StringOperand(res.op,res.tag);
  5429. ELSE
  5430. Evaluate(expression,res);
  5431. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  5432. IF res.op.type.sizeInBits < IntermediateCode.Bits64 THEN
  5433. Convert(res.op,int64);
  5434. END;
  5435. Integer(res.op);
  5436. ELSIF expression.type.resolved IS SyntaxTree.BooleanType THEN
  5437. Boolean(res.op);
  5438. ELSIF expression.type.resolved IS SyntaxTree.SetType THEN
  5439. Set(res.op);
  5440. ELSIF expression.type.resolved IS SyntaxTree.FloatType THEN
  5441. IF res.op.type.sizeInBits = IntermediateCode.Bits32 THEN
  5442. Convert(res.op,float64);
  5443. END;
  5444. Float(res.op);
  5445. ELSIF (expression.type.resolved IS SyntaxTree.CharacterType) & (expression.type.resolved.sizeInBits = 8) THEN
  5446. Char(res.op);
  5447. ELSIF expression.type.resolved IS SyntaxTree.AddressType THEN
  5448. Address(res.op);String("H");
  5449. ELSIF expression.type.resolved IS SyntaxTree.SizeType THEN
  5450. Address(res.op);String("H");
  5451. ELSIF (expression.type.resolved IS SyntaxTree.PointerType) OR IsPointerToRecord(expression.type,recordType) THEN
  5452. Address(res.op);String("H");
  5453. ELSIF (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  5454. Address(res.op);String("H");
  5455. ELSIF expression.type.resolved IS SyntaxTree.NilType THEN
  5456. String("NIL");
  5457. ELSE HALT(200);
  5458. END;
  5459. END;
  5460. ReleaseOperand(res);
  5461. String("; ");
  5462. END;
  5463. IF GetProcedure("Exit") THEN
  5464. CallProcedure
  5465. ELSE
  5466. Ln;
  5467. END;
  5468. END;
  5469. END SystemTrace;
  5470. PROCEDURE InitFields(type: SyntaxTree.Type; CONST adr: IntermediateCode.Operand; offset: LONGINT);
  5471. VAR baseType: SyntaxTree.Type; imm,mem: IntermediateCode.Operand; dim,size: LONGINT;
  5472. variable: SyntaxTree.Variable; i: LONGINT; initializerOp: Operand;
  5473. BEGIN
  5474. type := type.resolved;
  5475. IF type IS SyntaxTree.RecordType THEN
  5476. WITH type: SyntaxTree.RecordType DO
  5477. baseType := type.baseType;
  5478. IF baseType # NIL THEN
  5479. baseType := baseType.resolved;
  5480. IF baseType IS SyntaxTree.PointerType THEN baseType := baseType(SyntaxTree.PointerType).pointerBase END;
  5481. InitFields(baseType,adr,offset);
  5482. END;
  5483. variable := type.recordScope.firstVariable;
  5484. WHILE variable # NIL DO
  5485. IF variable.initializer # NIL THEN
  5486. Evaluate(variable.initializer,initializerOp);
  5487. MakeMemory(mem,adr,IntermediateCode.GetType(system,variable.type),offset+ ToMemoryUnits(system,variable.offsetInBits));
  5488. Emit(Mov(position,mem,initializerOp.op));
  5489. ReleaseOperand(initializerOp);
  5490. ReleaseIntermediateOperand(mem);
  5491. END;
  5492. InitFields(variable.type, adr, offset+ ToMemoryUnits(system,variable.offsetInBits));
  5493. variable := variable.nextVariable
  5494. END;
  5495. END;
  5496. ELSIF (type IS SyntaxTree.ArrayType) THEN
  5497. WITH type: SyntaxTree.ArrayType DO
  5498. IF type.form = SyntaxTree.Static THEN
  5499. baseType := type.arrayBase;
  5500. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  5501. FOR i := 0 TO type.staticLength-1 DO
  5502. InitFields(baseType,adr,offset+i*size);
  5503. END;
  5504. END;
  5505. END;
  5506. ELSIF type IS SyntaxTree.MathArrayType THEN
  5507. WITH type: SyntaxTree.MathArrayType DO
  5508. IF type.form = SyntaxTree.Open THEN
  5509. dim := DynamicDim(type);
  5510. imm := IntermediateCode.Immediate(addressType,dim);
  5511. PutMathArrayFieldOffset(adr,imm,MathDimOffset,offset);
  5512. baseType := SemanticChecker.ArrayBase(type,dim);
  5513. IF baseType = NIL THEN size := 0 ELSE size := system.AlignedSizeOf(baseType) END;
  5514. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,size));
  5515. PutMathArrayFieldOffset(adr,imm,MathElementSizeOffset,offset);
  5516. ReleaseIntermediateOperand(imm);
  5517. (* flags remain empty (=0) for open array *)
  5518. ELSIF type.form = SyntaxTree.Static THEN
  5519. baseType := type.arrayBase;
  5520. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  5521. FOR i := 0 TO type.staticLength-1 DO
  5522. InitFields(baseType,adr,offset+i*size);
  5523. END;
  5524. END;
  5525. END;
  5526. END;
  5527. END InitFields;
  5528. PROCEDURE InitVariable(VAR variable: SyntaxTree.Variable);
  5529. VAR type: SyntaxTree.Type; operand: Operand; tmp: IntermediateCode.Operand; reference: SyntaxTree.Expression;
  5530. BEGIN
  5531. type := variable.type.resolved;
  5532. IF (type IS SyntaxTree.MathArrayType) THEN
  5533. WITH type: SyntaxTree.MathArrayType DO
  5534. IF type.form = SyntaxTree.Open THEN
  5535. Symbol(variable,operand);
  5536. InitFields(type, operand.tag,0);
  5537. ELSIF type.form = SyntaxTree.Tensor THEN
  5538. Symbol(variable, operand);
  5539. MakeMemory(tmp,operand.op,addressType,0);
  5540. ReleaseOperand(operand);
  5541. Emit(Mov(position,tmp, nil ) );
  5542. ReleaseIntermediateOperand(tmp);
  5543. END;
  5544. END;
  5545. ELSE
  5546. Symbol(variable,operand);
  5547. IF variable.initializer # NIL THEN
  5548. reference := SyntaxTree.NewSymbolDesignator(variable.initializer.position,NIL,variable);
  5549. reference.SetType(variable.type.resolved);
  5550. reference.SetAssignable(TRUE);
  5551. Assign(reference,variable.initializer);
  5552. END;
  5553. InitFields(type, operand.op,0);
  5554. ReleaseOperand(operand);
  5555. END;
  5556. END InitVariable;
  5557. PROCEDURE MathArrayDim(type: SyntaxTree.MathArrayType; CONST base: IntermediateCode.Operand; VAR result: Operand);
  5558. VAR end: Label;
  5559. BEGIN
  5560. IF type.form = SyntaxTree.Tensor THEN
  5561. InitOperand(result,ModeValue);
  5562. ReuseCopy(result.op,base);
  5563. end := NewLabel();
  5564. BreqL(end,result.op,IntermediateCode.Immediate(addressType,0));
  5565. Emit(MovReplace(position,result.op,IntermediateCode.Memory(addressType,result.op,ToMemoryUnits(system,MathDimOffset*addressType.sizeInBits))));
  5566. SetLabel(end);
  5567. Convert(result.op,int32);
  5568. ELSE
  5569. InitOperand(result,ModeValue);
  5570. IntermediateCode.InitImmediate(result.op, int32, SemanticChecker.Dimension(type,{SyntaxTree.Open, SyntaxTree.Static}));
  5571. END
  5572. END MathArrayDim;
  5573. PROCEDURE PutMathArrayField(base,value: IntermediateCode.Operand; fieldOffset: LONGINT);
  5574. VAR mem: IntermediateCode.Operand; offset: LONGINT;
  5575. BEGIN
  5576. offset := ToMemoryUnits(system,fieldOffset*addressType.sizeInBits);
  5577. MakeMemory(mem,base,addressType,offset);
  5578. Emit(Mov(position,mem,value));
  5579. ReleaseIntermediateOperand(mem);
  5580. END PutMathArrayField;
  5581. PROCEDURE PutMathArrayFieldOffset(base,value: IntermediateCode.Operand; fieldOffset, offset: LONGINT);
  5582. VAR mem: IntermediateCode.Operand;
  5583. BEGIN
  5584. offset := offset + ToMemoryUnits(system,fieldOffset*addressType.sizeInBits);
  5585. MakeMemory(mem,base,addressType,offset);
  5586. Emit(Mov(position,mem,value));
  5587. ReleaseIntermediateOperand(mem);
  5588. END PutMathArrayFieldOffset;
  5589. PROCEDURE GetMathArrayField(VAR value: IntermediateCode.Operand; base: IntermediateCode.Operand; offset: LONGINT);
  5590. BEGIN
  5591. offset := ToMemoryUnits(system,offset*addressType.sizeInBits);
  5592. MakeMemory(value,base,addressType,offset);
  5593. END GetMathArrayField;
  5594. PROCEDURE PutMathArrayLenOrIncr(CONST base,value,dim: IntermediateCode.Operand; incr: BOOLEAN);
  5595. VAR offset: LONGINT; reg,mem: IntermediateCode.Operand;
  5596. BEGIN
  5597. IF incr THEN
  5598. offset := ToMemoryUnits(system,MathIncrOffset*addressType.sizeInBits);
  5599. ELSE
  5600. offset := ToMemoryUnits(system,MathLenOffset*addressType.sizeInBits);
  5601. END;
  5602. IF dim.mode=IntermediateCode.ModeImmediate THEN
  5603. PutMathArrayField(base,value,offset + ToMemoryUnits(system,SHORT(dim.intValue) * 2 * addressType.sizeInBits));
  5604. ELSE
  5605. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  5606. Emit(Mov(position,reg,dim));
  5607. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,2*addressType.sizeInBits))));
  5608. Emit(Add(position,reg,reg,base));
  5609. MakeMemory(mem, reg, addressType, offset);
  5610. ReleaseIntermediateOperand(reg);
  5611. Emit(Mov(position,mem,value));
  5612. ReleaseIntermediateOperand(mem);
  5613. END;
  5614. END PutMathArrayLenOrIncr;
  5615. PROCEDURE PutMathArrayLength(base,value: IntermediateCode.Operand; dim: LONGINT);
  5616. BEGIN
  5617. PutMathArrayField(base,value,MathLenOffset + dim * 2);
  5618. END PutMathArrayLength;
  5619. PROCEDURE PutMathArrayIncrement(base,value: IntermediateCode.Operand; dim: LONGINT);
  5620. BEGIN
  5621. PutMathArrayField(base,value,MathIncrOffset + dim * 2);
  5622. END PutMathArrayIncrement;
  5623. PROCEDURE GetMathArrayIncrement(type: SyntaxTree.MathArrayType; CONST operand: Operand; VAR dim: IntermediateCode.Operand; check: BOOLEAN; VAR result: Operand);
  5624. BEGIN
  5625. MathArrayLenOrIncr(type,operand,dim,TRUE,check,result);
  5626. END GetMathArrayIncrement;
  5627. PROCEDURE GetMathArrayLength(type: SyntaxTree.MathArrayType; CONST operand: Operand; VAR dim: IntermediateCode.Operand; check: BOOLEAN; VAR result: Operand);
  5628. BEGIN
  5629. MathArrayLenOrIncr(type,operand,dim,FALSE,check,result);
  5630. END GetMathArrayLength;
  5631. PROCEDURE GetMathArrayLengthAt(type: SyntaxTree.MathArrayType; CONST operand: Operand; dim: LONGINT; check: BOOLEAN; VAR result: Operand);
  5632. VAR dimOp: IntermediateCode.Operand;
  5633. BEGIN
  5634. dimOp := IntermediateCode.Immediate(int32, dim);
  5635. GetMathArrayLength(type, operand, dimOp, check, result);
  5636. END GetMathArrayLengthAt;
  5637. PROCEDURE GetMathArrayIncrementAt(type: SyntaxTree.MathArrayType; CONST operand: Operand; dim: LONGINT; check: BOOLEAN; VAR result: Operand);
  5638. VAR dimOp: IntermediateCode.Operand;
  5639. BEGIN
  5640. dimOp := IntermediateCode.Immediate(int32, dim);
  5641. GetMathArrayIncrement(type, operand, dimOp, check, result);
  5642. END GetMathArrayIncrementAt;
  5643. PROCEDURE MathArrayLenOrIncr(type: SyntaxTree.MathArrayType; CONST operand: Operand; VAR dim: IntermediateCode.Operand; increment: BOOLEAN; check: BOOLEAN; VAR result: Operand );
  5644. VAR val: LONGINT; res,res2: IntermediateCode.Operand; end,next: Label; t: SyntaxTree.Type; imm: IntermediateCode.Operand; hasDynamicPart: BOOLEAN;
  5645. offset: LONGINT;
  5646. BEGIN
  5647. IF increment THEN
  5648. offset := MathIncrOffset;
  5649. ELSE
  5650. offset := MathLenOffset;
  5651. END;
  5652. INC(offset,operand.dimOffset*2);
  5653. IF check & (type.form = SyntaxTree.Tensor) & ~isUnchecked THEN
  5654. TrapC(BrneL,operand.tag,IntermediateCode.Immediate(addressType,0),IndexCheckTrap);
  5655. END;
  5656. (* static dimension *)
  5657. IF dim.mode = IntermediateCode.ModeImmediate THEN
  5658. IF check & (type.form = SyntaxTree.Tensor) THEN
  5659. DimensionCheck(operand.tag,dim,BrltL);
  5660. END;
  5661. val := SHORT(dim.intValue);
  5662. IF type.form # SyntaxTree.Tensor THEN
  5663. t := SemanticChecker.ArrayBase(type,val);
  5664. type := t.resolved(SyntaxTree.MathArrayType);
  5665. IF type.form = SyntaxTree.Static THEN
  5666. IF increment THEN
  5667. res := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,type.staticIncrementInBits));
  5668. ELSE
  5669. res := IntermediateCode.Immediate(addressType,type.staticLength);
  5670. END;
  5671. InitOperand(result,ModeValue);
  5672. result.op := res;
  5673. RETURN;
  5674. END;
  5675. END;
  5676. offset := ToMemoryUnits(system, (val*2+offset)*addressType.sizeInBits);
  5677. MakeMemory(res,operand.tag,addressType,offset);
  5678. (*
  5679. res := IntermediateCode.Memory(addressType,operand.tag,offset);
  5680. *)
  5681. InitOperand(result,ModeValue);
  5682. result.op := res;
  5683. ELSE
  5684. Convert(dim,addressType);
  5685. IF check THEN
  5686. IF type.form = SyntaxTree.Tensor THEN
  5687. DimensionCheck(operand.tag,dim,BrltL);
  5688. ELSIF isUnchecked THEN (* do nothing *)
  5689. ELSE
  5690. TrapC(BrltL,dim,IntermediateCode.Immediate(addressType,SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static})), IndexCheckTrap);
  5691. END;
  5692. END;
  5693. end := NewLabel(); next := NIL;
  5694. IntermediateCode.InitRegister(res,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  5695. Emit(Mov(position,res,dim));
  5696. Convert(res,int32);
  5697. t := type; val := operand.dimOffset; hasDynamicPart := FALSE;
  5698. WHILE t IS SyntaxTree.MathArrayType DO
  5699. type := t(SyntaxTree.MathArrayType);
  5700. IF type.form = SyntaxTree.Static THEN
  5701. imm := IntermediateCode.Immediate(int32,val);
  5702. next := NewLabel();
  5703. BrneL(next,imm,res);
  5704. IF increment THEN
  5705. imm := IntermediateCode.Immediate(int32,ToMemoryUnits(system,type.staticIncrementInBits));
  5706. ELSE
  5707. imm := IntermediateCode.Immediate(int32,type.staticLength);
  5708. END;
  5709. Emit(MovReplace(position,res,imm));
  5710. BrL(end);
  5711. ELSE hasDynamicPart := TRUE;
  5712. END;
  5713. t := type.arrayBase.resolved;
  5714. val := val + 1;
  5715. IF next # NIL THEN SetLabel(next) END;
  5716. END;
  5717. IF hasDynamicPart THEN
  5718. IntermediateCode.InitRegister(res2,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  5719. Emit(Mov(position,res2,dim));
  5720. Emit(Mul(position,res2,res2,IntermediateCode.Immediate(addressType,2*ToMemoryUnits(system,addressType.sizeInBits))));
  5721. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,offset*addressType.sizeInBits));
  5722. Emit(Add(position,res2,res2,imm));
  5723. Emit(Add(position,res2,res2,operand.tag));
  5724. IntermediateCode.MakeMemory(res2,int32);
  5725. Emit(MovReplace(position,res,res2));
  5726. ReleaseIntermediateOperand(res2);
  5727. END;
  5728. SetLabel(end);
  5729. Convert(res,int32);
  5730. InitOperand(result,ModeValue);
  5731. result.op := res;
  5732. END;
  5733. END MathArrayLenOrIncr;
  5734. PROCEDURE ArrayLen(type: SyntaxTree.ArrayType; VAR operand: Operand; VAR dim: IntermediateCode.Operand; VAR result: Operand );
  5735. VAR val: LONGINT; res,res2: IntermediateCode.Operand; end,next: Label; t: SyntaxTree.Type; imm: IntermediateCode.Operand; hasDynamicPart: BOOLEAN;
  5736. offset: LONGINT;
  5737. BEGIN
  5738. offset := operand.dimOffset+DynamicDim(type)-1;
  5739. IF dim.mode = IntermediateCode.ModeImmediate THEN
  5740. ASSERT(type.form IN {SyntaxTree.Open, SyntaxTree.SemiDynamic});
  5741. val := SHORT(dim.intValue);
  5742. t := SemanticChecker.ArrayBase(type,val);
  5743. type := t.resolved(SyntaxTree.ArrayType);
  5744. IF type.form = SyntaxTree.Static THEN
  5745. res := IntermediateCode.Immediate(addressType,type.staticLength);
  5746. ELSE
  5747. offset := ToMemoryUnits(system, (offset-val)*addressType.sizeInBits);
  5748. res := IntermediateCode.Memory(addressType,operand.tag,offset);
  5749. END;
  5750. UseIntermediateOperand(res);
  5751. InitOperand(result,ModeValue);
  5752. result.op := res;
  5753. ELSE
  5754. Convert(dim,addressType);
  5755. IF ~isUnchecked THEN
  5756. TrapC(BrltL,dim,IntermediateCode.Immediate(addressType,SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static})), IndexCheckTrap);
  5757. END;
  5758. end := NewLabel(); next := NIL;
  5759. (* ReuseCopy(dim,res); *)
  5760. IntermediateCode.InitRegister(res,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType, IntermediateCode.GeneralPurposeRegister));
  5761. Emit(Mov(position,res,dim));
  5762. Convert(res,int32);
  5763. Convert(res,int32);
  5764. t := type; val := operand.dimOffset; hasDynamicPart := FALSE;
  5765. WHILE t IS SyntaxTree.ArrayType DO
  5766. type := t(SyntaxTree.ArrayType);
  5767. IF type.form = SyntaxTree.Static THEN
  5768. imm := IntermediateCode.Immediate(int32,val);
  5769. next := NewLabel();
  5770. BrneL(next,imm,res);
  5771. imm := IntermediateCode.Immediate(int32,type.staticLength);
  5772. Emit(MovReplace(position,res,imm));
  5773. BrL(end);
  5774. ELSE hasDynamicPart := TRUE;
  5775. END;
  5776. t := type.arrayBase.resolved;
  5777. val := val + 1;
  5778. IF next # NIL THEN SetLabel(next) END;
  5779. END;
  5780. IF hasDynamicPart THEN
  5781. ReuseCopy(res2,dim); (* dim is now in register res2 *)
  5782. Convert(res2,addressType);
  5783. Emit(Mul(position,res2,res2,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,addressType.sizeInBits))));
  5784. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,offset*addressType.sizeInBits));
  5785. Emit(Sub(position,res2,imm,res2));
  5786. Emit(Add(position,res2,res2,operand.tag));
  5787. IntermediateCode.MakeMemory(res2,int32);
  5788. Emit(MovReplace(position,res,res2));
  5789. ReleaseIntermediateOperand(res2);
  5790. END;
  5791. SetLabel(end);
  5792. Convert(res,int32);
  5793. InitOperand(result,ModeValue);
  5794. result.op := res;
  5795. END;
  5796. END ArrayLen;
  5797. (**
  5798. create a temporary variable in current scope
  5799. **)
  5800. PROCEDURE GetTemporaryVariable(type: SyntaxTree.Type; register: BOOLEAN): SyntaxTree.Variable;
  5801. VAR name: SyntaxTree.Identifier; string: SyntaxTree.IdentifierString ; variable: SyntaxTree.Variable;
  5802. scope: SyntaxTree.Scope; duplicate: BOOLEAN; offset, index: LONGINT;
  5803. BEGIN
  5804. IF ~register THEN
  5805. variable := temporaries.GetFreeVariable(type, index);
  5806. ELSE
  5807. index := temporaries.registerIndex; INC(temporaries.registerIndex);
  5808. END;
  5809. scope := currentScope;
  5810. IF variable = NIL THEN
  5811. COPY("@hiddenIRVar",string);
  5812. Basic.AppendNumber(string,index);
  5813. name := SyntaxTree.NewIdentifier(string);
  5814. variable := SyntaxTree.NewVariable(Diagnostics.Invalid,name);
  5815. variable.SetType(type);
  5816. variable.SetAccess(SyntaxTree.Hidden);
  5817. IF ~register THEN
  5818. temporaries.AddVariable(variable);
  5819. IF scope.lastVariable # NIL THEN
  5820. offset := scope.lastVariable.offsetInBits;
  5821. ELSE
  5822. offset := 0;
  5823. END;
  5824. DEC(offset,system.SizeOf(variable.type));
  5825. Basic.Align(offset,-system.AlignmentOf(system.variableAlignment,variable.type));
  5826. variable(SyntaxTree.Variable).SetOffset(offset);
  5827. scope.AddVariable(variable(SyntaxTree.Variable));
  5828. scope.EnterSymbol(variable, duplicate);
  5829. ASSERT(~duplicate);
  5830. InitVariable(variable(SyntaxTree.Variable));
  5831. ELSE
  5832. variable.SetUseRegister(TRUE);
  5833. variable(SyntaxTree.Variable).SetOffset(0);
  5834. END;
  5835. ELSE
  5836. InitVariable(variable(SyntaxTree.Variable));
  5837. (*
  5838. ASSERT(variable.type.resolved = type.resolved)
  5839. *)
  5840. END;
  5841. RETURN variable(SyntaxTree.Variable)
  5842. END GetTemporaryVariable;
  5843. PROCEDURE GetMathArrayDescriptorType(dimensions: LONGINT): SyntaxTree.Type;
  5844. VAR name: ARRAY 32 OF CHAR; symbol: SyntaxTree.Symbol; typeDeclaration: SyntaxTree.TypeDeclaration;
  5845. recordType: SyntaxTree.RecordType; type: SyntaxTree.Type;
  5846. recordScope: SyntaxTree.RecordScope; parentScope: SyntaxTree.Scope; identifier: SyntaxTree.Identifier;
  5847. i: LONGINT; duplicate: BOOLEAN;
  5848. PROCEDURE AddVariable(CONST name: ARRAY OF CHAR; type: SyntaxTree.Type);
  5849. VAR variable: SyntaxTree.Variable;
  5850. BEGIN
  5851. variable := SyntaxTree.NewVariable(-1,SyntaxTree.NewIdentifier(name));
  5852. variable.SetType(type);
  5853. recordScope.AddVariable(variable);
  5854. END AddVariable;
  5855. BEGIN
  5856. name := "@ArrayDescriptor";
  5857. Basic.AppendNumber(name,dimensions);
  5858. identifier := SyntaxTree.NewIdentifier(name);
  5859. parentScope := module.module.moduleScope;
  5860. symbol := parentScope.FindSymbol(identifier);
  5861. IF symbol # NIL THEN
  5862. typeDeclaration := symbol(SyntaxTree.TypeDeclaration);
  5863. type := typeDeclaration.declaredType;
  5864. ELSE
  5865. typeDeclaration := SyntaxTree.NewTypeDeclaration(-1,SyntaxTree.NewIdentifier(name));
  5866. typeDeclaration.SetAccess(SyntaxTree.Hidden);
  5867. recordScope := SyntaxTree.NewRecordScope(parentScope);
  5868. recordType := SyntaxTree.NewRecordType( -1, parentScope, recordScope);
  5869. recordType.SetTypeDeclaration(typeDeclaration);
  5870. recordType.SetState(SyntaxTree.Resolved);
  5871. typeDeclaration.SetDeclaredType(recordType);
  5872. AddVariable("@ptr",system.anyType);
  5873. AddVariable("@adr",system.addressType);
  5874. AddVariable("@flags",system.addressType);
  5875. AddVariable("@dim",system.addressType);
  5876. AddVariable("@elementSize",system.addressType);
  5877. FOR i := 0 TO dimensions-1 DO
  5878. name := "@len";
  5879. Basic.AppendNumber(name,i);
  5880. AddVariable(name,system.addressType);
  5881. name := "@incr";
  5882. Basic.AppendNumber(name,i);
  5883. AddVariable(name,system.addressType);
  5884. END;
  5885. parentScope.AddTypeDeclaration(typeDeclaration);
  5886. parentScope.EnterSymbol(typeDeclaration,duplicate);
  5887. ASSERT(~duplicate);
  5888. type := recordType;
  5889. END;
  5890. RETURN type
  5891. END GetMathArrayDescriptorType;
  5892. PROCEDURE NewMathArrayDescriptor(op: Operand; dimensions: LONGINT);
  5893. VAR reg: IntermediateCode.Operand; type: SyntaxTree.Type;
  5894. BEGIN
  5895. type := GetMathArrayDescriptorType(dimensions);
  5896. Emit(Push(position,op.op));
  5897. (* push type descriptor *)
  5898. reg := TypeDescriptorAdr(type);
  5899. IF ~newObjectFile THEN
  5900. IntermediateCode.MakeMemory(reg,addressType);
  5901. END;
  5902. Emit(Push(position,reg));
  5903. ReleaseIntermediateOperand(reg);
  5904. (* push realtime flag: false by default *)
  5905. Emit(Push(position,false));
  5906. CallThis(position,"Heaps","NewRec",3);
  5907. END NewMathArrayDescriptor;
  5908. PROCEDURE PushConstString(CONST s: ARRAY OF CHAR);
  5909. VAR res: Operand; string: SyntaxTree.String; sv: SyntaxTree.StringValue; type: SyntaxTree.Type;
  5910. BEGIN
  5911. NEW(string, LEN(s)); COPY(s, string^);
  5912. sv := SyntaxTree.NewStringValue(-1,string);
  5913. type := SyntaxTree.NewStringType(-1,system.characterType,Strings.Length(s));
  5914. sv.SetType(type);
  5915. Designate(sv,res);
  5916. Emit(Push(position,res.tag));
  5917. Emit(Push(position,res.op));
  5918. ReleaseOperand(res);
  5919. END PushConstString;
  5920. PROCEDURE PushConstBoolean(b: BOOLEAN);
  5921. BEGIN
  5922. IF b THEN
  5923. Emit(Push(-1, true));
  5924. ELSE
  5925. Emit(Push(-1, false));
  5926. END;
  5927. END PushConstBoolean;
  5928. PROCEDURE PushConstSet(v: SET);
  5929. VAR value: SyntaxTree.Value; op: Operand;
  5930. BEGIN
  5931. value := SyntaxTree.NewSetValue(-1, v);
  5932. value.SetType(system.setType);
  5933. Evaluate(value, op);
  5934. Emit(Push(-1, op.op));
  5935. ReleaseOperand(op);
  5936. END PushConstSet;
  5937. PROCEDURE PushConstInteger(v: LONGINT);
  5938. VAR value: SyntaxTree.Value; op: Operand;
  5939. BEGIN
  5940. value := SyntaxTree.NewIntegerValue(-1, v);
  5941. value.SetType(system.longintType);
  5942. Evaluate(value, op);
  5943. Emit(Push(-1, op.op));
  5944. ReleaseOperand(op);
  5945. END PushConstInteger;
  5946. PROCEDURE OpenInitializer(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope): IntermediateCode.Section;
  5947. VAR name: Basic.SegmentedName; procedure: SyntaxTree.Procedure; procedureScope: SyntaxTree.ProcedureScope;
  5948. section: IntermediateCode.Section;
  5949. BEGIN
  5950. procedureScope := SyntaxTree.NewProcedureScope(scope);
  5951. Global.GetSymbolSegmentedName(symbol, name);
  5952. Basic.SuffixSegmentedName(name, Basic.MakeString("@Initializer"));
  5953. procedure := SyntaxTree.NewProcedure(-1, SyntaxTree.NewIdentifier(""), procedureScope);
  5954. procedure.SetScope(moduleScope);
  5955. procedure.SetType(SyntaxTree.NewProcedureType(-1,scope));
  5956. procedure.type(SyntaxTree.ProcedureType).SetDelegate(TRUE);
  5957. procedure.SetAccess(SyntaxTree.Hidden);
  5958. currentScope := procedureScope;
  5959. section := NewSection(module.allSections, Sections.CodeSection, name, NIL,commentPrintout # NIL);
  5960. EmitEnter(section, -1,NIL,0,0,0);
  5961. RETURN section;
  5962. END OpenInitializer;
  5963. PROCEDURE CloseInitializer(prev: IntermediateCode.Section);
  5964. BEGIN
  5965. EmitLeave(section, 0, 0 );
  5966. Emit(Exit(-1,ToMemoryUnits(system,addressType.sizeInBits),0));
  5967. section := prev;
  5968. END CloseInitializer;
  5969. PROCEDURE AddPorts(cell: SyntaxTree.Symbol; x: SyntaxTree.CellType);
  5970. VAR name: SyntaxTree.IdentifierString;
  5971. variable: SyntaxTree.Variable;
  5972. type: SyntaxTree.Type;
  5973. PROCEDURE Field(symbol: SyntaxTree.Symbol; VAR op: Operand);
  5974. BEGIN
  5975. InitOperand(op,ModeReference);
  5976. op.op := fp;
  5977. IntermediateCode.AddOffset(op.op,ToMemoryUnits(system,2*addressType.sizeInBits));
  5978. Dereference(op, x, FALSE);
  5979. result := op;
  5980. Symbol(symbol, op);
  5981. END Field;
  5982. PROCEDURE Direction(direction: LONGINT): SET;
  5983. BEGIN
  5984. IF direction = SyntaxTree.OutPort THEN RETURN {0}
  5985. ELSIF direction = SyntaxTree.InPort THEN RETURN {1}
  5986. ELSE HALT(100);
  5987. END;
  5988. END Direction;
  5989. PROCEDURE AddPortProperty(port: SyntaxTree.Variable; modifier: SyntaxTree.Modifier; value: SyntaxTree.Expression);
  5990. VAR name: ARRAY 256 OF CHAR; op: Operand;
  5991. BEGIN
  5992. Field(port, op);
  5993. ToMemory(op.op,addressType,0);
  5994. Emit(Push(-1, op.op));
  5995. ReleaseOperand(op);
  5996. Basic.GetString(modifier.identifier, name);
  5997. PushConstString(name);
  5998. IF SemanticChecker.IsStringType(modifier.expression.type) THEN
  5999. ASSERT(SemanticChecker.IsStringType(value.type));
  6000. Designate(value, op);
  6001. Emit(Push(modifier.position, op.tag));
  6002. Emit(Push(modifier.position, op.op));
  6003. ReleaseOperand(op);
  6004. CallThis(position,"ActiveCellsRuntime","AddPortStringProperty",5);
  6005. ELSIF (modifier.expression.type.resolved IS SyntaxTree.IntegerType) THEN
  6006. ASSERT(value.type.resolved IS SyntaxTree.IntegerType);
  6007. Evaluate(value, op);
  6008. Emit(Push(modifier.position, op.op));
  6009. ReleaseOperand(op);
  6010. CallThis(position,"ActiveCellsRuntime","AddPortIntegerProperty",4);
  6011. ELSE
  6012. CallThis(position,"ActiveCellsRuntime","AddPortFlagProperty",3);
  6013. END;
  6014. END AddPortProperty;
  6015. PROCEDURE AddPortProperties(variable: SyntaxTree.Variable);
  6016. VAR modifier: SyntaxTree.Modifier;
  6017. BEGIN
  6018. modifier := variable.modifiers;
  6019. WHILE modifier # NIL DO
  6020. AddPortProperty(variable,modifier, modifier.expression);
  6021. modifier := modifier.nextModifier;
  6022. END;
  6023. END AddPortProperties;
  6024. PROCEDURE Variable(name: ARRAY OF CHAR; variable: SyntaxTree.Variable);
  6025. VAR op : Operand; portType: SyntaxTree.PortType; baseType: SyntaxTree.Type;
  6026. size, reg: IntermediateCode.Operand; dim: LONGINT;
  6027. PROCEDURE PushLens(type: SyntaxTree.Type);
  6028. BEGIN
  6029. IF type IS SyntaxTree.ArrayType THEN
  6030. PushLens(type(SyntaxTree.ArrayType).arrayBase.resolved);
  6031. Evaluate(type(SyntaxTree.ArrayType).length, op);
  6032. Emit(Push(-1, op.op));
  6033. ReleaseOperand(op);
  6034. INC(dim);
  6035. ELSE
  6036. baseType := type;
  6037. END;
  6038. END PushLens;
  6039. BEGIN
  6040. (* cell *)
  6041. IF variable.type IS SyntaxTree.ArrayType THEN
  6042. type := variable.type;
  6043. dim := 0;
  6044. PushLens(type);
  6045. portType := baseType.resolved(SyntaxTree.PortType);
  6046. ELSE
  6047. portType := variable.type(SyntaxTree.PortType);
  6048. END;
  6049. PushSelfPointer();
  6050. (* port *)
  6051. Field(variable, op);
  6052. (*left := SyntaxTree.NewSymbolDesignator(-1,left,cell); left.SetType(system.anyType);
  6053. left := SyntaxTree.NewDereferenceDesignator(-1, left); left.SetType(x);
  6054. d := SyntaxTree.NewSymbolDesignator(-1, left, variable); d.SetType(variable.type);
  6055. Designate(d, op);*)
  6056. Emit(Push(-1, op.op));
  6057. ReleaseOperand(op);
  6058. (* name *)
  6059. PushConstString(name);
  6060. (* inout *)
  6061. PushConstSet(Direction(portType.direction));
  6062. (* width *)
  6063. PushConstInteger(portType.sizeInBits);
  6064. IF variable.type IS SyntaxTree.PortType THEN
  6065. CallThis(variable.position,"ActiveCellsRuntime","AddPort",6);
  6066. AddPortProperties(variable);
  6067. ELSIF variable.type IS SyntaxTree.ArrayType THEN
  6068. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6069. size := IntermediateCode.Immediate(addressType, ToMemoryUnits(system,6*addressType.sizeInBits));
  6070. Emit(Add(position,reg, sp, size));
  6071. (* dim *)
  6072. PushConstInteger(dim);
  6073. (* len array *)
  6074. Emit(Push(position, reg));
  6075. ReleaseIntermediateOperand(reg);
  6076. CallThis(position,"ActiveCellsRuntime","AddPortArray",8);
  6077. size := IntermediateCode.Immediate(addressType, ToMemoryUnits(system,dim*addressType.sizeInBits));
  6078. Emit(Add(position, sp,sp, size));
  6079. END;
  6080. END Variable;
  6081. BEGIN
  6082. IF backend.cellsAreObjects THEN
  6083. variable := x.cellScope.firstVariable;
  6084. WHILE (variable # NIL) DO
  6085. type := variable.type.resolved;
  6086. WHILE (type IS SyntaxTree.ArrayType) DO
  6087. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  6088. END;
  6089. IF (type IS SyntaxTree.PortType) THEN (* port found *)
  6090. (*!!! check port arrays !! *)
  6091. Global.GetSymbolName(variable,name);
  6092. Variable(name,variable);
  6093. END;
  6094. variable := variable.nextVariable;
  6095. END;
  6096. ELSE HALT(200)
  6097. END;
  6098. (*prevActiveCellsScope := currentActiveCellsScope;*)
  6099. (*
  6100. x.typeDeclaration.GetName(componentName);
  6101. instanceType := currentActiveCellsScope.NewType(componentName); (*backend.cification.NewType(componentName);*)
  6102. IF HasValue(x.modifiers,Global.StringDataMemorySize,dataMemorySize) THEN
  6103. instanceType.SetDataMemorySize(dataMemorySize);
  6104. END;
  6105. IF HasValue(x.modifiers,Global.StringCodeMemorySize,codeMemorySize) THEN
  6106. instanceType.SetInstructionMemorySize(codeMemorySize)
  6107. END;
  6108. IF HasFlag(x.modifiers, Global.StringVector) THEN
  6109. instanceType.AddCapability(ActiveCells.VectorCapability)
  6110. END;
  6111. IF HasFlag(x.modifiers, Global.StringTRMS) THEN
  6112. instanceType.AddCapability(ActiveCells.TRMSCapability)
  6113. END;
  6114. IF HasFlag(x.modifiers, Global.StringFloatingPoint) THEN
  6115. instanceType.AddCapability(ActiveCells.FloatingPointCapability)
  6116. END;
  6117. AddDevices(instanceType, x);
  6118. *)
  6119. (*
  6120. IF x.isCellNet THEN
  6121. IF HasValue(x.modifiers,Global.StringFrequencyDivider,value) THEN backend.activeCellsSpecification.SetFrequencyDivider(value) END;
  6122. END;
  6123. *)
  6124. (*currentActiveCellsScope := instanceType;*)
  6125. (*
  6126. parameter := x.firstParameter;
  6127. portIndex := 0;
  6128. WHILE parameter # NIL DO
  6129. parameter.GetName(parameterName);
  6130. parameterType := parameter.type.resolved;
  6131. Parameter(parameterName, parameterType);
  6132. (*
  6133. IF SemanticChecker.IsStaticArray(parameterType,parameterType,len) THEN
  6134. ParameterArray(parameterType);
  6135. direction := Direction(parameterType(SyntaxTree.PortType).direction);
  6136. FOR i := 0 TO len-1 DO
  6137. COPY(parameterName,name);
  6138. AppendIndex(name,i);
  6139. port := instanceType.NewPort(name,direction,backend.activeCellsSpecification.GetPortAddress(portIndex));
  6140. port.SetWidth(parameterType(SyntaxTree.PortType).sizeInBits);
  6141. INC(portIndex);
  6142. END;
  6143. ELSE
  6144. direction := Direction(parameterType(SyntaxTree.PortType).direction);
  6145. port := instanceType.NewPort(parameterName,direction,backend.activeCellsSpecification.GetPortAddress(portIndex));
  6146. port.SetWidth(parameterType(SyntaxTree.PortType).sizeInBits);
  6147. INC(portIndex);
  6148. END;
  6149. *)
  6150. parameter := parameter.nextParameter;
  6151. END;
  6152. *)
  6153. (*
  6154. Scope(x.cellScope);
  6155. currentActiveCellsScope := prevActiveCellsScope;
  6156. AddModules(instanceType,x.cellScope);
  6157. *)
  6158. END AddPorts;
  6159. PROCEDURE AddProperty(cellType: SyntaxTree.CellType; cell: SyntaxTree.Symbol; property: SyntaxTree.Property; value: SyntaxTree.Expression);
  6160. VAR name: ARRAY 256 OF CHAR; op: Operand; left, d: SyntaxTree.Designator;
  6161. BEGIN
  6162. Symbol(cell,op);
  6163. ToMemory(op.op,addressType,0);
  6164. Emit(Push(position,op.op));
  6165. ReleaseOperand(op);
  6166. Basic.GetString(property.name, name);
  6167. PushConstString(name);
  6168. IF SemanticChecker.IsStringType(property.type) OR (property.type.resolved IS SyntaxTree.IntegerType) THEN
  6169. left := SyntaxTree.NewSymbolDesignator(-1,left,cell); left.SetType(system.anyType);
  6170. left := SyntaxTree.NewDereferenceDesignator(-1, left); left.SetType(cellType);
  6171. d := SyntaxTree.NewSymbolDesignator(-1, left, property); d.SetType(property.type);
  6172. Designate(d, op);
  6173. IF SemanticChecker.IsStringType(property.type) THEN
  6174. Emit(Push(-1, op.tag))
  6175. END;
  6176. Emit(Push(-1, op.op));
  6177. ReleaseOperand(op);
  6178. END;
  6179. IF SemanticChecker.IsStringType(property.type) THEN
  6180. ASSERT(SemanticChecker.IsStringType(value.type));
  6181. Designate(value, op);
  6182. Emit(Push(property.position, op.tag));
  6183. Emit(Push(property.position, op.op));
  6184. ReleaseOperand(op);
  6185. CallThis(position,"ActiveCellsRuntime","AddStringProperty",7);
  6186. ELSIF (property.type.resolved IS SyntaxTree.IntegerType) THEN
  6187. ASSERT(value.type.resolved IS SyntaxTree.IntegerType);
  6188. Evaluate(value, op);
  6189. Emit(Push(property.position, op.op));
  6190. ReleaseOperand(op);
  6191. CallThis(position,"ActiveCellsRuntime","AddIntegerProperty",5);
  6192. ELSE
  6193. CallThis(position,"ActiveCellsRuntime","AddFlagProperty",3);
  6194. END;
  6195. END AddProperty;
  6196. PROCEDURE AddModifiers(cellType: SyntaxTree.CellType; cell: SyntaxTree.Symbol; modifier: SyntaxTree.Modifier);
  6197. VAR symbol: SyntaxTree.Symbol;
  6198. BEGIN
  6199. WHILE modifier # NIL DO
  6200. symbol := cellType.cellScope.FindSymbol(modifier.identifier);
  6201. IF (symbol # NIL) & (symbol IS SyntaxTree.Property) THEN
  6202. AddProperty(cellType, cell, symbol(SyntaxTree.Property), modifier.expression);
  6203. ELSE
  6204. (*! move this check to checker *)
  6205. Error(modifier.position, "undefined property");
  6206. END;
  6207. modifier := modifier.nextModifier;
  6208. END;
  6209. END AddModifiers;
  6210. PROCEDURE AppendModifier(VAR to: SyntaxTree.Modifier; this: SyntaxTree.Modifier);
  6211. VAR last: SyntaxTree.Modifier;
  6212. BEGIN
  6213. IF to = NIL THEN
  6214. to := SyntaxTree.NewModifier(this.position, this.identifier, this.expression);
  6215. ELSE
  6216. last := to;
  6217. WHILE (last.nextModifier # NIL) & (this.identifier # last.identifier) DO
  6218. last := last.nextModifier;
  6219. END;
  6220. IF last.identifier # this.identifier THEN
  6221. ASSERT(last.nextModifier = NIL);
  6222. last.SetNext(SyntaxTree.NewModifier(this.position, this.identifier, this.expression));
  6223. END;
  6224. END;
  6225. END AppendModifier;
  6226. PROCEDURE AppendModifiers(VAR to: SyntaxTree.Modifier; this: SyntaxTree.Modifier);
  6227. BEGIN
  6228. WHILE this # NIL DO
  6229. AppendModifier(to, this);
  6230. this := this.nextModifier;
  6231. END;
  6232. END AppendModifiers;
  6233. PROCEDURE PushPort(p: SyntaxTree.Expression);
  6234. VAR op: Operand;
  6235. BEGIN
  6236. Evaluate(p, op);
  6237. Emit(Push(p.position, op.op));
  6238. ReleaseOperand(op);
  6239. (*
  6240. WHILE (p # NIL) & ~(p.type.resolved IS SyntaxTree.CellType) DO
  6241. p := p(SyntaxTree.Designator).left;
  6242. END;
  6243. IF p # NIL THEN
  6244. Evaluate(p, op);
  6245. Emit(Push(p.position, op.op));
  6246. ReleaseOperand(op);
  6247. ELSE
  6248. Emit(Push(-1, nil));
  6249. END;
  6250. *)
  6251. END PushPort;
  6252. PROCEDURE VisitBuiltinCallDesignator(x: SyntaxTree.BuiltinCallDesignator);
  6253. VAR
  6254. p0,p1,p2,parameter: SyntaxTree.Expression; len,val: LONGINT; l,r: Operand; res,adr,reg: IntermediateCode.Operand; type, componentType: SyntaxTree.Type;
  6255. constructor: SyntaxTree.Procedure; s0,s1,s2: Operand; hint: HUGEINT;
  6256. i: LONGINT; formalParameter: SyntaxTree.Parameter;
  6257. tmp:IntermediateCode.Operand;
  6258. size: LONGINT; dim,openDim: LONGINT; pointer: IntermediateCode.Operand; t,t0,t1,t2: SyntaxTree.Type; trueL,falseL,ignore: Label;
  6259. exit,else,end: Label; procedureType: SyntaxTree.ProcedureType;
  6260. name: Basic.SegmentedName; symbol: Sections.Section; operand: Operand;
  6261. dest: IntermediateCode.Operand;
  6262. staticLength: LONGINT; itype: IntermediateCode.Type;
  6263. convert,isTensor: BOOLEAN;
  6264. recordType: SyntaxTree.RecordType;
  6265. baseType: SyntaxTree.Type;
  6266. flags: SET;
  6267. left: SyntaxTree.Expression;
  6268. call: SyntaxTree.Designator;
  6269. procedure: SyntaxTree.Procedure;
  6270. temporaryVariable: SyntaxTree.Variable;
  6271. dummy: IntermediateCode.Operand;
  6272. customBuiltin: SyntaxTree.CustomBuiltin;
  6273. isVarPar: ARRAY 3 OF BOOLEAN;
  6274. callsection: Sections.Section;
  6275. segmentedName: Basic.SegmentedName;
  6276. needsTrace: BOOLEAN;
  6277. n: ARRAY 256 OF CHAR;
  6278. modifier: SyntaxTree.Modifier;
  6279. previous, init: IntermediateCode.Section;
  6280. prevScope: SyntaxTree.Scope;
  6281. PROCEDURE CallBodies(self: IntermediateCode.Operand; type: SyntaxTree.Type);
  6282. VAR recordScope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure; body: SyntaxTree.Body; flags: LONGINT;
  6283. priority: IntermediateCode.Operand;
  6284. op,callop: Operand;
  6285. BEGIN
  6286. IF type = NIL THEN RETURN END;
  6287. type := type.resolved;
  6288. IF type IS SyntaxTree.PointerType THEN
  6289. type := type(SyntaxTree.PointerType).pointerBase.resolved
  6290. END;
  6291. IF type IS SyntaxTree.MathArrayType THEN RETURN END;
  6292. CallBodies(self,type(SyntaxTree.RecordType).baseType);
  6293. recordScope := type(SyntaxTree.RecordType).recordScope;
  6294. IF recordScope.bodyProcedure # NIL THEN
  6295. procedure := recordScope.bodyProcedure;
  6296. body := procedure.procedureScope.body;
  6297. Emit(Push(position,self));
  6298. IF body.isActive THEN
  6299. StaticCallOperand(callop,procedure);
  6300. Emit(Push(position,callop.op));
  6301. IF body.priority # NIL THEN Evaluate(body.priority,op); priority := op.op;
  6302. Convert(priority,sizeType);
  6303. ELSE priority := IntermediateCode.Immediate(sizeType,0)
  6304. END;
  6305. Emit(Push(position,priority));
  6306. ReleaseIntermediateOperand(priority);
  6307. IF backend.cooperative THEN
  6308. Emit(Push(position,self));
  6309. CallThis(position,"Activities","Create",3)
  6310. ELSE
  6311. flags := 0;
  6312. IF body.isSafe THEN
  6313. flags := 1;
  6314. END;
  6315. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.setType),flags)));
  6316. Emit(Push(position,self));
  6317. CallThis(position,"Objects","CreateProcess",4)
  6318. END;
  6319. ELSE
  6320. Emit(Push(position,self));
  6321. StaticCallOperand(callop,procedure);
  6322. Emit(Call(position,callop.op,ProcedureParametersSize(system,procedure)));
  6323. END;
  6324. Emit(Pop(position,self));
  6325. END;
  6326. END CallBodies;
  6327. PROCEDURE PushString(op: Operand; actualType: SyntaxTree.Type);
  6328. BEGIN
  6329. actualType := actualType.resolved;
  6330. IF actualType IS SyntaxTree.StringType THEN
  6331. Emit(Push(position,IntermediateCode.Immediate(addressType,actualType(SyntaxTree.StringType).length)));
  6332. ELSIF actualType(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  6333. Emit(Push(position,IntermediateCode.Immediate(addressType,actualType(SyntaxTree.ArrayType).staticLength)));
  6334. ELSE
  6335. tmp := op.tag;
  6336. IntermediateCode.MakeMemory(tmp,addressType);
  6337. Emit(Push(position,tmp));
  6338. END;
  6339. Emit(Push(position,op.op))
  6340. END PushString;
  6341. PROCEDURE PushTD(type: SyntaxTree.Type);
  6342. VAR op: IntermediateCode.Operand;
  6343. BEGIN
  6344. IF type = NIL THEN Emit(Push(position,IntermediateCode.Immediate(addressType,0)))
  6345. ELSIF type.resolved IS SyntaxTree.AnyType THEN Emit(Push(position,IntermediateCode.Immediate(addressType,1)))
  6346. ELSE
  6347. IF type.resolved IS SyntaxTree.PointerType THEN
  6348. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  6349. END;
  6350. op := TypeDescriptorAdr(type.resolved);
  6351. IF ~newObjectFile THEN
  6352. IntermediateCode.MakeMemory(op,addressType);
  6353. END;
  6354. Emit(Push(position,op));
  6355. END
  6356. END PushTD;
  6357. BEGIN
  6358. IF Trace THEN TraceEnter("VisitBuiltinCallDesignator") END;
  6359. dest := destination; destination := emptyOperand;
  6360. p0 := NIL; p1 := NIL; p2 := NIL; len := x.parameters.Length();
  6361. IF len > 0 THEN p0 := x.parameters.GetExpression(0); t0 := p0.type.resolved END;
  6362. IF len > 1 THEN p1 := x.parameters.GetExpression(1); t1 := p1.type.resolved END;
  6363. IF len > 2 THEN p2 := x.parameters.GetExpression(2); t2 := p2.type.resolved END;
  6364. CASE x.id OF
  6365. (* ---- COPY ----- *)
  6366. |Global.Copy:
  6367. CopyString(p1,p0); (* roles exchanged: COPY ( src => dest ) *)
  6368. (* ---- EXCL, INCL----- *)
  6369. |Global.Excl,Global.Incl:
  6370. Evaluate(p0,s0);
  6371. Evaluate(p1,s1);
  6372. Convert(s1.op,setType);
  6373. IF (s1.op.mode # IntermediateCode.ModeImmediate) & ~isUnchecked THEN
  6374. TrapC(BrltL,s1.op,IntermediateCode.Immediate(setType,setType.sizeInBits),IndexCheckTrap);
  6375. END;
  6376. ReuseCopy(res,s0.op);
  6377. ReleaseOperand(s0);
  6378. Reuse1(tmp,s1.op);
  6379. ReleaseOperand(s1);
  6380. Emit(Shl(position,tmp,IntermediateCode.Immediate(setType,1),s1.op));
  6381. IF x.id = Global.Excl THEN
  6382. Emit(Not(position,tmp,tmp));
  6383. Emit(And(position,res,res,tmp));
  6384. ELSE
  6385. Emit(Or(position,res,res,tmp));
  6386. END;
  6387. ReleaseIntermediateOperand(tmp);
  6388. Designate(p0,s0);
  6389. ToMemory(s0.op,setType,0);
  6390. Emit(Mov(position,s0.op,res));
  6391. ReleaseOperand(s0); ReleaseIntermediateOperand(res);
  6392. (* ---- DISPOSE ----- *)
  6393. |Global.Dispose:
  6394. Designate(p0,s0);
  6395. Emit(Push(position,s0.op));
  6396. ReleaseOperand(s0);
  6397. CallThis(position,DefaultRuntimeModuleName,"Dispose", 1);
  6398. (* ---- GETPROCEDURE ----- *)
  6399. |Global.GetProcedure:
  6400. Designate(p0,s0);
  6401. PushString(s0,p0.type);
  6402. Designate(p1,s1);
  6403. PushString(s1,p1.type);
  6404. procedureType := p2.type.resolved(SyntaxTree.ProcedureType);
  6405. IF (procedureType.firstParameter = NIL) OR (procedureType.firstParameter.access = SyntaxTree.Hidden) THEN PushTD(NIL)
  6406. ELSE PushTD(procedureType.firstParameter.type)
  6407. END;
  6408. PushTD(procedureType.returnType);
  6409. Designate(p2,s2);
  6410. Emit(Push(position,s2.op));
  6411. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  6412. CallThis(position,"Modules","GetProcedure", 7);
  6413. (* ---- ASH, LSH, ROT ----- *)
  6414. |Global.Ash, Global.Asr, Global.Lsh, Global.Rot, Global.Ror:
  6415. Evaluate(p0,s0);
  6416. IF (x.id = Global.Lsh) OR (x.id = Global.Rot) OR (x.id = Global.Ror)THEN
  6417. (* make unsigned arguments in order to produced a logical shift *)
  6418. IF s0.op.type.form = IntermediateCode.SignedInteger THEN
  6419. convert:= TRUE;
  6420. itype := s0.op.type;
  6421. IntermediateCode.InitType(itype,IntermediateCode.UnsignedInteger,s0.op.type.sizeInBits);
  6422. Convert(s0.op,itype);
  6423. ELSE
  6424. convert := FALSE;
  6425. END;
  6426. END;
  6427. Evaluate(p1,s1);
  6428. IF IsIntegerConstant(p1,hint) THEN
  6429. ReuseCopy(reg,s0.op);
  6430. IF hint > 0 THEN
  6431. IntermediateCode.InitImmediate(s1.op,s1.op.type,hint);
  6432. IF x.id = Global.Ash THEN Emit(Shl(position,reg,s0.op,s1.op))
  6433. ELSIF x.id = Global.Lsh THEN Emit(Shl(position,reg,s0.op,s1.op))
  6434. ELSIF x.id = Global.Rot THEN Emit(Rol(position,reg,s0.op,s1.op))
  6435. ELSIF x.id = Global.Ror THEN Emit(Ror(position,reg,s0.op,s1.op))
  6436. ELSIF x.id = Global.Asr THEN Emit(Shr(position,reg,s0.op,s1.op))
  6437. END;
  6438. ELSIF hint < 0 THEN
  6439. IntermediateCode.InitImmediate(s1.op,s1.op.type,-hint);
  6440. IF x.id = Global.Ash THEN Emit(Shr(position,reg,s0.op,s1.op));
  6441. ELSIF x.id = Global.Lsh THEN Emit(Shr(position,reg,s0.op,s1.op));
  6442. ELSIF x.id = Global.Rot THEN Emit(Ror(position,reg,s0.op,s1.op));
  6443. ELSIF x.id = Global.Ror THEN Emit(Rol(position,reg,s0.op,s1.op))
  6444. ELSIF x.id = Global.Asr THEN Emit(Shl(position,reg,s0.op,s1.op))
  6445. END;
  6446. END;
  6447. ReleaseOperand(s0); ReleaseOperand(s1);
  6448. ELSE
  6449. exit := NewLabel();
  6450. end := NewLabel();
  6451. ReuseCopy(reg,s0.op);
  6452. BrgeL(exit,s1.op,IntermediateCode.Immediate(IntermediateCode.GetType(system,p1.type),0));
  6453. Reuse1(tmp,s1.op);
  6454. Emit(Neg(position,tmp,s1.op));
  6455. Convert(tmp,s1.op.type);
  6456. IF x.id = Global.Ash THEN Emit(Shr(position,reg,reg,tmp))
  6457. ELSIF x.id = Global.Lsh THEN Emit(Shr(position,reg,reg,tmp))
  6458. ELSIF x.id = Global.Rot THEN Emit(Ror(position,reg,reg,tmp))
  6459. ELSIF x.id = Global.Ror THEN Emit(Rol(position,reg,reg,tmp))
  6460. ELSIF x.id = Global.Asr THEN Emit(Shl(position,reg,reg,tmp))
  6461. END;
  6462. ReleaseIntermediateOperand(tmp);
  6463. BrL(end);
  6464. SetLabel(exit);
  6465. ReuseCopy(tmp,s1.op);
  6466. Convert(tmp,s1.op.type);
  6467. IF x.id = Global.Ash THEN Emit(Shl(position,reg,reg,tmp))
  6468. ELSIF x.id = Global.Lsh THEN Emit(Shl(position,reg,reg,tmp))
  6469. ELSIF x.id = Global.Rot THEN Emit(Rol(position,reg,reg,tmp))
  6470. ELSIF x.id = Global.Ror THEN Emit(Ror(position,reg,reg,tmp))
  6471. ELSIF x.id = Global.Asr THEN Emit(Shr(position,reg,reg,tmp))
  6472. END;
  6473. ReleaseIntermediateOperand(tmp);
  6474. SetLabel(end);
  6475. ReleaseOperand(s0); ReleaseOperand(s1);
  6476. END;
  6477. InitOperand(result,ModeValue);
  6478. IF convert THEN
  6479. itype := reg.type;
  6480. IntermediateCode.InitType(itype,IntermediateCode.SignedInteger,reg.type.sizeInBits);
  6481. Convert(reg,itype);
  6482. END;
  6483. result.op := reg;
  6484. (* ---- CAP ----- *)
  6485. |Global.Cap:
  6486. Evaluate(p0,result);
  6487. ReuseCopy(reg,result.op);
  6488. ReleaseIntermediateOperand(result.op);
  6489. ignore := NewLabel();
  6490. BrltL(ignore, reg,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.characterType),ORD("a")));
  6491. BrltL(ignore,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.characterType),ORD("z")),reg);
  6492. Emit(And(position,reg,reg,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.characterType),5FH)));
  6493. SetLabel(ignore);
  6494. result.op := reg;
  6495. (* ---- CHR ----- *)
  6496. |Global.Chr, Global.Chr32:
  6497. Evaluate(p0,result);
  6498. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6499. |Global.Entier, Global.EntierH:
  6500. Evaluate(p0,result);
  6501. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6502. (* ---- MIN and MAX ----- *)
  6503. |Global.Max,Global.Min:
  6504. Evaluate(p0,s0);
  6505. Evaluate(p1,s1);
  6506. Reuse2(res,s0.op,s1.op);
  6507. else := NewLabel();
  6508. IF x.id = Global.Max THEN BrltL(else,s0.op,s1.op);
  6509. ELSE BrltL(else,s1.op,s0.op) END;
  6510. Emit(Mov(position,res,s0.op));
  6511. ReleaseOperand(s0);
  6512. end := NewLabel();
  6513. BrL(end);
  6514. SetLabel(else);
  6515. Emit(MovReplace(position,res,s1.op));
  6516. SetLabel(end);
  6517. ReleaseOperand(s1);
  6518. InitOperand(result,ModeValue);
  6519. result.op := res;
  6520. (* ---- ODD ----- *)
  6521. |Global.Odd:
  6522. IF ~conditional THEN
  6523. ConditionToValue(x)
  6524. ELSE
  6525. Evaluate(p0,result);
  6526. res := IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1);
  6527. Reuse1(res,result.op);
  6528. Emit(And(position,res,result.op,IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1)));
  6529. ReleaseIntermediateOperand(result.op);
  6530. result.op := res;
  6531. BreqL(trueLabel,IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1),result.op);
  6532. ReleaseOperand(result);
  6533. BrL(falseLabel);
  6534. END;
  6535. (* ---- ORD ----- *)
  6536. |Global.Ord, Global.Ord32:
  6537. Evaluate(p0,result);
  6538. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6539. (* ---- SHORT, LONG ----- *)
  6540. |Global.Short, Global.Long:
  6541. Evaluate(p0,result);
  6542. IF x.type IS SyntaxTree.ComplexType THEN
  6543. componentType := x.type(SyntaxTree.ComplexType).componentType;
  6544. Convert(result.op, IntermediateCode.GetType(system, componentType));
  6545. Convert(result.tag, IntermediateCode.GetType(system, componentType));
  6546. ELSE
  6547. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6548. END
  6549. (* ---- HALT, SYSTEM.HALT----- *)
  6550. |Global.Halt, Global.systemHalt:
  6551. val := p0.resolved(SyntaxTree.IntegerValue).value;
  6552. EmitTrap (position, val);
  6553. (* ---- ASSERT ----- *)
  6554. |Global.Assert:
  6555. IF ~backend.noAsserts & (p0.resolved = NIL) THEN
  6556. trueL := NewLabel();
  6557. falseL := NewLabel();
  6558. Condition(p0,trueL,falseL);
  6559. IF p1 = NIL THEN val := AssertTrap
  6560. ELSE val := p1.resolved(SyntaxTree.IntegerValue).value;
  6561. END;
  6562. SetLabel(falseL);
  6563. EmitTrap(position,val);
  6564. SetLabel(trueL);
  6565. END;
  6566. (*
  6567. Emit(TrapC(result.op,val);
  6568. *)
  6569. (* ---- INC, DEC----- *)
  6570. |Global.Inc,Global.Dec:
  6571. Expression(p0); adr := result.op;
  6572. LoadValue(result,p0.type); l := result;
  6573. IF p1 = NIL THEN r.op := IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1);
  6574. ELSE Expression(p1); LoadValue(result,p1.type); r := result;
  6575. END;
  6576. IF x.id = Global.Inc THEN
  6577. Emit(Add(position,l.op,l.op,r.op));
  6578. ELSE
  6579. Emit(Sub(position,l.op,l.op,r.op));
  6580. END;
  6581. ReleaseOperand(l); ReleaseOperand(r);
  6582. (* ---- LEN ----- *)
  6583. |Global.Len: (* dynamic length, static length done by checker *)
  6584. Designate(p0,operand);
  6585. IF p1 = NIL THEN
  6586. InitOperand(l,ModeValue);
  6587. l.op := IntermediateCode.Immediate(int32,0);
  6588. ELSE
  6589. Evaluate(p1,l);
  6590. END;
  6591. IF p0.type.resolved IS SyntaxTree.ArrayType THEN
  6592. IF (p0.type.resolved(SyntaxTree.ArrayType).form= SyntaxTree.SemiDynamic) THEN
  6593. Dereference(operand, p0.type.resolved, FALSE);
  6594. END;
  6595. ArrayLen(p0.type.resolved(SyntaxTree.ArrayType),operand,l.op, result);
  6596. ReleaseOperand(operand); ReleaseOperand(l);
  6597. ELSIF p0.type.resolved IS SyntaxTree.MathArrayType THEN
  6598. ASSERT(p1 # NIL);
  6599. IF p0.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  6600. Dereference(operand,p0.type.resolved,FALSE);
  6601. END;
  6602. GetMathArrayLength(p0.type.resolved(SyntaxTree.MathArrayType),operand, l.op, TRUE, result);
  6603. ReleaseOperand(operand); ReleaseOperand(l);
  6604. ELSE HALT(100);
  6605. END;
  6606. Convert(result.op,IntermediateCode.GetType(system, x.type));
  6607. (* ---- FIRST ---- *)
  6608. |Global.First:
  6609. IF p0 IS SyntaxTree.RangeExpression THEN (* optimization, could also work with designate *)
  6610. Evaluate(p0(SyntaxTree.RangeExpression).first, result)
  6611. ELSE
  6612. Designate(p0, result)
  6613. END
  6614. (* ---- LAST ---- *)
  6615. |Global.Last:
  6616. IF p0 IS SyntaxTree.RangeExpression THEN (* optimization, could also work with designate *)
  6617. Evaluate(p0(SyntaxTree.RangeExpression).last, result)
  6618. ELSE
  6619. Designate(p0, result);
  6620. (* make sure result.op is a register *)
  6621. tmp := result.op;
  6622. ReuseCopy(result.op, result.op);
  6623. ReleaseIntermediateOperand(tmp);
  6624. (* add offset to result.op *)
  6625. IntermediateCode.AddOffset(result.op, ToMemoryUnits(system, system.SizeOf(system.longintType)))
  6626. END
  6627. (* ---- STEP ---- *)
  6628. |Global.Step:
  6629. IF p0 IS SyntaxTree.RangeExpression THEN (* optimization, could also work with designate *)
  6630. Evaluate(p0(SyntaxTree.RangeExpression).step, result)
  6631. ELSE
  6632. Designate(p0, result);
  6633. (* make sure result.op is a register *)
  6634. tmp := result.op;
  6635. ReuseCopy(result.op, result.op);
  6636. ReleaseIntermediateOperand(tmp);
  6637. (* add offset to result.op *)
  6638. IntermediateCode.AddOffset(result.op, 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)))
  6639. END
  6640. (* ---- RE ---- *)
  6641. |Global.Re:
  6642. IF p0.type.resolved IS SyntaxTree.ComplexType THEN
  6643. Designate(p0, result)
  6644. ELSE
  6645. Evaluate(p0, result)
  6646. END
  6647. (* ---- IM ---- *)
  6648. |Global.Im:
  6649. ASSERT(p0.type.resolved IS SyntaxTree.ComplexType);
  6650. componentType := p0.type.resolved(SyntaxTree.ComplexType).componentType;
  6651. Designate(p0, result);
  6652. (* make sure result.op is a register *)
  6653. tmp := result.op;
  6654. ReuseCopy(result.op, result.op);
  6655. ReleaseIntermediateOperand(tmp);
  6656. (* add offset to result.op *)
  6657. IntermediateCode.AddOffset(result.op, ToMemoryUnits(system, system.SizeOf(componentType)));
  6658. (* ---- ABS ----- *)
  6659. |Global.Abs:
  6660. Evaluate(p0,operand);
  6661. type := p0.type.resolved;
  6662. InitOperand(result,ModeValue);
  6663. Reuse1a(result.op,operand.op,dest);
  6664. Emit(Abs(position,result.op,operand.op));
  6665. ReleaseOperand(operand);
  6666. (* ---- WAIT ----- *)
  6667. |Global.Wait:
  6668. Evaluate(p0,operand);
  6669. Emit(Push(position,operand.op));
  6670. ReleaseOperand(operand);
  6671. CallThis(position,"Activities","Wait", 1);
  6672. (* ---- NEW ----- *)
  6673. |Global.New:
  6674. (*! the following code is only correct for "standard" Oberon calling convention *)
  6675. type := p0.type.resolved;
  6676. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  6677. THEN
  6678. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  6679. IF backend.cooperative THEN
  6680. size := ToMemoryUnits(system,system.SizeOf(recordType));
  6681. IF ~type(SyntaxTree.PointerType).isPlain THEN
  6682. IF recordType.isObject THEN
  6683. INC (size, BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  6684. IF recordType.IsActive() THEN
  6685. INC (size, ActionTypeSize * ToMemoryUnits(system,addressType.sizeInBits))
  6686. END;
  6687. IF recordType.IsProtected() THEN
  6688. INC (size, MonitorTypeSize * ToMemoryUnits(system,addressType.sizeInBits))
  6689. END;
  6690. ELSE
  6691. INC (size, BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  6692. END;
  6693. END;
  6694. Emit(Push(position,IntermediateCode.Immediate(sizeType,size)));
  6695. CallThis(position,"Runtime","New", 1);
  6696. pointer := NewRegisterOperand(IntermediateCode.GetType(system, p0.type));
  6697. Emit(Result(position, pointer));
  6698. exit := NewLabel();
  6699. BreqL(exit,pointer,nil);
  6700. IF ~type(SyntaxTree.PointerType).isPlain THEN
  6701. GetRecordTypeName (recordType,name);
  6702. IF ~recordType.isObject THEN
  6703. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  6704. END;
  6705. IntermediateCode.InitAddress(adr, addressType, name , 0, 0);
  6706. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,0),adr));
  6707. IF recordType.isObject THEN
  6708. IF recordType.IsProtected() THEN
  6709. DEC (size, MonitorTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  6710. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,MonitorOffset * ToMemoryUnits(system,addressType.sizeInBits)), IntermediateCode.RegisterOffset(addressType,IntermediateCode.GeneralPurposeRegister,pointer.register,size)));
  6711. END;
  6712. IF recordType.IsActive() THEN
  6713. DEC (size, ActionTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  6714. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ActionOffset * ToMemoryUnits(system,addressType.sizeInBits)), IntermediateCode.RegisterOffset(addressType,IntermediateCode.GeneralPurposeRegister,pointer.register,size)));
  6715. END;
  6716. END;
  6717. END;
  6718. (* initialize fields *)
  6719. IF type(SyntaxTree.PointerType).isPlain THEN
  6720. size := 0;
  6721. ELSIF recordType.isObject THEN
  6722. size := BaseObjectTypeSize;
  6723. ELSE
  6724. size := BaseRecordTypeSize;
  6725. END;
  6726. InitFields(recordType, pointer,size*ToMemoryUnits(system,addressType.sizeInBits));
  6727. (* call initializer *)
  6728. constructor := GetConstructor(recordType);
  6729. IF constructor # NIL THEN
  6730. (*! should be unified with ProcedureCallDesignator *)
  6731. Emit(Push(position,pointer));
  6732. ReleaseIntermediateOperand(pointer);
  6733. formalParameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  6734. FOR i := 1 TO x.parameters.Length()-1 DO
  6735. PushParameter(x.parameters.GetExpression(i), formalParameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  6736. formalParameter := formalParameter.nextParameter;
  6737. END;
  6738. (* static call of the constructor *)
  6739. GetCodeSectionNameForSymbol(constructor,name);
  6740. ASSERT(~constructor.isInline);
  6741. IF constructor.scope.ownerModule # module.module THEN
  6742. symbol := NewSection(module.importedSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  6743. ELSE
  6744. symbol := NewSection(module.allSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  6745. END;
  6746. Emit(Call(position,IntermediateCode.Address(addressType, symbol.name, GetFingerprint(constructor), 0),ProcedureParametersSize(system,constructor) - ToMemoryUnits(system,addressType.sizeInBits)));
  6747. IntermediateCode.InitRegister(pointer,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6748. Emit(Pop(position,pointer));
  6749. END;
  6750. (* call bodies *)
  6751. CallBodies(pointer,p0.type);
  6752. SetLabel(exit);
  6753. needsTrace := p0.NeedsTrace();
  6754. IF needsTrace THEN ModifyAssignments(true) END;
  6755. IF ~type(SyntaxTree.PointerType).isDisposable THEN
  6756. Emit(Push(position, pointer));
  6757. CallThis(position,"GarbageCollector","Watch",1);
  6758. Emit(Pop(position, pointer));
  6759. END;
  6760. Designate(p0,l);
  6761. IF needsTrace THEN
  6762. CallAssignPointer(l.op, pointer);
  6763. ELSE
  6764. ToMemory(l.op,addressType,0);
  6765. Emit(Mov(position,l.op,pointer));
  6766. END;
  6767. ReleaseIntermediateOperand(pointer);
  6768. ReleaseOperand(l);
  6769. IF needsTrace THEN ModifyAssignments(false) END;
  6770. ELSE
  6771. temporaryVariable := GetTemporaryVariable(type, FALSE);
  6772. IF temporaryVariable # NIL THEN
  6773. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  6774. ELSE
  6775. Designate(p0,l);
  6776. END;
  6777. (* l.op contains address of pointer to record *)
  6778. Emit(Push(position,l.op)); (* address for use after syscall *)
  6779. Emit(Push(position,l.op));
  6780. ReleaseOperand(l);
  6781. (* push type descriptor *)
  6782. reg := TypeDescriptorAdr(recordType);
  6783. IF ~newObjectFile THEN
  6784. IntermediateCode.MakeMemory(reg,addressType);
  6785. END;
  6786. Emit(Push(position,reg));
  6787. ReleaseIntermediateOperand(reg);
  6788. (* push realtime flag *)
  6789. IF (p0.type.resolved.isRealtime) THEN Emit(Push(position,true));
  6790. ELSE Emit(Push(position,false));
  6791. END;
  6792. CallThis(position,"Heaps","NewRec", 3);
  6793. (* check allocation success, if not successful then do not call initializers and bodies *)
  6794. IntermediateCode.InitRegister(pointer,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6795. Emit(Pop(position,pointer));
  6796. MakeMemory(reg,pointer,addressType,0);
  6797. ReleaseIntermediateOperand(pointer);
  6798. pointer := reg;
  6799. exit := NewLabel();
  6800. BreqL(exit,pointer,nil);
  6801. Emit(Push(position,pointer));
  6802. (* initialize fields *)
  6803. InitFields(recordType, pointer,0);
  6804. (* call initializer *)
  6805. constructor := GetConstructor(recordType);
  6806. IF constructor # NIL THEN
  6807. (*! should be unified with ProcedureCallDesignator *)
  6808. Emit(Push(position,pointer));
  6809. ReleaseIntermediateOperand(pointer);
  6810. formalParameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  6811. FOR i := 1 TO x.parameters.Length()-1 DO
  6812. PushParameter(x.parameters.GetExpression(i), formalParameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  6813. formalParameter := formalParameter.nextParameter;
  6814. END;
  6815. (* static call of the constructor *)
  6816. GetCodeSectionNameForSymbol(constructor,name);
  6817. ASSERT(~constructor.isInline);
  6818. IF constructor.scope.ownerModule # module.module THEN
  6819. symbol := NewSection(module.importedSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  6820. ELSE
  6821. symbol := NewSection(module.allSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  6822. END;
  6823. Emit(Call(position,IntermediateCode.Address(addressType, symbol.name, GetFingerprint(constructor), 0),ProcedureParametersSize(system,constructor)));
  6824. ELSE
  6825. ReleaseIntermediateOperand(pointer);
  6826. END;
  6827. IntermediateCode.InitRegister(pointer,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6828. Emit(Pop(position,pointer));
  6829. IF temporaryVariable # NIL THEN
  6830. Designate(p0,l);
  6831. ToMemory(l.op,addressType,0);
  6832. Emit(Mov(position,l.op,pointer));
  6833. ReleaseOperand(l);
  6834. result.tag := emptyOperand;
  6835. END;
  6836. (* call bodies *)
  6837. CallBodies(pointer,p0.type);
  6838. ReleaseIntermediateOperand(pointer);
  6839. IF temporaryVariable # NIL THEN
  6840. end := NewLabel();
  6841. BrL(end);
  6842. SetLabel(exit);
  6843. Designate(p0,l);
  6844. ToMemory(l.op,addressType,0);
  6845. Emit(Mov(position,l.op,nil)); (* write NIL to adr *)
  6846. ReleaseOperand(l);
  6847. SetLabel(end);
  6848. ELSE
  6849. SetLabel(exit);
  6850. END;
  6851. END;
  6852. ELSIF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN
  6853. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  6854. dim := 0;
  6855. IF p1 # NIL THEN
  6856. FOR i := 1 TO x.parameters.Length()-1 DO
  6857. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  6858. parameter := x.parameters.GetExpression(i);
  6859. Evaluate(parameter,r);
  6860. IF (r.op.mode # IntermediateCode.ModeImmediate) & ~isUnchecked THEN
  6861. IntermediateCode.InitImmediate(tmp,IntermediateCode.GetType(system,parameter.type),0);
  6862. TrapC(BrgeL,r.op,tmp,ArraySizeTrap);
  6863. END;
  6864. Emit(Push(position,r.op));
  6865. IF i=1 THEN
  6866. ReuseCopy(reg,r.op);
  6867. ELSE
  6868. Emit(Mul(position,reg,reg,r.op)); (*! optimize the multiplication of immediate operands *)
  6869. END;
  6870. ReleaseOperand(r);
  6871. INC(dim);
  6872. END;
  6873. Convert(reg,addressType);
  6874. ELSE
  6875. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6876. Emit(Mov(position,reg,IntermediateCode.Immediate(addressType,1)));
  6877. END;
  6878. openDim := dim;
  6879. ASSERT(~(type IS SyntaxTree.ArrayType) OR (type(SyntaxTree.ArrayType).form = SyntaxTree.Static));
  6880. IF backend.cooperative THEN
  6881. size := ToMemoryUnits(system,system.SizeOf(type));
  6882. WHILE type IS SyntaxTree.ArrayType DO
  6883. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  6884. END;
  6885. size := size DIV ToMemoryUnits(system,system.SizeOf(type));
  6886. IF (size # 1) THEN
  6887. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  6888. END;
  6889. Emit(Push(position,reg));
  6890. size := ToMemoryUnits(system,system.SizeOf(type));
  6891. IF (size # 1) THEN
  6892. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  6893. END;
  6894. Emit(Add(position,reg,reg,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,(BaseArrayTypeSize + openDim)* system.addressSize))));
  6895. Emit(Push(position,reg));
  6896. ReleaseIntermediateOperand(reg);
  6897. CallThis(position,"Runtime","New", 1);
  6898. pointer := NewRegisterOperand(IntermediateCode.GetType(system, p0.type));
  6899. Emit(Result(position, pointer));
  6900. exit := NewLabel();
  6901. else := NewLabel();
  6902. BreqL(else,pointer,nil);
  6903. IF ~type.hasPointers THEN
  6904. Basic.ToSegmentedName ("BaseTypes.Array",name);
  6905. ELSIF type IS SyntaxTree.RecordType THEN
  6906. Basic.ToSegmentedName ("BaseTypes.RecordArray",name);
  6907. ELSIF type IS SyntaxTree.ProcedureType THEN
  6908. Basic.ToSegmentedName ("BaseTypes.DelegateArray",name);
  6909. ELSE
  6910. Basic.ToSegmentedName ("BaseTypes.PointerArray",name);
  6911. END;
  6912. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,0),IntermediateCode.Address(addressType,name,0,0)));
  6913. Emit(Pop(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,LengthOffset * system.addressSize))));
  6914. 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))));
  6915. IF type IS SyntaxTree.RecordType THEN
  6916. GetRecordTypeName(type(SyntaxTree.RecordType),name);
  6917. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,DescriptorOffset * system.addressSize)),IntermediateCode.Address(addressType,name,0,0)));
  6918. ELSE
  6919. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,DescriptorOffset * system.addressSize)),nil));
  6920. END;
  6921. i := openDim;
  6922. WHILE i > 0 DO
  6923. DEC (i);
  6924. Emit(Pop(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,(BaseArrayTypeSize + i)* system.addressSize))));
  6925. END;
  6926. needsTrace := p0.NeedsTrace();
  6927. IF needsTrace THEN ModifyAssignments(true) END;
  6928. IF ~p0.type.resolved(SyntaxTree.PointerType).isDisposable THEN
  6929. Emit(Push(position, pointer));
  6930. CallThis(position,"GarbageCollector","Watch",1);
  6931. Emit(Pop(position, pointer));
  6932. END;
  6933. Designate(p0,l);
  6934. IF needsTrace THEN
  6935. CallAssignPointer(l.op, pointer);
  6936. ModifyAssignments(false);
  6937. ELSE
  6938. ToMemory(l.op,addressType,0);
  6939. Emit(Mov(position,l.op,pointer));
  6940. END;
  6941. ReleaseIntermediateOperand(pointer);
  6942. ReleaseOperand(l);
  6943. BrL(exit);
  6944. SetLabel(else);
  6945. Emit(Add(position,sp,sp,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,(openDim+1)*system.addressSize))));
  6946. Designate(p0,l);
  6947. IF needsTrace THEN
  6948. CallResetProcedure(l.op,l.tag,p0.type.resolved);
  6949. ELSE
  6950. ToMemory(l.op,addressType,0);
  6951. Emit(Mov(position,l.op,pointer));
  6952. END;
  6953. ReleaseOperand(l);
  6954. SetLabel(exit);
  6955. ELSE
  6956. (*! the following code is only correct for "standard" Oberon calling convention *)
  6957. IF SemanticChecker.ContainsPointer(type) THEN
  6958. IF type IS SyntaxTree.ArrayType THEN
  6959. staticLength := 1;
  6960. WHILE (type IS SyntaxTree.ArrayType) DO (* static array *)
  6961. staticLength := staticLength * type(SyntaxTree.ArrayType).staticLength;
  6962. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  6963. END;
  6964. tmp := IntermediateCode.Immediate(reg.type,staticLength);
  6965. Emit(Mul(position,reg,reg,tmp));
  6966. END;
  6967. Designate(p0,l);
  6968. IF openDim > 0 THEN
  6969. Emit(Push(position,l.op)); (* address for use after syscall *)
  6970. END;
  6971. Emit(Push(position,l.op)); (* address *)
  6972. ReleaseOperand(l);
  6973. tmp := TypeDescriptorAdr(type);
  6974. IF ~newObjectFile THEN
  6975. IntermediateCode.MakeMemory(tmp,addressType);
  6976. END;
  6977. Emit(Push(position,tmp)); (* type descriptor *)
  6978. ReleaseIntermediateOperand(tmp);
  6979. Emit(Push(position,reg)); (* number Elements *)
  6980. ReleaseIntermediateOperand(reg);
  6981. tmp := IntermediateCode.Immediate(addressType,dim);
  6982. Emit(Push(position,tmp)); (* dimensions *)
  6983. (* push realtime flag *)
  6984. IF (p0.type.resolved.isRealtime) THEN Emit(Push(position,true));
  6985. ELSE Emit(Push(position,false));
  6986. END;
  6987. CallThis(position,"Heaps","NewArr",5)
  6988. ELSE
  6989. size := ToMemoryUnits(system,system.SizeOf(type));
  6990. IF (size # 1) THEN
  6991. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  6992. END;
  6993. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,ArrayDimTable * system.addressSize+ system.addressSize+ system.addressSize * 2 * (openDim DIV 2)));
  6994. (* DIV 2 term for some strange alignment, don't understand it at the moment - copied from PCC *)
  6995. Emit(Add(position,reg,reg,tmp));
  6996. Designate(p0,l);
  6997. IF openDim >0 THEN
  6998. Emit(Push(position,l.op)); (* address for use after syscall *)
  6999. END;
  7000. Emit(Push(position,l.op)); (* address for syscall *)
  7001. ReleaseOperand(l); (* pointer address *)
  7002. Emit(Push(position,reg)); (* size *)
  7003. ReleaseIntermediateOperand(reg);
  7004. (* push realtime flag *)
  7005. IF (p0.type.resolved.isRealtime) THEN Emit(Push(position,true));
  7006. ELSE Emit(Push(position,false));
  7007. END;
  7008. CallThis(position,"Heaps","NewSys", 3)
  7009. END;
  7010. IF openDim > 0 THEN
  7011. IntermediateCode.InitRegister(adr,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7012. Emit(Pop(position,adr));
  7013. ToMemory(adr,addressType,0);
  7014. ReuseCopy(tmp,adr);
  7015. ReleaseIntermediateOperand(adr);
  7016. adr := tmp;
  7017. else := NewLabel();
  7018. BreqL(else,adr,IntermediateCode.Immediate(addressType,0));
  7019. i := openDim-1;
  7020. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7021. WHILE (i >= 0) DO
  7022. Emit(Pop(position,reg));
  7023. IntermediateCode.InitMemory(res,addressType,adr,ToMemoryUnits(system,ArrayDimTable* system.addressSize + system.addressSize*((openDim-1)-i)));
  7024. Emit(Mov(position,res,reg));
  7025. DEC(i);
  7026. END;
  7027. ReleaseIntermediateOperand(adr);
  7028. ReleaseIntermediateOperand(reg);
  7029. exit := NewLabel();
  7030. BrL(exit);
  7031. SetLabel(else);
  7032. (* else part: array could not be allocated *)
  7033. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,openDim*system.addressSize));
  7034. Emit(Add(position,sp,sp,tmp));
  7035. SetLabel(exit);
  7036. END;
  7037. END;
  7038. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  7039. IF t1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  7040. IF GetRuntimeProcedure("FoxArrayBase","AllocateTensorX",procedure,TRUE) THEN
  7041. left := SyntaxTree.NewSymbolDesignator(Diagnostics.Invalid,NIL,procedure);
  7042. procedureType := procedure.type(SyntaxTree.ProcedureType);
  7043. left.SetType(procedure.type);
  7044. formalParameter := procedureType.firstParameter;
  7045. (* push array to allocate *)
  7046. PushParameter(p0, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  7047. formalParameter :=formalParameter.nextParameter;
  7048. (* push length array *)
  7049. PushParameter(p1, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  7050. (* push size *)
  7051. type := t0;
  7052. WHILE (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) DO
  7053. type := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  7054. END;
  7055. tmp := IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType),ToMemoryUnits(system,system.SizeOf(type))); (* alignment *)
  7056. Emit(Push(position,tmp));
  7057. (* *)
  7058. IF SemanticChecker.ContainsPointer(type) THEN
  7059. tmp := TypeDescriptorAdr(type);
  7060. IF ~newObjectFile THEN
  7061. IntermediateCode.MakeMemory(tmp,addressType);
  7062. END;
  7063. ELSE
  7064. tmp := IntermediateCode.Immediate(addressType, 0);
  7065. END;
  7066. Emit(Push(position,tmp)); (* type descriptor *)
  7067. StaticCallOperand(result,procedure);
  7068. Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  7069. ReleaseOperand(result);
  7070. END;
  7071. (*
  7072. designator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.ArrayBaseName);
  7073. designator := SyntaxTree.NewSelectorDesignator(InvalidPosition, designator, SyntaxTree.NewIdentifier(InvalidPosition, "AllocateTensorX"));
  7074. result := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, designator, actualParameters));
  7075. *)
  7076. ELSE
  7077. dim := 0;
  7078. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7079. (* generate geometry descriptor *)
  7080. Designate(p0,l);
  7081. NewMathArrayDescriptor(l, x.parameters.Length()-1);
  7082. ReleaseOperand(l);
  7083. isTensor := TRUE;
  7084. ELSE
  7085. isTensor := FALSE;
  7086. END;
  7087. FOR i := 1 TO x.parameters.Length()-1 DO
  7088. IF ~isTensor THEN
  7089. type := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  7090. END;
  7091. parameter := x.parameters.GetExpression(i);
  7092. Evaluate(parameter,r);
  7093. IF (r.op.mode # IntermediateCode.ModeImmediate) & ~isUnchecked THEN
  7094. IntermediateCode.InitImmediate(tmp,IntermediateCode.GetType(system,parameter.type),0);
  7095. TrapC(BrgeL,r.op,tmp,ArraySizeTrap);
  7096. END;
  7097. Emit(Push(position,r.op));
  7098. IF i=1 THEN
  7099. ReuseCopy(reg,r.op);
  7100. ELSE
  7101. Emit(Mul(position,reg,reg,r.op)); (*! optimize the multiplication of immediate operands *)
  7102. END;
  7103. ReleaseOperand(r);
  7104. INC(dim);
  7105. END;
  7106. Convert(reg,addressType);
  7107. openDim := dim;
  7108. ASSERT(~(type IS SyntaxTree.MathArrayType) OR (type(SyntaxTree.MathArrayType).form IN {SyntaxTree.Static,SyntaxTree.Tensor}));
  7109. (*! the following code is only correct for "standard" Oberon calling convention *)
  7110. IF SemanticChecker.ContainsPointer(SemanticChecker.ArrayBase(type,MAX(LONGINT))) THEN
  7111. t := type;
  7112. IF ~isTensor & (t IS SyntaxTree.MathArrayType) THEN
  7113. staticLength := 1;
  7114. WHILE (t IS SyntaxTree.MathArrayType) DO (* static array *)
  7115. staticLength := staticLength * t(SyntaxTree.MathArrayType).staticLength;
  7116. t := t(SyntaxTree.MathArrayType).arrayBase.resolved;
  7117. END;
  7118. tmp := IntermediateCode.Immediate(reg.type,staticLength);
  7119. Emit(Mul(position,reg,reg,tmp));
  7120. END;
  7121. Designate(p0,l);
  7122. IF isTensor THEN
  7123. Dereference(l,type,FALSE);
  7124. t := SemanticChecker.ArrayBase(type,MAX(LONGINT));
  7125. END;
  7126. Emit(Push(position,l.tag)); (* address for use after syscall *)
  7127. Emit(Push(position,l.tag)); (* address *)
  7128. ReleaseOperand(l);
  7129. tmp := TypeDescriptorAdr(t);
  7130. IF ~newObjectFile THEN
  7131. IntermediateCode.MakeMemory(tmp,addressType);
  7132. END;
  7133. Emit(Push(position,tmp)); (* type descriptor *)
  7134. ReleaseIntermediateOperand(tmp);
  7135. Emit(Push(position,reg)); (* number Elements *)
  7136. ReleaseIntermediateOperand(reg);
  7137. tmp := IntermediateCode.Immediate(addressType,0);
  7138. Emit(Push(position,tmp)); (* dimensions = 0, we control dimensions in the geometry descriptor *)
  7139. (* push realtime flag: false by default *)
  7140. Emit(Push(position,false));
  7141. CallThis(position,"Heaps","NewArr",5);
  7142. IntermediateCode.InitRegister(adr,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7143. Emit(Pop(position,adr));
  7144. GetMathArrayField(tmp,adr,MathPtrOffset);
  7145. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7146. Emit(Add(position,reg,tmp,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,ArrDataArrayOffset))));
  7147. PutMathArrayField(adr,reg,MathAdrOffset);
  7148. ReleaseIntermediateOperand(tmp);
  7149. ReleaseIntermediateOperand(reg);
  7150. ELSE
  7151. IF isTensor THEN
  7152. size := ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,MAX(LONGINT))));
  7153. ELSE
  7154. size := ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,openDim)));
  7155. END;
  7156. IF (size # 1) THEN
  7157. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  7158. END;
  7159. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,SysDataArrayOffset));
  7160. Emit(Add(position,reg,reg,tmp));
  7161. Designate(p0,l);
  7162. IF isTensor THEN
  7163. Dereference(l,type,FALSE);
  7164. END;
  7165. Emit(Push(position,l.tag)); (* address for use after syscall *)
  7166. Emit(Push(position,l.tag)); (* address for syscall *)
  7167. ReleaseOperand(l); (* pointer address *)
  7168. Emit(Push(position,reg)); (* size *)
  7169. ReleaseIntermediateOperand(reg);
  7170. (* push realtime flag: false by default *)
  7171. Emit(Push(position,false));
  7172. CallThis(position,"Heaps","NewSys",3);
  7173. IntermediateCode.InitRegister(adr,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7174. Emit(Pop(position,adr));
  7175. GetMathArrayField(tmp,adr,MathPtrOffset);
  7176. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7177. Emit(Add(position,reg,tmp,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,SysDataArrayOffset))));
  7178. PutMathArrayField(adr,reg,MathAdrOffset);
  7179. ReleaseIntermediateOperand(tmp);
  7180. ReleaseIntermediateOperand(reg);
  7181. END;
  7182. flags := {};
  7183. IntermediateCode.InitImmediate(tmp,addressType,SYSTEM.VAL(LONGINT,flags));
  7184. PutMathArrayField(adr,tmp,MathFlagsOffset);
  7185. IntermediateCode.InitImmediate(tmp,addressType,openDim);
  7186. PutMathArrayField(adr,tmp,MathDimOffset);
  7187. else := NewLabel();
  7188. BreqL(else,IntermediateCode.Memory(addressType,adr,0),IntermediateCode.Immediate(addressType,0));
  7189. i := openDim-1;
  7190. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7191. IF isTensor THEN
  7192. IntermediateCode.InitImmediate(tmp,addressType,ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,MAX(LONGINT)))));
  7193. ELSE
  7194. IntermediateCode.InitImmediate(tmp,addressType,ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,openDim))));
  7195. END;
  7196. PutMathArrayField(adr,tmp,MathElementSizeOffset);
  7197. WHILE (i >= 0) DO
  7198. Emit(Pop(position,reg));
  7199. PutMathArrayLength(adr,reg,i);
  7200. PutMathArrayIncrement(adr,tmp,i);
  7201. IF i > 0 THEN
  7202. IF i=openDim-1 THEN
  7203. ReuseCopy(tmp,tmp);
  7204. END;
  7205. Emit(Mul(position,tmp,tmp,reg));
  7206. END;
  7207. DEC(i);
  7208. END;
  7209. ReleaseIntermediateOperand(adr);
  7210. ReleaseIntermediateOperand(reg);
  7211. ReleaseIntermediateOperand(tmp);
  7212. exit := NewLabel();
  7213. BrL(exit);
  7214. SetLabel(else);
  7215. (* else part: array could not be allocated *)
  7216. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,openDim*system.addressSize));
  7217. Emit(Add(position,sp,sp,tmp));
  7218. SetLabel(exit);
  7219. END;
  7220. ELSIF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.CellType)
  7221. THEN
  7222. IF ~backend.cellsAreObjects THEN RETURN END;
  7223. IF InCellScope(currentScope) THEN
  7224. PushSelfPointer()
  7225. ELSE
  7226. Emit(Push(position, nil));
  7227. END;
  7228. (* push temp address *)
  7229. baseType := type(SyntaxTree.PointerType).pointerBase.resolved;
  7230. temporaryVariable := GetTemporaryVariable(type, FALSE);
  7231. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7232. (* l.op contains address of pointer to record *)
  7233. Emit(Push(position,l.op)); (* address for use after syscall *)
  7234. ReleaseOperand(l);
  7235. (* push type descriptor *)
  7236. reg := TypeDescriptorAdr(baseType);
  7237. IF ~newObjectFile THEN
  7238. IntermediateCode.MakeMemory(reg,addressType);
  7239. END;
  7240. Emit(Push(position,reg));
  7241. ReleaseIntermediateOperand(reg);
  7242. (* push name *)
  7243. (*Global.GetSymbolName(p0, n);*)
  7244. IF currentScope IS SyntaxTree.ProcedureScope THEN
  7245. Global.GetSymbolName(currentScope(SyntaxTree.ProcedureScope).ownerProcedure, n)
  7246. ELSE
  7247. Global.GetModuleName(module.module, n);
  7248. END;
  7249. Strings.Append(n,"@"); Strings.AppendInt(n, p0.position);
  7250. (*type.typeDeclaration.GetName(n);*)
  7251. PushConstString(n);
  7252. (* push cellnet boolean *)
  7253. PushConstBoolean(baseType(SyntaxTree.CellType).isCellNet);
  7254. (* push engine boolean *)
  7255. PushConstBoolean(baseType(SyntaxTree.CellType).FindProperty(Global.NameEngine) # NIL);
  7256. (* allocate *)
  7257. CallThis(position,"ActiveCellsRuntime","Allocate",7);
  7258. (* add capabilities *)
  7259. modifier := p0(SyntaxTree.Designator).modifiers;
  7260. IF (p0 IS SyntaxTree.SymbolDesignator) & (p0(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  7261. (*modifier := p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).modifiers;*)
  7262. AppendModifiers(modifier, p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).modifiers );
  7263. (* AddModifiers(baseType(SyntaxTree.CellType), temporaryVariable, modifier);*)
  7264. END;
  7265. AppendModifiers(modifier, baseType(SyntaxTree.CellType).modifiers);
  7266. (*
  7267. modifier := baseType(SyntaxTree.CellType).modifiers;
  7268. AddProperties(baseType(SyntaxTree.CellType), temporaryVariable, baseType(SyntaxTree.CellType).firstProperty);
  7269. modifier := p0(SyntaxTree.Designator).modifiers;
  7270. *)
  7271. AddModifiers(baseType(SyntaxTree.CellType), temporaryVariable, modifier);
  7272. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7273. (* l.op contains address of pointer to record *)
  7274. ToMemory(l.op,addressType,0);
  7275. (* l.op contains value of pointer to record *)
  7276. Emit(Push(position,l.op)); (* address for use after syscall *)
  7277. ReleaseOperand(l);
  7278. CallThis(position,"ActiveCellsRuntime","FinishedProperties",1);
  7279. prevScope := currentScope;
  7280. init := OpenInitializer(temporaryVariable, baseType(SyntaxTree.CellType).cellScope);
  7281. previous := section;
  7282. section := init;
  7283. (* add ports *)
  7284. AddPorts(temporaryVariable, baseType(SyntaxTree.CellType));
  7285. CloseInitializer(previous);
  7286. currentScope := prevScope;
  7287. Symbol(temporaryVariable,l);
  7288. ToMemory(l.op,addressType,0);
  7289. Emit(Push(position,l.op));
  7290. Emit(Call(position,IntermediateCode.Address(addressType, init.name, 0, 0), ToMemoryUnits(system, addressType.sizeInBits)));
  7291. (*
  7292. constructor := type(SyntaxTree.CellType).cellScope.constructor;
  7293. IF constructor # NIL THEN
  7294. parameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7295. FOR i := 1 TO x.parameters.Length()-1 DO
  7296. p := x.parameters.GetExpression(i);
  7297. Global.GetSymbolName(parameter,name);
  7298. Evaluate(p, value);
  7299. ASSERT(value.type # NIL);
  7300. IF value.type.resolved IS SyntaxTree.IntegerType THEN
  7301. par := instance.AddParameter(name);
  7302. par.SetInteger(value.integer);
  7303. ELSIF value.type.resolved IS SyntaxTree.BooleanType THEN
  7304. par := instance.AddParameter(name);
  7305. par.SetBoolean(value.boolean);
  7306. ELSE Error(x.position,NotYetImplemented)
  7307. END;
  7308. parameter := parameter.nextParameter
  7309. END;
  7310. END;
  7311. *)
  7312. (* call initializer *)
  7313. constructor := baseType(SyntaxTree.CellType).cellScope.constructor (*GetConstructor(p0.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType))*);
  7314. IF constructor # NIL THEN
  7315. (*! should be unified with ProcedureCallDesignator *)
  7316. IF backend.cellsAreObjects THEN
  7317. Symbol(temporaryVariable,l);
  7318. ToMemory(l.op,addressType,0);
  7319. Emit(Push(position,l.op));
  7320. ReleaseOperand(l);
  7321. END;
  7322. formalParameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7323. FOR i := 1 TO x.parameters.Length()-1 DO
  7324. PushParameter(x.parameters.GetExpression(i), formalParameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  7325. formalParameter := formalParameter.nextParameter;
  7326. END;
  7327. (* static call of the constructor *)
  7328. Global.GetSymbolSegmentedName(constructor,name);
  7329. ASSERT(~constructor.isInline);
  7330. IF constructor.scope.ownerModule # module.module THEN
  7331. symbol := NewSection(module.importedSections, Sections.CodeSection, name, constructor, commentPrintout # NIL);
  7332. ELSE
  7333. symbol := NewSection(module.allSections, Sections.CodeSection, name, constructor, commentPrintout # NIL);
  7334. END;
  7335. Emit(Call(position,IntermediateCode.Address(addressType, symbol.name, GetFingerprint(constructor), 0),ProcedureParametersSize(system,constructor)));
  7336. (*ELSE
  7337. ReleaseIntermediateOperand(pointer);*)
  7338. END;
  7339. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7340. ToMemory(l.op, addressType, 0);
  7341. Designate(p0,s0);
  7342. ToMemory(s0.op,addressType,0);
  7343. Emit(Mov(position,s0.op,l.op));
  7344. ReleaseOperand(l);
  7345. ReleaseOperand(s0);
  7346. result.tag := emptyOperand;
  7347. (* start *)
  7348. IF baseType(SyntaxTree.CellType).cellScope.bodyProcedure # NIL THEN
  7349. (* push cell *)
  7350. Symbol(temporaryVariable, l);
  7351. ToMemory(l.op,addressType,0);
  7352. Emit(Push(-1,l.op));
  7353. (* push delegate *)
  7354. Emit(Push(-1,l.op));
  7355. ReleaseOperand(l);
  7356. StaticCallOperand(s1,baseType(SyntaxTree.CellType).cellScope.bodyProcedure);
  7357. Emit(Push(position, s1.op));
  7358. ReleaseOperand(s1);
  7359. CallThis(position,"ActiveCellsRuntime","Start",3);
  7360. END;
  7361. (*IF temporaryVariable # NIL THEN
  7362. end := NewLabel();
  7363. BrL(end);
  7364. SetLabel(exit);
  7365. Designate(p0,l);
  7366. ToMemory(l.op,addressType,0);
  7367. Emit(Mov(position,l.op,nil)); (* write NIL to adr *)
  7368. ReleaseOperand(l);
  7369. SetLabel(end);
  7370. ELSE
  7371. SetLabel(exit);
  7372. END;
  7373. *)
  7374. (*Error(p0.position,"cannot be allocated in runtime yet");*)
  7375. ELSE (* no pointer to record, no pointer to array *)
  7376. IF ~backend.cellsAreObjects & (type IS SyntaxTree.CellType) THEN
  7377. (* ignore new statement *)
  7378. Warning(p0.position, "cannot run on final hardware");
  7379. ELSE
  7380. HALT(200);
  7381. END;
  7382. END;
  7383. (* ---- ADDRESSOF----- *)
  7384. |Global.systemAdr:
  7385. Designate(p0,s0);
  7386. s0.mode := ModeValue;
  7387. IF (t0 IS SyntaxTree.MathArrayType) & (t0(SyntaxTree.MathArrayType).form = SyntaxTree.Open) THEN
  7388. ReleaseIntermediateOperand(s0.op);
  7389. s0.op := s0.tag;
  7390. IntermediateCode.InitOperand(s0.tag);
  7391. END;
  7392. Convert(s0.op,IntermediateCode.GetType(system,x.type));
  7393. result := s0;
  7394. (* ---- BIT ----- *)
  7395. |Global.systemBit:
  7396. Evaluate(p0,s0);
  7397. ToMemory(s0.op,addressType,0);
  7398. ReuseCopy(res,s0.op);
  7399. ReleaseOperand(s0);
  7400. Evaluate(p1,s1);
  7401. Emit(Ror(position,res,res,s1.op));
  7402. ReleaseOperand(s1);
  7403. Emit(And(position,res,res,IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1)));
  7404. Convert(res,IntermediateCode.GetType(system,system.booleanType));
  7405. IF ~conditional THEN
  7406. InitOperand(result,ModeValue); result.op := res;
  7407. ELSE
  7408. BreqL(trueLabel,IntermediateCode.Immediate(res.type,1),res);
  7409. BrL(falseLabel);
  7410. ReleaseIntermediateOperand(res);
  7411. END;
  7412. (* --- MSK ----*)
  7413. |Global.systemMsk:
  7414. Evaluate(p0,s0);
  7415. Evaluate(p1,s1);
  7416. ReuseCopy(res,s0.op);
  7417. ReleaseOperand(s0);
  7418. Emit(And(position,res,res,s1.op));
  7419. ReleaseOperand(s1);
  7420. InitOperand(result,ModeValue);
  7421. result.op := res;
  7422. (* ---- SYSTEM.GET8|16|32|64 ----- *)
  7423. |Global.systemGet8, Global.systemGet16, Global.systemGet32, Global.systemGet64:
  7424. Evaluate(p0,s0);
  7425. MakeMemory(res,s0.op,IntermediateCode.GetType(system,x.type),0);
  7426. ReleaseOperand(s0);
  7427. InitOperand(result,ModeValue);
  7428. result.op := res;
  7429. (* ---- SYSTEM.GetStackPointer ----- *)
  7430. |Global.systemGetStackPointer:
  7431. InitOperand(result,ModeValue);
  7432. result.op := sp;
  7433. (* ---- SYSTEM.GetFramePointer ----- *)
  7434. |Global.systemGetFramePointer:
  7435. InitOperand(result,ModeValue);
  7436. result.op := fp;
  7437. (* ---- SYSTEM.GetActivity ----- *)
  7438. |Global.systemGetActivity:
  7439. ASSERT(backend.cooperative);
  7440. InitOperand(result,ModeValue);
  7441. result.op := ap;
  7442. (* ---- SYSTEM.SetStackPointer ----- *)
  7443. |Global.systemSetStackPointer:
  7444. Evaluate(p0,s0); (* *)
  7445. Emit(Mov(position,sp,s0.op));
  7446. ReleaseOperand(s0);
  7447. (* ---- SYSTEM.SetFramePointer ----- *)
  7448. |Global.systemSetFramePointer:
  7449. Evaluate(p0,s0); (* *)
  7450. Emit(Mov(position,fp,s0.op));
  7451. ReleaseOperand(s0);
  7452. (* ---- SYSTEM.Activity ----- *)
  7453. |Global.systemSetActivity:
  7454. ASSERT(backend.cooperative);
  7455. Evaluate(p0,s0); (* *)
  7456. Emit(Mov(position,ap,s0.op));
  7457. ReleaseOperand(s0);
  7458. (* ---- SYSTEM.VAL ----- *)
  7459. |Global.systemVal:
  7460. Expression(p1);
  7461. s1 := result;
  7462. type :=p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  7463. IF s1.mode = ModeReference THEN
  7464. (* nothing to be done if not record type, just take over new type *)
  7465. IF (type IS SyntaxTree.RecordType) THEN
  7466. ReleaseIntermediateOperand(s1.tag);
  7467. s1.tag := TypeDescriptorAdr(type);
  7468. IF ~newObjectFile THEN
  7469. IntermediateCode.MakeMemory(s1.tag,addressType);
  7470. END;
  7471. UseIntermediateOperand(s1.tag);
  7472. END;
  7473. result := s1;
  7474. ELSE (* copy over result to different type, may not use convert *)
  7475. itype := IntermediateCode.GetType(system,type);
  7476. IF itype.sizeInBits = s1.op.type.sizeInBits THEN
  7477. IntermediateCode.InitRegister(s0.op,itype,IntermediateCode.GeneralPurposeRegister,AcquireRegister(itype,IntermediateCode.GeneralPurposeRegister));
  7478. Emit(Mov(position,s0.op,s1.op));
  7479. ReleaseOperand(s1);
  7480. InitOperand(result,ModeValue);
  7481. result.op := s0.op;
  7482. ELSE (* different size, must convert *)
  7483. (*! this is not very clean, should we forbid conversions between operands with different sizes or should we implement a cutting move ??? *)
  7484. Convert(s1.op, IntermediateCode.GetType(system,type));
  7485. result := s1;
  7486. END;
  7487. END;
  7488. (* ---- SYSTEM.GET ----- *)
  7489. |Global.systemGet:
  7490. Evaluate(p0,s0); (* adr *)
  7491. Designate(p1,s1); (* variable *)
  7492. ToMemory(s0.op,IntermediateCode.GetType(system,p1.type),0);
  7493. ToMemory(s1.op,IntermediateCode.GetType(system,p1.type),0);
  7494. Emit(Mov(position,s1.op,s0.op));
  7495. ReleaseOperand(s1);
  7496. ReleaseOperand(s0);
  7497. (* ---- SYSTEM.PUT 8|16|32|64 ----- *)
  7498. |Global.systemPut, Global.systemPut64, Global.systemPut32, Global.systemPut16, Global.systemPut8:
  7499. Evaluate(p0,s0); (* *)
  7500. Evaluate(p1,s1); (* variable *)
  7501. IF p1.type.resolved IS SyntaxTree.ComplexType THEN
  7502. componentType := p1.type.resolved(SyntaxTree.ComplexType).componentType;
  7503. (* real part *)
  7504. MakeMemory(res, s0.op, IntermediateCode.GetType(system, componentType), 0);
  7505. Emit(Mov(position,res, s1.op));
  7506. ReleaseIntermediateOperand(res);
  7507. (* imaginary part *)
  7508. MakeMemory(res, s0.op, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  7509. Emit(Mov(position,res, s1.tag));
  7510. ReleaseIntermediateOperand(res);
  7511. ReleaseOperand(s1);
  7512. ReleaseOperand(s0);
  7513. ELSE
  7514. MakeMemory(res,s0.op,IntermediateCode.GetType(system,p1.type),0);
  7515. ReleaseOperand(s0);
  7516. Emit(Mov(position,res,s1.op));
  7517. ReleaseIntermediateOperand(res);
  7518. ReleaseOperand(s1);
  7519. END;
  7520. (* ---- SYSTEM.MOVE ----- *)
  7521. |Global.systemMove:
  7522. Evaluate(p0,s0);
  7523. Evaluate(p1,s1);
  7524. Evaluate(p2,s2);
  7525. Emit(Copy(position,s1.op,s0.op,s2.op));
  7526. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  7527. (* ---- SYSTEM.NEW ----- *)
  7528. |Global.systemNew:
  7529. Designate(p0,s0);
  7530. Emit(Push(position,s0.op));
  7531. ReleaseOperand(s0);
  7532. Evaluate(p1,s1);
  7533. Emit(Push(position,s1.op));
  7534. ReleaseOperand(s1);
  7535. (* push realtime flag: false by default *)
  7536. Emit(Push(position,false));
  7537. CallThis(position,"Heaps","NewSys",3);
  7538. (* ---- SYSTEM.CALL ----- *)
  7539. |Global.systemRef:
  7540. Basic.ToSegmentedName(p0(SyntaxTree.StringValue).value^, segmentedName);
  7541. callsection := NewSection(module.allSections, Sections.CodeSection, segmentedName, NIL,commentPrintout # NIL);
  7542. s0.mode := ModeValue;
  7543. IntermediateCode.InitAddress(s0.op, addressType, callsection.name, 0, 0);
  7544. result := s0
  7545. (* ---- INCR ----- *)
  7546. |Global.Incr:
  7547. Designate(p0,operand);
  7548. IF p0.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7549. Dereference(operand,p0.type.resolved,FALSE);
  7550. END;
  7551. ASSERT(p1 # NIL);
  7552. Evaluate(p1,l);
  7553. GetMathArrayIncrement(p0.type.resolved(SyntaxTree.MathArrayType),operand, l.op,TRUE, result);
  7554. ReleaseOperand(operand); ReleaseOperand(l);
  7555. Convert(result.op,IntermediateCode.GetType(system, x.type));
  7556. (* ---- SUM ----- *)
  7557. |Global.Sum: HALT(200);
  7558. (* ---- CAS ----- *)
  7559. |Global.Cas:
  7560. needsTrace := p0.NeedsTrace();
  7561. IF needsTrace THEN ModifyAssignments(true) END;
  7562. Designate(p0,s0);
  7563. Evaluate(p1,s1);
  7564. Evaluate(p2,s2);
  7565. IF needsTrace THEN
  7566. Emit(Push(position, s0.op));
  7567. Emit(Push(position, s1.op));
  7568. Emit(Push(position, s2.op));
  7569. CallThis(position,"GarbageCollector","CompareAndSwap",3);
  7570. ELSE
  7571. Emit(Cas(position,s0.op,s1.op,s2.op));
  7572. END;
  7573. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  7574. IF needsTrace THEN ModifyAssignments(false) END;
  7575. res := NewRegisterOperand(IntermediateCode.GetType(system, p0.type));
  7576. Emit(Result(position, res)); result.op := res; result.mode := ModeValue;
  7577. IF conditional THEN
  7578. BreqL(trueLabel,IntermediateCode.Immediate(res.type,1),res);
  7579. BrL(falseLabel);
  7580. ReleaseIntermediateOperand(res);
  7581. END;
  7582. (* ---- DIM ----- *)
  7583. |Global.Dim:
  7584. ASSERT(~SemanticChecker.IsArrayStructuredObjectType(p0.type));
  7585. Designate(p0,s0);
  7586. IF p0.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7587. Dereference(s0,p0.type.resolved,FALSE);
  7588. END;
  7589. MathArrayDim(p0.type.resolved(SyntaxTree.MathArrayType),s0.tag,result);
  7590. ReleaseOperand(s0);
  7591. (* ---- RESHAPE ----- *)
  7592. |Global.Reshape:
  7593. IF GetRuntimeProcedure("FoxArrayBase","Reshape",procedure,TRUE) THEN
  7594. left := SyntaxTree.NewSymbolDesignator(Diagnostics.Invalid,NIL,procedure);
  7595. left.SetType(procedure.type);
  7596. call := SyntaxTree.NewProcedureCallDesignator(position,left(SyntaxTree.Designator),x.parameters);
  7597. VisitProcedureCallDesignator(call(SyntaxTree.ProcedureCallDesignator));
  7598. END;
  7599. (* ---- SYSTEM.TYPECODE ----- *)
  7600. |Global.systemTypeCode:
  7601. type := p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  7602. IF type.resolved IS SyntaxTree.PointerType THEN
  7603. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  7604. END;
  7605. result.op := TypeDescriptorAdr(type);
  7606. IF ~newObjectFile THEN
  7607. ToMemory(result.op,IntermediateCode.GetType(system,x.type),0);
  7608. ELSE Convert(result.op, IntermediateCode.GetType(system,x.type));
  7609. END;
  7610. result.mode := ModeValue;
  7611. (* ---- SYSTEM.TRACE ----- *)
  7612. |Global.systemTrace:
  7613. SystemTrace(x.parameters, x.position);
  7614. (* ----- CONNECT ------*)
  7615. |Global.Connect:
  7616. IF backend.cellsAreObjects THEN
  7617. PushPort(p0);
  7618. PushPort(p1);
  7619. IF p2 # NIL THEN
  7620. Evaluate(p2, s2);
  7621. Emit(Push(p2.position, s2.op));
  7622. ReleaseOperand(s2);
  7623. ELSE
  7624. Emit(Push(-1, IntermediateCode.Immediate(int32, -1)));
  7625. END;
  7626. CallThis(position,"ActiveCellsRuntime","Connect",3);
  7627. ELSE
  7628. Warning(x.position, "cannot run on final hardware");
  7629. END;
  7630. (* ----- DELEGATE ------*)
  7631. |Global.Delegate:
  7632. IF backend.cellsAreObjects THEN
  7633. PushPort(p0);
  7634. PushPort(p1);
  7635. CallThis(position,"ActiveCellsRuntime","Delegate",2);
  7636. ELSE
  7637. Warning(x.position, "cannot run on final hardware");
  7638. END;
  7639. (* ----- SEND ------*)
  7640. |Global.Send:
  7641. Evaluate(p0,s0);
  7642. Evaluate(p1,s1);
  7643. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  7644. Emit(Push(position,s0.op));
  7645. Emit(Push(position,s1.op));
  7646. (*
  7647. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  7648. *)
  7649. IF ~backend.cellsAreObjects THEN
  7650. IF size # 1 THEN Error(p1.position,"send not implemented for complex data types") END;
  7651. END;
  7652. ReleaseOperand(s0);
  7653. ReleaseOperand(s1);
  7654. IF backend.cellsAreObjects THEN
  7655. CallThis(position,"ActiveCellsRuntime","Send",2);
  7656. ELSE
  7657. CallThis(position,ChannelModuleName,"Send",2);
  7658. END;
  7659. (* ----- RECEIVE ------*)
  7660. |Global.Receive:
  7661. Evaluate(p0,s0);
  7662. Emit(Push(position,s0.op));
  7663. Designate(p1,s1);
  7664. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  7665. Emit(Push(position,s1.op));
  7666. IF p2 # NIL THEN
  7667. Designate(p2,s2);
  7668. Emit(Push(position,s2.op));
  7669. END;
  7670. (*
  7671. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  7672. *)
  7673. IF ~backend.cellsAreObjects THEN
  7674. IF size # 1 THEN Error(p1.position,"receive not implemented for complex data types") END;
  7675. END;
  7676. ReleaseOperand(s0);
  7677. ReleaseOperand(s1);
  7678. ReleaseOperand(s2);
  7679. IF backend.cellsAreObjects THEN
  7680. IF p2 = NIL THEN
  7681. CallThis(position,"ActiveCellsRuntime","Receive",2)
  7682. ELSE
  7683. CallThis(position,"ActiveCellsRuntime","ReceiveNonBlockingVar",3)
  7684. END;
  7685. ELSE
  7686. IF p2 = NIL THEN
  7687. CallThis(position,ChannelModuleName,"Receive",2)
  7688. ELSE
  7689. CallThis(position,ChannelModuleName,"ReceiveNonBlockingVar",3)
  7690. END;
  7691. END;
  7692. | Global.systemSpecial:
  7693. customBuiltin := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.CustomBuiltin);
  7694. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  7695. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  7696. (* determine if parameters are of the VAR kind *)
  7697. ASSERT(x.parameters.Length() <= 3);
  7698. formalParameter := procedureType.firstParameter;
  7699. FOR i := 0 TO x.parameters.Length() - 1 DO
  7700. isVarPar[i] := formalParameter.kind = SyntaxTree.VarParameter;
  7701. formalParameter := formalParameter.nextParameter
  7702. END;
  7703. IF p0 # NIL THEN IF isVarPar[0] THEN Designate(p0, s0) ELSE Evaluate(p0,s0) END ELSE InitOperand(s0, ModeValue) END;
  7704. IF p1 # NIL THEN IF isVarPar[1] THEN Designate(p1, s1) ELSE Evaluate(p1,s1) END ELSE InitOperand(s1, ModeValue) END;
  7705. IF p2 # NIL THEN IF isVarPar[2] THEN Designate(p2, s2) ELSE Evaluate(p2,s2) END ELSE InitOperand(s2, ModeValue) END;
  7706. Emit(SpecialInstruction(x.position, customBuiltin.subType,s0.op, s1.op, s2.op));
  7707. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  7708. IF procedureType.returnType # NIL THEN
  7709. res := NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  7710. Emit(Result(position, res));
  7711. (*InitOperand(result,ModeValue);
  7712. result.op := res;
  7713. *)
  7714. IF ~conditional THEN
  7715. InitOperand(result,ModeValue); result.op := res;
  7716. ELSE
  7717. BreqL(trueLabel,IntermediateCode.Immediate(res.type,1),res);
  7718. BrL(falseLabel);
  7719. ReleaseIntermediateOperand(res);
  7720. END;
  7721. END
  7722. ELSE (* function not yet implemented *)
  7723. Error(position,"not yet implemented");
  7724. END;
  7725. destination := dest;
  7726. IF Trace THEN TraceExit("VisitBuiltinCallDesignator") END;
  7727. END VisitBuiltinCallDesignator;
  7728. PROCEDURE VisitTypeGuardDesignator(x: SyntaxTree.TypeGuardDesignator);
  7729. VAR trueL,falseL: Label; recordType: SyntaxTree.RecordType; dest,tag,ptr: IntermediateCode.Operand;
  7730. BEGIN
  7731. IF Trace THEN TraceEnter("VisitTypeGuardDesignator") END;
  7732. dest := destination; destination := emptyOperand;
  7733. Expression(x.left);
  7734. IF x.left.type.resolved = x.type.resolved THEN (* always true: do nothing *)
  7735. ELSIF isUnchecked THEN (* no check *)
  7736. ELSE
  7737. trueL := NewLabel();
  7738. falseL := NewLabel();
  7739. IF IsPointerToRecord(x.left.type,recordType) THEN
  7740. IntermediateCode.InitRegister(tag,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7741. Emit(Mov(position,tag, result.op));
  7742. IF result.mode # ModeValue THEN
  7743. ptr := tag;
  7744. IntermediateCode.MakeMemory(ptr,addressType);
  7745. Emit(Mov(position,tag, ptr));
  7746. END;
  7747. IF ~backend.cooperative THEN
  7748. IntermediateCode.AddOffset(tag,ToMemoryUnits(system,-addressType.sizeInBits));
  7749. END;
  7750. IntermediateCode.MakeMemory(tag,addressType);
  7751. ELSE
  7752. tag := result.tag;
  7753. END;
  7754. TypeTest(tag,x.type,trueL,falseL);
  7755. ReleaseIntermediateOperand(tag);
  7756. SetLabel(falseL);
  7757. EmitTrap(position,TypeCheckTrap);
  7758. SetLabel(trueL);
  7759. END;
  7760. destination := dest;
  7761. IF Trace THEN TraceExit("VisitTypeGuardDesignator") END;
  7762. END VisitTypeGuardDesignator;
  7763. PROCEDURE Dereference(VAR operand: Operand; type: SyntaxTree.Type; isUnsafe: BOOLEAN);
  7764. VAR dereferenced: IntermediateCode.Operand; arrayDataOffset: LONGINT;
  7765. PROCEDURE NilCheck(CONST op: IntermediateCode.Operand);
  7766. VAR label: Label; pc: LONGINT;
  7767. BEGIN
  7768. IF backend.cooperative & ~isUnchecked THEN
  7769. pc := section.pc;
  7770. label := NewLabel();
  7771. BrneL(label, operand.op, nil);
  7772. EmitTrap(position, NilPointerTrap);
  7773. SetLabel(label);
  7774. INC(statCoopNilCheck, section.pc - pc);
  7775. END;
  7776. END NilCheck;
  7777. BEGIN
  7778. LoadValue(operand,system.addressType); (* in case operand is not a value yet *)
  7779. ReuseCopy(dereferenced,operand.op);
  7780. ReleaseOperand(operand);
  7781. operand.mode := ModeReference;
  7782. operand.op := dereferenced;
  7783. operand.tag := dereferenced;
  7784. UseIntermediateOperand(operand.tag);
  7785. IF (type=NIL) OR (type IS SyntaxTree.RecordType)OR (type IS SyntaxTree.CellType) THEN
  7786. IF isUnsafe & ((type = NIL) OR ~type(SyntaxTree.RecordType).isObject) THEN
  7787. ReleaseIntermediateOperand(operand.tag);
  7788. operand.tag := TypeDescriptorAdr(type);
  7789. IF ~newObjectFile THEN
  7790. IntermediateCode.MakeMemory(operand.tag,addressType);
  7791. END;
  7792. ELSE
  7793. IF ~backend.cooperative THEN
  7794. IntermediateCode.AddOffset(operand.tag,ToMemoryUnits(system,-addressType.sizeInBits));
  7795. END;
  7796. IntermediateCode.MakeMemory(operand.tag,addressType);
  7797. END;
  7798. NilCheck(operand.op);
  7799. ELSIF type IS SyntaxTree.ArrayType THEN
  7800. IF isUnsafe THEN
  7801. NilCheck(operand.op);
  7802. ReleaseIntermediateOperand(operand.tag);
  7803. IF type(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  7804. IntermediateCode.InitImmediate(operand.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  7805. ELSE
  7806. operand.tag := emptyOperand;
  7807. END;
  7808. ELSE
  7809. NilCheck(operand.op);
  7810. IF backend.cooperative THEN
  7811. arrayDataOffset := (BaseArrayTypeSize + DynamicDim(type)) * addressType.sizeInBits;
  7812. IntermediateCode.AddOffset(operand.op,ToMemoryUnits(system,arrayDataOffset));
  7813. IntermediateCode.AddOffset(operand.tag,ToMemoryUnits(system,BaseArrayTypeSize*system.addressSize));
  7814. ELSE
  7815. arrayDataOffset := DynamicDim(type) * addressType.sizeInBits + 3 * addressType.sizeInBits;
  7816. INC(arrayDataOffset, (-arrayDataOffset) MOD ArrayAlignment); (* round up to multiple of ArrayAlignment to ensure that first array element is aligned at 0 MOD ArrayAlignment *)
  7817. IntermediateCode.AddOffset(operand.op,ToMemoryUnits(system,arrayDataOffset));
  7818. IntermediateCode.AddOffset(operand.tag,ToMemoryUnits(system,ArrayDimTable*system.addressSize))
  7819. END;
  7820. END;
  7821. ELSIF type IS SyntaxTree.MathArrayType THEN
  7822. IntermediateCode.AddOffset(operand.op,ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  7823. IntermediateCode.MakeMemory(operand.op,addressType);
  7824. ELSE HALT(100);
  7825. END;
  7826. END Dereference;
  7827. PROCEDURE VisitDereferenceDesignator(x: SyntaxTree.DereferenceDesignator);
  7828. VAR type: SyntaxTree.Type; d: Operand; dest: IntermediateCode.Operand;prevIsUnchecked: BOOLEAN;
  7829. BEGIN
  7830. IF Trace THEN TraceEnter("VisitDereferenceDesignator") END;
  7831. dest := destination; destination := emptyOperand;
  7832. Evaluate(x.left,d);
  7833. type := x.type.resolved;
  7834. prevIsUnchecked := isUnchecked;
  7835. IF (x.left # NIL) & (x.left IS SyntaxTree.SelfDesignator) THEN
  7836. isUnchecked := TRUE; (* avoid NIL-pointer-check for SELF pointer *)
  7837. END;
  7838. Dereference(d,type,IsUnsafePointer(x.left.type));
  7839. isUnchecked := prevIsUnchecked;
  7840. result := d;
  7841. 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
  7842. IF (type IS SyntaxTree.RecordType) & ~type(SyntaxTree.RecordType).isObject THEN
  7843. IntermediateCode.AddOffset(result.op,BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  7844. END;
  7845. END;
  7846. destination := dest;
  7847. IF Trace THEN TraceExit("VisitDereferenceDesignator") END;
  7848. END VisitDereferenceDesignator;
  7849. PROCEDURE VisitSupercallDesignator(x: SyntaxTree.SupercallDesignator);
  7850. VAR procedure: SyntaxTree.Procedure; tag: IntermediateCode.Operand; dest: IntermediateCode.Operand;
  7851. BEGIN
  7852. IF Trace THEN TraceEnter("VisitSupercallDesignator") END;
  7853. dest := destination; destination := emptyOperand;
  7854. Designate(x.left(SyntaxTree.SymbolDesignator).left,result);
  7855. tag := result.op;
  7856. (*ReleaseIntermediateOperand(result.tag); not necessary -- done in StaticCallOperand *)
  7857. procedure := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  7858. StaticCallOperand(result,procedure.super);
  7859. ReleaseIntermediateOperand(result.tag);
  7860. UseIntermediateOperand(tag); (* necessary ? *)
  7861. result.tag := tag;
  7862. destination := dest;
  7863. IF Trace THEN TraceExit("VisitSupercallDesignator") END;
  7864. END VisitSupercallDesignator;
  7865. PROCEDURE VisitSelfDesignator(x: SyntaxTree.SelfDesignator);
  7866. VAR basereg: IntermediateCode.Operand; scope: SyntaxTree.Scope; dest: IntermediateCode.Operand;
  7867. moduleSection: IntermediateCode.Section; moduleOffset, parametersSize: LONGINT;
  7868. name: Basic.SegmentedName;
  7869. BEGIN
  7870. IF Trace THEN TraceEnter("VisitSelfDesignator") END;
  7871. dest := destination; destination := emptyOperand;
  7872. scope := currentScope;
  7873. WHILE (scope.outerScope # NIL) & (scope.outerScope IS SyntaxTree.ProcedureScope) DO
  7874. scope := scope.outerScope;
  7875. END;
  7876. IF scope.outerScope IS SyntaxTree.ModuleScope THEN
  7877. IF newObjectFile THEN
  7878. moduleSection := meta.ModuleSection();
  7879. IF backend.cooperative THEN
  7880. moduleOffset := 0;
  7881. ELSE
  7882. moduleOffset := moduleSection.pc;
  7883. END;
  7884. result.mode := ModeValue;
  7885. result.op := IntermediateCode.Address(addressType, moduleSection.name, GetFingerprint(moduleSection.symbol), moduleOffset);
  7886. ELSE
  7887. Symbol(moduleSelf,result);
  7888. IntermediateCode.MakeMemory(result.op,addressType);
  7889. END
  7890. ELSIF (scope.outerScope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN
  7891. result.mode := ModeValue;
  7892. Global.GetSymbolSegmentedName(scope.outerScope(SyntaxTree.CellScope).ownerCell.typeDeclaration, name);
  7893. result.op := IntermediateCode.Address(addressType, name, 0, moduleOffset);
  7894. ELSE
  7895. GetBaseRegister(basereg,currentScope,scope);
  7896. InitOperand(result,ModeReference);
  7897. result.op := basereg;
  7898. parametersSize := ProcedureParametersSize(system,scope(SyntaxTree.ProcedureScope).ownerProcedure);
  7899. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,addressType.sizeInBits)+parametersSize);
  7900. IF backend.cooperative THEN
  7901. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,addressType.sizeInBits));
  7902. END;
  7903. (* tag must be loaded when dereferencing SELF pointer *)
  7904. END;
  7905. destination := dest;
  7906. IF Trace THEN TraceExit("VisitSelfDesignator") END;
  7907. END VisitSelfDesignator;
  7908. PROCEDURE VisitResultDesignator(x: SyntaxTree.ResultDesignator);
  7909. VAR procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; parameter: SyntaxTree.Parameter;
  7910. BEGIN
  7911. IF Trace THEN TraceEnter("VisitResultDesignator") END;
  7912. procedure := currentScope(SyntaxTree.ProcedureScope).ownerProcedure;
  7913. procedureType := procedure.type(SyntaxTree.ProcedureType);
  7914. parameter := procedureType.returnParameter;
  7915. VisitParameter(parameter);
  7916. IF Trace THEN TraceExit("VisitResultDesignator") END;
  7917. END VisitResultDesignator;
  7918. (** values *)
  7919. PROCEDURE VisitBooleanValue(x: SyntaxTree.BooleanValue);
  7920. BEGIN
  7921. IF Trace THEN TraceEnter("VisitBooleanValue") END;
  7922. IF conditional THEN
  7923. IF x.value THEN BrL(trueLabel)
  7924. ELSE BrL(falseLabel)
  7925. END;
  7926. ELSE
  7927. InitOperand(result,ModeValue);
  7928. IF x.value THEN result.op := true ELSE result.op := false END;
  7929. END;
  7930. END VisitBooleanValue;
  7931. PROCEDURE GetDataSection*(): IntermediateCode.Section;
  7932. VAR name: Basic.SegmentedName; section: IntermediateCode.Section;
  7933. BEGIN
  7934. Global.GetModuleSegmentedName(module.module, name);
  7935. Basic.SuffixSegmentedName(name,Basic.MakeString("@Immediates"));
  7936. section := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,TRUE);
  7937. RETURN section
  7938. END GetDataSection;
  7939. PROCEDURE GetImmediateMem(VAR vop: IntermediateCode.Operand);
  7940. VAR data: IntermediateCode.Section;pc: LONGINT; type: IntermediateCode.Type;
  7941. BEGIN
  7942. type := vop.type;
  7943. data := GetDataSection();
  7944. pc := EnterImmediate(data,vop);
  7945. IntermediateCode.InitAddress(vop, addressType, data.name, 0, pc);
  7946. IntermediateCode.MakeMemory(vop, type);
  7947. END GetImmediateMem;
  7948. PROCEDURE VisitIntegerValue(x: SyntaxTree.IntegerValue);
  7949. BEGIN
  7950. IF Trace THEN TraceEnter("VisitIntegerValue") END;
  7951. InitOperand(result,ModeValue);
  7952. IntermediateCode.InitImmediate(result.op,IntermediateCode.GetType(system,x.type),x.hvalue);
  7953. IF ~supportedImmediate(result.op) &~inData THEN
  7954. GetImmediateMem(result.op)
  7955. END;
  7956. END VisitIntegerValue;
  7957. PROCEDURE VisitCharacterValue(x: SyntaxTree.CharacterValue);
  7958. BEGIN
  7959. IF Trace THEN TraceEnter("VisitCharacterValue") END;
  7960. InitOperand(result,ModeValue);
  7961. IntermediateCode.InitImmediate(result.op,IntermediateCode.GetType(system,x.type),ORD(x.value));
  7962. END VisitCharacterValue;
  7963. PROCEDURE VisitSetValue(x: SyntaxTree.SetValue);
  7964. BEGIN
  7965. IF Trace THEN TraceEnter("VisitSetValue") END;
  7966. InitOperand(result,ModeValue);
  7967. IntermediateCode.InitImmediate(result.op,IntermediateCode.GetType(system,x.type),SYSTEM.VAL(LONGINT,x.value));
  7968. END VisitSetValue;
  7969. PROCEDURE VisitMathArrayValue(x: SyntaxTree.MathArrayValue);
  7970. VAR irv: IntermediateCode.Section; name:Basic.SegmentedName;
  7971. PROCEDURE RecursiveData(x: SyntaxTree.MathArrayExpression);
  7972. VAR numberElements,i: LONGINT; expression: SyntaxTree.Expression; op: Operand;
  7973. BEGIN
  7974. numberElements := x.elements.Length();
  7975. FOR i := 0 TO numberElements-1 DO
  7976. expression := x.elements.GetExpression(i);
  7977. IF expression IS SyntaxTree.MathArrayExpression THEN
  7978. RecursiveData(expression(SyntaxTree.MathArrayExpression));
  7979. ELSE
  7980. inData := TRUE;
  7981. Evaluate(expression,op);
  7982. irv.Emit(Data(position,op.op));
  7983. inData := FALSE;
  7984. ReleaseOperand(op);
  7985. END;
  7986. END;
  7987. END RecursiveData;
  7988. BEGIN
  7989. IF Trace THEN TraceEnter("VisitMathArrayValue") END;
  7990. IF ~TryConstantDeclaration() THEN
  7991. IF constantDeclaration = NIL THEN constantDeclaration:=BuildConstant(module.module,x,constId) END;
  7992. GetCodeSectionNameForSymbol(constantDeclaration,name);
  7993. IF ~newObjectFile OR (constantDeclaration.scope = NIL) OR (constantDeclaration.scope.ownerModule = module.module) THEN
  7994. irv := NewSection(module.allSections,Sections.ConstSection,name,constantDeclaration,commentPrintout # NIL);
  7995. ELSE
  7996. irv := NewSection(module.importedSections, Sections.ConstSection, name,constantDeclaration,commentPrintout # NIL);
  7997. END;
  7998. RecursiveData(x.array);
  7999. InitOperand(result,ModeReference);
  8000. IntermediateCode.InitAddress(result.op, addressType, irv.name, GetFingerprint(irv.symbol), 0);
  8001. END
  8002. END VisitMathArrayValue;
  8003. PROCEDURE TryConstantDeclaration(): BOOLEAN;
  8004. VAR constant: Sections.Section;
  8005. BEGIN
  8006. IF constantDeclaration = NIL THEN
  8007. RETURN FALSE
  8008. ELSE
  8009. (* Is a constant in this module: did we generate it already? *)
  8010. constant := module.allSections.FindBySymbol(constantDeclaration); (*TODO*)
  8011. IF constant # NIL THEN
  8012. InitOperand(result,ModeReference);
  8013. IntermediateCode.InitAddress(result.op,addressType,constant.name,GetFingerprint(constant.symbol), 0);
  8014. RETURN TRUE;
  8015. END;
  8016. END;
  8017. RETURN FALSE
  8018. END TryConstantDeclaration;
  8019. PROCEDURE VisitConstant(x: SyntaxTree.Constant);
  8020. BEGIN
  8021. constantDeclaration := x;
  8022. x.value.resolved.Accept(SELF);
  8023. END VisitConstant;
  8024. PROCEDURE VisitRealValue(x: SyntaxTree.RealValue);
  8025. BEGIN
  8026. IF Trace THEN TraceEnter("VisitRealValue") END;
  8027. InitOperand(result,ModeValue);
  8028. IntermediateCode.InitFloatImmediate(result.op,IntermediateCode.GetType(system,x.type),x.value);
  8029. END VisitRealValue;
  8030. PROCEDURE VisitComplexValue(x: SyntaxTree.ComplexValue);
  8031. VAR
  8032. componentType: SyntaxTree.Type;
  8033. BEGIN
  8034. IF Trace THEN TraceEnter("VisitComplexValue") END;
  8035. ASSERT(x.type IS SyntaxTree.ComplexType);
  8036. componentType := x.type(SyntaxTree.ComplexType).componentType;
  8037. InitOperand(result,ModeValue);
  8038. IntermediateCode.InitFloatImmediate(result.op,IntermediateCode.GetType(system,componentType),x.realValue); (* real part *)
  8039. IntermediateCode.InitFloatImmediate(result.tag,IntermediateCode.GetType(system,componentType),x.imagValue); (* imaginary part *)
  8040. END VisitComplexValue;
  8041. PROCEDURE VisitStringValue(x: SyntaxTree.StringValue);
  8042. VAR i: LONGINT; name: Basic.SegmentedName;
  8043. irv: IntermediateCode.Section; op: IntermediateCode.Operand;
  8044. BEGIN
  8045. IF Trace THEN TraceEnter("VisitStringValue") END;
  8046. IF ~TryConstantDeclaration() THEN
  8047. IF constantDeclaration = NIL THEN constantDeclaration:=BuildConstant(module.module,x,constId) END;
  8048. GetCodeSectionNameForSymbol(constantDeclaration,name);
  8049. IF ~newObjectFile OR (constantDeclaration.scope = NIL) OR (constantDeclaration.scope.ownerModule = module.module) THEN
  8050. irv := NewSection(module.allSections, Sections.ConstSection, name,constantDeclaration,commentPrintout # NIL);
  8051. ELSE
  8052. irv := NewSection(module.importedSections, Sections.ConstSection, name,constantDeclaration,commentPrintout # NIL);
  8053. END;
  8054. FOR i := 0 TO x.length-1 DO
  8055. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(system,system.characterType),ORD(x.value[i]));
  8056. irv.Emit(Data(position,op));
  8057. END;
  8058. InitOperand(result,ModeReference);
  8059. IntermediateCode.InitAddress(result.op, addressType, irv.name, GetFingerprint(irv.symbol), 0);
  8060. result.tag := IntermediateCode.Immediate(addressType,x.length);
  8061. END
  8062. END VisitStringValue;
  8063. PROCEDURE VisitNilValue(x: SyntaxTree.NilValue);
  8064. BEGIN
  8065. IF Trace THEN TraceEnter("VisitNilValue") END;
  8066. InitOperand(result,ModeValue);
  8067. result.op := IntermediateCode.Immediate(IntermediateCode.GetType(system,x.type),0);
  8068. result.tag := IntermediateCode.Immediate(IntermediateCode.GetType(system,x.type),0);
  8069. END VisitNilValue;
  8070. PROCEDURE VisitEnumerationValue(x: SyntaxTree.EnumerationValue);
  8071. BEGIN
  8072. IF Trace THEN TraceEnter("VisitEnumerationValue") END;
  8073. InitOperand(result,ModeValue);
  8074. result.op := IntermediateCode.Immediate(IntermediateCode.GetType(system,x.type),x.value);
  8075. END VisitEnumerationValue;
  8076. (** symbols *)
  8077. PROCEDURE VisitImport(x: SyntaxTree.Import);
  8078. BEGIN (* nothing to be done, might however be called via some designator module.procedure *)
  8079. END VisitImport;
  8080. PROCEDURE GetBaseRegister(VAR result: IntermediateCode.Operand; scope,baseScope: SyntaxTree.Scope);
  8081. VAR left,right: IntermediateCode.Operand;level: LONGINT;
  8082. BEGIN
  8083. IF scope # baseScope THEN
  8084. (* left := [fp+8] *)
  8085. IntermediateCode.InitMemory(right,addressType,fp,ToMemoryUnits(system,2*addressType.sizeInBits));
  8086. IF backend.cooperative THEN IntermediateCode.AddOffset (right, ToMemoryUnits(system,addressType.sizeInBits)) END;
  8087. ReuseCopy(left,right);
  8088. ReleaseIntermediateOperand(right);
  8089. scope := scope.outerScope; DEC(level);
  8090. (* { left := [left+8] } *)
  8091. IntermediateCode.InitMemory(right,addressType,left,ToMemoryUnits(system,2*addressType.sizeInBits));
  8092. IF backend.cooperative THEN IntermediateCode.AddOffset (right, ToMemoryUnits(system,addressType.sizeInBits)) END;
  8093. WHILE (scope # baseScope) & (scope IS SyntaxTree.ProcedureScope) DO
  8094. Emit(Mov(position,left,right));
  8095. scope := scope.outerScope; DEC(level);
  8096. END;
  8097. ASSERT((scope = baseScope) OR (baseScope = NIL));
  8098. result := left;
  8099. ELSE
  8100. result := fp;
  8101. END;
  8102. END GetBaseRegister;
  8103. PROCEDURE VisitVariable(x: SyntaxTree.Variable);
  8104. VAR symbol: Sections.Section; type: SyntaxTree.Type; recordType: SyntaxTree.RecordType; name: Basic.SegmentedName; temp: IntermediateCode.Operand; reg: LONGINT;
  8105. BEGIN
  8106. IF Trace THEN TraceEnter("VisitVariable"); END;
  8107. type := x.type.resolved;
  8108. IF (x.useRegister) THEN
  8109. InitOperand(result, ModeValue);
  8110. IF x.registerNumber < 0 THEN
  8111. x.SetRegisterNumber(AcquireRegister(IntermediateCode.GetType(system, type),IntermediateCode.GeneralPurposeRegister));
  8112. reg := x.registerNumber;
  8113. ELSE
  8114. reg := registerUsageCount.Map(x.registerNumber);
  8115. UseRegister(reg);
  8116. END;
  8117. IntermediateCode.InitRegister(result.op,IntermediateCode.GetType(system, type),IntermediateCode.GeneralPurposeRegister,reg);
  8118. ELSIF x.externalName # NIL THEN
  8119. InitOperand(result,ModeReference);
  8120. Basic.ToSegmentedName(x.externalName^, name);
  8121. IntermediateCode.InitAddress(result.op, addressType, name, 0, 0);
  8122. ELSIF (x.scope IS SyntaxTree.ProcedureScope) THEN (* local variable (potentially via nested procedure) *)
  8123. InitOperand(result,ModeReference);
  8124. GetBaseRegister(result.op,currentScope,x.scope);
  8125. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8126. ELSIF (x.scope = moduleScope) OR (x.scope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN (* global variable *)
  8127. InitOperand(result,ModeReference);
  8128. GetCodeSectionNameForSymbol(x,name);
  8129. symbol := NewSection(module.allSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8130. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0);
  8131. ELSIF x.scope IS SyntaxTree.ModuleScope THEN (* global variable in imported module *)
  8132. InitOperand(result,ModeReference);
  8133. GetCodeSectionNameForSymbol(x,name);
  8134. symbol := NewSection(module.importedSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8135. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0)
  8136. ELSE (* field, left designator must have been emitted *)
  8137. ASSERT(result.mode = ModeReference);
  8138. IF result.op.mode = IntermediateCode.ModeMemory THEN
  8139. ReuseCopy(temp,result.op);
  8140. ReleaseIntermediateOperand(result.op);
  8141. result.op := temp;
  8142. END;
  8143. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8144. IF backend.cooperative & (x.scope IS SyntaxTree.RecordScope) THEN
  8145. recordType := x.scope(SyntaxTree.RecordScope).ownerRecord;
  8146. IF recordType.isObject & ~recordType.pointerType.isPlain THEN
  8147. IntermediateCode.AddOffset(result.op,BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  8148. END;
  8149. END;
  8150. END;
  8151. IF conditional & (x.type.resolved IS SyntaxTree.BooleanType) THEN
  8152. ValueToCondition(result);
  8153. ELSIF type IS SyntaxTree.ProcedureType THEN
  8154. ReleaseIntermediateOperand(result.tag);
  8155. IF type(SyntaxTree.ProcedureType).isDelegate THEN
  8156. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  8157. UseIntermediateOperand(result.tag);
  8158. ELSE
  8159. result.tag := nil; (* nil *)
  8160. END;
  8161. ELSIF (type IS SyntaxTree.ArrayType) THEN
  8162. IF type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic THEN
  8163. IF (x.scope IS SyntaxTree.ModuleScope) OR (x.scope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN
  8164. ReleaseIntermediateOperand(result.tag);
  8165. Global.GetSymbolSegmentedName(x,name);
  8166. Basic.AppendToSegmentedName(name,"@len");
  8167. symbol := NewSection(module.allSections, Sections.VarSection, name,NIL ,commentPrintout # NIL);
  8168. IntermediateCode.InitAddress(result.tag, addressType, symbol.name,0 , 0);
  8169. ELSE
  8170. END;
  8171. ELSE
  8172. ReleaseIntermediateOperand(result.tag);
  8173. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8174. END;
  8175. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  8176. IF type(SyntaxTree.MathArrayType).form IN {SyntaxTree.Open} THEN
  8177. ReleaseIntermediateOperand(result.tag);
  8178. result.tag := result.op;
  8179. UseIntermediateOperand(result.tag);
  8180. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8181. IntermediateCode.MakeMemory(result.op,addressType);
  8182. END;
  8183. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType = NIL) THEN
  8184. ReleaseIntermediateOperand(result.tag);
  8185. result.tag := TypeDescriptorAdr(type);
  8186. IF ~newObjectFile THEN
  8187. IntermediateCode.MakeMemory(result.tag,addressType);
  8188. END;
  8189. UseIntermediateOperand(result.tag);
  8190. (* tag for pointer type computed not here but during dereferencing *)
  8191. END;
  8192. IF Trace THEN TraceExit("VisitVariable") END;
  8193. END VisitVariable;
  8194. PROCEDURE VisitProperty(property: SyntaxTree.Property);
  8195. BEGIN
  8196. VisitVariable(property);
  8197. END VisitProperty;
  8198. PROCEDURE VisitParameter(x: SyntaxTree.Parameter);
  8199. VAR type: SyntaxTree.Type; basereg, mem: IntermediateCode.Operand; parameter: SyntaxTree.Parameter;adr: LONGINT; symbol: Sections.Section;
  8200. name: Basic.SegmentedName; parameterType, ptype: SyntaxTree.Type; len,inc: LONGINT;
  8201. BEGIN
  8202. type := x.type.resolved;
  8203. IF Trace THEN TraceEnter("VisitParameter") END;
  8204. IF x.ownerType IS SyntaxTree.CellType THEN
  8205. ptype := x.type.resolved;
  8206. IF ptype IS SyntaxTree.ArrayType THEN ptype := ptype(SyntaxTree.ArrayType).arrayBase.resolved END;
  8207. IF ~(ptype IS SyntaxTree.PortType) THEN
  8208. InitOperand(result,ModeReference);
  8209. GetCodeSectionNameForSymbol(x,name);
  8210. symbol := NewSection(module.allSections, Sections.ConstSection, name,x,commentPrintout # NIL);
  8211. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol),0);
  8212. RETURN
  8213. ELSE
  8214. InitOperand(result, ModeValue);
  8215. parameter := x.ownerType(SyntaxTree.CellType).firstParameter;
  8216. adr := 0;
  8217. WHILE parameter # x DO
  8218. parameterType := parameter.type;
  8219. inc := 1;
  8220. WHILE SemanticChecker.IsStaticArray(parameterType, parameterType, len) DO inc := inc * len END;
  8221. INC(adr, inc);
  8222. parameter := parameter.nextParameter
  8223. END;
  8224. adr := backend.activeCellsSpecification.GetPortAddress(adr);
  8225. IntermediateCode.InitImmediate(result.op,addressType,adr);
  8226. RETURN
  8227. END;
  8228. ELSIF ~backend.cellsAreObjects & (currentScope IS SyntaxTree.ProcedureScope) & (currentScope(SyntaxTree.ProcedureScope).ownerProcedure.isConstructor) & (currentScope.outerScope IS SyntaxTree.CellScope) THEN
  8229. InitOperand(result,ModeReference);
  8230. GetCodeSectionNameForSymbol(x,name);
  8231. symbol := NewSection(module.allSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8232. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0);
  8233. RETURN
  8234. ELSE
  8235. GetBaseRegister(basereg,currentScope,x.scope);
  8236. InitOperand(result,ModeReference);
  8237. result.op := basereg;
  8238. END;
  8239. IF IsOpenArray(type) THEN
  8240. result.tag := basereg;
  8241. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8242. IntermediateCode.MakeMemory(result.op,addressType);
  8243. IF Global.IsOberonProcedure(x.ownerType) THEN
  8244. IntermediateCode.AddOffset(result.tag,ToMemoryUnits(system,x.offsetInBits+addressType.sizeInBits));
  8245. UseIntermediateOperand(result.tag);
  8246. ELSE
  8247. IntermediateCode.InitImmediate(result.tag,addressType,MAX(LONGINT)); (* non-Oberon procedure => unbounded array length *)
  8248. END;
  8249. ELSIF IsStaticArray(type) & (x.kind = SyntaxTree.ValueParameter) THEN
  8250. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8251. ELSIF IsStaticArray(type) THEN
  8252. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8253. IntermediateCode.MakeMemory(result.op,addressType);
  8254. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8255. ELSIF type IS SyntaxTree.MathArrayType THEN
  8256. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8257. WITH type: SyntaxTree.MathArrayType DO
  8258. IF (x.kind = SyntaxTree.ValueParameter) OR (x.kind = SyntaxTree.ConstParameter) THEN
  8259. IF type.form = SyntaxTree.Tensor THEN
  8260. ELSIF type.form = SyntaxTree.Open THEN
  8261. result.tag := result.op;
  8262. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8263. IntermediateCode.MakeMemory(result.op,addressType);
  8264. UseIntermediateOperand(result.tag);
  8265. ELSIF type.form = SyntaxTree.Static THEN
  8266. IF x.kind = SyntaxTree.ConstParameter THEN
  8267. IntermediateCode.MakeMemory(result.op,addressType);
  8268. END;
  8269. ELSE HALT(100)
  8270. END;
  8271. ELSIF x.kind = SyntaxTree.VarParameter THEN
  8272. IF type.form = SyntaxTree.Tensor THEN
  8273. ToMemory(result.op,addressType,0);
  8274. ELSIF type.form = SyntaxTree.Open THEN
  8275. MakeMemory(mem, result.op, addressType, 0); (* offset already added above *)
  8276. ReuseCopy(result.tag, mem);
  8277. ReleaseIntermediateOperand(mem);
  8278. ReleaseIntermediateOperand(result.op);
  8279. MakeMemory(result.op, result.tag, addressType, ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8280. ELSIF type.form = SyntaxTree.Static THEN
  8281. IntermediateCode.MakeMemory(result.op,addressType);
  8282. ELSE HALT(100)
  8283. END;
  8284. ELSE HALT(100)
  8285. END;
  8286. END;
  8287. ELSIF (x.kind = SyntaxTree.VarParameter) OR (x.kind = SyntaxTree.ConstParameter) & (type IS SyntaxTree.RecordType) THEN
  8288. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8289. IntermediateCode.MakeMemory(result.op,addressType);
  8290. ELSIF (x.kind = SyntaxTree.ValueParameter) OR (x.kind = SyntaxTree.ConstParameter) THEN
  8291. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8292. END;
  8293. IF conditional & (x.type.resolved IS SyntaxTree.BooleanType) THEN
  8294. ValueToCondition(result);
  8295. ELSIF type IS SyntaxTree.ProcedureType THEN
  8296. ReleaseIntermediateOperand(result.tag);
  8297. IF type(SyntaxTree.ProcedureType).isDelegate THEN
  8298. IF x.kind = SyntaxTree.VarParameter THEN
  8299. ReuseCopy(result.tag,result.op);
  8300. IntermediateCode.AddOffset(result.tag,ToMemoryUnits(system,system.addressSize));
  8301. IntermediateCode.MakeMemory(result.tag,addressType);
  8302. ELSE
  8303. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  8304. UseIntermediateOperand(result.tag);
  8305. END;
  8306. ELSE
  8307. result.tag := nil;
  8308. END;
  8309. (* tag for pointer type computed not here but during dereferencing *)
  8310. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType= NIL) & (x.kind IN {SyntaxTree.VarParameter, SyntaxTree.ConstParameter}) THEN
  8311. ReleaseIntermediateOperand(result.tag);
  8312. result.tag := basereg;
  8313. IntermediateCode.AddOffset(result.tag,ToMemoryUnits(system,x.offsetInBits+system.addressSize));
  8314. IntermediateCode.MakeMemory(result.tag,addressType);
  8315. UseIntermediateOperand(result.tag);
  8316. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType= NIL) & (x.kind = SyntaxTree.ValueParameter) THEN
  8317. ReleaseIntermediateOperand(result.tag);
  8318. result.tag := TypeDescriptorAdr(type);
  8319. IF ~newObjectFile THEN
  8320. IntermediateCode.MakeMemory(result.tag,addressType);
  8321. END;
  8322. UseIntermediateOperand(result.tag);
  8323. END;
  8324. IF Trace THEN TraceExit("VisitParameter") END;
  8325. END VisitParameter;
  8326. PROCEDURE DynamicCallOperand(VAR operand: Operand; x: SyntaxTree.Procedure);
  8327. VAR tag,reg,tmp: IntermediateCode.Operand; offset: LONGINT; recordType: SyntaxTree.RecordType; name, stackFrame: Basic.SegmentedName;
  8328. BEGIN
  8329. IF Trace THEN TraceEnter("DynamicCallOperand") END;
  8330. (* left.p: left already emitted *)
  8331. tag := result.op; (* value of pointer to left *)
  8332. (* get type desc *)
  8333. tmp := result.tag;
  8334. IntermediateCode.MakeMemory(tmp,addressType);
  8335. (* get method adr *)
  8336. Reuse1(reg,tmp);
  8337. ReleaseIntermediateOperand(tmp);
  8338. IF backend.cooperative THEN
  8339. recordType := x.scope(SyntaxTree.RecordScope).ownerRecord;
  8340. WHILE recordType.baseType # NIL DO
  8341. recordType := recordType.GetBaseRecord ();
  8342. END;
  8343. GetRecordTypeName (recordType,name);
  8344. Basic.ToSegmentedName ("BaseTypes.StackFrame",stackFrame);
  8345. IF (name = stackFrame) OR HasExplicitTraceMethod (recordType) THEN
  8346. offset := 0;
  8347. ELSE
  8348. offset := 2;
  8349. END;
  8350. Emit(Add(position,reg,tmp,IntermediateCode.Immediate(addressType, ToMemoryUnits(system,system.addressSize *(meta.MethodTableOffset + x.methodNumber + offset)))));
  8351. ELSE
  8352. Emit(Add(position,reg,tmp,IntermediateCode.Immediate(addressType, ToMemoryUnits(system,system.addressSize *(meta.MethodTableOffset - x.methodNumber)))));
  8353. END;
  8354. InitOperand(operand,ModeReference);
  8355. (* then operand.op contains the method adr and operand.tag contains the potential self pointer value *)
  8356. operand.op := reg;
  8357. operand.tag := tag;
  8358. IF Trace THEN TraceExit("DynamicCallOperand") END;
  8359. END DynamicCallOperand;
  8360. PROCEDURE StaticCallOperand(VAR operand: Operand; x: SyntaxTree.Procedure);
  8361. VAR source: IntermediateCode.Section; tag,reg: IntermediateCode.Operand; name:Basic.SegmentedName; sectionType: SHORTINT;
  8362. binary: BinaryCode.Section; bits: SyntaxTree.BinaryCode;
  8363. BEGIN
  8364. IF Trace THEN TraceEnter("StaticCallOperand") END;
  8365. IF x.type(SyntaxTree.ProcedureType).isDelegate THEN
  8366. tag := operand.op;
  8367. ReleaseIntermediateOperand(operand.tag);
  8368. ELSE tag := nil
  8369. END;
  8370. IF x.isInline THEN
  8371. sectionType := Sections.InlineCodeSection;
  8372. ELSE
  8373. sectionType := Sections.CodeSection;
  8374. END;
  8375. IF x.externalName # NIL THEN
  8376. Basic.ToSegmentedName(x.externalName^, name);
  8377. IntermediateCode.InitAddress(reg, addressType, name, 0, 0);
  8378. ELSE
  8379. GetCodeSectionNameForSymbol(x, name);
  8380. IF (x.scope.ownerModule = module.module) THEN
  8381. source := NewSection(module.allSections, sectionType, name,x,commentPrintout # NIL);
  8382. ELSIF (sectionType = Sections.InlineCodeSection) & (x.procedureScope.body.code.sourceCode # NIL) THEN
  8383. source := NewSection(module.allSections, sectionType, name,x,commentPrintout # NIL);
  8384. IF source.pc = 0 THEN (* no code yet *)
  8385. source.Emit(Asm(position,x.procedureScope.body.code.sourceCode,NIL,NIL));
  8386. END;
  8387. ELSIF (sectionType = Sections.InlineCodeSection) & (x.procedureScope.body.code.inlineCode # NIL) THEN
  8388. bits := x.procedureScope.body.code.inlineCode;
  8389. source := NewSection(module.allSections, sectionType, name, x, commentPrintout # NIL);
  8390. binary := BinaryCode.NewBinarySection(source.type, source.priority, system.codeUnit, name, FALSE, FALSE);
  8391. binary.CopyBits(bits, 0, bits.GetSize());
  8392. source.SetResolved(binary);
  8393. ELSE
  8394. source := NewSection(module.importedSections, sectionType, name,x,commentPrintout # NIL);
  8395. END;
  8396. IntermediateCode.InitAddress(reg, addressType, source.name , GetFingerprint(source.symbol), 0);
  8397. END;
  8398. InitOperand(operand,ModeValue);
  8399. operand.op := reg;
  8400. operand.tag := tag;
  8401. IF Trace THEN TraceExit("StaticCallOperand") END;
  8402. END StaticCallOperand;
  8403. PROCEDURE VisitProcedure(x: SyntaxTree.Procedure);
  8404. (* handle expressions of the form designator.procedure or procedure *)
  8405. BEGIN
  8406. IF Trace THEN TraceEnter("VisitProcedure") END;
  8407. IF (x.type(SyntaxTree.ProcedureType).isDelegate) & ~SemanticChecker.IsStaticProcedure(x) THEN
  8408. DynamicCallOperand(result,x);
  8409. ELSIF x.isInline THEN
  8410. StaticCallOperand(result,x);
  8411. ELSE
  8412. StaticCallOperand(result,x);
  8413. END;
  8414. IF Trace THEN TraceExit("VisitProcedure") END;
  8415. END VisitProcedure;
  8416. PROCEDURE VisitOperator(x: SyntaxTree.Operator);
  8417. BEGIN
  8418. VisitProcedure(x);
  8419. END VisitOperator;
  8420. (** statements *)
  8421. PROCEDURE VisitProcedureCallStatement(x: SyntaxTree.ProcedureCallStatement);
  8422. BEGIN
  8423. IF Trace THEN TraceEnter("VisitProcedureCallStatement") END;
  8424. Expression(x.call);
  8425. IF (x.call.type # NIL) THEN (* WINAPI call: procedure returning unused value *)
  8426. ReleaseOperand(result)
  8427. END;
  8428. IF Trace THEN TraceExit("VisitProcedureCallStatement") END;
  8429. END VisitProcedureCallStatement;
  8430. PROCEDURE AssignMathArray(left,right: SyntaxTree.Expression);
  8431. VAR leftType, rightType: SyntaxTree.MathArrayType;
  8432. leftBase, rightBase: SyntaxTree.Type;
  8433. procedureName,s: SyntaxTree.IdentifierString;
  8434. arrayBase: SyntaxTree.Module; saved: RegisterEntry; procedure: SyntaxTree.Procedure; parameter: SyntaxTree.Parameter;
  8435. size: LONGINT; rightKind: LONGINT;
  8436. dummy: IntermediateCode.Operand;
  8437. CONST moduleName = "FoxArrayBase";
  8438. PROCEDURE OpenArray(from: SyntaxTree.MathArrayType): SyntaxTree.MathArrayType;
  8439. VAR result: SyntaxTree.MathArrayType; base: SyntaxTree.Type;
  8440. BEGIN
  8441. base := from(SyntaxTree.MathArrayType).arrayBase.resolved;
  8442. IF base IS SyntaxTree.MathArrayType THEN
  8443. base := OpenArray(base(SyntaxTree.MathArrayType));
  8444. END;
  8445. result := SyntaxTree.NewMathArrayType(left.position,currentScope,SyntaxTree.Open);
  8446. result.SetArrayBase(base);
  8447. RETURN result
  8448. END OpenArray;
  8449. BEGIN
  8450. IF AddImport(moduleName,arrayBase,TRUE) THEN
  8451. SaveRegisters();ReleaseUsedRegisters(saved);
  8452. leftType := left.type.resolved(SyntaxTree.MathArrayType);
  8453. rightType := right.type.resolved(SyntaxTree.MathArrayType);
  8454. leftBase := SemanticChecker.ArrayBase(leftType,MAX(LONGINT));
  8455. rightBase := SemanticChecker.ArrayBase(rightType,MAX(LONGINT));
  8456. ASSERT(leftBase.resolved.SameType(rightBase.resolved));
  8457. IF leftType.form = SyntaxTree.Tensor THEN
  8458. procedureName := "CopyTensor"; rightKind := SyntaxTree.ValueParameter;
  8459. ELSIF leftType.form = SyntaxTree.Open THEN
  8460. procedureName := "CopyArray"; rightKind := SyntaxTree.VarParameter;
  8461. ELSIF leftType.form = SyntaxTree.Static THEN
  8462. procedureName := "CopyArray";rightKind := SyntaxTree.VarParameter;
  8463. leftType := OpenArray(leftType); (* necessary since copy procedure presumes an open array *)
  8464. END;
  8465. procedure := arrayBase.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  8466. IF procedure = NIL THEN
  8467. s := "Instruction not supported on target, emulation procedure ";
  8468. Strings.Append(s,moduleName);
  8469. Strings.Append(s,".");
  8470. Strings.Append(s,procedureName);
  8471. Strings.Append(s," not present");
  8472. Error(position,s);
  8473. ELSE
  8474. parameter := SyntaxTree.NewParameter(left.position,procedure.type(SyntaxTree.ProcedureType),SyntaxTree.NewIdentifier("temp"), SyntaxTree.VarParameter);
  8475. parameter.SetType(leftType);
  8476. parameter.SetAccess(SyntaxTree.Internal);
  8477. PushParameter(left,parameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  8478. parameter.SetKind(rightKind);
  8479. PushParameter(right,parameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  8480. size := ToMemoryUnits(system,system.SizeOf(rightBase));
  8481. Emit(Push(position,IntermediateCode.Immediate(int32,size)));
  8482. StaticCallOperand(result,procedure);
  8483. Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  8484. ReleaseOperand(result);
  8485. END;
  8486. RestoreRegisters(saved);
  8487. END;
  8488. END AssignMathArray;
  8489. VAR modifyAssignmentCounter := 0: LONGINT;
  8490. PROCEDURE ModifyAssignments(CONST value: IntermediateCode.Operand);
  8491. VAR processor,mem,dst: IntermediateCode.Operand;
  8492. BEGIN
  8493. IF value.intValue = true.intValue THEN
  8494. INC(modifyAssignmentCounter);
  8495. IF (modifyAssignmentCounter > 1) THEN RETURN END;
  8496. modifyAssignmentsPC := section.pc;
  8497. ELSE
  8498. DEC(modifyAssignmentCounter);
  8499. IF (modifyAssignmentCounter > 0) THEN RETURN END;
  8500. INC(statCoopModifyAssignments , section.pc - modifyAssignmentsPC);
  8501. END;
  8502. IntermediateCode.InitMemory (processor, IntermediateCode.SignedIntegerType(addressType.sizeInBits), ap, ToMemoryUnits(system, ProcessorOffset * addressType.sizeInBits));
  8503. dst := NewRegisterOperand (addressType);
  8504. Emit(Mov(position,dst, processor));
  8505. IntermediateCode.InitMemory(mem,bool, dst, 0);
  8506. Emit(Mov(position,mem, value));
  8507. ReleaseIntermediateOperand(dst);
  8508. END ModifyAssignments;
  8509. PROCEDURE CopySize(left: SyntaxTree.Expression): IntermediateCode.Operand;
  8510. VAR type: SyntaxTree.Type; procedureType: SyntaxTree.ProcedureType; parameter: SyntaxTree.Parameter;
  8511. BEGIN
  8512. type := left.type.resolved;
  8513. IF (type IS SyntaxTree.RecordType) & (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  8514. parameter := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter);
  8515. procedureType := parameter.ownerType.resolved(SyntaxTree.ProcedureType);
  8516. IF procedureType.returnParameter = parameter THEN
  8517. RETURN IntermediateCode.Memory(addressType, fp, ToMemoryUnits(system, parameter.offsetInBits + system.addressSize));
  8518. END;
  8519. END;
  8520. RETURN IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.SizeOf(type)));
  8521. END CopySize;
  8522. PROCEDURE Assign(left,right: SyntaxTree.Expression);
  8523. VAR
  8524. leftO, rightO: Operand;
  8525. mem, sizeOp: IntermediateCode.Operand;
  8526. leftType, rightType, componentType: SyntaxTree.Type;
  8527. size: LONGINT;
  8528. parameters: SyntaxTree.ExpressionList;
  8529. procedure: SyntaxTree.Procedure;
  8530. call: SyntaxTree.ProcedureCallDesignator;
  8531. designator: SyntaxTree.Designator;
  8532. PROCEDURE CanPassAsResultParameter(right: SyntaxTree.Expression): BOOLEAN;
  8533. VAR procedureType: SyntaxTree.ProcedureType;
  8534. BEGIN
  8535. IF ReturnedAsParameter(right.type) THEN
  8536. IF right IS SyntaxTree.ProcedureCallDesignator THEN
  8537. procedureType := right(SyntaxTree.ProcedureCallDesignator).left.type.resolved(SyntaxTree.ProcedureType);
  8538. RETURN procedureType.callingConvention = SyntaxTree.OberonCallingConvention
  8539. ELSIF right IS SyntaxTree.BuiltinCallDesignator THEN
  8540. WITH right: SyntaxTree.BuiltinCallDesignator DO
  8541. IF right.id = Global.Reshape THEN RETURN TRUE
  8542. END;
  8543. END;
  8544. END;
  8545. END;
  8546. RETURN FALSE
  8547. END CanPassAsResultParameter;
  8548. BEGIN
  8549. ASSERT(left.type # NIL); ASSERT(right.type # NIL);
  8550. leftType := left.type.resolved; rightType:= right.type.resolved;
  8551. IF backend.cooperative & left.NeedsTrace() THEN
  8552. ModifyAssignments(true);
  8553. IF (leftType IS SyntaxTree.RecordType) OR IsStaticArray(leftType) THEN
  8554. Designate(right, rightO);
  8555. Designate(left, leftO);
  8556. ASSERT(leftO.mode = ModeReference);
  8557. TransferToRegister(leftO.op, leftO.op);
  8558. TransferToRegister(rightO.op, rightO.op);
  8559. Emit(Push(position, leftO.op));
  8560. Emit(Push(position, rightO.op));
  8561. CallAssignMethod(leftO.op, rightO.op, left.type);
  8562. Emit(Pop(position, rightO.op));
  8563. Emit(Pop(position, leftO.op));
  8564. sizeOp := CopySize(left);
  8565. Emit(Copy(position,leftO.op,rightO.op,sizeOp));
  8566. ReleaseOperand(leftO);
  8567. ReleaseOperand(rightO);
  8568. ELSE
  8569. Evaluate(right,rightO);
  8570. Designate(left,leftO);
  8571. ASSERT(leftO.mode = ModeReference);
  8572. IF (leftType IS SyntaxTree.ProcedureType) THEN
  8573. (* copy procedure address first *)
  8574. MakeMemory(mem,leftO.op,addressType,0);
  8575. Emit(Mov(position,mem,rightO.op));
  8576. ReleaseIntermediateOperand(mem);
  8577. (* copy pointer address *)
  8578. IntermediateCode.MakeAddress(leftO.tag, addressType);
  8579. CallAssignPointer(leftO.tag, rightO.tag);
  8580. ELSE
  8581. ASSERT(system.SizeOf(left.type) = system.addressSize);
  8582. CallAssignPointer(leftO.op, rightO.op);
  8583. END;
  8584. ReleaseOperand(leftO);
  8585. ReleaseOperand(rightO);
  8586. END;
  8587. ModifyAssignments(false);
  8588. RETURN;
  8589. END;
  8590. IF CanPassAsResultParameter(right) THEN
  8591. procedureResultDesignator := left(SyntaxTree.Designator);
  8592. Expression(right);
  8593. procedureResultDesignator := NIL;
  8594. ELSIF (right IS SyntaxTree.UnaryExpression) & (right(SyntaxTree.UnaryExpression).operator = Scanner.Alias) THEN
  8595. (* left <-- ALIAS OF right: zerocopy *)
  8596. IF GetRuntimeProcedure("FoxArrayBase","ZeroCopy",procedure,TRUE) THEN
  8597. designator := SyntaxTree.NewSymbolDesignator(Diagnostics.Invalid, NIL, procedure);
  8598. designator.SetType(procedure.type);
  8599. parameters := SyntaxTree.NewExpressionList(); parameters.AddExpression(right(SyntaxTree.UnaryExpression).left); parameters.AddExpression(left);
  8600. call := SyntaxTree.NewProcedureCallDesignator(position,designator(SyntaxTree.Designator),parameters);
  8601. VisitProcedureCallDesignator(call(SyntaxTree.ProcedureCallDesignator));
  8602. END;
  8603. ELSIF leftType IS SyntaxTree.RangeType THEN
  8604. (* LHS is of array range type *)
  8605. ASSERT(rightType IS SyntaxTree.RangeType); (* ensured by the checker *)
  8606. Evaluate(right, rightO);
  8607. Designate(left, leftO);(* The order is crucial. Do not reorder emission of left and right *)
  8608. (* first *)
  8609. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, system.longintType), 0);
  8610. Emit(Mov(position,mem, rightO.op));
  8611. ReleaseIntermediateOperand(mem);
  8612. (* last *)
  8613. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, system.longintType), ToMemoryUnits(system, system.SizeOf(system.longintType)));
  8614. Emit(Mov(position,mem, rightO.tag));
  8615. ReleaseIntermediateOperand(mem);
  8616. (* step *)
  8617. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, system.longintType), 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)));
  8618. Emit(Mov(position,mem, rightO.extra));
  8619. ReleaseIntermediateOperand(mem);
  8620. ReleaseOperand(rightO);
  8621. ReleaseOperand(leftO)
  8622. ELSIF leftType IS SyntaxTree.ComplexType THEN
  8623. ASSERT(leftType.SameType(rightType)); (* ensured by the checker *)
  8624. Evaluate(right, rightO);
  8625. Designate(left, leftO); (* The order is crucial. Do not reorder emission of left and right *)
  8626. componentType := leftType(SyntaxTree.ComplexType).componentType;
  8627. (* real part *)
  8628. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, componentType), 0);
  8629. Emit(Mov(position,mem, rightO.op));
  8630. ReleaseIntermediateOperand(mem);
  8631. (* imaginary part *)
  8632. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  8633. Emit(Mov(position,mem, rightO.tag));
  8634. ReleaseIntermediateOperand(mem);
  8635. ReleaseOperand(rightO);
  8636. ReleaseOperand(leftO)
  8637. ELSIF (leftType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.PointerType) OR (leftType IS SyntaxTree.EnumerationType)
  8638. OR (leftType IS SyntaxTree.PortType) THEN
  8639. (* rightO := leftO;*)
  8640. Evaluate(right,rightO);
  8641. (* DO NOT REORDER EMISSION OF LEFT AND RIGHT OPERAND *)
  8642. Designate(left,leftO);
  8643. IF leftO.mode = ModeReference THEN
  8644. MakeMemory(mem,leftO.op,IntermediateCode.GetType(system,left.type),0);
  8645. destination := mem;
  8646. ELSE
  8647. destination := leftO.op;
  8648. END;
  8649. ReleaseOperand(leftO);
  8650. IF destination.mode # IntermediateCode.Undefined THEN
  8651. Emit(Mov(position,destination,rightO.op));
  8652. END;
  8653. ReleaseOperand(rightO);
  8654. ReleaseIntermediateOperand(mem);
  8655. IntermediateCode.InitOperand(destination);
  8656. ELSIF (leftType IS SyntaxTree.ProcedureType) THEN
  8657. Evaluate(right,rightO);
  8658. Designate(left,leftO);
  8659. MakeMemory(mem,leftO.op,addressType,0);
  8660. Emit(Mov(position,mem,rightO.op));
  8661. ReleaseIntermediateOperand(mem);
  8662. IF leftType(SyntaxTree.ProcedureType).isDelegate THEN
  8663. (* delegate *)
  8664. (*
  8665. MakeMemory(leftO.tag,leftO.tag,addressType); no! is already memory
  8666. *)
  8667. Emit(Mov(position,leftO.tag,rightO.tag));
  8668. END;
  8669. ReleaseOperand(leftO);
  8670. ReleaseOperand(rightO);
  8671. ELSIF (leftType IS SyntaxTree.RecordType) THEN
  8672. Designate(right,rightO);
  8673. Designate(left,leftO);
  8674. sizeOp := CopySize(left);
  8675. Emit(Copy(position,leftO.op,rightO.op,sizeOp));
  8676. ReleaseOperand(leftO); ReleaseOperand(rightO);
  8677. ELSIF (leftType IS SyntaxTree.ArrayType) THEN
  8678. IF (rightType IS SyntaxTree.StringType) THEN
  8679. CopyString(left,right);
  8680. 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
  8681. Designate(right,rightO);
  8682. Designate(left,leftO);
  8683. size := ToMemoryUnits(system,system.SizeOf(rightType));
  8684. Emit(Copy(position,leftO.op, rightO.op, IntermediateCode.Immediate(addressType,size)));
  8685. ReleaseOperand(leftO); ReleaseOperand(rightO);
  8686. ELSE
  8687. HALT(201)
  8688. END;
  8689. ELSIF (leftType IS SyntaxTree.MathArrayType) THEN
  8690. AssignMathArray(left,right);
  8691. ELSE
  8692. HALT(200);
  8693. END;
  8694. END Assign;
  8695. PROCEDURE VisitAssignment(x: SyntaxTree.Assignment);
  8696. BEGIN
  8697. IF Trace THEN TraceEnter("VisitAssignment") END;
  8698. Assign(x.left,x.right);
  8699. IF Trace THEN TraceExit("VisitAssignment") END;
  8700. END VisitAssignment;
  8701. PROCEDURE EmitCooperativeSwitch;
  8702. VAR quantum, offset, zero: IntermediateCode.Operand; skip: Label; pc: LONGINT;
  8703. BEGIN
  8704. ASSERT (cooperativeSwitches);
  8705. pc := section.pc;
  8706. IF lastSwitchPC = section.pc THEN RETURN END;
  8707. IntermediateCode.InitMemory (quantum, IntermediateCode.SignedIntegerType(addressType.sizeInBits), ap, ToMemoryUnits(system, QuantumOffset * addressType.sizeInBits));
  8708. IntermediateCode.InitImmediate(offset, quantum.type, section.pc - lastSwitchPC); IntermediateCode.InitImmediate(zero, quantum.type, 0);
  8709. Emit(Sub(position,quantum,quantum, offset)); skip := NewLabel(); BrgeL(skip, quantum, zero);
  8710. lastSwitchPC := section.pc; CallThis(position,"Activities","Switch",0); SetLabel(skip);
  8711. INC(statCoopSwitch, section.pc - pc);
  8712. END EmitCooperativeSwitch;
  8713. PROCEDURE VisitCommunicationStatement(communication: SyntaxTree.CommunicationStatement);
  8714. VAR p0,p1,tmp: SyntaxTree.Expression; s0,s1: Operand; size: LONGINT;
  8715. BEGIN
  8716. p0 := communication.left; p1 := communication.right;
  8717. IF (communication.op = Scanner.ExclamationMark) OR (communication.op = Scanner.LessLess) & (communication.left.type.resolved IS SyntaxTree.PortType) THEN
  8718. Evaluate(p0,s0);
  8719. Evaluate(p1,s1);
  8720. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  8721. Emit(Push(position,s0.op));
  8722. Emit(Push(position,s1.op));
  8723. (*
  8724. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  8725. *)
  8726. IF ~backend.cellsAreObjects THEN
  8727. IF size > ToMemoryUnits(system, system.addressSize) THEN Error(p1.position,"send not implemented for complex data types") END;
  8728. END;
  8729. ReleaseOperand(s0);
  8730. ReleaseOperand(s1);
  8731. IF backend.cellsAreObjects THEN
  8732. CallThis(position,"ActiveCellsRuntime","Send",2);
  8733. ELSE
  8734. CallThis(position,ChannelModuleName,"Send",2);
  8735. END;
  8736. (* ----- RECEIVE ------*)
  8737. ELSE
  8738. IF (communication.op = Scanner.LessLess) & (communication.right.type.resolved IS SyntaxTree.PortType) THEN
  8739. tmp := p0; p0 := p1; p1 := tmp;
  8740. END;
  8741. Evaluate(p0,s0);
  8742. Emit(Push(position,s0.op));
  8743. Designate(p1,s1);
  8744. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  8745. Emit(Push(position,s1.op));
  8746. (*
  8747. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  8748. *)
  8749. IF ~backend.cellsAreObjects THEN
  8750. IF size > ToMemoryUnits(system, system.addressSize) THEN Error(p1.position,"receive not implemented for complex data types") END;
  8751. END;
  8752. ReleaseOperand(s0);
  8753. ReleaseOperand(s1);
  8754. IF backend.cellsAreObjects THEN
  8755. CallThis(position,"ActiveCellsRuntime","Receive",2);
  8756. ELSE
  8757. CallThis(position,ChannelModuleName,"Receive",2)
  8758. END;
  8759. END;
  8760. END VisitCommunicationStatement;
  8761. PROCEDURE VisitIfStatement(x: SyntaxTree.IfStatement);
  8762. VAR end: Label; i,elsifs: LONGINT; elsif: SyntaxTree.IfPart; escape: BOOLEAN;
  8763. PROCEDURE IfPart(if: SyntaxTree.IfPart);
  8764. VAR true, false: Label; condition, value: BOOLEAN;
  8765. BEGIN
  8766. condition := ~SemanticChecker.IsBooleanValue(if.condition, value);
  8767. IF condition THEN
  8768. true := NewLabel();
  8769. false := NewLabel();
  8770. Condition(if.condition,true,false);
  8771. SetLabel(true);
  8772. StatementSequence(if.statements);
  8773. BrL(end);
  8774. SetLabel(false);
  8775. ELSE
  8776. IF value THEN (* always true *)
  8777. escape := TRUE;
  8778. StatementSequence(if.statements);
  8779. (* no branch necessary -- rest skipped *)
  8780. END;
  8781. END;
  8782. END IfPart;
  8783. BEGIN
  8784. IF Trace THEN TraceEnter("VisitIfStatement") END;
  8785. end := NewLabel();
  8786. elsifs := x.ElsifParts();
  8787. IfPart(x.ifPart);
  8788. FOR i := 0 TO elsifs-1 DO
  8789. IF ~escape THEN
  8790. elsif := x.GetElsifPart(i);
  8791. IfPart(elsif);
  8792. END;
  8793. END;
  8794. IF (x.elsePart # NIL) & ~escape THEN
  8795. StatementSequence(x.elsePart);
  8796. END;
  8797. SetLabel(end);
  8798. IF Trace THEN TraceExit("VisitIfStatement") END;
  8799. END VisitIfStatement;
  8800. PROCEDURE WithPart(x: SyntaxTree.WithPart; VAR falseL, endL: Label);
  8801. VAR trueL: Label; res: Operand; recordType: SyntaxTree.RecordType;
  8802. BEGIN
  8803. (*IF x.variable.type.resolved = x.type.resolved THEN
  8804. (* always true, do nothing *)
  8805. ELSE*)
  8806. Designate(x.variable,res);
  8807. IF IsPointerToRecord(x.variable.type,recordType) THEN
  8808. Dereference(res,recordType,IsUnsafePointer(x.variable.type))
  8809. END;
  8810. trueL := NewLabel();
  8811. TypeTest(res.tag,x.type,trueL,falseL);
  8812. ReleaseOperand(res);
  8813. SetLabel(trueL);
  8814. StatementSequence(x.statements);
  8815. BrL(endL);
  8816. END WithPart;
  8817. PROCEDURE VisitWithStatement(x: SyntaxTree.WithStatement);
  8818. VAR endL,falseL: Label;i: LONGINT;
  8819. BEGIN
  8820. IF Trace THEN TraceEnter("VisitWithStatement") END;
  8821. endL := NewLabel();
  8822. FOR i := 0 TO x.WithParts()-1 DO
  8823. falseL := NewLabel();
  8824. WithPart(x.GetWithPart(i),falseL,endL);
  8825. SetLabel(falseL);
  8826. END;
  8827. IF x.elsePart = NIL THEN
  8828. IF ~isUnchecked THEN
  8829. EmitTrap(position,WithTrap);
  8830. END;
  8831. ELSE
  8832. StatementSequence(x.elsePart)
  8833. END;
  8834. SetLabel(endL);
  8835. IF Trace THEN TraceExit("VisitWithStatement") END;
  8836. END VisitWithStatement;
  8837. PROCEDURE VisitCaseStatement(x: SyntaxTree.CaseStatement);
  8838. VAR var: Operand; jmp,res,op,tmp: IntermediateCode.Operand; j,i,size: LONGINT; part: SyntaxTree.CasePart; constant: SyntaxTree.CaseConstant;
  8839. out,else: Label; label: Label;
  8840. fixups: POINTER TO ARRAY OF Label; section: IntermediateCode.Section; name: Basic.SegmentedName; string: ARRAY 32 OF CHAR;
  8841. symbol: SyntaxTree.Symbol;
  8842. BEGIN
  8843. (*! split case statement into if-elsif statements for large case label lists *)
  8844. IF Trace THEN TraceEnter("VisitCaseStatement") END;
  8845. size := x.max-x.min+1;
  8846. IF (size<0) OR (size > 1024*1024) THEN Error(x.position,"implementation restriction: case table size too large"); RETURN
  8847. END;
  8848. Evaluate(x.variable,var);
  8849. ReuseCopy(tmp,var.op);
  8850. ReleaseIntermediateOperand(var.op);
  8851. var.op := tmp;
  8852. Emit(Sub(position,var.op,var.op,IntermediateCode.Immediate(IntermediateCode.GetType(system,x.variable.type),x.min)));
  8853. Convert(var.op,addressType);
  8854. size := x.max-x.min+1;
  8855. else := NewLabel();
  8856. BrgeL(else,var.op,IntermediateCode.Immediate(addressType,size));
  8857. (*
  8858. UniqueId(name,module.module,"case",caseId);
  8859. *)
  8860. string := "@case"; Basic.AppendNumber(string, caseId); INC(caseId);
  8861. Global.GetModuleSegmentedName(module.module, name);
  8862. Basic.SuffixSegmentedName(name,Basic.MakeString(string));
  8863. symbol := SyntaxTree.NewSymbol(name[1]);
  8864. symbol.SetScope(moduleScope);
  8865. NEW(fixups,size); FOR i := 0 TO size-1 DO fixups[i] := NIL END;
  8866. section := NewSection(module.allSections, Sections.ConstSection,name,SyntaxTree.NewSymbol(name[1]),commentPrintout # NIL);
  8867. section.isCaseTable := TRUE;
  8868. IntermediateCode.InitAddress(jmp, addressType, section.name, GetFingerprint(section.symbol), 0);
  8869. ReuseCopy(res,var.op);
  8870. ReleaseOperand(var);
  8871. Emit(Mul(position,res,res,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.addressSize))));
  8872. Emit(Add(position,res,res,jmp));
  8873. IntermediateCode.MakeMemory(res,addressType);
  8874. Emit(Br(position,res));
  8875. ReleaseIntermediateOperand(res);
  8876. out := NewLabel();
  8877. FOR i := 0 TO x.caseParts.Length()-1 DO (* case parts *)
  8878. part := x.GetCasePart(i);
  8879. constant := part.firstConstant;
  8880. label := NewLabel();
  8881. SetLabel(label);
  8882. WHILE(constant # NIL) DO (* case labels for this case part *)
  8883. FOR j := constant.min TO constant.max DO
  8884. fixups[j-x.min] := label;
  8885. END;
  8886. constant := constant.next;
  8887. END;
  8888. StatementSequence(part.statements);
  8889. BrL(out);
  8890. END;
  8891. SetLabel(else);
  8892. FOR i := 0 TO size-1 DO
  8893. IF fixups[i] = NIL THEN
  8894. fixups[i] := else;
  8895. END;
  8896. END;
  8897. IF x.elsePart # NIL THEN
  8898. StatementSequence(x.elsePart);
  8899. ELSIF ~isUnchecked THEN
  8900. EmitTrap(position,CaseTrap);
  8901. END;
  8902. SetLabel(out);
  8903. FOR i := 0 TO size-1 DO
  8904. IntermediateCode.InitAddress(op, addressType, fixups[i].section.name, GetFingerprint(fixups[i].section.symbol), fixups[i].pc);
  8905. section.Emit(Data(position,op));
  8906. END;
  8907. IF Trace THEN TraceExit("VisitCaseStatement") END;
  8908. END VisitCaseStatement;
  8909. PROCEDURE VisitWhileStatement(x: SyntaxTree.WhileStatement);
  8910. VAR start: Label; true,false: Label;
  8911. BEGIN
  8912. IF Trace THEN TraceEnter("VisitWhileStatement") END;
  8913. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8914. start := NewLabel();
  8915. true := NewLabel();
  8916. false := NewLabel();
  8917. SetLabel(start);
  8918. Condition(x.condition,true,false);
  8919. SetLabel(true);
  8920. StatementSequence(x.statements);
  8921. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8922. BrL(start);
  8923. SetLabel(false);
  8924. IF Trace THEN TraceExit("VisitWhileStatement") END;
  8925. END VisitWhileStatement;
  8926. PROCEDURE VisitRepeatStatement(x: SyntaxTree.RepeatStatement);
  8927. VAR false,true: Label;
  8928. BEGIN
  8929. IF Trace THEN TraceEnter("VisitRepeatStatement") END;
  8930. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8931. true := NewLabel();
  8932. false := NewLabel();
  8933. SetLabel(false);
  8934. StatementSequence(x.statements);
  8935. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8936. Condition(x.condition,true,false);
  8937. SetLabel(true);
  8938. IF Trace THEN TraceExit("VisitRepeatStatement") END;
  8939. END VisitRepeatStatement;
  8940. PROCEDURE VisitForStatement(x: SyntaxTree.ForStatement);
  8941. VAR
  8942. binary: SyntaxTree.BinaryExpression; start,true,false : Label; cmp: LONGINT; by: HUGEINT;
  8943. temporaryVariable: SyntaxTree.Variable;
  8944. temporaryVariableDesignator : SyntaxTree.Designator;
  8945. BEGIN
  8946. IF Trace THEN TraceEnter("VisitForStatement") END;
  8947. true := NewLabel();
  8948. false := NewLabel();
  8949. start := NewLabel();
  8950. Assign(x.variable,x.from);
  8951. temporaryVariable := GetTemporaryVariable(x.variable.type, FALSE);
  8952. temporaryVariableDesignator := SyntaxTree.NewSymbolDesignator(SemanticChecker.InvalidPosition, NIL, temporaryVariable);
  8953. temporaryVariableDesignator.SetType(x.variable.type.resolved);
  8954. Assign(temporaryVariableDesignator,x.to);
  8955. IF x.by = NIL THEN by := 1 ELSE by := x.by.resolved(SyntaxTree.IntegerValue).hvalue END;
  8956. IF by > 0 THEN
  8957. cmp := Scanner.LessEqual
  8958. ELSE
  8959. cmp := Scanner.GreaterEqual
  8960. END;
  8961. binary := SyntaxTree.NewBinaryExpression(0,x.variable,temporaryVariableDesignator,cmp);
  8962. binary.SetType(system.booleanType);
  8963. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8964. SetLabel(start);
  8965. Condition(binary,true,false);
  8966. SetLabel(true);
  8967. StatementSequence(x.statements);
  8968. binary := SyntaxTree.NewBinaryExpression(0,x.variable,x.by,Scanner.Plus);
  8969. binary.SetType(x.variable.type);
  8970. Assign(x.variable,binary);
  8971. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8972. BrL(start);
  8973. SetLabel(false);
  8974. IF Trace THEN TraceExit("VisitForStatement") END;
  8975. END VisitForStatement;
  8976. PROCEDURE VisitExitableBlock(x: SyntaxTree.ExitableBlock);
  8977. VAR prevLoop: Label;
  8978. BEGIN
  8979. IF Trace THEN TraceEnter("VisitExitableBlock") END;
  8980. prevLoop := currentLoop;
  8981. currentLoop := NewLabel();
  8982. StatementSequence(x.statements);
  8983. SetLabel(currentLoop);
  8984. currentLoop := prevLoop;
  8985. IF Trace THEN TraceExit("VisitExitableBlock") END;
  8986. END VisitExitableBlock;
  8987. PROCEDURE VisitLoopStatement(x: SyntaxTree.LoopStatement);
  8988. VAR prevLoop,start: Label;
  8989. BEGIN
  8990. IF Trace THEN TraceEnter("VisitLoopStatement") END;
  8991. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8992. start := NewLabel();
  8993. prevLoop := currentLoop;
  8994. SetLabel(start);
  8995. currentLoop := NewLabel();
  8996. StatementSequence(x.statements);
  8997. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8998. BrL(start);
  8999. SetLabel(currentLoop);
  9000. currentLoop := prevLoop;
  9001. IF Trace THEN TraceExit("VisitLoopStatement") END;
  9002. END VisitLoopStatement;
  9003. PROCEDURE VisitExitStatement(x: SyntaxTree.ExitStatement);
  9004. VAR outer: SyntaxTree.Statement;
  9005. BEGIN
  9006. IF Trace THEN TraceEnter("VisitExitStatement") END;
  9007. IF locked THEN (* r if we jump out of an exclusive block *)
  9008. outer := x.outer;
  9009. WHILE ~(outer IS SyntaxTree.ExitableBlock) & ~((outer IS SyntaxTree.StatementBlock) & outer(SyntaxTree.StatementBlock).isExclusive) DO
  9010. outer := outer.outer;
  9011. END;
  9012. IF ~(outer IS SyntaxTree.ExitableBlock) THEN
  9013. Lock(FALSE);
  9014. END;
  9015. END;
  9016. BrL(currentLoop);
  9017. IF Trace THEN TraceExit("VisitExitStatement") END;
  9018. END VisitExitStatement;
  9019. PROCEDURE VisitReturnStatement(x: SyntaxTree.ReturnStatement);
  9020. VAR
  9021. expression, parameterDesignator: SyntaxTree.Expression;
  9022. type, componentType: SyntaxTree.Type;
  9023. res, right: Operand;
  9024. left, mem, reg: IntermediateCode.Operand;
  9025. parameter: SyntaxTree.Parameter;
  9026. procedure: SyntaxTree.Procedure;
  9027. procedureType: SyntaxTree.ProcedureType;
  9028. returnTypeOffset: LONGINT;
  9029. delegate: BOOLEAN;
  9030. map: SymbolMap;
  9031. BEGIN
  9032. IF Trace THEN TraceEnter("VisitReturnStatement") END;
  9033. expression := x.returnValue;
  9034. procedure := currentScope(SyntaxTree.ProcedureScope).ownerProcedure;
  9035. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9036. IF currentIsInline THEN
  9037. IF expression # NIL THEN
  9038. map := currentMapper.Get(NIL);
  9039. IF map # NIL THEN
  9040. Assign(map.to, expression);
  9041. END;
  9042. END;
  9043. BrL(currentInlineExit);
  9044. RETURN;
  9045. END;
  9046. IF expression # NIL THEN
  9047. type := expression.type.resolved;
  9048. IF (expression IS SyntaxTree.ResultDesignator) THEN
  9049. IF locked THEN Lock(FALSE) END;
  9050. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9051. (* "RETURN RESULT" -> no assignment, it is assumed that result has been written to return parameter via structured return type *)
  9052. ELSIF (type IS SyntaxTree.BasicType) & ~(type IS SyntaxTree.RangeType) & ~(type IS SyntaxTree.ComplexType) & ~type.IsPointer() OR (procedureType.callingConvention # SyntaxTree.OberonCallingConvention) THEN
  9053. (* return without structured return parameter *)
  9054. Evaluate(expression,res);
  9055. delegate := (type IS SyntaxTree.ProcedureType) & (type(SyntaxTree.ProcedureType).isDelegate);
  9056. IF locked OR profile THEN
  9057. Emit(Push(position,res.op));
  9058. IF delegate THEN HALT(200) END;
  9059. ReleaseOperand(res);
  9060. IF locked THEN Lock(FALSE) END;
  9061. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9062. reg := NewRegisterOperand(res.op.type);
  9063. Emit(Pop(position,reg));
  9064. Emit(Return(position,reg));
  9065. ReleaseIntermediateOperand(reg);
  9066. ELSE
  9067. Emit(Return(position,res.op));
  9068. ReleaseOperand(res);
  9069. END;
  9070. ELSIF (type IS SyntaxTree.RecordType) OR (type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.RangeType) OR (type IS SyntaxTree.ComplexType) OR type.IsPointer()
  9071. THEN
  9072. (* return using structured return parameter *)
  9073. 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)
  9074. OR SemanticChecker.IsPointerType(type));
  9075. (* parameter := currentScope(SyntaxTree.ProcedureScope).FindParameter(Global.ReturnParameterName); *)
  9076. parameter :=procedureType.returnParameter;
  9077. ASSERT(parameter # NIL);
  9078. returnTypeOffset := parameter.offsetInBits;
  9079. (*
  9080. IF parameter# NIL THEN
  9081. returnTypeOffset := parameter.offsetInBits + system.SizeOfParameter(parameter);
  9082. INC(returnTypeOffset,(-returnTypeOffset) MOD system.AlignmentOf(system.parameterAlignment,parameter.type));
  9083. ELSE
  9084. returnTypeOffset := system.offsetFirstParameter
  9085. END;
  9086. *)
  9087. left := IntermediateCode.Memory(addressType,fp,ToMemoryUnits(system,returnTypeOffset));
  9088. IF type IS SyntaxTree.RangeType THEN
  9089. (* array range type *)
  9090. Evaluate(expression, right);
  9091. MakeMemory(mem, left, IntermediateCode.GetType(system, system.longintType), 0);
  9092. Emit(Mov(position,mem, right.op)); (* first *)
  9093. ReleaseIntermediateOperand(mem);
  9094. MakeMemory(mem, left, IntermediateCode.GetType(system, system.longintType), ToMemoryUnits(system, system.SizeOf(system.longintType)));
  9095. Emit(Mov(position,mem, right.tag)); (* last *)
  9096. ReleaseIntermediateOperand(mem);
  9097. MakeMemory(mem, left, IntermediateCode.GetType(system, system.longintType), 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)));
  9098. Emit(Mov(position,mem, right.extra)); (* step *)
  9099. ReleaseIntermediateOperand(mem);
  9100. ReleaseOperand(right);
  9101. ELSIF type IS SyntaxTree.ComplexType THEN
  9102. Evaluate(expression, right);
  9103. componentType := type(SyntaxTree.ComplexType).componentType;
  9104. MakeMemory(mem, left, IntermediateCode.GetType(system, componentType), 0);
  9105. Emit(Mov(position,mem, right.op)); (* real part *)
  9106. ReleaseIntermediateOperand(mem);
  9107. MakeMemory(mem, left, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  9108. Emit(Mov(position,mem, right.tag)); (* imaginary part *)
  9109. ReleaseIntermediateOperand(mem);
  9110. ReleaseOperand(right);
  9111. ELSE (* covers cases: pointer / record / array *)
  9112. parameter := procedureType.returnParameter;
  9113. checker.SetCurrentScope(currentScope);
  9114. ASSERT(parameter # NIL);
  9115. parameterDesignator := checker.NewSymbolDesignator(expression.position,NIL,parameter);
  9116. Assign(parameterDesignator,expression);
  9117. END;
  9118. ReleaseIntermediateOperand(left);
  9119. IF locked THEN Lock(FALSE) END;
  9120. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9121. ELSIF (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.ProcedureType) THEN
  9122. parameter := procedureType.returnParameter;
  9123. checker.SetCurrentScope(currentScope);
  9124. IF parameter = NIL THEN
  9125. Error(procedure.position, "structured return of parameter of procedure not found");
  9126. ELSE
  9127. parameterDesignator := checker.NewSymbolDesignator(expression.position,NIL,parameter);
  9128. Assign(parameterDesignator,expression);
  9129. END;
  9130. IF locked THEN Lock(FALSE) END;
  9131. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9132. ELSE
  9133. HALT(200);
  9134. END;
  9135. ELSE
  9136. IF locked THEN Lock(FALSE) END;
  9137. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9138. END;
  9139. IF backend.cooperative THEN
  9140. BrL(exitLabel);
  9141. ELSE
  9142. EmitLeave(section, position,procedure.type(SyntaxTree.ProcedureType).callingConvention);
  9143. Emit(Exit(position,procedure.type(SyntaxTree.ProcedureType).pcOffset,procedure.type(SyntaxTree.ProcedureType).callingConvention));
  9144. END;
  9145. IF Trace THEN TraceExit("VisitReturnStatement") END;
  9146. END VisitReturnStatement;
  9147. PROCEDURE MakeAwaitProcedure(x: SyntaxTree.AwaitStatement): SyntaxTree.Procedure;
  9148. VAR procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; procedureScope: SyntaxTree.ProcedureScope;
  9149. identifier: SyntaxTree.Identifier; body: SyntaxTree.Body; returnStatement : SyntaxTree.ReturnStatement;
  9150. statements: SyntaxTree.StatementSequence;
  9151. name, suffix: SyntaxTree.IdentifierString;
  9152. BEGIN
  9153. Strings.IntToStr(awaitProcCounter,suffix);
  9154. Strings.Concat("@AwaitProcedure",suffix,name);
  9155. identifier := SyntaxTree.NewIdentifier(name);
  9156. INC(awaitProcCounter);
  9157. ASSERT(currentScope IS SyntaxTree.ProcedureScope);
  9158. procedureScope := SyntaxTree.NewProcedureScope(currentScope);
  9159. ASSERT(procedureScope.outerScope IS SyntaxTree.ProcedureScope);
  9160. procedure := SyntaxTree.NewProcedure(x.position,identifier,procedureScope);
  9161. procedure.SetAccess(SyntaxTree.Hidden);
  9162. procedure.SetScope(currentScope);
  9163. procedureType := SyntaxTree.NewProcedureType(x.position,currentScope);
  9164. procedureType.SetReturnType(system.booleanType);
  9165. procedure.SetType(procedureType);
  9166. body := SyntaxTree.NewBody(x.position,procedureScope);
  9167. procedureScope.SetBody(body);
  9168. returnStatement := SyntaxTree.NewReturnStatement(x.position,body);
  9169. returnStatement.SetReturnValue(x.condition);
  9170. statements := SyntaxTree.NewStatementSequence();
  9171. statements.AddStatement(returnStatement);
  9172. body.SetStatementSequence(statements);
  9173. currentScope.AddProcedure(procedure);
  9174. RETURN procedure
  9175. END MakeAwaitProcedure;
  9176. PROCEDURE VisitAwaitStatement(x: SyntaxTree.AwaitStatement);
  9177. VAR proc: SyntaxTree.Procedure; res: IntermediateCode.Operand; symbol: Sections.Section;
  9178. call: IntermediateCode.Operand; label, start, true, false: Label; name: Basic.SegmentedName;
  9179. BEGIN
  9180. IF Trace THEN TraceEnter("VisitAwaitStatement") END;
  9181. IF profile THEN ProfilerEnterExit(numberProcedures, FALSE) END;
  9182. IF backend.cooperative THEN
  9183. start := NewLabel();
  9184. true := NewLabel();
  9185. false := NewLabel();
  9186. SetLabel(start);
  9187. Condition(x.condition,true,false);
  9188. SetLabel(false);
  9189. PushSelfPointer();
  9190. CallThis(position,"ExclusiveBlocks","Await",1);
  9191. BrL(start);
  9192. SetLabel(true);
  9193. PushSelfPointer();
  9194. CallThis(position,"ExclusiveBlocks","FinalizeAwait",1);
  9195. ELSE
  9196. proc := MakeAwaitProcedure(x);
  9197. Emit(Push(position,fp));
  9198. GetCodeSectionNameForSymbol(proc,name);
  9199. symbol := NewSection(module.allSections, Sections.CodeSection, name,proc,commentPrintout # NIL);
  9200. IntermediateCode.InitAddress(call,addressType,name, GetFingerprint(proc), 0);
  9201. res := NewRegisterOperand(IntermediateCode.GetType(system,system.booleanType));
  9202. Emit(Call(position,call,ProcedureParametersSize(system,proc)));
  9203. Emit(Result(position,res));
  9204. (*
  9205. AcquireThisRegister(IntermediateCode.GetType(system,system.booleanType),IntermediateCode.Result);
  9206. IntermediateCode.InitRegister(res,IntermediateCode.GetType(system,system.booleanType),IntermediateCode.Result);
  9207. *)
  9208. InitOperand(result,ModeValue);
  9209. result.op := res;
  9210. label := NewLabel();
  9211. BreqL(label, result.op, SELF.true);
  9212. ReleaseOperand(result);
  9213. symbol := NewSection(module.allSections, Sections.CodeSection, name,proc,commentPrintout # NIL);
  9214. IntermediateCode.InitAddress(res, addressType, name,GetFingerprint(proc), 0);
  9215. Emit(Push(position,res));
  9216. Emit(Push(position,fp));
  9217. PushSelfPointer();
  9218. Emit(Push(position,nil));
  9219. CallThis(position,"Objects","Await",4);
  9220. SetLabel(label);
  9221. END;
  9222. IF profile THEN ProfilerEnterExit(numberProcedures, TRUE) END;
  9223. IF Trace THEN TraceExit("VisitAwaitStatement") END;
  9224. END VisitAwaitStatement;
  9225. PROCEDURE StatementSequence(x: SyntaxTree.StatementSequence);
  9226. VAR statement: SyntaxTree.Statement; i: LONGINT; (* pos: LONGINT; *)
  9227. BEGIN
  9228. FOR i := 0 TO x.Length() - 1 DO
  9229. statement := x.GetStatement( i );
  9230. Statement(statement);
  9231. IF cooperativeSwitches & (section.pc - lastSwitchPC > 1000) THEN EmitCooperativeSwitch END;
  9232. END;
  9233. END StatementSequence;
  9234. PROCEDURE PushSelfPointer;
  9235. VAR scope: SyntaxTree.Scope; op: Operand; moduleSection: IntermediateCode.Section; moduleOffset, parametersSize: LONGINT;
  9236. BEGIN
  9237. scope := currentScope;
  9238. WHILE(scope.outerScope IS SyntaxTree.ProcedureScope) DO
  9239. scope := scope.outerScope;
  9240. END;
  9241. IF scope.outerScope IS SyntaxTree.ModuleScope THEN
  9242. IF ~newObjectFile THEN
  9243. Symbol(moduleSelf,op);
  9244. IntermediateCode.MakeMemory(op.op,addressType);
  9245. ELSE
  9246. moduleSection := meta.ModuleSection();
  9247. IF backend.cooperative THEN
  9248. moduleOffset := 0;
  9249. ELSE
  9250. moduleOffset := moduleSection.pc;
  9251. END;
  9252. op.op := IntermediateCode.Address(addressType, moduleSection.name, GetFingerprint(moduleSection.symbol), moduleOffset);
  9253. END;
  9254. ELSE
  9255. GetBaseRegister(op.op,currentScope,scope);
  9256. parametersSize := ProcedureParametersSize(system,scope(SyntaxTree.ProcedureScope).ownerProcedure);
  9257. IntermediateCode.AddOffset(op.op,ToMemoryUnits(system,addressType.sizeInBits)+parametersSize);
  9258. IF backend.cooperative THEN
  9259. IntermediateCode.AddOffset(op.op,ToMemoryUnits(system,addressType.sizeInBits));
  9260. END;
  9261. IntermediateCode.MakeMemory(op.op,addressType);
  9262. END;
  9263. Emit(Push(position,op.op));
  9264. ReleaseOperand(op);
  9265. END PushSelfPointer;
  9266. PROCEDURE Lock(lock: BOOLEAN);
  9267. BEGIN
  9268. IF Trace THEN TraceEnter("Lock") END;
  9269. IF profile THEN ProfilerEnterExit(numberProcedures, FALSE) END;
  9270. CheckRegistersFree(); (* no register may be in use as operands should not be preserved over the lock / unlock boundary *)
  9271. ASSERT(modifyAssignmentCounter = 0);
  9272. IF dump # NIL THEN
  9273. IF lock THEN dump.String("lock") ELSE dump.String("unlock") END;
  9274. dump.Ln;dump.Update;
  9275. END;
  9276. PushSelfPointer;
  9277. IF backend.cooperative THEN
  9278. Emit(Push(position,IntermediateCode.Immediate(sizeType, 1)));
  9279. IF lock THEN CallThis(position,"ExclusiveBlocks","Enter",2)
  9280. ELSE CallThis(position,"ExclusiveBlocks","Exit",2);
  9281. END;
  9282. ELSE
  9283. Emit(Push(position,true));
  9284. IF lock THEN CallThis(position,"Objects","Lock",2)
  9285. ELSE CallThis(position,"Objects","Unlock",2);
  9286. END;
  9287. END;
  9288. IF profile THEN ProfilerEnterExit(numberProcedures, TRUE) END;
  9289. IF Trace THEN TraceExit("Lock") END;
  9290. END Lock;
  9291. PROCEDURE VisitStatementBlock(x: SyntaxTree.StatementBlock);
  9292. VAR previouslyUnchecked, previouslyCooperativeSwitches: BOOLEAN;
  9293. BEGIN
  9294. IF Trace THEN TraceEnter("VisitStatementBlock") END;
  9295. IF emitLabels THEN Emit(LabelInstruction(x.position)) END;
  9296. previouslyUnchecked := isUnchecked;
  9297. isUnchecked := isUnchecked OR x.isUnchecked;
  9298. previouslyCooperativeSwitches := cooperativeSwitches;
  9299. cooperativeSwitches := cooperativeSwitches & ~x.isUncooperative;
  9300. IF x.isExclusive THEN Lock(TRUE); ASSERT(~locked); locked := TRUE; END;
  9301. IF x.statements # NIL THEN
  9302. StatementSequence(x.statements);
  9303. END;
  9304. IF x.isExclusive THEN Lock(FALSE); ASSERT(locked); locked := FALSE; END;
  9305. isUnchecked := previouslyUnchecked;
  9306. cooperativeSwitches := previouslyCooperativeSwitches;
  9307. IF Trace THEN TraceExit("VisitStatementBlock") END;
  9308. END VisitStatementBlock;
  9309. PROCEDURE VisitCode(x: SyntaxTree.Code);
  9310. VAR (* inline: Sections.CellNet; symbol: SyntaxTree.Symbol; *)
  9311. in, out: IntermediateCode.Rules; statement: SyntaxTree.Statement; i: LONGINT; operand,par: Operand; str: POINTER TO ARRAY OF CHAR;
  9312. result, mem: IntermediateCode.Operand; scope: SyntaxTree.Scope; procedureType: SyntaxTree.ProcedureType; return: IntermediateCode.Operand;
  9313. procedure: SyntaxTree.Procedure;
  9314. map: SymbolMap;
  9315. BEGIN
  9316. scope := currentScope;
  9317. WHILE ~(scope IS SyntaxTree.ProcedureScope) DO scope := scope.outerScope END;
  9318. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  9319. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9320. return := emptyOperand;
  9321. IF Trace THEN TraceEnter("VisitCode") END;
  9322. IF (x.inRules # NIL) & (x.inRules.Length()>0) THEN
  9323. NEW(in, x.inRules.Length());
  9324. FOR i := 0 TO LEN(in)-1 DO
  9325. statement := x.inRules.GetStatement(i);
  9326. WITH statement: SyntaxTree.Assignment DO
  9327. Evaluate(statement.right, operand);
  9328. result := operand.op;
  9329. NEW(str, 64);
  9330. Basic.GetString(statement.left(SyntaxTree.IdentifierDesignator).identifier, str^);
  9331. in[i] := result; IntermediateCode.SetString(in[i], str);
  9332. ReleaseIntermediateOperand(operand.tag);
  9333. END;
  9334. END;
  9335. ELSE in := NIL
  9336. END;
  9337. IF (x.outRules # NIL) & (x.outRules.Length()>0) THEN
  9338. NEW(out, x.outRules.Length());
  9339. FOR i := 0 TO LEN(out)-1 DO
  9340. statement := x.outRules.GetStatement(i);
  9341. IF statement IS SyntaxTree.StatementBlock THEN statement := statement(SyntaxTree.StatementBlock).statements.GetStatement(0) END;
  9342. WITH statement: SyntaxTree.Assignment DO
  9343. Evaluate(statement.left, operand); (*?? or designate *)
  9344. result := operand.op;
  9345. NEW(str, 64);
  9346. Basic.GetString(statement.right(SyntaxTree.IdentifierDesignator).identifier, str^);
  9347. out[i] := result; IntermediateCode.SetString(out[i], str);
  9348. ReleaseIntermediateOperand(operand.tag);
  9349. |statement: SyntaxTree.ReturnStatement DO
  9350. NEW(str, 64);
  9351. Basic.GetString(statement.returnValue(SyntaxTree.IdentifierDesignator).identifier, str^);
  9352. IF currentIsInline THEN
  9353. map := currentMapper.Get(NIL);
  9354. Evaluate(map.to, operand);
  9355. out[i] := operand.op;
  9356. ELSE
  9357. out[i] :=NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  9358. END;
  9359. IntermediateCode.SetString(out[i], str);
  9360. ReleaseIntermediateOperand(operand.tag);
  9361. return := out[i];
  9362. ELSE
  9363. END;
  9364. END;
  9365. ELSE out := NIL
  9366. END;
  9367. Emit(Asm(x.position,x.sourceCode, in, out));
  9368. IF in # NIL THEN
  9369. FOR i := 0 TO LEN(in)-1 DO
  9370. ReleaseIntermediateOperand(in[i]);
  9371. END;
  9372. END;
  9373. IF out # NIL THEN
  9374. FOR i := 0 TO LEN(out)-1 DO
  9375. WITH statement: SyntaxTree.Assignment DO
  9376. ReleaseIntermediateOperand(out[i]);
  9377. ELSE
  9378. IF currentIsInline THEN
  9379. ReleaseIntermediateOperand(out[i]);
  9380. END;
  9381. END;
  9382. statement := x.outRules.GetStatement(i);
  9383. END;
  9384. END;
  9385. IF return.mode # IntermediateCode.Undefined THEN
  9386. IF currentIsInline THEN
  9387. ELSIF ReturnedAsParameter(procedureType.returnType) THEN
  9388. Symbol(procedureType.returnParameter, par);
  9389. MakeMemory(mem, par.op, return.type, 0);
  9390. ReleaseOperand(par);
  9391. Emit(Mov(position, mem, return));
  9392. ReleaseIntermediateOperand(mem);
  9393. ELSE
  9394. Emit(Return(position,return));
  9395. END;
  9396. IF currentIsInline THEN RETURN END;
  9397. EmitLeave(section, position,procedureType(SyntaxTree.ProcedureType).callingConvention);
  9398. Emit(Exit(position,procedureType(SyntaxTree.ProcedureType).pcOffset,procedureType(SyntaxTree.ProcedureType).callingConvention));
  9399. ReleaseIntermediateOperand(return);
  9400. END;
  9401. IF Trace THEN TraceExit("VisitCode") END;
  9402. END VisitCode;
  9403. PROCEDURE ParameterCopies(x: SyntaxTree.ProcedureType);
  9404. VAR parameter: SyntaxTree.Parameter; type, base: SyntaxTree.Type;
  9405. op: Operand; temp,size,par,dst, length,null: IntermediateCode.Operand;
  9406. const, call: IntermediateCode.Operand;
  9407. parameterDesignator: SyntaxTree.Expression;
  9408. saved: RegisterEntry;
  9409. name: Basic.SegmentedName;
  9410. BEGIN
  9411. IF Trace THEN TraceEnter("ParameterCopies") END;
  9412. parameter := x.firstParameter;
  9413. WHILE parameter # NIL DO
  9414. IF parameter.kind = SyntaxTree.ValueParameter THEN
  9415. type := parameter.type.resolved;
  9416. IF IsOpenArray(type) THEN
  9417. VisitParameter(parameter);
  9418. op := result;
  9419. IF backend.cooperative & parameter.NeedsTrace() THEN
  9420. length := GetArrayLength(type, op.tag);
  9421. size := NewRegisterOperand(addressType);
  9422. base := ArrayBaseType(type);
  9423. const := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.SizeOf(base)));
  9424. Emit(Mul(position, size, length, const));
  9425. dst := NewRegisterOperand (addressType);
  9426. Emit(Mov(position, dst, size));
  9427. const := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,-system.addressSize)); (* alignment *)
  9428. Emit(Sub(position,dst,sp,dst));
  9429. Emit(And(position,dst,dst,const));
  9430. Emit(Mov(position,sp,dst));
  9431. par := fp;
  9432. IntermediateCode.AddOffset(par,ToMemoryUnits(system,parameter.offsetInBits));
  9433. IntermediateCode.InitImmediate(null, byteType, 0);
  9434. Emit(Fill(position, dst, size, null));
  9435. ReleaseIntermediateOperand(dst);
  9436. ReleaseIntermediateOperand(length);
  9437. SaveRegisters();ReleaseUsedRegisters(saved);
  9438. (* register dst has been freed before SaveRegisters already *)
  9439. base := ArrayBaseType(type);
  9440. (* assign method of open array *)
  9441. IF base.IsRecordType() THEN
  9442. Emit (Push(position, length));
  9443. Emit (Push(position, dst));
  9444. Emit (Push(position, op.op));
  9445. GetRecordTypeName (base.resolved(SyntaxTree.RecordType),name);
  9446. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  9447. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  9448. IntermediateCode.InitAddress(call, addressType, name , 0, 0);
  9449. Emit(Call(position,call,ToMemoryUnits(system, 3*system.addressSize)));
  9450. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  9451. Emit (Push(position,length));
  9452. Emit (Push(position, dst));
  9453. Emit (Push(position, length));
  9454. Emit (Push(position, op.op));
  9455. CallThis(position,"GarbageCollector","AssignDelegateArray", 4);
  9456. ELSE
  9457. Emit (Push(position, length));
  9458. Emit (Push(position, dst));
  9459. Emit (Push(position, length));
  9460. Emit (Push(position, op.op));
  9461. CallThis(position,"GarbageCollector","AssignPointerArray", 4);
  9462. ASSERT(ArrayBaseType(type).IsPointer());
  9463. END;
  9464. RestoreRegisters(saved);
  9465. ELSE
  9466. temp := GetDynamicSize(type,op.tag);
  9467. ReuseCopy(size,temp);
  9468. ReleaseIntermediateOperand(temp);
  9469. const := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,-system.addressSize)); (* alignment *)
  9470. Emit(Sub(position,size,sp,size));
  9471. Emit(And(position,size,size,const));
  9472. Emit(Mov(position,sp,size));
  9473. par := fp;
  9474. IntermediateCode.AddOffset(par,ToMemoryUnits(system,parameter.offsetInBits));
  9475. ReleaseIntermediateOperand(size);
  9476. size := GetDynamicSize(type,op.tag);
  9477. END;
  9478. Emit(Copy(position,sp,op.op,size));
  9479. ReleaseIntermediateOperand(size);
  9480. ReleaseOperand(op);
  9481. IntermediateCode.MakeMemory(par,addressType);
  9482. Emit(Mov(position,par,sp));
  9483. ELSIF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  9484. checker.SetCurrentScope(currentScope);
  9485. parameterDesignator := checker.NewSymbolDesignator(position,NIL,parameter);
  9486. Assign(parameterDesignator,parameterDesignator);
  9487. END;
  9488. END;
  9489. parameter := parameter.nextParameter;
  9490. END;
  9491. IF Trace THEN TraceExit("ParameterCopies") END;
  9492. END ParameterCopies;
  9493. PROCEDURE InitVariables(scope: SyntaxTree.Scope);
  9494. VAR x: SyntaxTree.Variable;
  9495. BEGIN
  9496. x := scope.firstVariable;
  9497. WHILE x # NIL DO
  9498. InitVariable(x);
  9499. x := x.nextVariable;
  9500. END;
  9501. END InitVariables;
  9502. PROCEDURE GetFingerprint(symbol: SyntaxTree.Symbol): LONGINT;
  9503. BEGIN
  9504. IF (symbol # NIL) THEN
  9505. RETURN fingerPrinter.SymbolFP(symbol).shallow
  9506. ELSE
  9507. RETURN 0
  9508. END;
  9509. END GetFingerprint;
  9510. PROCEDURE Body(x: SyntaxTree.Body; scope: SyntaxTree.Scope; ir: IntermediateCode.Section; moduleBody: BOOLEAN);
  9511. VAR prevScope: SyntaxTree.Scope; procedureType: SyntaxTree.ProcedureType; procedure: SyntaxTree.Procedure;
  9512. cellScope: SyntaxTree.CellScope; op: Operand; string: SyntaxTree.IdentifierString;
  9513. saved: RegisterEntry;
  9514. BEGIN
  9515. IF Trace THEN TraceEnter("Body") END;
  9516. ReleaseUsedRegisters(saved); (* just in case ... *)
  9517. section := ir;
  9518. exitLabel := NewLabel ();
  9519. IF moduleBody THEN moduleBodySection := section END;
  9520. IF ir.comments # NIL THEN
  9521. commentPrintout := Printout.NewPrinter(ir.comments,Printout.SourceCode,FALSE);
  9522. commentPrintout.SingleStatement(TRUE);
  9523. dump := ir.comments;
  9524. ELSE
  9525. commentPrintout := NIL;
  9526. dump := NIL;
  9527. END;
  9528. prevScope := currentScope;
  9529. currentScope := scope;
  9530. lastSwitchPC := 0;
  9531. cooperativeSwitches := backend.cooperative;
  9532. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  9533. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9534. IF x # NIL THEN
  9535. IF emitLabels THEN Emit(LabelInstruction(x.position)) END;
  9536. IF profile & (x.code = NIL) THEN (* do not profile assembler code sections *)
  9537. IF moduleBody THEN
  9538. ProfilerInit();
  9539. ELSE
  9540. Basic.SegmentedNameToString(ir.name, string);
  9541. ProfilerAddProcedure(numberProcedures,string);
  9542. ProfilerEnterExit(numberProcedures,TRUE);
  9543. END;
  9544. END;
  9545. IF moduleBody & (operatorInitializationCodeSection # NIL) THEN
  9546. Emit(Call(position,IntermediateCode.Address(addressType, operatorInitializationCodeSection.name, GetFingerprint(operatorInitializationCodeSection.symbol), 0), 0))
  9547. END;
  9548. section.SetPositionOrAlignment(procedure.fixed, procedure.alignment);
  9549. IF moduleBody THEN
  9550. InitVariables(moduleScope)
  9551. END;
  9552. IF (scope.outerScope # NIL) & (scope.outerScope IS SyntaxTree.CellScope) THEN
  9553. cellScope := scope.outerScope(SyntaxTree.CellScope);
  9554. IF procedure = cellScope.bodyProcedure THEN
  9555. IF (cellScope.constructor # NIL) & ~backend.cellsAreObjects THEN
  9556. StaticCallOperand(op, cellScope.constructor);
  9557. Emit(Call(position,op.op,0));
  9558. END;
  9559. END;
  9560. END;
  9561. ParameterCopies(procedureType);
  9562. InitVariables(scope);
  9563. IF x.code = NIL THEN
  9564. VisitStatementBlock(x);
  9565. ELSE
  9566. VisitCode(x.code)
  9567. END;
  9568. IF x.finally # NIL THEN (*! mark finally block for object file *)
  9569. ir.SetFinally(ir.pc);
  9570. StatementSequence(x.finally)
  9571. END;
  9572. IF profile & (x.code = NIL) & ~moduleBody THEN (* do not profile assembler code sections *)
  9573. IF ~backend.cooperative THEN
  9574. ProfilerEnterExit(numberProcedures,FALSE);
  9575. END;
  9576. INC(numberProcedures);
  9577. END;
  9578. END;
  9579. IF backend.cooperative THEN
  9580. IF HasPointers (procedure.procedureScope) THEN CreateResetMethod (procedure.procedureScope) END;
  9581. IF HasPointers (procedure.procedureScope) OR HasVariableParameters (procedure.procedureScope) OR IsNested (procedure) THEN CreateProcedureDescriptor (procedure) END;
  9582. ELSIF newObjectFile & moduleBody & ~suppressModuleRegistration & ~meta.simple THEN
  9583. (*! not required any more? check and delete!
  9584. PushSelfPointer();
  9585. CallThis(position,"Modules","SetInitialized",1);
  9586. *)
  9587. (*
  9588. SetLabel(end);
  9589. *)
  9590. END;
  9591. IF x # NIL THEN
  9592. SELF.position := x.position;
  9593. END;
  9594. CheckRegistersFree();
  9595. ASSERT(modifyAssignmentCounter = 0);
  9596. currentScope := prevScope;
  9597. IF Trace THEN TraceExit("Body") END;
  9598. END Body;
  9599. END ImplementationVisitor;
  9600. MetaDataGenerator=OBJECT
  9601. VAR
  9602. implementationVisitor: ImplementationVisitor;
  9603. declarationVisitor: DeclarationVisitor;
  9604. module: Sections.Module;
  9605. moduleName: ARRAY 128 OF CHAR;
  9606. simple: BOOLEAN; (* simple = no methods, no module loading, no reflection *)
  9607. MethodTableOffset: LONGINT; (* method table offset from zero *)
  9608. BaseTypesTableOffset: LONGINT; (* table with all record extensions offset *)
  9609. TypeTags: LONGINT; (* type extension level support *)
  9610. TypeRecordBaseOffset: LONGINT; (* offset of type zero offset (without method entries) *)
  9611. PROCEDURE &InitMetaDataGenerator(implementationVisitor: ImplementationVisitor; declarationVisitor: DeclarationVisitor; simple: BOOLEAN);
  9612. BEGIN
  9613. IF implementationVisitor.backend.cooperative THEN
  9614. TypeTags := MAX(LONGINT); (* only 3 extensions allowed *)
  9615. BaseTypesTableOffset := 0;
  9616. MethodTableOffset := 2;
  9617. TypeRecordBaseOffset := 0;
  9618. ELSIF simple THEN
  9619. TypeTags := 3; (* only 3 extensions allowed *)
  9620. BaseTypesTableOffset := -1;
  9621. MethodTableOffset := -TypeTags+BaseTypesTableOffset;
  9622. TypeRecordBaseOffset := TypeTags;
  9623. ELSE
  9624. TypeTags := 16;
  9625. BaseTypesTableOffset := -2; (* typeInfo and size field *)
  9626. MethodTableOffset := -TypeTags+BaseTypesTableOffset;
  9627. TypeRecordBaseOffset := TypeTags + 2; (* MPO, typeInfo *)
  9628. END;
  9629. SELF.simple := simple;
  9630. SELF.implementationVisitor := implementationVisitor;
  9631. SELF.declarationVisitor := declarationVisitor;
  9632. implementationVisitor.meta := SELF;
  9633. declarationVisitor.meta := SELF;
  9634. END InitMetaDataGenerator;
  9635. PROCEDURE SetModule(module: Sections.Module);
  9636. BEGIN
  9637. SELF.module := module
  9638. END SetModule;
  9639. PROCEDURE GetTypeRecordBaseOffset(numberMethods: LONGINT): LONGINT;
  9640. BEGIN
  9641. IF implementationVisitor.backend.cooperative THEN RETURN 0 ELSE RETURN TypeRecordBaseOffset + numberMethods END;
  9642. END GetTypeRecordBaseOffset;
  9643. PROCEDURE HeapBlock(CONST moduleName, typeName: ARRAY OF CHAR; section: IntermediateCode.Section; dataAdrOffset: LONGINT);
  9644. VAR offset: LONGINT; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol;
  9645. BEGIN
  9646. INC(dataAdrOffset,6);
  9647. Info(section,"headerAdr");
  9648. Address(section,0);
  9649. Info(section,"typeDesc");
  9650. symbol := implementationVisitor.GetTypeDescriptor(moduleName,typeName, name);
  9651. offset := ToMemoryUnits(module.system,TypeRecordBaseOffset*module.system.addressSize);
  9652. NamedSymbol(section, name, symbol, 0, offset);
  9653. Info(section,"mark: LONGINT;");
  9654. Longint(section,-1);
  9655. Info(section,"dataAdr-: ADDRESS");
  9656. Symbol(section,section, dataAdrOffset,0);
  9657. Info(section,"size-: SIZE");
  9658. Address(section,0);
  9659. Info(section,"nextRealtime: HeapBlock;");
  9660. Address(section,0);
  9661. END HeapBlock;
  9662. PROCEDURE ProtectedHeapBlock(CONST moduleName, typeName: ARRAY OF CHAR; section: IntermediateCode.Section; dataAdrOffset: LONGINT);
  9663. VAR i: LONGINT;
  9664. BEGIN
  9665. INC(dataAdrOffset,14);
  9666. HeapBlock(moduleName,typeName,section,dataAdrOffset);
  9667. Info(section,"count*: LONGINT");
  9668. Longint(section,0);
  9669. Info(section,"locked*: BOOLEAN");
  9670. Longint(section,0);
  9671. Info(section,"awaitingLock*: ProcessQueue");
  9672. Address(section,0);
  9673. Address(section,0);
  9674. Info(section,"awaitingCond*: ProcessQueue");
  9675. Address(section,0);
  9676. Address(section,0);
  9677. Info(section,"lockedBy*: ANY");
  9678. Address(section,0);
  9679. Info(section,"lock*: ANY");
  9680. Address(section,0);
  9681. Info(section,"waitingPriorities*: ARRAY NumPriorities OF LONGINT");
  9682. Longint(section,1);
  9683. FOR i := 2 TO 6 DO
  9684. Longint(section,0);
  9685. END;
  9686. END ProtectedHeapBlock;
  9687. PROCEDURE Info(section: IntermediateCode.Section; CONST s: ARRAY OF CHAR);
  9688. BEGIN
  9689. IF section.comments # NIL THEN section.comments.String(s); section.comments.Ln; section.comments.Update END;
  9690. END Info;
  9691. PROCEDURE Address(section: IntermediateCode.Section; value: LONGINT);
  9692. VAR op: IntermediateCode.Operand;
  9693. BEGIN
  9694. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),value);
  9695. section.Emit(Data(-11,op));
  9696. END Address;
  9697. PROCEDURE Size(section: IntermediateCode.Section; value: LONGINT);
  9698. VAR op: IntermediateCode.Operand;
  9699. BEGIN
  9700. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.sizeType),value);
  9701. section.Emit(Data(-12,op));
  9702. END Size;
  9703. PROCEDURE Set(section: IntermediateCode.Section; value: SET);
  9704. VAR op: IntermediateCode.Operand;
  9705. BEGIN
  9706. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.longintType),SYSTEM.VAL(LONGINT,value));
  9707. section.Emit(Data(-1,op));
  9708. END Set;
  9709. PROCEDURE Longint(section: IntermediateCode.Section; value: LONGINT);
  9710. VAR op: IntermediateCode.Operand;
  9711. BEGIN
  9712. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.longintType),value);
  9713. section.Emit(Data(-1,op));
  9714. END Longint;
  9715. PROCEDURE PatchLongint(section: IntermediateCode.Section; pc: LONGINT; value: LONGINT);
  9716. VAR op,noOperand: IntermediateCode.Operand;
  9717. BEGIN
  9718. IntermediateCode.InitOperand(noOperand);
  9719. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.longintType),value);
  9720. section.PatchOperands(pc,op,noOperand,noOperand);
  9721. END PatchLongint;
  9722. PROCEDURE PatchSymbol(section: IntermediateCode.Section; pc: LONGINT; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol; virtualOffset, realOffset: LONGINT);
  9723. VAR op, noOperand: IntermediateCode.Operand;
  9724. BEGIN
  9725. IntermediateCode.InitOperand(noOperand);
  9726. IntermediateCode.InitAddress(op, IntermediateCode.GetType(module.system, module.system.addressType), name,implementationVisitor.GetFingerprint(symbol), virtualOffset);
  9727. section.PatchOperands(pc,op,noOperand,noOperand);
  9728. END PatchSymbol;
  9729. PROCEDURE Boolean(section: IntermediateCode.Section; value: BOOLEAN);
  9730. VAR op: IntermediateCode.Operand; intValue: LONGINT;
  9731. BEGIN
  9732. IF value = FALSE THEN intValue := 0 ELSE intValue :=1 END;
  9733. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.booleanType),intValue);
  9734. section.Emit(Data(-1,op));
  9735. END Boolean;
  9736. PROCEDURE Char(section: IntermediateCode.Section; char: CHAR);
  9737. VAR op: IntermediateCode.Operand;
  9738. BEGIN
  9739. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.characterType),ORD(char));
  9740. section.Emit(Data(-1,op));
  9741. END Char;
  9742. PROCEDURE String(section: IntermediateCode.Section; CONST str: ARRAY OF CHAR);
  9743. VAR i: LONGINT;
  9744. BEGIN
  9745. Info(section,str);
  9746. i := 0;
  9747. WHILE(str[i] # 0X) DO
  9748. Char(section,str[i]);
  9749. INC(i);
  9750. END;
  9751. Char(section,0X);
  9752. END String;
  9753. PROCEDURE NamedSymbol(section: IntermediateCode.Section; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol; virtualOffset, realOffset: LONGINT);
  9754. VAR op: IntermediateCode.Operand;
  9755. BEGIN
  9756. IntermediateCode.InitAddress(op, IntermediateCode.GetType(module.system, module.system.addressType), name,implementationVisitor.GetFingerprint(symbol), virtualOffset);
  9757. IntermediateCode.SetOffset(op,realOffset);
  9758. section.Emit(Data(-1,op));
  9759. END NamedSymbol;
  9760. PROCEDURE Symbol(section: IntermediateCode.Section; symbol: Sections.Section; virtualOffset, realOffset: LONGINT);
  9761. BEGIN
  9762. IF symbol= NIL THEN
  9763. Address( section, realOffset);
  9764. ASSERT(virtualOffset = 0);
  9765. ELSE
  9766. NamedSymbol(section, symbol.name, symbol.symbol, virtualOffset, realOffset)
  9767. END;
  9768. END Symbol;
  9769. (* OutPointers delivers
  9770. {pointerOffset}
  9771. *)
  9772. PROCEDURE Pointers(offset: LONGINT; symbol: Sections.Section; section: IntermediateCode.Section; type: SyntaxTree.Type; VAR numberPointers: LONGINT);
  9773. VAR variable: SyntaxTree.Variable; i,n,size: LONGINT; base: SyntaxTree.Type;
  9774. BEGIN
  9775. type := type.resolved;
  9776. IF type IS SyntaxTree.AnyType THEN
  9777. Symbol(section, symbol, 0, (offset ));
  9778. INC(numberPointers);
  9779. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1); D.Ln; END;
  9780. ELSIF (type IS SyntaxTree.PortType) & implementationVisitor.backend.cellsAreObjects THEN
  9781. Symbol(section, symbol, 0, offset); INC(numberPointers);
  9782. ELSIF type IS SyntaxTree.PointerType THEN
  9783. Symbol(section, symbol, 0, (offset )); INC(numberPointers);
  9784. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1);D.Ln; END;
  9785. ELSIF (type IS SyntaxTree.ProcedureType) & (type(SyntaxTree.ProcedureType).isDelegate) THEN
  9786. Symbol(section, symbol, 0, (offset )+ToMemoryUnits(module.system,module.system.addressSize)); INC(numberPointers);
  9787. IF Trace THEN D.Str("ptr at offset="); D.Int(offset+ToMemoryUnits(module.system,module.system.addressSize),1); END;
  9788. ELSIF (type IS SyntaxTree.RecordType) THEN
  9789. (* never treat a record like a pointer, even if the pointer field is set! *)
  9790. WITH type: SyntaxTree.RecordType DO
  9791. base := type.GetBaseRecord();
  9792. IF base # NIL THEN
  9793. Pointers(offset,symbol,section, base,numberPointers);
  9794. END;
  9795. variable := type.recordScope.firstVariable;
  9796. WHILE(variable # NIL) DO
  9797. IF ~(variable.untraced) THEN
  9798. Pointers(offset+ToMemoryUnits(module.system,variable.offsetInBits), symbol, section, variable.type,numberPointers);
  9799. END;
  9800. variable := variable.nextVariable;
  9801. END;
  9802. END;
  9803. ELSIF (type IS SyntaxTree.CellType) THEN
  9804. WITH type: SyntaxTree.CellType DO
  9805. base := type.GetBaseRecord();
  9806. IF base # NIL THEN
  9807. Pointers(offset,symbol,section, base,numberPointers);
  9808. END;
  9809. variable := type.cellScope.firstVariable;
  9810. WHILE(variable # NIL) DO
  9811. IF ~(variable.untraced) THEN
  9812. Pointers(offset+ToMemoryUnits(module.system,variable.offsetInBits), symbol, section, variable.type,numberPointers);
  9813. END;
  9814. variable := variable.nextVariable;
  9815. END;
  9816. END;
  9817. ELSIF (type IS SyntaxTree.ArrayType) THEN
  9818. WITH type: SyntaxTree.ArrayType DO
  9819. IF type.form= SyntaxTree.Static THEN
  9820. n := type.staticLength;
  9821. base := type.arrayBase.resolved;
  9822. WHILE(base IS SyntaxTree.ArrayType) DO
  9823. type := base(SyntaxTree.ArrayType);
  9824. n := n* type.staticLength;
  9825. base := type.arrayBase.resolved;
  9826. END;
  9827. size := ToMemoryUnits(module.system,module.system.AlignedSizeOf(base));
  9828. IF SemanticChecker.ContainsPointer(base) THEN
  9829. ASSERT(n<1000000); (* not more than one million pointers on the stack ... *)
  9830. FOR i := 0 TO n-1 DO
  9831. Pointers(offset+i*size, symbol, section, base,numberPointers);
  9832. END;
  9833. END;
  9834. ELSE
  9835. Symbol( section, symbol, 0, (offset )); INC(numberPointers);
  9836. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1); D.Ln; END;
  9837. END;
  9838. END;
  9839. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  9840. WITH type: SyntaxTree.MathArrayType DO
  9841. IF type.form = SyntaxTree.Static THEN
  9842. n := type.staticLength;
  9843. base := type.arrayBase.resolved;
  9844. WHILE(base IS SyntaxTree.MathArrayType) DO
  9845. type := base(SyntaxTree.MathArrayType);
  9846. n := n* type.staticLength;
  9847. base := type.arrayBase.resolved;
  9848. END;
  9849. size := ToMemoryUnits(module.system,module.system.AlignedSizeOf(base));
  9850. IF SemanticChecker.ContainsPointer(base) THEN
  9851. ASSERT(n<1000000); (* not more than one million pointers on the stack ... *)
  9852. FOR i := 0 TO n-1 DO
  9853. Pointers(offset+i*size, symbol, section, base,numberPointers);
  9854. END;
  9855. END;
  9856. ELSE
  9857. Symbol(section, symbol, 0, (offset )); INC(numberPointers); (* GC relevant pointer is at offset 0 *)
  9858. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1); D.Ln; END;
  9859. END
  9860. END;
  9861. (* ELSE no pointers in type *)
  9862. END;
  9863. END Pointers;
  9864. PROCEDURE DynamicName(source: IntermediateCode.Section; name: StringPool.Index; pool: Basic.HashTableInt): LONGINT;
  9865. VAR s: Basic.SectionName; i: LONGINT; ch: CHAR; position: LONGINT;
  9866. BEGIN
  9867. IF pool.Has(name) THEN
  9868. RETURN pool.GetInt(name)
  9869. ELSE
  9870. position := source.pc;
  9871. pool.PutInt(name, position);
  9872. StringPool.GetString(name, s);
  9873. Info(source, s);
  9874. i := 0;
  9875. REPEAT
  9876. ch := s[i]; INC(i);
  9877. Char( source, ch);
  9878. UNTIL ch = 0X;
  9879. END;
  9880. RETURN position;
  9881. END DynamicName;
  9882. PROCEDURE Block(CONST mName, typeName, suffix: ARRAY OF CHAR; VAR offset: LONGINT): IntermediateCode.Section;
  9883. VAR name: ARRAY 128 OF CHAR; section: IntermediateCode.Section; pooledName: Basic.SegmentedName;
  9884. BEGIN
  9885. COPY(moduleName,name);
  9886. Strings.Append(name,suffix);
  9887. Basic.ToSegmentedName(name, pooledName);
  9888. section := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, pooledName, NIL,TRUE);
  9889. IF implementationVisitor.backend.cooperative THEN
  9890. Info(section, "TypeDescriptor");
  9891. Basic.ToSegmentedName("BaseTypes.Array", pooledName);
  9892. NamedSymbol(section, pooledName,NIL, 0, 0);
  9893. BasePointer(section);
  9894. offset := 0;
  9895. ELSE
  9896. HeapBlock(mName,typeName,section,2);
  9897. Info(section, "HeapBlock");
  9898. (*
  9899. Symbol(section,section,2,0);
  9900. *)
  9901. Address(section,0); (* empty such that GC does not go on traversing *)
  9902. Info(section, "TypeDescriptor");
  9903. Address(section,0);
  9904. offset := section.pc;
  9905. END;
  9906. RETURN section
  9907. END Block;
  9908. PROCEDURE Array(source: IntermediateCode.Section; VAR sizePC: LONGINT; CONST baseType: ARRAY OF CHAR);
  9909. VAR name: Basic.SegmentedName;
  9910. BEGIN
  9911. Info(source,"ArrayHeader");
  9912. IF implementationVisitor.backend.cooperative THEN
  9913. sizePC := source.pc;
  9914. Address(source,0);
  9915. NamedSymbol(source,source.name,NIL,0,ToMemoryUnits(implementationVisitor.system,(BaseArrayTypeSize + 1)*implementationVisitor.addressType.sizeInBits));
  9916. IF baseType # "" THEN
  9917. Basic.ToSegmentedName(baseType, name);
  9918. NamedSymbol(source, name,NIL, 0, 0);
  9919. ELSE
  9920. Address(source,0);
  9921. END;
  9922. Address(source,0);
  9923. ELSE
  9924. Address(source,0);
  9925. Address(source,0);
  9926. Address(source,0);
  9927. sizePC := source.pc;
  9928. Address(source,0);
  9929. Info(source,"array data");
  9930. END;
  9931. END Array;
  9932. PROCEDURE PatchArray(section: IntermediateCode.Section; pc: LONGINT; size: LONGINT);
  9933. BEGIN
  9934. IF implementationVisitor.backend.cooperative THEN
  9935. PatchLongint(section, pc, size);
  9936. PatchLongint(section, pc + 3, size);
  9937. ELSE
  9938. PatchLongint(section, pc, size);
  9939. END;
  9940. END PatchArray;
  9941. PROCEDURE ExportDesc(source: IntermediateCode.Section);
  9942. VAR
  9943. i: LONGINT; section: Sections.Section; fingerPrinter : FingerPrinter.FingerPrinter;
  9944. sectionArray: POINTER TO ARRAY OF Sections.Section;
  9945. poolMap: Basic.HashTableInt;
  9946. namePool: IntermediateCode.Section;
  9947. namePoolOffset: LONGINT;
  9948. PROCEDURE Compare(VAR s1, s2: Sections.Section): BOOLEAN;
  9949. VAR n1, n2: Basic.SectionName; index: LONGINT; ch1, ch2: CHAR;
  9950. BEGIN
  9951. Basic.SegmentedNameToString(s1.name,n1);
  9952. Basic.SegmentedNameToString(s2.name,n2);
  9953. index := 0;
  9954. ch1 := n1[index];
  9955. ch2 := n2[index];
  9956. WHILE (ch1 # 0X) & (ch1 = ch2) DO
  9957. INC(index);
  9958. ch1 := n1[index];
  9959. ch2 := n2[index];
  9960. END;
  9961. RETURN ch1 < ch2;
  9962. END Compare;
  9963. PROCEDURE QuickSort(VAR list: ARRAY OF Sections.Section; lo, hi: LONGINT);
  9964. VAR
  9965. i, j: LONGINT;
  9966. x, t: Sections.Section;
  9967. BEGIN
  9968. IF lo < hi THEN
  9969. i := lo; j := hi; x:= list[(lo+hi) DIV 2];
  9970. WHILE i <= j DO
  9971. WHILE Compare(list[i], x) DO INC(i) END;
  9972. WHILE Compare(x, list[j]) DO DEC(j) END;
  9973. IF i <= j THEN
  9974. t := list[i]; list[i] := list[j]; list[j] := t; (* swap i and j *)
  9975. INC(i); DEC(j)
  9976. END
  9977. END;
  9978. IF lo < j THEN QuickSort(list, lo, j) END;
  9979. IF i < hi THEN QuickSort(list, i, hi) END
  9980. END;
  9981. END QuickSort;
  9982. (*
  9983. ExportDesc* = RECORD
  9984. fp*: ADDRESS;
  9985. name* {UNTRACED}: DynamicName;
  9986. adr*: ADDRESS;
  9987. exports*: LONGINT;
  9988. dsc* {UNTRACED}: ExportArray
  9989. END;
  9990. ExportArray* = POINTER {UNSAFE} TO ARRAY OF ExportDesc;
  9991. *)
  9992. PROCEDURE ExportDesc2(
  9993. source: IntermediateCode.Section;
  9994. namePool: IntermediateCode.Section;
  9995. fingerPrinter: FingerPrinter.FingerPrinter;
  9996. symbol: Sections.Section;
  9997. name: StringPool.Index;
  9998. VAR patchAdr: LONGINT
  9999. ): BOOLEAN;
  10000. VAR fingerPrint: SyntaxTree.FingerPrint;
  10001. BEGIN
  10002. (*IF (implementationVisitor.backend.cooperative) & (symbol.symbol = NIL) OR (symbol.symbol # NIL) & (symbol.type # Sections.InitCodeSection)
  10003. & (symbol.type # Sections.InlineCodeSection)
  10004. THEN
  10005. *)
  10006. IF (symbol = NIL) OR ( (implementationVisitor.backend.cooperative) & (symbol.symbol = NIL) OR (symbol.symbol # NIL) & (symbol.type # Sections.InitCodeSection)
  10007. & (symbol.type # Sections.InlineCodeSection))
  10008. THEN
  10009. IF (symbol = NIL) OR (symbol # NIL) & (symbol.type # Sections.InlineCodeSection) THEN
  10010. IF (symbol # NIL) & (symbol.symbol # NIL) THEN
  10011. fingerPrint := fingerPrinter.SymbolFP(symbol.symbol);
  10012. Longint(source,fingerPrint.shallow);
  10013. ELSE
  10014. Longint(source, 0);
  10015. END;
  10016. Symbol(source, namePool, DynamicName(namePool, name, poolMap), 0); (* reference to dynamic name *)
  10017. Symbol(source, symbol,0,0);
  10018. patchAdr := source.pc;
  10019. Address(source,0);
  10020. Address(source,0);
  10021. END;
  10022. RETURN TRUE
  10023. ELSE
  10024. RETURN FALSE
  10025. END;
  10026. END ExportDesc2;
  10027. PROCEDURE Export(CONST sections: ARRAY OF Sections.Section);
  10028. VAR level, olevel, s: LONGINT; prev, this: Basic.SegmentedName; name: ARRAY 256 OF CHAR;
  10029. scopes: ARRAY LEN(prev)+1 OF Scope; arrayName: ARRAY 32 OF CHAR;
  10030. sym: Sections.Section; ignore: LONGINT; symbol: Sections.Section;
  10031. TYPE
  10032. Scope = RECORD
  10033. elements: LONGINT;
  10034. gelements: LONGINT;
  10035. section: IntermediateCode.Section;
  10036. patchAdr: LONGINT;
  10037. arraySizePC: LONGINT;
  10038. beginPC: LONGINT; (* current scope start pc *)
  10039. END;
  10040. BEGIN
  10041. Basic.InitSegmentedName(prev);
  10042. olevel := -1;
  10043. scopes[0].section := source;
  10044. FOR s := 0 TO LEN(sections)-1 DO
  10045. symbol := sections[s];
  10046. IF (symbol # NIL) & (implementationVisitor.backend.cooperative) & (symbol.symbol = NIL) OR (symbol.symbol # NIL) & (symbol.type # Sections.InitCodeSection) & (symbol.type # Sections.InlineCodeSection) THEN
  10047. this := sections[s].name;
  10048. level := 0;
  10049. WHILE (this[level] > 0) DO
  10050. WHILE (this[level] > 0) & (prev[level] = this[level]) DO
  10051. INC(level);
  10052. END;
  10053. WHILE level < olevel DO
  10054. (*TRACE("closing",olevel,scopes[olevel].elements); *)
  10055. IF olevel > 0 THEN
  10056. PatchLongint(scopes[olevel-1].section,scopes[olevel-1].patchAdr, scopes[olevel].elements);
  10057. PatchSymbol(scopes[olevel-1].section,scopes[olevel-1].patchAdr+1, scopes[olevel].section.name, scopes[olevel].section.symbol, scopes[olevel].beginPC, 0);
  10058. END;
  10059. scopes[olevel].gelements := scopes[olevel].gelements + scopes[olevel].elements;
  10060. DEC(olevel);
  10061. END;
  10062. IF (this[level] > 0) THEN
  10063. IF level > olevel THEN
  10064. (*TRACE("opening",level); *)
  10065. IF scopes[level].section = NIL THEN
  10066. arrayName := ".@ExportArray";
  10067. Strings.AppendInt(arrayName, level);
  10068. scopes[level].section := Block("Heaps","SystemBlockDesc",arrayName,ignore);
  10069. Array(scopes[level].section,scopes[level].arraySizePC,"Modules.ExportDesc");
  10070. END;
  10071. scopes[level].beginPC := scopes[level].section.pc;
  10072. olevel := level;
  10073. scopes[olevel].elements := 0;
  10074. END;
  10075. IF (level = LEN(this)-1) OR (this[level+1] <= 0) THEN
  10076. sym := sections[s];
  10077. ELSE
  10078. sym := NIL;
  10079. END;
  10080. IF ExportDesc2(scopes[level].section, namePool, fingerPrinter, sym, this[level], scopes[level].patchAdr)
  10081. THEN
  10082. INC(scopes[olevel].elements);
  10083. END;
  10084. (*StringPool.GetString(this[level], name);*)
  10085. (*TRACE(level, name);*)
  10086. (* enter string in scope *)
  10087. INC(level);
  10088. END;
  10089. END;
  10090. Basic.SegmentedNameToString(this, name);
  10091. (*TRACE(level, "enter", name);*)
  10092. prev := this;
  10093. END;
  10094. END;
  10095. WHILE 0 <= olevel DO
  10096. (*TRACE("closing",olevel,scopes[olevel].elements); *)
  10097. IF olevel > 0 THEN
  10098. PatchLongint(scopes[olevel-1].section,scopes[olevel-1].patchAdr, scopes[olevel].elements);
  10099. PatchSymbol(scopes[olevel-1].section,scopes[olevel-1].patchAdr+1, scopes[olevel].section.name, scopes[olevel].section.symbol, scopes[olevel].beginPC, 0);
  10100. END;
  10101. scopes[olevel].gelements := scopes[olevel].gelements + scopes[olevel].elements;
  10102. DEC(olevel);
  10103. END;
  10104. level := 0;
  10105. WHILE (level < LEN(scopes)) DO
  10106. IF scopes[level].section # NIL THEN
  10107. PatchArray(scopes[level].section, scopes[level].arraySizePC, scopes[level].gelements);
  10108. END;
  10109. INC(level);
  10110. END;
  10111. END Export;
  10112. BEGIN
  10113. NEW(fingerPrinter, module.system);
  10114. NEW(poolMap, 64);
  10115. namePool := Block("Heaps","SystemBlockDesc",".@NamePool",namePoolOffset);
  10116. NEW(sectionArray, module.allSections.Length());
  10117. FOR i := 0 TO module.allSections.Length() - 1 DO
  10118. section := module.allSections.GetSection(i);
  10119. sectionArray[i] := section;
  10120. END;
  10121. QuickSort(sectionArray^,0,module.allSections.Length()-1);
  10122. Export(sectionArray^);
  10123. END ExportDesc;
  10124. PROCEDURE ExceptionArray(source: IntermediateCode.Section);
  10125. VAR
  10126. p: Sections.Section; finallyPC, sizePC, size, i: LONGINT;
  10127. BEGIN
  10128. Info(source, "exception table offsets array descriptor");
  10129. size := 0;
  10130. Array(source,sizePC,"Modules.ExceptionTableEntry");
  10131. Info(source, "exception table content");
  10132. FOR i := 0 TO module.allSections.Length() - 1 DO
  10133. p := module.allSections.GetSection(i);
  10134. IF p.type = Sections.CodeSection THEN
  10135. finallyPC := p(IntermediateCode.Section).finally;
  10136. IF finallyPC>=0 THEN
  10137. Symbol( source, p, 0,0);
  10138. Symbol( source, p, finallyPC, 0);
  10139. Symbol( source, p, finallyPC,0);
  10140. INC(size);
  10141. END;
  10142. END
  10143. END;
  10144. PatchArray(source,sizePC,size);
  10145. END ExceptionArray;
  10146. PROCEDURE Name(section: IntermediateCode.Section; CONST name: ARRAY OF CHAR);
  10147. VAR i: LONGINT; ch: CHAR;
  10148. BEGIN
  10149. i := 0;
  10150. REPEAT
  10151. ch := name[i]; INC(i);
  10152. Char( section, ch);
  10153. UNTIL ch = 0X;
  10154. WHILE i < 32 DO
  10155. Char( section, 0X); INC(i);
  10156. END;
  10157. END Name;
  10158. PROCEDURE LongName(section: IntermediateCode.Section; CONST name: ARRAY OF CHAR);
  10159. VAR i: LONGINT; ch: CHAR;
  10160. BEGIN
  10161. i := 0;
  10162. REPEAT
  10163. ch := name[i]; INC(i);
  10164. Char( section, ch);
  10165. UNTIL ch = 0X;
  10166. WHILE i < 128 DO
  10167. Char( section, 0X); INC(i);
  10168. END;
  10169. END LongName;
  10170. PROCEDURE References(section: IntermediateCode.Section);
  10171. CONST
  10172. rfDirect = 1X; rfIndirect = 3X;
  10173. rfStaticArray= 12X; rfDynamicArray=14X; rfOpenArray=15X;
  10174. rfByte = 1X; rfBoolean = 2X; rfChar8=3X; rfShortint=04X; rfInteger = 05X; rfLongint = 06X;
  10175. rfReal = 07X; rfLongreal = 08X; rfSet = 09X; rfDelegate = 0EX; rfString = 0FH; rfPointer = 0DX; rfHugeint = 10X;
  10176. rfChar16=11X; rfChar32=12X; rfAll=13X; rfSame=14X; rfRange=15X; rfRecord=16X; rfRecordPointer=1DX;
  10177. rfArrayFlag = 80X;
  10178. VAR
  10179. size: LONGINT; s: Sections.Section; sizePC, i: LONGINT;
  10180. PROCEDURE BaseType(arrayOf: BOOLEAN; type: SyntaxTree.Type);
  10181. VAR char: CHAR;
  10182. BEGIN
  10183. IF type = NIL THEN char := rfLongint
  10184. ELSIF type IS SyntaxTree.ByteType THEN char := rfByte
  10185. ELSIF type IS SyntaxTree.BooleanType THEN char := rfBoolean
  10186. ELSIF type IS SyntaxTree.CharacterType THEN
  10187. IF type.sizeInBits = 8 THEN char := rfChar8
  10188. ELSIF type.sizeInBits = 16 THEN char := rfChar16
  10189. ELSIF type.sizeInBits = 32 THEN char := rfChar32
  10190. END;
  10191. ELSIF type IS SyntaxTree.IntegerType THEN
  10192. IF type.sizeInBits = 8 THEN char := rfShortint
  10193. ELSIF type.sizeInBits = 16 THEN char := rfInteger
  10194. ELSIF type.sizeInBits = 32 THEN char := rfLongint
  10195. ELSIF type.sizeInBits = 64 THEN char := rfHugeint
  10196. END;
  10197. ELSIF type IS SyntaxTree.SizeType THEN char := rfLongint
  10198. ELSIF type IS SyntaxTree.AddressType THEN char := rfLongint
  10199. ELSIF type IS SyntaxTree.FloatType THEN
  10200. IF type.sizeInBits = 32 THEN char := rfReal
  10201. ELSIF type.sizeInBits = 64 THEN char := rfLongreal
  10202. END;
  10203. ELSIF type IS SyntaxTree.SetType THEN char := rfSet
  10204. ELSIF type IS SyntaxTree.AnyType THEN char := rfPointer
  10205. ELSIF type IS SyntaxTree.ObjectType THEN char := rfPointer
  10206. ELSIF type IS SyntaxTree.PointerType THEN char := rfPointer
  10207. ELSIF type IS SyntaxTree.ProcedureType THEN char := rfDelegate
  10208. ELSE (*ASSERT(arrayOf);*) char := rfPointer; (*RETURN (* ARRAY OF unknown (record): do not write anything *)*)
  10209. END;
  10210. IF arrayOf THEN
  10211. Char(section,CHR(ORD(char)+ORD(rfArrayFlag)));
  10212. ELSE
  10213. Char(section,char)
  10214. END;
  10215. INC(size);
  10216. END BaseType;
  10217. PROCEDURE RecordType(type: SyntaxTree.RecordType);
  10218. VAR destination: Sections.Section; name: SyntaxTree.IdentifierString;
  10219. BEGIN
  10220. destination := module.allSections.FindBySymbol(type.typeDeclaration); (*TODO*)
  10221. IF destination = NIL THEN
  10222. (* imported unused record type *)
  10223. Char(section,0X); (* nil type *)
  10224. INC(size);
  10225. type.typeDeclaration.GetName(name);
  10226. (*
  10227. this happens when a symbol from a different module is used but the type desciptor is not necessary to be present in the current module
  10228. D.Str("Warning: Unreferenced record type encountered: "); D.String(name); D.String(" unused? "); D.Ln;
  10229. *)
  10230. ELSE
  10231. IF type.pointerType # NIL THEN
  10232. Char(section,rfRecordPointer)
  10233. ELSE
  10234. Char(section,rfRecord);
  10235. END;
  10236. INC(size);
  10237. Longint(section,(destination.offset ));
  10238. INC(size,4);
  10239. END;
  10240. END RecordType;
  10241. PROCEDURE ArrayType(type: SyntaxTree.ArrayType);
  10242. BEGIN
  10243. IF type.arrayBase.resolved IS SyntaxTree.ArrayType THEN
  10244. Char(section,CHR(ORD(rfPointer)+ORD(rfArrayFlag)));
  10245. INC(size);
  10246. ELSE BaseType(TRUE,type.arrayBase.resolved)
  10247. END;
  10248. IF type.form = SyntaxTree.Static THEN
  10249. Longint(section,type.staticLength)
  10250. ELSE
  10251. Longint(section,0)
  10252. END;
  10253. INC(size,4);
  10254. END ArrayType;
  10255. PROCEDURE MathArrayType(type: SyntaxTree.MathArrayType);
  10256. BEGIN
  10257. IF type.arrayBase = NIL THEN BaseType(TRUE,NIL)
  10258. ELSIF type.arrayBase.resolved IS SyntaxTree.MathArrayType THEN
  10259. Char(section,CHR(ORD(rfPointer)+ORD(rfArrayFlag)));
  10260. INC(size);
  10261. ELSE BaseType(TRUE,type.arrayBase.resolved)
  10262. END;
  10263. IF type.form = SyntaxTree.Static THEN
  10264. Longint(section,type.staticLength)
  10265. ELSE
  10266. Longint(section,0)
  10267. END;
  10268. INC(size,4);
  10269. END MathArrayType;
  10270. PROCEDURE Type(type: SyntaxTree.Type);
  10271. BEGIN
  10272. IF type = NIL THEN Char(section,0X); INC(size); RETURN ELSE type := type.resolved END;
  10273. IF type IS SyntaxTree.BasicType THEN
  10274. BaseType(FALSE,type)
  10275. ELSIF type IS SyntaxTree.RecordType THEN
  10276. RecordType(type(SyntaxTree.RecordType));
  10277. ELSIF type IS SyntaxTree.ArrayType THEN
  10278. ArrayType(type(SyntaxTree.ArrayType))
  10279. ELSIF type IS SyntaxTree.EnumerationType THEN
  10280. BaseType(FALSE,module.system.longintType)
  10281. ELSIF type IS SyntaxTree.PointerType THEN
  10282. IF type(SyntaxTree.PointerType).pointerBase IS SyntaxTree.RecordType THEN
  10283. RecordType(type(SyntaxTree.PointerType).pointerBase(SyntaxTree.RecordType));
  10284. ELSE
  10285. BaseType(FALSE,type)
  10286. END;
  10287. ELSIF type IS SyntaxTree.ProcedureType THEN
  10288. BaseType(FALSE,type);
  10289. ELSIF type IS SyntaxTree.MathArrayType THEN
  10290. MathArrayType(type(SyntaxTree.MathArrayType));
  10291. ELSIF type IS SyntaxTree.CellType THEN
  10292. BaseType(FALSE,module.system.anyType);
  10293. ELSE HALT(200)
  10294. END;
  10295. END Type;
  10296. PROCEDURE WriteVariable(variable: SyntaxTree.Variable; indirect: BOOLEAN);
  10297. VAR name: ARRAY 256 OF CHAR;
  10298. BEGIN
  10299. IF variable.externalName # NIL THEN RETURN END;
  10300. IF indirect THEN Char(section,rfIndirect) ELSE Char(section,rfDirect) END;
  10301. INC(size);
  10302. variable.GetName(name);
  10303. Type(variable.type);
  10304. Longint(section,ToMemoryUnits(module.system,variable.offsetInBits));
  10305. INC(size,4);
  10306. String(section,name);
  10307. INC(size,Strings.Length(name)+1);
  10308. END WriteVariable;
  10309. PROCEDURE WriteParameter(variable: SyntaxTree.Parameter; indirect: BOOLEAN);
  10310. VAR name: ARRAY 256 OF CHAR;
  10311. BEGIN
  10312. IF indirect THEN Char(section,rfIndirect) ELSE Char(section,rfDirect) END;
  10313. INC(size);
  10314. variable.GetName(name);
  10315. Type(variable.type);
  10316. Longint(section,ToMemoryUnits(module.system,variable.offsetInBits));
  10317. INC(size,4);
  10318. variable.GetName(name);
  10319. String(section,name);
  10320. INC(size,Strings.Length(name)+1);
  10321. END WriteParameter;
  10322. PROCEDURE ReturnType(type: SyntaxTree.Type);
  10323. BEGIN
  10324. IF type = NIL THEN Char(section,0X); INC(size); RETURN ELSE type := type.resolved END;
  10325. IF type IS SyntaxTree.ArrayType THEN
  10326. WITH type: SyntaxTree.ArrayType DO
  10327. IF type.form = SyntaxTree.Static THEN Char(section,rfStaticArray)
  10328. ELSE Char(section,rfOpenArray)
  10329. END;
  10330. INC(size);
  10331. END
  10332. ELSIF type IS SyntaxTree.MathArrayType THEN
  10333. WITH type: SyntaxTree.MathArrayType DO
  10334. IF type.form = SyntaxTree.Static THEN Char(section,rfStaticArray)
  10335. ELSE Char(section,rfOpenArray)
  10336. END;
  10337. INC(size);
  10338. END
  10339. ELSIF type IS SyntaxTree.RecordType THEN
  10340. Char(section,rfRecord);
  10341. INC(size);
  10342. ELSE
  10343. BaseType(FALSE,type);
  10344. END;
  10345. END ReturnType;
  10346. PROCEDURE Procedure(s: Sections.Section);
  10347. VAR procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType;
  10348. parameter: SyntaxTree.Parameter; variable: SyntaxTree.Variable;
  10349. name: ARRAY 256 OF CHAR;
  10350. BEGIN
  10351. procedure := s.symbol(SyntaxTree.Procedure); (*! check for variable or type symbol for object body *)
  10352. procedure.GetName(name);
  10353. procedureType := procedure.type(SyntaxTree.ProcedureType);
  10354. Char(section,0F9X);
  10355. INC(size);
  10356. Symbol(section,s,0,0);
  10357. INC(size,4);
  10358. Symbol(section,s,s(IntermediateCode.Section).pc,0);
  10359. INC(size,4);
  10360. Longint(section,procedureType.numberParameters);
  10361. INC(size,4);
  10362. ReturnType(procedureType.returnType);
  10363. Longint(section,0); (*! level *)
  10364. INC(size,4);
  10365. Longint(section,0);
  10366. INC(size,4);
  10367. Global.GetSymbolNameInScope(procedure, module.module.moduleScope, name);
  10368. (*
  10369. IF procedure.scope IS SyntaxTree.RecordScope THEN (* add object name *)
  10370. record := procedure.scope(SyntaxTree.RecordScope).ownerRecord;
  10371. recordName := "";
  10372. IF record.pointerType # NIL THEN
  10373. DeclarationName(record.pointerType.typeDeclaration,recordName);
  10374. ELSE
  10375. DeclarationName(record.typeDeclaration,recordName);
  10376. END;
  10377. i := 0;
  10378. Info(section,recordName);
  10379. WHILE recordName[i] # 0X DO
  10380. Char(section,recordName[i]); INC(i);
  10381. INC(size);
  10382. END;
  10383. Char(section,".");
  10384. INC(size);
  10385. END;
  10386. *)
  10387. String(section,name);
  10388. INC(size,Strings.Length(name)+1);
  10389. parameter := procedureType.firstParameter;
  10390. WHILE(parameter # NIL) DO
  10391. WriteParameter(parameter,parameter.kind # SyntaxTree.ValueParameter); (*!treat exceptions !*)
  10392. parameter := parameter.nextParameter;
  10393. END;
  10394. variable := procedure.procedureScope.firstVariable;
  10395. WHILE(variable # NIL) DO
  10396. WriteVariable(variable,FALSE);
  10397. variable := variable.nextVariable;
  10398. END;
  10399. END Procedure;
  10400. PROCEDURE Scope(s: Sections.Section);
  10401. BEGIN
  10402. Char(section,0F8X);
  10403. INC(size);
  10404. Symbol(section,s,0,0); (* start *)
  10405. INC(size,4);
  10406. Symbol(section,s,s(IntermediateCode.Section).pc,0); (* end *)
  10407. INC(size,4);
  10408. String(section,"$$");
  10409. INC(size,3);
  10410. (* removed variables -- wrongly interpreted by Reflection
  10411. variable := module.module.moduleScope.firstVariable;
  10412. WHILE(variable # NIL) DO
  10413. WriteVariable(variable,FALSE);
  10414. variable := variable.nextVariable;
  10415. END;
  10416. *)
  10417. END Scope;
  10418. BEGIN
  10419. Array(section,sizePC,"");
  10420. size := 0;
  10421. Char(section,0FFX); (* sign for trap writer *)
  10422. INC(size);
  10423. FOR i := 0 TO module.allSections.Length() - 1 DO
  10424. s := module.allSections.GetSection(i);
  10425. IF (s.type # Sections.InitCodeSection) & (s.symbol = module.module.moduleScope.bodyProcedure) THEN
  10426. Scope(s) (*! must be first procedure in ref section *)
  10427. END
  10428. END;
  10429. FOR i := 0 TO module.allSections.Length() - 1 DO
  10430. s := module.allSections.GetSection(i);
  10431. IF (s.symbol = module.module.moduleScope.bodyProcedure) THEN (* already done, see above *)
  10432. ELSIF (s.type # Sections.InitCodeSection) & (s.symbol # NIL) & (s.symbol IS SyntaxTree.Procedure) & ~s.symbol(SyntaxTree.Procedure).isInline THEN
  10433. Procedure(s)
  10434. END
  10435. END;
  10436. PatchArray(section,sizePC,size);
  10437. END References;
  10438. (*
  10439. Command* = RECORD
  10440. (* Fields exported for initialization by loader/linker only! Consider read-only! *)
  10441. name*: Name; (* name of the procedure *)
  10442. argTdAdr*, retTdAdr* : ADDRESS; (* address of type descriptors of argument and return type, 0 if no type *)
  10443. entryAdr* : ADDRESS; (* entry address of procedure *)
  10444. END;
  10445. *)
  10446. PROCEDURE CommandArray(source: IntermediateCode.Section);
  10447. VAR
  10448. p: Sections.Section; sizePC, numberCommands: LONGINT;
  10449. procedure : SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType;
  10450. name: ARRAY 32 OF CHAR; numberParameters, i: LONGINT;
  10451. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  10452. PROCEDURE GetProcedureAllowed() : BOOLEAN;
  10453. PROCEDURE TypeAllowed(type : SyntaxTree.Type) : BOOLEAN;
  10454. BEGIN
  10455. RETURN
  10456. (type = NIL) OR
  10457. (type.resolved IS SyntaxTree.RecordType) OR
  10458. (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) OR
  10459. (type.resolved IS SyntaxTree.AnyType);
  10460. END TypeAllowed;
  10461. BEGIN
  10462. numberParameters := procedureType.numberParameters;
  10463. RETURN
  10464. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  10465. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  10466. (numberParameters = 1) & (procedureType.firstParameter.type.resolved IS SyntaxTree.AnyType) & (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.AnyType);
  10467. END GetProcedureAllowed;
  10468. PROCEDURE WriteType(type : SyntaxTree.Type);
  10469. VAR typeDeclaration: SyntaxTree.TypeDeclaration; section: Sections.Section;
  10470. name: Basic.SegmentedName; offset: LONGINT;
  10471. BEGIN
  10472. IF type = NIL THEN
  10473. Address(source,0);
  10474. ELSIF (type.resolved IS SyntaxTree.AnyType) OR (type.resolved IS SyntaxTree.ObjectType) THEN
  10475. Address(source,1);
  10476. ELSE
  10477. type := type.resolved;
  10478. IF type IS SyntaxTree.PointerType THEN
  10479. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  10480. END;
  10481. typeDeclaration := type.typeDeclaration; (* must be non-nil *)
  10482. IF (typeDeclaration.scope = NIL) OR (typeDeclaration.scope.ownerModule = module.module) THEN
  10483. name[0] := typeDeclaration.name; name[1] := -1;
  10484. section := module.allSections.FindBySymbol(type.typeDeclaration); (*TODO*)
  10485. ASSERT(section # NIL);
  10486. ELSE
  10487. Global.GetSymbolSegmentedName(typeDeclaration,name);
  10488. (* TODO *)
  10489. section := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,typeDeclaration, source.comments # NIL);
  10490. END;
  10491. IF implementationVisitor.backend.cooperative THEN
  10492. offset := 0;
  10493. ELSE
  10494. offset := 1 + type(SyntaxTree.RecordType).recordScope.numberMethods+16+1;
  10495. END;
  10496. Symbol(source,section, 0, ToMemoryUnits(module.system,offset*module.system.addressSize));
  10497. END;
  10498. END WriteType;
  10499. BEGIN
  10500. Info(source, "command array descriptor");
  10501. Array(source,sizePC,"Modules.Command");
  10502. numberCommands := 0;
  10503. Info(source, "command array content");
  10504. FOR i := 0 TO module.allSections.Length() - 1 DO
  10505. p := module.allSections.GetSection(i);
  10506. IF (p.symbol # NIL) & (p.symbol IS SyntaxTree.Procedure) THEN
  10507. procedure := p.symbol(SyntaxTree.Procedure);
  10508. procedureType := procedure.type(SyntaxTree.ProcedureType);
  10509. IF (SyntaxTree.PublicWrite IN procedure.access) & ~(procedure.isInline) & ~(procedureType.isDelegate) & GetProcedureAllowed() THEN
  10510. procedure.GetName(name);
  10511. Name(source,name);
  10512. numberParameters := procedureType.numberParameters;
  10513. (* offset of type of first parameter *)
  10514. IF (numberParameters = 0 ) THEN WriteType(NIL)
  10515. ELSE WriteType(procedureType.firstParameter.type)
  10516. END;
  10517. (* offset of type of return parameter *)
  10518. WriteType(procedureType.returnType);
  10519. (* command name *)
  10520. (* command code offset *)
  10521. Symbol(source,p,0,0);
  10522. INC(numberCommands);
  10523. IF Trace THEN
  10524. D.Ln;
  10525. END;
  10526. END;
  10527. END
  10528. END;
  10529. PatchArray(source,sizePC,numberCommands);
  10530. END CommandArray;
  10531. (* to prevent from double import of different module aliases *)
  10532. PROCEDURE IsFirstDirectOccurence(import: SyntaxTree.Import): BOOLEAN; (*! inefficient *)
  10533. VAR i: SyntaxTree.Import;
  10534. BEGIN
  10535. i := module.module.moduleScope.firstImport;
  10536. WHILE (i # NIL) & ((i.module # import.module) OR ~i.direct) DO
  10537. i := i.nextImport;
  10538. END;
  10539. RETURN i = import
  10540. END IsFirstDirectOccurence;
  10541. PROCEDURE ImportsArray(source: IntermediateCode.Section);
  10542. VAR import: SyntaxTree.Import ; pc: LONGINT;name: Basic.SegmentedName; numberImports: LONGINT; offset: LONGINT;
  10543. BEGIN
  10544. Array(source,pc,"");
  10545. Info(source, "import module array data");
  10546. IF implementationVisitor.backend.cooperative THEN
  10547. offset := 0;
  10548. ELSE
  10549. offset := ToMemoryUnits(module.system, 22* module.system.addressSize) (* Module pointer offset -- cf. ModuleSection(), how to encode generically correct? *);
  10550. END;
  10551. import := module.module.moduleScope.firstImport;
  10552. numberImports := 0;
  10553. WHILE import # NIL DO
  10554. IF import.direct & ~Global.IsSystemModule(import.module) & IsFirstDirectOccurence(import) THEN
  10555. Global.GetModuleSegmentedName(import.module,name);
  10556. Basic.SuffixSegmentedName(name, StringPool.GetIndex1("@Module"));
  10557. NamedSymbol(source, name, NIL, 0, offset);
  10558. INC(numberImports);
  10559. END;
  10560. import := import.nextImport
  10561. END;
  10562. PatchArray(source,pc,numberImports);
  10563. END ImportsArray;
  10564. PROCEDURE TypeInfoSection(source: IntermediateCode.Section);
  10565. VAR
  10566. p: Sections.Section; sizePC, size, i: LONGINT;
  10567. BEGIN
  10568. Info(source, "Type info section");
  10569. size := 0;
  10570. Array(source,sizePC,"Modules.TypeDesc");
  10571. FOR i := 0 TO module.allSections.Length() - 1 DO
  10572. p := module.allSections.GetSection(i);
  10573. WITH p: IntermediateCode.Section DO
  10574. IF Basic.SegmentedNameEndsWith(p.name,"@Info") THEN
  10575. Symbol(source,p,0,0);
  10576. INC(size);
  10577. END;
  10578. END
  10579. END;
  10580. PatchArray(source,sizePC,size);
  10581. END TypeInfoSection;
  10582. (*
  10583. ProcTableEntry* = RECORD
  10584. pcFrom*, pcLimit*, pcStatementBegin*, pcStatementEnd*: ADDRESS;
  10585. noPtr*: LONGINT;
  10586. END;
  10587. ProcTable* = POINTER TO ARRAY OF ProcTableEntry;
  10588. PtrTable* = POINTER TO ARRAY OF ADDRESS;
  10589. *)
  10590. PROCEDURE PointersInProcTables(procArray, pointerArray: IntermediateCode.Section; VAR procArraySize, maxPointers: LONGINT);
  10591. VAR
  10592. destination: Sections.Section;
  10593. pointerArraySizePC, procArraySizePC, pointerArraySize, i: LONGINT;
  10594. PROCEDURE PointerOffsets(destination : IntermediateCode.Section);
  10595. VAR numberPointers: LONGINT; procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType;
  10596. variable: SyntaxTree.Variable; parameter: SyntaxTree.Parameter; string: Basic.SectionName;
  10597. BEGIN
  10598. Info(procArray,"pcFrom");
  10599. Symbol(procArray,destination,0,0);
  10600. Info(procArray,"pcTo");
  10601. Symbol(procArray,destination,destination.pc,0);
  10602. Info(procArray,"pcStatementBegin");
  10603. Symbol(procArray,destination,destination.validPAFEnter,0);
  10604. Info(procArray,"pcStatementEnd");
  10605. Symbol(procArray,destination,destination.validPAFExit,0);
  10606. IF ~implementationVisitor.backend.cooperative THEN
  10607. Basic.SegmentedNameToString(destination.name, string);
  10608. Info(pointerArray,string);
  10609. procedure := destination.symbol(SyntaxTree.Procedure);
  10610. procedureType := procedure.type(SyntaxTree.ProcedureType);
  10611. variable := procedure.procedureScope.firstVariable;
  10612. WHILE(variable # NIL) DO
  10613. IF ~(variable.untraced) THEN
  10614. Pointers(ToMemoryUnits(module.system,variable.offsetInBits), NIL, pointerArray, variable.type, numberPointers);
  10615. END;
  10616. variable := variable.nextVariable
  10617. END;
  10618. parameter := procedureType.firstParameter;
  10619. WHILE(parameter # NIL) DO
  10620. IF ~(parameter.untraced) THEN
  10621. Pointers(ToMemoryUnits(module.system,parameter.offsetInBits), NIL, pointerArray, parameter.type, numberPointers);
  10622. END;
  10623. parameter := parameter.nextParameter;
  10624. END;
  10625. END;
  10626. Info(procArray,"numberPointers");
  10627. Longint(procArray,numberPointers);
  10628. IF numberPointers > maxPointers THEN maxPointers := numberPointers END;
  10629. INC(pointerArraySize, numberPointers);
  10630. END PointerOffsets;
  10631. BEGIN
  10632. maxPointers := 0;
  10633. Info(procArray, "proc array descriptor");
  10634. Address(procArray,0);
  10635. Address(procArray,0);
  10636. Address(procArray,0);
  10637. procArraySizePC := procArray.pc;
  10638. Address(procArray,0);
  10639. procArraySize := 0;
  10640. IF ~implementationVisitor.backend.cooperative THEN
  10641. Info(pointerArray, "pointer array descriptor");
  10642. Address(pointerArray,0);
  10643. Address(pointerArray,0);
  10644. Address(pointerArray,0);
  10645. pointerArraySizePC := pointerArray.pc;
  10646. Address(pointerArray,0);
  10647. pointerArraySize := 0;
  10648. END;
  10649. procArraySize := 0;
  10650. FOR i := 0 TO module.allSections.Length() - 1 DO
  10651. destination := module.allSections.GetSection(i);
  10652. IF (destination.type IN {Sections.CodeSection, Sections.BodyCodeSection}) & (destination.symbol # NIL) & (destination.symbol IS SyntaxTree.Procedure) & ~destination.symbol(SyntaxTree.Procedure).isInline THEN
  10653. PointerOffsets(destination(IntermediateCode.Section));
  10654. INC(procArraySize);
  10655. END
  10656. END;
  10657. PatchLongint(procArray,procArraySizePC,procArraySize);
  10658. IF ~implementationVisitor.backend.cooperative THEN
  10659. PatchLongint(pointerArray,pointerArraySizePC,pointerArraySize);
  10660. END;
  10661. END PointersInProcTables;
  10662. (*
  10663. Module* = OBJECT (Heaps.RootObject) (* cf. Linker0 & Heaps.WriteType *)
  10664. VAR
  10665. next*: Module; (** once a module is published, all fields are read-only *)
  10666. name*: Name;
  10667. init, published: BOOLEAN;
  10668. refcnt*: LONGINT; (* counts loaded modules that import this module *)
  10669. sb*: ADDRESS; <- set to beginning of data section by loader
  10670. entry*: POINTER TO ARRAY OF ADDRESS; <- not needed in new loader
  10671. command*: POINTER TO ARRAY OF Command;
  10672. ptrAdr*: POINTER TO ARRAY OF ADDRESS;
  10673. typeInfo*: POINTER TO ARRAY OF TypeDesc;
  10674. module*: POINTER TO ARRAY OF Module; <---- currently done by loader
  10675. procTable*: ProcTable; (* information inserted by loader, removed after use in Publish *)
  10676. ptrTable*: PtrTable; (* information inserted by loader, removed after use in Publish *)
  10677. data*, code*: Bytes;
  10678. staticTypeDescs* (* ug *), refs*: Bytes; <- staticTypeDescs in data section, refs currently unsupported
  10679. export*: ExportDesc;
  10680. term*: TerminationHandler;
  10681. exTable*: ExceptionTable;
  10682. noProcs*: LONGINT;
  10683. firstProc*: ADDRESS; <- done by loader
  10684. maxPtrs*: LONGINT;
  10685. crc*: LONGINT;
  10686. *)
  10687. PROCEDURE BasePointer (section: IntermediateCode.Section);
  10688. BEGIN
  10689. Info(section, "cycle");
  10690. Size(section,0);
  10691. Info(section, "references");
  10692. Size(section,0);
  10693. Info(section, "nextMarked");
  10694. Address(section,0);
  10695. Info(section, "nextWatched");
  10696. Address(section,0);
  10697. END BasePointer;
  10698. PROCEDURE BaseObject (section: IntermediateCode.Section);
  10699. BEGIN
  10700. BasePointer(section);
  10701. Info(section, "action");
  10702. Address(section,0);
  10703. Info(section, "monitor");
  10704. Address(section,0);
  10705. END BaseObject;
  10706. PROCEDURE ModuleDescriptor(section: IntermediateCode.Section);
  10707. VAR descriptorSection: IntermediateCode.Section; name: ARRAY 128 OF CHAR;
  10708. pooledName: Basic.SegmentedName;
  10709. symbol: SyntaxTree.Symbol;
  10710. BEGIN
  10711. Global.GetModuleName(module.module,name);
  10712. Strings.Append(name,".@Module.@Descriptor");
  10713. Basic.ToSegmentedName(name, pooledName);
  10714. descriptorSection := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, pooledName,NIL,TRUE);
  10715. Symbol(section,descriptorSection,0,0);
  10716. Info(descriptorSection, "descriptor");
  10717. symbol := implementationVisitor.GetTypeDescriptor("Modules","Module", pooledName);
  10718. NamedSymbol(descriptorSection, pooledName,symbol, 0, 0);
  10719. Address(descriptorSection,0);
  10720. Global.GetModuleName(module.module,name);
  10721. Strings.Append(name,".@Trace");
  10722. Basic.ToSegmentedName(name, pooledName);
  10723. NamedSymbol(descriptorSection, pooledName,NIL, 0, 0);
  10724. Basic.ToSegmentedName ("BaseTypes.Object.Finalize",pooledName);
  10725. NamedSymbol(descriptorSection, pooledName,NIL, 0, 0);
  10726. END ModuleDescriptor;
  10727. PROCEDURE ModuleSection(): IntermediateCode.Section;
  10728. VAR name: ARRAY 128 OF CHAR;
  10729. moduleSection: IntermediateCode.Section; offset: LONGINT; pooledName: Basic.SegmentedName;
  10730. symbol: SyntaxTree.Symbol;
  10731. BEGIN
  10732. Global.GetModuleName(module.module,name);
  10733. Strings.Append(name,".@Module");
  10734. Basic.ToSegmentedName(name, pooledName);
  10735. moduleSection := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, pooledName,NIL,TRUE);
  10736. moduleSection.SetExported(TRUE);
  10737. IF moduleSection.pc = 0 THEN
  10738. IF implementationVisitor.backend.cooperative THEN
  10739. Info(moduleSection, "descriptor");
  10740. ModuleDescriptor(moduleSection);
  10741. BaseObject(moduleSection);
  10742. implementationVisitor.CreateTraceModuleMethod(module.module);
  10743. ELSE
  10744. ProtectedHeapBlock("Heaps","ProtRecBlockDesc",moduleSection,2);
  10745. Info(moduleSection, "HeapBlock");
  10746. Symbol(moduleSection,moduleSection,2,0);
  10747. Info(moduleSection, "TypeDescriptor");
  10748. symbol := implementationVisitor.GetTypeDescriptor("Modules","Module", pooledName);
  10749. offset := ToMemoryUnits(module.system,(TypeRecordBaseOffset + 1 (*= numberMethods*))*module.system.addressSize);
  10750. NamedSymbol(moduleSection, pooledName,symbol, 0, offset);
  10751. END;
  10752. END;
  10753. RETURN moduleSection;
  10754. END ModuleSection;
  10755. PROCEDURE Module(bodyProc: IntermediateCode.Section);
  10756. VAR
  10757. moduleSection, pointerSection, importSection, emptyArraySection, exceptionSection, commandsSection,
  10758. typeInfoSection, procTableSection, ptrTableSection, referenceSection : IntermediateCode.Section;
  10759. emptyArraySectionOffset, pointerSectionOffset, importSectionOffset, numberPointers,
  10760. exceptionSectionOffset, commandsSectionOffset, typeInfoSectionOffset, procTableSectionOffset, ptrTableSectionOffset, maxPointers, numberProcs,temp,
  10761. referenceSectionOffset : LONGINT;
  10762. BEGIN
  10763. Global.GetModuleName(module.module,moduleName);
  10764. pointerSection := Block("Heaps","SystemBlockDesc",".@PointerArray",pointerSectionOffset);
  10765. PointerArray(pointerSection,module.module.moduleScope, numberPointers);
  10766. importSection := Block("Heaps","SystemBlockDesc",".@ImportsArray",importSectionOffset);
  10767. ImportsArray(importSection);
  10768. commandsSection := Block("Heaps","SystemBlockDesc",".@CommandArray",commandsSectionOffset);
  10769. CommandArray(commandsSection);
  10770. exceptionSection := Block("Heaps","SystemBlockDesc",".@ExceptionArray",exceptionSectionOffset);
  10771. ExceptionArray(exceptionSection);
  10772. typeInfoSection := Block("Heaps","SystemBlockDesc",".@TypeInfoArray",typeInfoSectionOffset);
  10773. TypeInfoSection(typeInfoSection);
  10774. referenceSection := Block("Heaps","SystemBlockDesc",".@References",referenceSectionOffset);
  10775. References(referenceSection);
  10776. procTableSection := Block("Heaps","SystemBlockDesc",".@ProcTable",procTableSectionOffset);
  10777. IF ~implementationVisitor.backend.cooperative THEN
  10778. ptrTableSection := Block("Heaps","SystemBlockDesc",".@PtrTable",ptrTableSectionOffset);
  10779. ELSE
  10780. ptrTableSection := NIL;
  10781. END;
  10782. PointersInProcTables(procTableSection,ptrTableSection,numberProcs,maxPointers);
  10783. emptyArraySection := Block("Heaps","SystemBlockDesc",".@EmptyArray",emptyArraySectionOffset);
  10784. Array(emptyArraySection,temp,"");
  10785. moduleSection := ModuleSection();
  10786. Info(moduleSection, "nextRoot*: RootObject");
  10787. Address(moduleSection,0);
  10788. Info(moduleSection, "next*: Module");
  10789. Address(moduleSection,0);
  10790. Info(moduleSection, "name*: Name");
  10791. Name(moduleSection,moduleName);
  10792. Info(moduleSection, "init, published: BOOLEAN");
  10793. Boolean(moduleSection,FALSE);
  10794. Boolean(moduleSection,FALSE);
  10795. Info(moduleSection,"filler"); (*! introduce alignment! *)
  10796. Boolean(moduleSection,FALSE);
  10797. Boolean(moduleSection,FALSE);
  10798. Info(moduleSection, "refcnt*: LONGINT");
  10799. Longint(moduleSection,0);
  10800. Info(moduleSection, "sb*: ADDRESS");
  10801. Address(moduleSection,0);
  10802. Info(moduleSection, "entry*: POINTER TO ARRAY OF ADDRESS");
  10803. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  10804. Info(moduleSection, "command*: POINTER TO ARRAY OF Command");
  10805. Symbol(moduleSection,commandsSection,commandsSectionOffset,0);
  10806. Info(moduleSection, "ptrAdr*: POINTER TO ARRAY OF ADDRESS");
  10807. Symbol(moduleSection,pointerSection,pointerSectionOffset,0);
  10808. Info(moduleSection, "typeInfo*: POINTER TO ARRAY OF TypeDesc");
  10809. Symbol(moduleSection,typeInfoSection,typeInfoSectionOffset,0);
  10810. Info(moduleSection, "module*: POINTER TO ARRAY OF Module");
  10811. Symbol(moduleSection,importSection,emptyArraySectionOffset,0);
  10812. Info(moduleSection, "procTable*: ProcTable");
  10813. Symbol(moduleSection,procTableSection,procTableSectionOffset,0);
  10814. Info(moduleSection, "ptrTable*: PtrTable");
  10815. IF ~implementationVisitor.backend.cooperative THEN
  10816. Symbol(moduleSection,ptrTableSection,ptrTableSectionOffset,0);
  10817. ELSE
  10818. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  10819. END;
  10820. Info(moduleSection, "data*, code*, staticTypeDescs*, refs*: Bytes");
  10821. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  10822. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  10823. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  10824. Symbol(moduleSection,referenceSection,referenceSectionOffset,0);
  10825. Info(moduleSection, "export*: ExportDesc");
  10826. ExportDesc(moduleSection);
  10827. Info(moduleSection, "term*: TerminationHandler");
  10828. Address(moduleSection,0);
  10829. Info(moduleSection, "exTable*: ExceptionTable");
  10830. Symbol(moduleSection,exceptionSection,exceptionSectionOffset,0);
  10831. Info(moduleSection, "noProcs*: LONGINT");
  10832. Longint(moduleSection,numberProcs);
  10833. Info(moduleSection, "firstProc*: ADDRESS");
  10834. Address(moduleSection,0);
  10835. Info(moduleSection, "maxPtrs*: LONGINT");
  10836. Longint(moduleSection,maxPointers);
  10837. Info(moduleSection, "crc*: LONGINT");
  10838. Longint(moduleSection, 0); (*! must be implemented *)
  10839. Info(moduleSection, "body*: ADDRESS");
  10840. Symbol(moduleSection, bodyProc, 0,0);
  10841. IF implementationVisitor.backend.cooperative THEN
  10842. PatchSymbol(moduleSection,MonitorOffset,moduleSection.name,NIL,moduleSection.pc,0);
  10843. Info(moduleSection, "monitor.owner");
  10844. Address(moduleSection,0);
  10845. Info(moduleSection, "monitor.nestingLevel");
  10846. Address(moduleSection,0);
  10847. Info(moduleSection, "monitor.blockedQueue");
  10848. Address(moduleSection,0); Address(moduleSection,0);
  10849. Info(moduleSection, "monitor.waitingQueue");
  10850. Address(moduleSection,0); Address(moduleSection,0);
  10851. Info(moduleSection, "monitor.waitingSentinel");
  10852. Address(moduleSection,0);
  10853. END;
  10854. END Module;
  10855. PROCEDURE PointerArray(source: IntermediateCode.Section; scope: SyntaxTree.Scope; VAR numberPointers: LONGINT);
  10856. VAR variable: SyntaxTree.Variable; pc: LONGINT; symbol: Sections.Section;
  10857. BEGIN
  10858. Array(source,pc,"");
  10859. Info(source, "pointer offsets array data");
  10860. IF scope IS SyntaxTree.RecordScope THEN
  10861. Pointers(0,symbol, source,scope(SyntaxTree.RecordScope).ownerRecord,numberPointers);
  10862. ELSIF scope IS SyntaxTree.CellScope THEN
  10863. Pointers(0, symbol, source, scope(SyntaxTree.CellScope).ownerCell, numberPointers);
  10864. ELSIF scope IS SyntaxTree.ModuleScope THEN
  10865. variable := scope(SyntaxTree.ModuleScope).firstVariable;
  10866. WHILE variable # NIL DO
  10867. IF ~(variable.untraced) & (variable.externalName = NIL) THEN
  10868. symbol := module.allSections.FindBySymbol(variable);
  10869. ASSERT(symbol # NIL);
  10870. Pointers(0,symbol, source,variable.type,numberPointers);
  10871. END;
  10872. variable := variable.nextVariable;
  10873. END;
  10874. END;
  10875. PatchArray(source,pc,numberPointers);
  10876. END PointerArray;
  10877. PROCEDURE CheckTypeDeclaration(x: SyntaxTree.Type);
  10878. VAR recordType: SyntaxTree.RecordType;
  10879. tir: IntermediateCode.Section; op: IntermediateCode.Operand; name: Basic.SegmentedName; td: SyntaxTree.TypeDeclaration;
  10880. section: Sections.Section; type: SyntaxTree.Type; cellType: SyntaxTree.CellType;
  10881. PROCEDURE FieldArray(source: IntermediateCode.Section);
  10882. VAR variable: SyntaxTree.Variable; pc, offset: LONGINT; tir: Sections.Section; size: LONGINT; name: ARRAY 128 OF CHAR;
  10883. segmentedName: Basic.SegmentedName;
  10884. td: SyntaxTree.TypeDeclaration;
  10885. BEGIN
  10886. Array(source,pc,"Modules.FieldEntry");
  10887. Info(source, "FieldArray");
  10888. IF x IS SyntaxTree.RecordType THEN
  10889. variable := x(SyntaxTree.RecordType).recordScope.firstVariable
  10890. ELSE
  10891. variable := NIL;
  10892. END;
  10893. size :=0;
  10894. WHILE variable # NIL DO
  10895. type := variable.type.resolved;
  10896. Info(source,"offset");
  10897. Size(source, ToMemoryUnits(module.system,variable.offsetInBits));
  10898. Info(source,"type class");
  10899. IF type IS SyntaxTree.PointerType THEN
  10900. Size(source, 1);
  10901. ELSIF type IS SyntaxTree.RecordType THEN
  10902. Size(source, 2);
  10903. ELSIF type IS SyntaxTree.NumberType THEN
  10904. Size(source, 3);
  10905. ELSE
  10906. Size(source, 0);
  10907. END;
  10908. Info(source, "type desc");
  10909. IF type IS SyntaxTree.RecordType THEN
  10910. td := type(SyntaxTree.RecordType).typeDeclaration;
  10911. Global.GetSymbolSegmentedName(td,segmentedName);
  10912. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  10913. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10914. ELSE
  10915. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10916. END;
  10917. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(type(SyntaxTree.RecordType).recordScope.numberMethods)*module.system.addressSize);
  10918. Symbol(source, tir, 0, offset);
  10919. ELSE
  10920. Address(source, 0);
  10921. END;
  10922. Info(source,"flags");
  10923. Set(source, {});
  10924. Info(source,"name");
  10925. variable.GetName(name);
  10926. LongName(source, name);
  10927. variable := variable.nextVariable;
  10928. INC(size);
  10929. END;
  10930. PatchArray(source,pc,size);
  10931. END FieldArray;
  10932. PROCEDURE NewTypeDescriptorInfo(tag: Sections.Section; offset: LONGINT; isProtected: BOOLEAN): Sections.Section;
  10933. VAR name: Basic.SegmentedName;source, fieldSection: IntermediateCode.Section;
  10934. moduleSection: IntermediateCode.Section; i: LONGINT; flags: SET;
  10935. sectionName: Basic.SectionName;
  10936. CONST MPO=-40000000H;
  10937. BEGIN
  10938. (*
  10939. TypeDesc* = POINTER TO RECORD
  10940. descSize: LONGINT;
  10941. sentinel: LONGINT; (* = MPO-4 *)
  10942. tag*: ADDRESS; (* pointer to static type descriptor, only used by linker and loader *)
  10943. flags*: SET;
  10944. mod*: Module; (* hint only, because module may have been freed (at Heaps.ModOfs) *)
  10945. name*: Name;
  10946. END;
  10947. *)
  10948. (* source := module.sections.FindByName(...) *)
  10949. Global.GetSymbolSegmentedName(td,name);
  10950. Basic.AppendToSegmentedName(name,"@Info");
  10951. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  10952. Info(source, "type info size"); Address(source, 3*ToMemoryUnits(module.system,module.system.addressSize)+32);
  10953. Address(source,MPO-4);
  10954. Info(source, "type tag pointer");
  10955. Symbol( source, tag, offset, 0);
  10956. Info(source, "type flags");
  10957. flags := {};
  10958. IF isProtected THEN INCL(flags,31) END;
  10959. Set( source, flags);
  10960. Info(source, "pointer to module");
  10961. moduleSection := ModuleSection();
  10962. Symbol( source, moduleSection, moduleSection.pc,0);
  10963. Info(source, "type name");
  10964. i := 0;
  10965. Global.GetSymbolNameInScope(td, module.module.moduleScope, sectionName);
  10966. (*
  10967. Global.GetSymbolSegmentedName(td,name);
  10968. Basic.SegmentedNameToString(name, sectionName);
  10969. *)
  10970. Name(source,sectionName);
  10971. source.SetReferenced(FALSE);
  10972. Global.GetSymbolSegmentedName(td,name);
  10973. Basic.AppendToSegmentedName(name,"@Fields");
  10974. fieldSection := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name, NIL,TRUE);
  10975. HeapBlock("Heaps","SystemBlockDesc", fieldSection, 2);
  10976. Info(fieldSection, "HeapBlock");
  10977. Address(fieldSection,0); (* empty such that GC does not go on traversing *)
  10978. Info(fieldSection, "TypeDescriptor");
  10979. Address(fieldSection,0);
  10980. Info(source, "FieldArray ref");
  10981. Symbol(source, fieldSection, fieldSection.pc, 0);
  10982. FieldArray(fieldSection);
  10983. RETURN source;
  10984. END NewTypeDescriptorInfo;
  10985. PROCEDURE NewTypeDescriptor;
  10986. VAR name: Basic.SegmentedName; op: IntermediateCode.Operand; source, base: IntermediateCode.Section;
  10987. procedure: SyntaxTree.Procedure;
  10988. baseRecord: SyntaxTree.RecordType; baseTD: SyntaxTree.TypeDeclaration; sym: SyntaxTree.Symbol;
  10989. numberPointers: LONGINT; padding,offset, i: LONGINT;
  10990. CONST MPO=-40000000H;
  10991. PROCEDURE TdTable(size: LONGINT);
  10992. VAR i: LONGINT;
  10993. BEGIN
  10994. Info(source, "tag table");
  10995. baseRecord := recordType;
  10996. i := 0;
  10997. WHILE baseRecord # NIL DO
  10998. INC(i);
  10999. baseRecord := baseRecord.GetBaseRecord();
  11000. END;
  11001. IF i > size THEN implementationVisitor.Error(x.position,"maximal extension level exceeded") END;
  11002. WHILE i < size DO
  11003. Address(source,0);
  11004. INC(i);
  11005. END;
  11006. baseRecord := recordType;
  11007. WHILE baseRecord # NIL DO
  11008. baseTD := baseRecord.typeDeclaration;
  11009. Global.GetSymbolSegmentedName(baseTD,name);
  11010. IF (baseTD.scope = NIL) OR (baseTD.scope.ownerModule = module.module) THEN
  11011. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11012. ELSE
  11013. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11014. END;
  11015. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(baseRecord.recordScope.numberMethods)*module.system.addressSize);
  11016. Symbol(source, tir, 0, offset);
  11017. baseRecord := baseRecord.GetBaseRecord();
  11018. END;
  11019. END TdTable;
  11020. PROCEDURE MethodTable;
  11021. VAR i,methods: LONGINT;
  11022. BEGIN
  11023. Info(source, "method table");
  11024. IF recordType # NIL THEN
  11025. methods := recordType.recordScope.numberMethods;
  11026. FOR i := methods-1 TO 0 BY -1 DO
  11027. procedure := recordType.recordScope.FindMethod(i);
  11028. Global.GetSymbolSegmentedName(procedure, name);
  11029. NamedSymbol(source, name,procedure, 0,0);
  11030. END;
  11031. END;
  11032. END MethodTable;
  11033. PROCEDURE CooperativeMethodTable(pointer: BOOLEAN);
  11034. VAR baseRecord: SyntaxTree.RecordType; name, stackFrame: Basic.SegmentedName; i,start,methods: LONGINT;
  11035. BEGIN
  11036. Info(source, "method table");
  11037. baseRecord := recordType;
  11038. WHILE baseRecord.baseType # NIL DO
  11039. baseRecord := baseRecord.GetBaseRecord ();
  11040. END;
  11041. GetRecordTypeName (baseRecord, name);
  11042. Basic.ToSegmentedName ("BaseTypes.StackFrame", stackFrame);
  11043. IF name = stackFrame THEN
  11044. start := 0;
  11045. ELSIF ~HasExplicitTraceMethod(recordType) THEN
  11046. baseRecord := recordType;
  11047. WHILE (baseRecord # NIL) & ~baseRecord.hasPointers DO
  11048. baseRecord := baseRecord.GetBaseRecord ();
  11049. END;
  11050. IF baseRecord # NIL THEN
  11051. GetRecordTypeName (baseRecord, name);
  11052. IF pointer & ~baseRecord.isObject THEN
  11053. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  11054. END;
  11055. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  11056. ELSIF recordType.isObject THEN
  11057. Basic.ToSegmentedName ("BaseTypes.Object.@Trace",name);
  11058. ELSIF pointer THEN
  11059. Basic.ToSegmentedName ("BaseTypes.Pointer.Trace",name);
  11060. ELSE
  11061. Basic.ToSegmentedName ("BaseTypes.Record.@Trace",name);
  11062. END;
  11063. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11064. Symbol(source, tir, 0, 0);
  11065. start := 0;
  11066. baseRecord := recordType;
  11067. WHILE (baseRecord # NIL) DO
  11068. IF HasExplicitTraceMethod(baseRecord) THEN start := 1 END;
  11069. baseRecord := baseRecord.GetBaseRecord ();
  11070. END;
  11071. ELSE
  11072. (* explicit trace method: *)
  11073. procedure := recordType.recordScope.FindMethod(0);
  11074. IF ~procedure.isFinalizer THEN
  11075. Global.GetSymbolSegmentedName(procedure, name);
  11076. NamedSymbol(source, name,procedure, 0,0);
  11077. END;
  11078. start := 1;
  11079. END;
  11080. IF (name # stackFrame) & recordType.isObject THEN
  11081. baseRecord := recordType;
  11082. WHILE (baseRecord # NIL) & (baseRecord.recordScope.finalizer = NIL) DO
  11083. baseRecord := baseRecord.GetBaseRecord ();
  11084. END;
  11085. IF (baseRecord = NIL) OR (baseRecord.recordScope.finalizer = NIL) THEN
  11086. Basic.ToSegmentedName ("BaseTypes.Object.Finalize",name);
  11087. ELSE
  11088. Global.GetSymbolSegmentedName(baseRecord.recordScope.finalizer, name);
  11089. END;
  11090. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11091. Symbol(source, tir, 0, 0);
  11092. END;
  11093. methods := recordType.recordScope.numberMethods;
  11094. FOR i := start TO methods-1 DO
  11095. procedure := recordType.recordScope.FindMethod(i);
  11096. IF ~procedure.isFinalizer THEN
  11097. Global.GetSymbolSegmentedName(procedure, name);
  11098. NamedSymbol(source, name,procedure, 0,0);
  11099. END;
  11100. END;
  11101. END CooperativeMethodTable;
  11102. BEGIN
  11103. Global.GetSymbolSegmentedName(td,name);
  11104. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,td,declarationVisitor.dump);
  11105. source.SetExported(IsExported(td));
  11106. IF (cellType # NIL) THEN recordType := cellType.GetBaseRecord() END;
  11107. IF implementationVisitor.backend.cooperative THEN
  11108. base := NIL;
  11109. baseRecord := recordType.GetBaseRecord();
  11110. IF baseRecord # NIL THEN
  11111. baseTD := baseRecord.typeDeclaration;
  11112. END;
  11113. IF ~recordType.isObject THEN
  11114. Info(source, "parent");
  11115. IF baseRecord # NIL THEN
  11116. Global.GetSymbolSegmentedName(baseTD,name);
  11117. IF (baseTD.scope = NIL) OR (baseTD.scope.ownerModule = module.module) THEN
  11118. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11119. ELSE
  11120. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11121. END;
  11122. Symbol(source, tir, 0, 0);
  11123. ELSE
  11124. Address(source,0);
  11125. END;
  11126. Info(source, "record size");
  11127. Address(source, ToMemoryUnits(module.system,module.system.SizeOf(recordType)));
  11128. source.SetReferenced(FALSE);
  11129. CooperativeMethodTable(FALSE);
  11130. base := source;
  11131. Global.GetSymbolSegmentedName(td,name);
  11132. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  11133. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11134. source.SetExported(IsExported(td));
  11135. source.SetReferenced(FALSE);
  11136. END;
  11137. Info(source, "parent");
  11138. IF baseRecord # NIL THEN
  11139. Global.GetSymbolSegmentedName(baseTD,name);
  11140. sym := baseTD;
  11141. IF ~recordType.isObject THEN
  11142. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  11143. sym := NIL;
  11144. END;
  11145. IF (baseTD.scope = NIL) OR (baseTD.scope.ownerModule = module.module) THEN
  11146. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,sym,declarationVisitor.dump);
  11147. ELSE
  11148. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,sym,declarationVisitor.dump);
  11149. END;
  11150. Symbol(source, tir, 0, 0);
  11151. ELSIF (recordType.pointerType # NIL) & recordType.pointerType.isPlain THEN
  11152. Address(source,0);
  11153. ELSE
  11154. IF recordType.isObject THEN
  11155. Basic.ToSegmentedName ("BaseTypes.Object",name);
  11156. ELSE
  11157. Basic.ToSegmentedName ("BaseTypes.Record",name);
  11158. END;
  11159. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11160. Symbol(source, tir, 0, 0);
  11161. END;
  11162. Info(source, "base record descriptor");
  11163. Symbol(source, base, 0, 0);
  11164. CooperativeMethodTable(TRUE);
  11165. source.SetReferenced(FALSE);
  11166. IF recordType.hasPointers THEN
  11167. IF ~HasExplicitTraceMethod (recordType) THEN
  11168. implementationVisitor.CreateTraceMethod(recordType);
  11169. END;
  11170. implementationVisitor.CreateResetProcedure(recordType);
  11171. implementationVisitor.CreateAssignProcedure(recordType);
  11172. END;
  11173. ELSIF ~simple THEN
  11174. (*
  11175. MethodEnd = MPO
  11176. ---
  11177. methods (# methods)
  11178. ---
  11179. tags (16)
  11180. ---
  11181. TypeDesc = TypeInfoAdr
  11182. ---
  11183. td adr ---> rec size
  11184. ----
  11185. pointer offsets
  11186. ----
  11187. (padding)
  11188. -----
  11189. empty [2 addresses aligned]
  11190. empty
  11191. empty
  11192. numPtrs
  11193. ---
  11194. pointer offsets
  11195. ---
  11196. *)
  11197. Info(source, "MethodEnd = MPO");
  11198. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),MPO);
  11199. source(IntermediateCode.Section).Emit(Data(-1,op));
  11200. MethodTable;
  11201. TdTable(TypeTags);
  11202. Info(source, "type descriptor info pointer");
  11203. Symbol(source, NewTypeDescriptorInfo(source,source.pc+1,recordType.IsProtected()),0,0);
  11204. IF (cellType # NIL) THEN
  11205. IF cellType.sizeInBits < 0 THEN
  11206. ASSERT(module.system.GenerateVariableOffsets(cellType.cellScope));
  11207. END;
  11208. Info(source, "cell size");
  11209. Address(source, ToMemoryUnits(module.system,cellType.sizeInBits));
  11210. ELSE
  11211. Info(source, "record size");
  11212. Address(source, ToMemoryUnits(module.system,module.system.SizeOf(recordType)));
  11213. END;
  11214. Info(source, "pointer offsets pointer");
  11215. padding := 1- source.pc MOD 2;
  11216. Symbol(source, source, source.pc+1+padding,0);
  11217. IF padding >0 THEN
  11218. Info(source, "padding");
  11219. FOR i := 1 TO padding DO Address(source,0) END;
  11220. END;
  11221. IF cellType # NIL THEN
  11222. PointerArray(source, cellType.cellScope, numberPointers);
  11223. ELSE
  11224. PointerArray(source, recordType.recordScope, numberPointers);
  11225. END;
  11226. ELSE
  11227. (*
  11228. simple:
  11229. methods
  11230. tag(2)
  11231. tag(1)
  11232. td adr ---> tag(0)
  11233. td adr ---> size
  11234. *)
  11235. MethodTable;
  11236. TdTable(TypeTags);
  11237. Info(source, "record size");
  11238. Address(source, ToMemoryUnits(module.system,module.system.SizeOf(recordType)));
  11239. source.SetReferenced(FALSE);
  11240. END;
  11241. END NewTypeDescriptor;
  11242. BEGIN
  11243. x := x.resolved;
  11244. IF (x IS SyntaxTree.PointerType) THEN
  11245. x := x(SyntaxTree.PointerType).pointerBase.resolved;
  11246. END;
  11247. IF (x IS SyntaxTree.RecordType) THEN (* enter: insert only if not already inserted *)
  11248. recordType := x(SyntaxTree.RecordType);
  11249. td := x.typeDeclaration;
  11250. IF td = NIL THEN td := recordType.pointerType.resolved.typeDeclaration END; (* for compatibility with paco *)
  11251. ASSERT(td # NIL);
  11252. section := module.allSections.FindBySymbol(td); (* TODO *)
  11253. IF (section = NIL) OR (section(IntermediateCode.Section).pc = 0) THEN
  11254. IF implementationVisitor.newObjectFile THEN
  11255. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  11256. NewTypeDescriptor
  11257. END;
  11258. ELSE
  11259. (* data section in intermediate code *)
  11260. Global.GetSymbolSegmentedName(td,name);
  11261. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,td,implementationVisitor.dump # NIL);
  11262. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),0);
  11263. tir.Emit(Data(-1,op));
  11264. END;
  11265. END;
  11266. ELSIF (x IS SyntaxTree.CellType) & implementationVisitor.backend.cellsAreObjects THEN
  11267. cellType := x(SyntaxTree.CellType);
  11268. td := x.typeDeclaration;
  11269. section := module.allSections.FindBySymbol(td); (* TODO *)
  11270. IF (section = NIL) OR (section(IntermediateCode.Section).pc = 0) THEN
  11271. IF implementationVisitor.newObjectFile THEN
  11272. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  11273. NewTypeDescriptor
  11274. END;
  11275. END;
  11276. END;
  11277. END
  11278. END CheckTypeDeclaration
  11279. END MetaDataGenerator;
  11280. IntermediateBackend*= OBJECT (IntermediateCode.IntermediateBackend)
  11281. VAR
  11282. trace-: BOOLEAN;
  11283. traceString-: SyntaxTree.IdentifierString;
  11284. traceModuleName-: SyntaxTree.IdentifierString;
  11285. newObjectFile-: BOOLEAN;
  11286. profile-: BOOLEAN;
  11287. noRuntimeChecks: BOOLEAN;
  11288. simpleMetaData-: BOOLEAN;
  11289. noAsserts: BOOLEAN;
  11290. optimize-: BOOLEAN;
  11291. cooperative-: BOOLEAN;
  11292. preregisterStatic-: BOOLEAN;
  11293. dump-: Basic.Writer;
  11294. cellsAreObjects: BOOLEAN;
  11295. PROCEDURE &InitIntermediateBackend*;
  11296. BEGIN
  11297. simpleMetaData := FALSE;
  11298. newObjectFile := FALSE;
  11299. InitBackend;
  11300. SetRuntimeModuleName(DefaultRuntimeModuleName);
  11301. SetTraceModuleName(DefaultTraceModuleName);
  11302. END InitIntermediateBackend;
  11303. PROCEDURE GenerateIntermediate*(x: SyntaxTree.Module; supportedInstruction: SupportedInstructionProcedure; supportedImmediate: SupportedImmediateProcedure): Sections.Module;
  11304. VAR
  11305. declarationVisitor: DeclarationVisitor;
  11306. implementationVisitor: ImplementationVisitor;
  11307. module: Sections.Module;
  11308. name, instructionSet, platformName: SyntaxTree.IdentifierString;
  11309. meta: MetaDataGenerator;
  11310. BEGIN
  11311. ResetError;
  11312. Global.GetSymbolName(x,name);
  11313. IF activeCellsSpecification # NIL THEN
  11314. GetDescription(instructionSet);
  11315. activeCellsSpecification.SetInstructionSet(instructionSet)
  11316. END;
  11317. NEW(module,x,system); (* backend structures *)
  11318. Global.GetModuleName(x, name);
  11319. module.SetModuleName(name);
  11320. NEW(implementationVisitor,system,checker,supportedInstruction, supportedImmediate, Compiler.FindPC IN flags, runtimeModuleName, SELF, newObjectFile);
  11321. NEW(declarationVisitor,system,implementationVisitor,SELF,Compiler.ForceModuleBodies IN flags,trace & (Compiler.Info IN flags));
  11322. NEW(meta, implementationVisitor, declarationVisitor,simpleMetaData);
  11323. declarationVisitor.Module(x,module);
  11324. IF newObjectFile & ~meta.simple THEN
  11325. meta.Module(implementationVisitor.moduleBodySection);
  11326. END;
  11327. GetDescription(platformName);
  11328. module.SetPlatformName(platformName);
  11329. RETURN module
  11330. END GenerateIntermediate;
  11331. PROCEDURE SupportedImmediate*(CONST op: IntermediateCode.Operand): BOOLEAN;
  11332. BEGIN RETURN TRUE
  11333. END SupportedImmediate;
  11334. PROCEDURE ProcessSyntaxTreeModule(syntaxTreeModule: SyntaxTree.Module): Formats.GeneratedModule;
  11335. BEGIN RETURN ProcessIntermediateCodeModule(GenerateIntermediate(syntaxTreeModule, SupportedInstruction, SupportedImmediate))
  11336. END ProcessSyntaxTreeModule;
  11337. PROCEDURE ProcessIntermediateCodeModule(intermediateCodeModule: Formats.GeneratedModule): Formats.GeneratedModule;
  11338. VAR
  11339. result: Sections.Module;
  11340. traceName: Basic.MessageString;
  11341. BEGIN
  11342. ASSERT(intermediateCodeModule IS Sections.Module);
  11343. result := intermediateCodeModule(Sections.Module);
  11344. IF trace THEN
  11345. traceName := "intermediate code trace: ";
  11346. Strings.Append(traceName,traceString);
  11347. dump := Basic.GetWriter(Basic.GetDebugWriter(traceName));
  11348. IF (traceString="") OR (traceString="*") THEN
  11349. result.Dump(dump);
  11350. dump.Update
  11351. ELSE
  11352. Sections.DumpFiltered(dump, result, traceString);
  11353. END
  11354. END;
  11355. RETURN result
  11356. END ProcessIntermediateCodeModule;
  11357. PROCEDURE GetDescription*(VAR instructionSet: ARRAY OF CHAR);
  11358. BEGIN instructionSet := "Intermediate";
  11359. END GetDescription;
  11360. PROCEDURE SetNewObjectFile*(newObjectFile: BOOLEAN; simpleMetaData: BOOLEAN);
  11361. BEGIN
  11362. SELF.newObjectFile := newObjectFile;
  11363. SELF.simpleMetaData := simpleMetaData;
  11364. END SetNewObjectFile;
  11365. PROCEDURE SetTraceModuleName(CONST name: ARRAY OF CHAR);
  11366. BEGIN COPY(name, traceModuleName)
  11367. END SetTraceModuleName;
  11368. PROCEDURE DefineOptions(options: Options.Options);
  11369. BEGIN
  11370. DefineOptions^(options);
  11371. options.Add(0X,"trace",Options.String);
  11372. options.Add(0X,"runtime",Options.String);
  11373. options.Add(0X,"newObjectFile",Options.Flag);
  11374. options.Add(0X,"traceModule",Options.String);
  11375. options.Add(0X,"profile",Options.Flag);
  11376. options.Add(0X,"noRuntimeChecks",Options.Flag);
  11377. options.Add(0X,"noAsserts",Options.Flag);
  11378. options.Add(0X,"metaData",Options.String);
  11379. options.Add('o',"optimize", Options.Flag);
  11380. options.Add(0X,"preregisterStatic", Options.Flag);
  11381. options.Add(0X,"cellsAreObjects", Options.Flag);
  11382. END DefineOptions;
  11383. PROCEDURE GetOptions(options: Options.Options);
  11384. VAR name,string: SyntaxTree.IdentifierString;
  11385. BEGIN
  11386. GetOptions^(options);
  11387. trace := options.GetString("trace",traceString);
  11388. profile := options.GetFlag("profile");
  11389. noRuntimeChecks := options.GetFlag("noRuntimeChecks");
  11390. noAsserts := options.GetFlag("noAsserts");
  11391. cooperative := options.GetFlag("cooperative");
  11392. IF options.GetFlag("newObjectFile") THEN newObjectFile := TRUE;
  11393. IF cooperative THEN
  11394. SetRuntimeModuleName("CPU") END
  11395. END;
  11396. IF options.GetString("objectFile",string) & (string = "Minos") THEN
  11397. simpleMetaData := TRUE
  11398. END;
  11399. IF options.GetString("metaData",string) THEN
  11400. IF string = "simple" THEN simpleMetaData := TRUE
  11401. ELSIF string ="full" THEN simpleMetaData := FALSE
  11402. END;
  11403. END;
  11404. IF options.GetString("runtime",name) THEN SetRuntimeModuleName(name) END;
  11405. IF options.GetString("traceModule",name) THEN SetTraceModuleName(name) END;
  11406. optimize := options.GetFlag("optimize");
  11407. preregisterStatic := options.GetFlag("preregisterStatic");
  11408. cellsAreObjects := options.GetFlag("cellsAreObjects");
  11409. END GetOptions;
  11410. PROCEDURE DefaultSymbolFileFormat(): Formats.SymbolFileFormat;
  11411. BEGIN RETURN SymbolFileFormat.Get()
  11412. END DefaultSymbolFileFormat;
  11413. END IntermediateBackend;
  11414. (* ----------------------------------- register allocation ------------------------------------- *)
  11415. (* register mapping scheme
  11416. virtual register number --> register mapping = part(0) --> ticket <--> physical register
  11417. spill offset
  11418. part(n) --> ticket <--> physical register
  11419. spill offset
  11420. *)
  11421. VAR int8-, int16-, int32-, int64-, uint8-, uint16-, uint32-, uint64-, float32-, float64-: IntermediateCode.Type;
  11422. emptyOperand: IntermediateCode.Operand;
  11423. systemCalls: ARRAY NumberSystemCalls OF SyntaxTree.Symbol;
  11424. statCoopResetVariables: LONGINT;
  11425. statCoopModifyAssignments: LONGINT;
  11426. modifyAssignmentsPC : LONGINT;
  11427. statCoopNilCheck: LONGINT;
  11428. statCoopSwitch: LONGINT;
  11429. statCoopAssignProcedure: LONGINT;
  11430. statCoopTraceMethod: LONGINT;
  11431. statCoopResetProcedure: LONGINT;
  11432. statCoopTraceModule: LONGINT;
  11433. PROCEDURE ResetStatistics*;
  11434. BEGIN
  11435. statCoopResetVariables := 0;
  11436. statCoopModifyAssignments := 0;
  11437. statCoopNilCheck:= 0;
  11438. statCoopSwitch:= 0;
  11439. statCoopAssignProcedure:= 0;
  11440. statCoopTraceMethod:= 0;
  11441. statCoopResetProcedure:= 0;
  11442. statCoopTraceModule:= 0;
  11443. END ResetStatistics;
  11444. PROCEDURE Statistics*;
  11445. BEGIN
  11446. TRACE(statCoopResetVariables, statCoopModifyAssignments);
  11447. TRACE(statCoopNilCheck, statCoopSwitch);
  11448. TRACE(statCoopAssignProcedure,
  11449. statCoopTraceMethod,
  11450. statCoopResetProcedure,
  11451. statCoopTraceModule)
  11452. END Statistics;
  11453. PROCEDURE GCD(a,b: LONGINT): LONGINT;
  11454. VAR h: LONGINT;
  11455. BEGIN
  11456. WHILE b # 0 DO
  11457. h := a MOD b;
  11458. a := b;
  11459. b := h;
  11460. END;
  11461. RETURN a
  11462. END GCD;
  11463. PROCEDURE SCM(a,b: LONGINT): LONGINT;
  11464. BEGIN
  11465. RETURN a*b DIV GCD(a,b)
  11466. END SCM;
  11467. PROCEDURE CommonAlignment(a,b: LONGINT): LONGINT;
  11468. BEGIN
  11469. (*TRACE(a,b);*)
  11470. IF a = 0 THEN RETURN b
  11471. ELSIF b = 0 THEN RETURN a
  11472. ELSE RETURN SCM(a,b)
  11473. END;
  11474. END CommonAlignment;
  11475. PROCEDURE PassBySingleReference(parameter: SyntaxTree.Parameter): BOOLEAN;
  11476. BEGIN
  11477. IF parameter.kind = SyntaxTree.ValueParameter THEN RETURN FALSE
  11478. ELSIF parameter.kind = SyntaxTree.ConstParameter THEN
  11479. RETURN (parameter.type.resolved IS SyntaxTree.RecordType) OR (parameter.type.resolved IS SyntaxTree.ArrayType) & (parameter.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.CCallingConvention)
  11480. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  11481. RETURN ~(parameter.type.resolved IS SyntaxTree.ArrayType) & ~(parameter.type.resolved IS SyntaxTree.MathArrayType) OR (parameter.type.resolved IS SyntaxTree.ArrayType) & (parameter.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.CCallingConvention)
  11482. END
  11483. END PassBySingleReference;
  11484. PROCEDURE PassInRegister(parameter: SyntaxTree.Parameter): BOOLEAN;
  11485. BEGIN
  11486. RETURN ~parameter.type.IsComposite() OR PassBySingleReference(parameter)
  11487. END PassInRegister;
  11488. PROCEDURE AddRegisterEntry(VAR queue: RegisterEntry; register: LONGINT; class: IntermediateCode.RegisterClass; type: IntermediateCode.Type);
  11489. VAR new: RegisterEntry;
  11490. BEGIN
  11491. NEW(new); new.register := register; new.registerClass := class; new.type := type; new.next := NIL; new.prev := NIL;
  11492. IF queue = NIL THEN
  11493. queue := new
  11494. ELSE
  11495. new.next := queue;
  11496. IF queue#NIL THEN queue.prev := new END;
  11497. queue := new
  11498. END;
  11499. END AddRegisterEntry;
  11500. PROCEDURE RemoveRegisterEntry(VAR queue: RegisterEntry; register: LONGINT): BOOLEAN;
  11501. VAR this: RegisterEntry;
  11502. BEGIN
  11503. this := queue;
  11504. WHILE (this # NIL) & (this.register # register) DO
  11505. this := this.next;
  11506. END;
  11507. IF this = NIL THEN
  11508. RETURN FALSE
  11509. END;
  11510. ASSERT(this # NIL);
  11511. IF this = queue THEN queue := queue.next END;
  11512. IF this.prev # NIL THEN this.prev.next := this.next END;
  11513. IF this.next # NIL THEN this.next.prev := this.prev END;
  11514. RETURN TRUE
  11515. END RemoveRegisterEntry;
  11516. PROCEDURE Assert(cond: BOOLEAN; CONST reason: ARRAY OF CHAR);
  11517. BEGIN ASSERT(cond);
  11518. END Assert;
  11519. PROCEDURE ReusableRegister(op: IntermediateCode.Operand): BOOLEAN;
  11520. BEGIN
  11521. RETURN (op.mode = IntermediateCode.ModeRegister) & (op.register > 0) & (op.offset = 0);
  11522. END ReusableRegister;
  11523. PROCEDURE EnsureBodyProcedure(moduleScope: SyntaxTree.ModuleScope);
  11524. VAR procedure: SyntaxTree.Procedure; procedureScope: SyntaxTree.ProcedureScope;
  11525. BEGIN
  11526. procedure := moduleScope.bodyProcedure;
  11527. IF procedure = NIL THEN (* artificially add body procedure if not existing. Really needed? *)
  11528. procedureScope := SyntaxTree.NewProcedureScope(moduleScope);
  11529. procedure := SyntaxTree.NewProcedure(-1,Global.ModuleBodyName, procedureScope);
  11530. procedure.SetScope(moduleScope);
  11531. procedure.SetType(SyntaxTree.NewProcedureType(-1,moduleScope));
  11532. procedure.SetAccess(SyntaxTree.Hidden);
  11533. moduleScope.SetBodyProcedure(procedure);
  11534. moduleScope.AddProcedure(procedure);
  11535. procedureScope.SetBody(SyntaxTree.NewBody(-1,procedureScope)); (* empty body *)
  11536. END;
  11537. END EnsureBodyProcedure;
  11538. PROCEDURE GetSymbol*(scope: SyntaxTree.ModuleScope; CONST moduleName, symbolName: ARRAY OF CHAR): SyntaxTree.Symbol;
  11539. VAR import: SyntaxTree.Import;
  11540. s: Basic.MessageString;
  11541. selfName: SyntaxTree.IdentifierString;
  11542. module: SyntaxTree.Module;
  11543. BEGIN
  11544. scope.ownerModule.GetName(selfName);
  11545. IF (moduleName = selfName) & (scope.ownerModule.context = Global.A2Name) THEN
  11546. module := scope.ownerModule
  11547. ELSE
  11548. import := scope.ImportByModuleName(SyntaxTree.NewIdentifier(moduleName),SyntaxTree.NewIdentifier("A2"));
  11549. IF import = NIL THEN
  11550. RETURN NIL
  11551. ELSIF import.module = NIL THEN
  11552. RETURN NIL
  11553. ELSE module := import.module
  11554. END;
  11555. END;
  11556. RETURN module.moduleScope.FindSymbol(SyntaxTree.NewIdentifier(symbolName));
  11557. END GetSymbol;
  11558. PROCEDURE InitOperand(VAR op: Operand; mode: SHORTINT);
  11559. BEGIN
  11560. op.mode := mode;
  11561. IntermediateCode.InitOperand(op.op);
  11562. IntermediateCode.InitOperand(op.tag);
  11563. IntermediateCode.InitOperand(op.extra);
  11564. op.dimOffset := 0;
  11565. END InitOperand;
  11566. (* TODO: remove this, and redirect calls to 'IntermediateCode.GetType' directly *)
  11567. PROCEDURE GetType*(system: Global.System; type: SyntaxTree.Type): IntermediateCode.Type;
  11568. BEGIN RETURN IntermediateCode.GetType(system, type)
  11569. END GetType;
  11570. PROCEDURE BuildConstant(module: SyntaxTree.Module; value: SyntaxTree.Value; VAR adr: LONGINT): SyntaxTree.Constant;
  11571. VAR name: SyntaxTree.IdentifierString; constant: SyntaxTree.Constant;
  11572. BEGIN
  11573. name := "@const"; Basic.AppendNumber(name, adr); INC(adr);
  11574. (*
  11575. UniqueId(name,module,name,adr);
  11576. *)
  11577. constant := SyntaxTree.NewConstant(-1,SyntaxTree.NewIdentifier(name));
  11578. constant.SetValue(value);
  11579. module.moduleScope.AddConstant(constant);
  11580. constant.SetScope(module.moduleScope);
  11581. RETURN constant
  11582. END BuildConstant;
  11583. PROCEDURE HasPointers (scope: SyntaxTree.ProcedureScope): BOOLEAN;
  11584. VAR variable: SyntaxTree.Variable; parameter: SyntaxTree.Parameter;
  11585. BEGIN
  11586. variable := scope.firstVariable;
  11587. WHILE variable # NIL DO
  11588. IF variable.NeedsTrace() THEN
  11589. RETURN TRUE;
  11590. END;
  11591. variable := variable.nextVariable;
  11592. END;
  11593. parameter := scope.ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  11594. WHILE parameter # NIL DO
  11595. IF parameter.NeedsTrace() & ~IsVariableParameter(parameter) THEN
  11596. RETURN TRUE;
  11597. END;
  11598. parameter := parameter.nextParameter;
  11599. END;
  11600. RETURN FALSE;
  11601. END HasPointers;
  11602. PROCEDURE IsVariableParameter (parameter: SyntaxTree.Parameter): BOOLEAN;
  11603. BEGIN RETURN (parameter.kind = SyntaxTree.VarParameter) OR (parameter.kind = SyntaxTree.ConstParameter) & ((parameter.type.resolved IS SyntaxTree.RecordType) OR (parameter.type.resolved IS SyntaxTree.ArrayType));
  11604. END IsVariableParameter;
  11605. PROCEDURE HasVariableParameters(scope: SyntaxTree.ProcedureScope): BOOLEAN;
  11606. VAR parameter: SyntaxTree.Parameter;
  11607. BEGIN
  11608. parameter := scope.ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  11609. WHILE parameter # NIL DO
  11610. IF IsVariableParameter (parameter) THEN RETURN TRUE END;
  11611. IF parameter.movable THEN RETURN TRUE END;
  11612. parameter := parameter.nextParameter;
  11613. END;
  11614. RETURN scope.ownerProcedure.type(SyntaxTree.ProcedureType).returnParameter # NIL;
  11615. END HasVariableParameters;
  11616. PROCEDURE HasExplicitTraceMethod(recordType: SyntaxTree.RecordType): BOOLEAN;
  11617. BEGIN
  11618. IF (recordType.pointerType # NIL) & ~recordType.pointerType.isPlain THEN RETURN FALSE END;
  11619. RETURN (recordType.recordScope.firstProcedure # NIL) & Basic.StringEqual (Basic.MakeString ("Trace"), recordType.recordScope.firstProcedure.name);
  11620. END HasExplicitTraceMethod;
  11621. PROCEDURE IsIntegerConstant(expression: SyntaxTree.Expression; VAR val: HUGEINT): BOOLEAN;
  11622. BEGIN
  11623. IF expression.resolved # NIL THEN expression := expression.resolved END;
  11624. IF (expression IS SyntaxTree.IntegerValue) THEN
  11625. val := expression(SyntaxTree.IntegerValue).value;
  11626. RETURN TRUE
  11627. ELSE
  11628. RETURN FALSE
  11629. END;
  11630. END IsIntegerConstant;
  11631. PROCEDURE PowerOf2(val: HUGEINT; VAR exp: LONGINT): BOOLEAN;
  11632. BEGIN
  11633. IF val <= 0 THEN RETURN FALSE END;
  11634. exp := 0;
  11635. WHILE ~ODD(val) DO
  11636. val := val DIV 2;
  11637. INC(exp)
  11638. END;
  11639. RETURN val = 1
  11640. END PowerOf2;
  11641. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  11642. VAR procedure: SyntaxTree.Procedure;
  11643. BEGIN
  11644. procedure := record.recordScope.constructor;
  11645. IF procedure = NIL THEN
  11646. record := record.GetBaseRecord();
  11647. IF record # NIL THEN
  11648. procedure := GetConstructor(record)
  11649. END;
  11650. END;
  11651. RETURN procedure;
  11652. END GetConstructor;
  11653. PROCEDURE IsIntegerImmediate(CONST op: IntermediateCode.Operand; VAR value: LONGINT): BOOLEAN;
  11654. BEGIN
  11655. value := SHORT(op.intValue);
  11656. RETURN op.mode = IntermediateCode.ModeImmediate;
  11657. END IsIntegerImmediate;
  11658. (** whether a type strictily is a pointer to record or object type
  11659. (however, the basic type <<OBJECT>> is explicitly excluded) **)
  11660. PROCEDURE IsStrictlyPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  11661. BEGIN
  11662. IF type = NIL THEN
  11663. RETURN FALSE
  11664. ELSIF type.resolved IS SyntaxTree.PointerType THEN
  11665. RETURN type.resolved(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType
  11666. ELSE
  11667. RETURN FALSE
  11668. END
  11669. END IsStrictlyPointerToRecord;
  11670. PROCEDURE IsUnsafePointer(type: SyntaxTree.Type): BOOLEAN;
  11671. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & type.resolved(SyntaxTree.PointerType).isUnsafe
  11672. END IsUnsafePointer;
  11673. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  11674. BEGIN type := type.resolved;
  11675. IF type IS SyntaxTree.PointerType THEN
  11676. type := type(SyntaxTree.PointerType).pointerBase;
  11677. type := type.resolved;
  11678. IF type IS SyntaxTree.RecordType THEN
  11679. recordType := type(SyntaxTree.RecordType);
  11680. RETURN TRUE
  11681. ELSE
  11682. RETURN FALSE
  11683. END
  11684. ELSIF type IS SyntaxTree.RecordType THEN
  11685. recordType := type(SyntaxTree.RecordType);
  11686. RETURN type(SyntaxTree.RecordType).pointerType # NIL
  11687. ELSIF type IS SyntaxTree.ObjectType THEN
  11688. RETURN TRUE
  11689. ELSIF type IS SyntaxTree.AnyType THEN
  11690. RETURN TRUE (*! potentially is a pointer to record, treat it this way?? *)
  11691. ELSE
  11692. RETURN FALSE
  11693. END;
  11694. END IsPointerToRecord;
  11695. PROCEDURE IsArrayOfSystemByte(type: SyntaxTree.Type): BOOLEAN;
  11696. BEGIN
  11697. type := type.resolved;
  11698. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open)
  11699. & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType);
  11700. END IsArrayOfSystemByte;
  11701. PROCEDURE IsOpenArray(type: SyntaxTree.Type): BOOLEAN;
  11702. BEGIN
  11703. IF type = NIL THEN RETURN FALSE END;
  11704. type := type.resolved;
  11705. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open);
  11706. END IsOpenArray;
  11707. PROCEDURE IsStaticArray(type: SyntaxTree.Type): BOOLEAN;
  11708. BEGIN
  11709. IF type = NIL THEN RETURN FALSE END;
  11710. type := type.resolved;
  11711. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static);
  11712. END IsStaticArray;
  11713. PROCEDURE StaticArrayNumElements(type: SyntaxTree.Type): LONGINT;
  11714. VAR size: LONGINT;
  11715. BEGIN
  11716. size := 1;
  11717. WHILE (IsStaticArray(type)) DO
  11718. size := size * type.resolved(SyntaxTree.ArrayType).staticLength;
  11719. type := type.resolved(SyntaxTree.ArrayType).arrayBase;
  11720. END;
  11721. RETURN size;
  11722. END StaticArrayNumElements;
  11723. PROCEDURE StaticArrayBaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  11724. BEGIN
  11725. WHILE (IsStaticArray(type)) DO
  11726. type := type.resolved(SyntaxTree.ArrayType).arrayBase;
  11727. END;
  11728. RETURN type;
  11729. END StaticArrayBaseType;
  11730. PROCEDURE ArrayBaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  11731. BEGIN
  11732. WHILE (type.resolved IS SyntaxTree.ArrayType) DO
  11733. type := type.resolved(SyntaxTree.ArrayType).arrayBase;
  11734. END;
  11735. RETURN type;
  11736. END ArrayBaseType;
  11737. PROCEDURE IsDelegate(type: SyntaxTree.Type): BOOLEAN;
  11738. BEGIN
  11739. IF type = NIL THEN RETURN FALSE END;
  11740. type := type.resolved;
  11741. RETURN (type IS SyntaxTree.ProcedureType) & (type(SyntaxTree.ProcedureType).isDelegate)
  11742. END IsDelegate;
  11743. PROCEDURE DynamicDim(type:SyntaxTree.Type): LONGINT;
  11744. VAR i: LONGINT;
  11745. BEGIN
  11746. i := 0; type := type.resolved;
  11747. WHILE(type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form # SyntaxTree.Static) DO
  11748. INC(i);
  11749. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  11750. END;
  11751. WHILE(type # NIL) & (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) DO
  11752. INC(i);
  11753. type := type(SyntaxTree.MathArrayType).arrayBase;
  11754. IF type # NIL THEN type := type.resolved END;
  11755. END;
  11756. RETURN i
  11757. END DynamicDim;
  11758. PROCEDURE StaticSize(system: Global.System; type: SyntaxTree.Type): LONGINT;
  11759. BEGIN
  11760. WHILE (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form # SyntaxTree.Static) DO
  11761. type := type(SyntaxTree.ArrayType).arrayBase;
  11762. END;
  11763. WHILE (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) DO
  11764. type := type(SyntaxTree.MathArrayType).arrayBase;
  11765. END;
  11766. RETURN ToMemoryUnits(system,system.AlignedSizeOf(type));
  11767. END StaticSize;
  11768. PROCEDURE IsImmediate(x: IntermediateCode.Operand): BOOLEAN;
  11769. BEGIN
  11770. RETURN (x.mode = IntermediateCode.ModeImmediate) & (x.symbol.name = "");
  11771. END IsImmediate;
  11772. PROCEDURE IsAddress(x: IntermediateCode.Operand): BOOLEAN;
  11773. BEGIN
  11774. RETURN (x.mode = IntermediateCode.ModeImmediate) & (x.symbol.name # "")
  11775. END IsAddress;
  11776. PROCEDURE IsRegister(x: IntermediateCode.Operand): BOOLEAN;
  11777. BEGIN
  11778. RETURN (x.mode = IntermediateCode.ModeRegister);
  11779. END IsRegister;
  11780. PROCEDURE GetRecordTypeName(recordType: SyntaxTree.RecordType; VAR name: Basic.SegmentedName);
  11781. VAR typeDeclaration: SyntaxTree.TypeDeclaration;
  11782. BEGIN
  11783. typeDeclaration := recordType.typeDeclaration;
  11784. IF typeDeclaration = NIL THEN typeDeclaration := recordType.pointerType.resolved.typeDeclaration END; (* for compatibility with paco *)
  11785. Global.GetSymbolSegmentedName(typeDeclaration,name);
  11786. END GetRecordTypeName;
  11787. PROCEDURE ParametersSize(system: Global.System; procedureType: SyntaxTree.ProcedureType; isNested: BOOLEAN): LONGINT;
  11788. VAR parSize: LONGINT; parameter: SyntaxTree.Parameter;
  11789. BEGIN
  11790. parSize := 0;
  11791. IF StructuredReturnType(procedureType) THEN
  11792. parameter := procedureType.returnParameter;
  11793. INC(parSize,system.SizeOfParameter(parameter));
  11794. parSize := parSize + (-parSize) MOD system.addressSize;
  11795. END;
  11796. parameter :=procedureType.lastParameter;
  11797. WHILE (parameter # NIL) DO
  11798. INC(parSize,system.SizeOfParameter(parameter));
  11799. parSize := parSize + (-parSize) MOD system.addressSize;
  11800. parameter := parameter.prevParameter;
  11801. END;
  11802. IF procedureType.isDelegate THEN INC(parSize,system.addressSize) END; (* method => self pointer *)
  11803. IF isNested THEN INC(parSize,system.addressSize) END; (* nested procedure => static base *)
  11804. RETURN ToMemoryUnits(system,parSize)
  11805. END ParametersSize;
  11806. PROCEDURE ReturnedAsParameter(type: SyntaxTree.Type): BOOLEAN;
  11807. BEGIN
  11808. IF type = NIL THEN RETURN FALSE
  11809. ELSE
  11810. type := type.resolved;
  11811. RETURN (type IS SyntaxTree.RecordType) OR (type IS SyntaxTree.RangeType) OR (type IS SyntaxTree.ComplexType) OR (type IS SyntaxTree.ProcedureType) OR SemanticChecker.IsPointerType(type)
  11812. OR (type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType);
  11813. END
  11814. END ReturnedAsParameter;
  11815. PROCEDURE StructuredReturnType(procedureType: SyntaxTree.ProcedureType): BOOLEAN;
  11816. BEGIN
  11817. RETURN (procedureType # NIL) & (procedureType.callingConvention=SyntaxTree.OberonCallingConvention) & ReturnedAsParameter(procedureType.returnType);
  11818. END StructuredReturnType;
  11819. PROCEDURE IsNested(procedure: SyntaxTree.Procedure): BOOLEAN;
  11820. BEGIN
  11821. RETURN (procedure.scope IS SyntaxTree.ProcedureScope) & (procedure.externalName = NIL);
  11822. END IsNested;
  11823. PROCEDURE InCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  11824. BEGIN
  11825. WHILE (scope # NIL) & ~(scope IS SyntaxTree.CellScope) DO
  11826. scope := scope.outerScope;
  11827. END;
  11828. RETURN scope # NIL;
  11829. END InCellScope;
  11830. PROCEDURE ProcedureParametersSize*(system: Global.System; procedure: SyntaxTree.Procedure): LONGINT;
  11831. BEGIN
  11832. (*IF (procedure.scope IS SyntaxTree.CellScope) & (procedure = procedure.scope(SyntaxTree.CellScope).constructor) & ~backend.cellsAreObjects THEN
  11833. RETURN 0
  11834. ELSE
  11835. *)
  11836. RETURN ParametersSize(system,procedure.type(SyntaxTree.ProcedureType),IsNested(procedure));
  11837. (*END;*)
  11838. END ProcedureParametersSize;
  11839. PROCEDURE ToMemoryUnits*(system: Global.System; size: LONGINT): LONGINT;
  11840. VAR dataUnit: LONGINT;
  11841. BEGIN dataUnit := system.dataUnit;
  11842. ASSERT(size MOD system.dataUnit = 0);
  11843. RETURN size DIV system.dataUnit
  11844. END ToMemoryUnits;
  11845. PROCEDURE Get*(): Backend.Backend;
  11846. VAR backend: IntermediateBackend;
  11847. BEGIN NEW(backend); RETURN backend
  11848. END Get;
  11849. PROCEDURE Nop(position: LONGINT):IntermediateCode.Instruction;
  11850. VAR instruction: IntermediateCode.Instruction;
  11851. BEGIN
  11852. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.nop,emptyOperand,emptyOperand,emptyOperand);
  11853. RETURN instruction
  11854. END Nop;
  11855. PROCEDURE Mov(position: LONGINT;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  11856. VAR instruction: IntermediateCode.Instruction;
  11857. BEGIN
  11858. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.mov,dest,src,emptyOperand);
  11859. RETURN instruction
  11860. END Mov;
  11861. (* like Mov but ensures that no new register will be allocated for dest *)
  11862. PROCEDURE MovReplace(position: LONGINT;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  11863. VAR instruction: IntermediateCode.Instruction;
  11864. BEGIN
  11865. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.mov,dest,src,dest);
  11866. RETURN instruction
  11867. END MovReplace;
  11868. PROCEDURE Conv(position: LONGINT;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  11869. VAR instruction: IntermediateCode.Instruction;
  11870. BEGIN
  11871. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.conv,dest,src,emptyOperand);
  11872. RETURN instruction
  11873. END Conv;
  11874. PROCEDURE Call*(position: LONGINT;op: IntermediateCode.Operand; parSize: LONGINT): IntermediateCode.Instruction;
  11875. VAR instruction: IntermediateCode.Instruction;
  11876. BEGIN
  11877. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.call,op,IntermediateCode.Number(parSize),emptyOperand);
  11878. RETURN instruction
  11879. END Call;
  11880. PROCEDURE Exit(position: LONGINT;pcOffset: LONGINT; callingConvention: LONGINT): IntermediateCode.Instruction;
  11881. VAR op1, op2: IntermediateCode.Operand;
  11882. VAR instruction: IntermediateCode.Instruction;
  11883. BEGIN
  11884. IntermediateCode.InitNumber(op1,pcOffset);
  11885. IntermediateCode.InitNumber(op2,callingConvention);
  11886. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.exit,op1,op2,emptyOperand);
  11887. RETURN instruction
  11888. END Exit;
  11889. PROCEDURE Return(position: LONGINT;res: IntermediateCode.Operand): IntermediateCode.Instruction;
  11890. VAR instruction: IntermediateCode.Instruction;
  11891. BEGIN
  11892. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.return,res,emptyOperand,emptyOperand);
  11893. RETURN instruction
  11894. END Return;
  11895. PROCEDURE Result*(position: LONGINT;res: IntermediateCode.Operand): IntermediateCode.Instruction;
  11896. VAR instruction: IntermediateCode.Instruction;
  11897. BEGIN
  11898. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.result,res,emptyOperand,emptyOperand);
  11899. RETURN instruction
  11900. END Result;
  11901. PROCEDURE Trap(position: LONGINT;nr: LONGINT): IntermediateCode.Instruction;
  11902. VAR op1: IntermediateCode.Operand;
  11903. VAR instruction: IntermediateCode.Instruction;
  11904. BEGIN
  11905. IntermediateCode.InitNumber(op1,nr);
  11906. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.trap,op1,emptyOperand,emptyOperand);
  11907. RETURN instruction
  11908. END Trap;
  11909. PROCEDURE Br(position: LONGINT;dest: IntermediateCode.Operand): IntermediateCode.Instruction;
  11910. VAR instruction: IntermediateCode.Instruction;
  11911. BEGIN
  11912. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.br,dest,emptyOperand,emptyOperand);
  11913. RETURN instruction
  11914. END Br;
  11915. PROCEDURE Breq(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11916. VAR instruction: IntermediateCode.Instruction;
  11917. BEGIN
  11918. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.breq,dest,left,right);
  11919. RETURN instruction
  11920. END Breq;
  11921. PROCEDURE Brne(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11922. VAR instruction: IntermediateCode.Instruction;
  11923. BEGIN
  11924. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.brne,dest,left,right);
  11925. RETURN instruction
  11926. END Brne;
  11927. PROCEDURE Brge(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11928. VAR instruction: IntermediateCode.Instruction;
  11929. BEGIN
  11930. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.brge,dest,left,right);
  11931. RETURN instruction
  11932. END Brge;
  11933. PROCEDURE Brlt(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11934. VAR instruction: IntermediateCode.Instruction;
  11935. BEGIN
  11936. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.brlt,dest,left,right);
  11937. RETURN instruction
  11938. END Brlt;
  11939. PROCEDURE Pop*(position: LONGINT;op:IntermediateCode.Operand): IntermediateCode.Instruction;
  11940. VAR instruction: IntermediateCode.Instruction;
  11941. BEGIN
  11942. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.pop,op,emptyOperand,emptyOperand);
  11943. RETURN instruction
  11944. END Pop;
  11945. PROCEDURE Push*(position: LONGINT;op: IntermediateCode.Operand): IntermediateCode.Instruction;
  11946. VAR instruction: IntermediateCode.Instruction;
  11947. BEGIN
  11948. ASSERT(op.mode # IntermediateCode.Undefined);
  11949. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.push,op,emptyOperand,emptyOperand);
  11950. RETURN instruction
  11951. END Push;
  11952. PROCEDURE Neg(position: LONGINT;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  11953. VAR instruction: IntermediateCode.Instruction;
  11954. BEGIN
  11955. IntermediateCode.InitInstruction(instruction,position, IntermediateCode.neg,dest,src,emptyOperand);
  11956. RETURN instruction
  11957. END Neg;
  11958. PROCEDURE Not(position: LONGINT;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  11959. VAR instruction: IntermediateCode.Instruction;
  11960. BEGIN
  11961. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.not,dest,src,emptyOperand);
  11962. RETURN instruction
  11963. END Not;
  11964. PROCEDURE Abs(position: LONGINT;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  11965. VAR instruction: IntermediateCode.Instruction;
  11966. BEGIN
  11967. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.abs,dest,src,emptyOperand);
  11968. RETURN instruction
  11969. END Abs;
  11970. PROCEDURE Mul(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11971. VAR instruction: IntermediateCode.Instruction;
  11972. BEGIN
  11973. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.mul,dest,left,right);
  11974. RETURN instruction
  11975. END Mul;
  11976. PROCEDURE Div(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11977. VAR instruction: IntermediateCode.Instruction;
  11978. BEGIN
  11979. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.div,dest,left,right);
  11980. RETURN instruction
  11981. END Div;
  11982. PROCEDURE Mod(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11983. VAR instruction: IntermediateCode.Instruction;
  11984. BEGIN
  11985. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.mod,dest,left,right);
  11986. RETURN instruction
  11987. END Mod;
  11988. PROCEDURE Sub(position: LONGINT;dest: IntermediateCode.Operand; left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11989. VAR instruction: IntermediateCode.Instruction;
  11990. BEGIN
  11991. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.sub,dest,left,right);
  11992. RETURN instruction
  11993. END Sub;
  11994. PROCEDURE Add(position: LONGINT;dest: IntermediateCode.Operand; left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11995. VAR instruction: IntermediateCode.Instruction;
  11996. BEGIN
  11997. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.add,dest,left,right);
  11998. RETURN instruction
  11999. END Add;
  12000. PROCEDURE And(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12001. VAR instruction: IntermediateCode.Instruction;
  12002. BEGIN
  12003. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.and,dest,left,right);
  12004. RETURN instruction
  12005. END And;
  12006. PROCEDURE Or(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12007. VAR instruction: IntermediateCode.Instruction;
  12008. BEGIN
  12009. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.or,dest,left,right);
  12010. RETURN instruction
  12011. END Or;
  12012. PROCEDURE Xor(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12013. VAR instruction: IntermediateCode.Instruction;
  12014. BEGIN
  12015. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.xor,dest,left,right);
  12016. RETURN instruction
  12017. END Xor;
  12018. PROCEDURE Shl(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12019. VAR instruction: IntermediateCode.Instruction;
  12020. BEGIN
  12021. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.shl,dest,left, IntermediateCode.ToUnsigned(right));
  12022. RETURN instruction
  12023. END Shl;
  12024. PROCEDURE Shr(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12025. VAR instruction: IntermediateCode.Instruction;
  12026. BEGIN
  12027. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.shr,dest,left, IntermediateCode.ToUnsigned(right));
  12028. RETURN instruction
  12029. END Shr;
  12030. PROCEDURE Rol(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12031. VAR instruction: IntermediateCode.Instruction;
  12032. BEGIN
  12033. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.rol,dest,left, IntermediateCode.ToUnsigned(right));
  12034. RETURN instruction
  12035. END Rol;
  12036. PROCEDURE Ror(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12037. VAR instruction: IntermediateCode.Instruction;
  12038. BEGIN
  12039. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.ror,dest,left, IntermediateCode.ToUnsigned(right));
  12040. RETURN instruction
  12041. END Ror;
  12042. PROCEDURE Cas(position: LONGINT;dest,src,size: IntermediateCode.Operand): IntermediateCode.Instruction;
  12043. VAR instruction: IntermediateCode.Instruction;
  12044. BEGIN
  12045. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.cas,dest,src,size);
  12046. RETURN instruction
  12047. END Cas;
  12048. PROCEDURE Copy(position: LONGINT;dest,src,size: IntermediateCode.Operand): IntermediateCode.Instruction;
  12049. VAR instruction: IntermediateCode.Instruction;
  12050. BEGIN
  12051. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.copy,dest,src,size);
  12052. RETURN instruction
  12053. END Copy;
  12054. PROCEDURE Fill(position: LONGINT;dest,size, value: IntermediateCode.Operand): IntermediateCode.Instruction;
  12055. VAR instruction: IntermediateCode.Instruction;
  12056. BEGIN
  12057. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.fill,dest,size,value);
  12058. RETURN instruction
  12059. END Fill;
  12060. PROCEDURE Asm(position: LONGINT;s: SyntaxTree.SourceCode; inRules, outRules: IntermediateCode.Rules): IntermediateCode.Instruction;
  12061. VAR instruction: IntermediateCode.Instruction; string, o1, o2: IntermediateCode.Operand;
  12062. BEGIN
  12063. string := IntermediateCode.String(s);
  12064. IntermediateCode.SetIntValue(string,position); (* for error reporting *)
  12065. IF inRules # NIL THEN IntermediateCode.InitRule(o1, inRules) ELSE o1 := emptyOperand END;
  12066. IF outRules # NIL THEN IntermediateCode.InitRule(o2, outRules) ELSE o2 := emptyOperand END;
  12067. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.asm,string,o1,o2);
  12068. RETURN instruction
  12069. END Asm;
  12070. PROCEDURE Data*(position: LONGINT;op: IntermediateCode.Operand): IntermediateCode.Instruction;
  12071. VAR instruction: IntermediateCode.Instruction;
  12072. BEGIN
  12073. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.data,op,emptyOperand,emptyOperand);
  12074. RETURN instruction
  12075. END Data;
  12076. PROCEDURE SpecialInstruction(position: LONGINT;subtype: SHORTINT; op1,op2,op3: IntermediateCode.Operand): IntermediateCode.Instruction;
  12077. VAR instruction: IntermediateCode.Instruction;
  12078. BEGIN
  12079. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.special,op1,op2,op3);
  12080. IntermediateCode.SetSubType(instruction, subtype);
  12081. RETURN instruction
  12082. END SpecialInstruction;
  12083. PROCEDURE Reserve(position: LONGINT;units: LONGINT): IntermediateCode.Instruction;
  12084. VAR op1: IntermediateCode.Operand;
  12085. VAR instruction: IntermediateCode.Instruction;
  12086. BEGIN
  12087. (*! generate a warning if size exceeds a certain limit *)
  12088. (*
  12089. ASSERT(bytes < 1000000); (* sanity check *)
  12090. *)
  12091. ASSERT(0 <= units); (* sanity check *)
  12092. IntermediateCode.InitNumber(op1,units);
  12093. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.reserve,op1,emptyOperand,emptyOperand);
  12094. RETURN instruction
  12095. END Reserve;
  12096. PROCEDURE LabelInstruction(position: LONGINT): IntermediateCode.Instruction;
  12097. VAR op1: IntermediateCode.Operand;
  12098. VAR instruction: IntermediateCode.Instruction;
  12099. BEGIN
  12100. IntermediateCode.InitNumber(op1,position);
  12101. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.label,op1,emptyOperand,emptyOperand);
  12102. RETURN instruction
  12103. END LabelInstruction;
  12104. PROCEDURE EnterImmediate*(data: IntermediateCode.Section; CONST vop: IntermediateCode.Operand): LONGINT;
  12105. VAR pc: LONGINT;
  12106. PROCEDURE ProvidesValue(CONST instr: IntermediateCode.Instruction; op: IntermediateCode.Operand): BOOLEAN;
  12107. BEGIN
  12108. IF instr.opcode # IntermediateCode.data THEN RETURN FALSE END;
  12109. ASSERT(instr.op1.mode = IntermediateCode.ModeImmediate);
  12110. IF instr.op1.type.sizeInBits # op.type.sizeInBits THEN RETURN FALSE END;
  12111. IF instr.op1.type.form # op.type.form THEN RETURN FALSE END;
  12112. IF instr.op1.type.form = IntermediateCode.Float THEN
  12113. RETURN instr.op1.floatValue = op.floatValue
  12114. ELSE
  12115. RETURN instr.op1.intValue = op.intValue
  12116. END;
  12117. END ProvidesValue;
  12118. BEGIN
  12119. ASSERT(vop.mode = IntermediateCode.ModeImmediate);
  12120. pc := 0;
  12121. WHILE (pc<data.pc) & ~ProvidesValue(data.instructions[pc],vop) DO
  12122. INC(pc);
  12123. END;
  12124. IF pc = data.pc THEN
  12125. data.Emit(Data(-1,vop));
  12126. END;
  12127. RETURN pc
  12128. END EnterImmediate;
  12129. PROCEDURE Init;
  12130. VAR i: LONGINT; name: SyntaxTree.IdentifierString;
  12131. BEGIN
  12132. int8 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits8);
  12133. int16 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits16);
  12134. int32 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits32);
  12135. int64 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits64);
  12136. uint8 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits8);
  12137. uint16 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits16);
  12138. uint32 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits32);
  12139. uint64 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits64);
  12140. float32 := IntermediateCode.NewType(IntermediateCode.Float,IntermediateCode.Bits32);
  12141. float64 := IntermediateCode.NewType(IntermediateCode.Float,IntermediateCode.Bits64);
  12142. IntermediateCode.InitOperand(emptyOperand);
  12143. FOR i := 0 TO NumberSystemCalls-1 DO
  12144. name := "@SystemCall";
  12145. Basic.AppendNumber(name,i);
  12146. systemCalls[i] := SyntaxTree.NewSymbol(SyntaxTree.NewIdentifier(name));
  12147. END;
  12148. END Init;
  12149. PROCEDURE IsExported(symbol: SyntaxTree.Symbol): BOOLEAN;
  12150. BEGIN
  12151. RETURN (symbol # NIL) & (symbol.access * SyntaxTree.Public # {})
  12152. END IsExported;
  12153. BEGIN
  12154. Init;
  12155. END FoxIntermediateBackend.
  12156. Compiler.Compile FoxIntermediateBackend.Mod ~