ninl.pas 252 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308
  1. {
  2. Copyright (c) 1998-2007 by Florian Klaempfl
  3. Type checking and register allocation for inline nodes
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ninl;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,htypechk,symtype,compinnr;
  22. type
  23. TInlineNodeFlag = (
  24. inf_inlineconst
  25. );
  26. TInlineNodeFlags = set of TInlineNodeFlag;
  27. tinlinenode = class(tunarynode)
  28. inlinenodeflags : TInlineNodeFlags;
  29. inlinenumber : tinlinenumber;
  30. constructor create(number : tinlinenumber;is_const:boolean;l : tnode);virtual;
  31. constructor createintern(number : tinlinenumber;is_const:boolean;l : tnode);virtual;
  32. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  33. procedure ppuwrite(ppufile:tcompilerppufile);override;
  34. function dogetcopy : tnode;override;
  35. procedure printnodeinfo(var t : text);override;
  36. {$ifdef DEBUG_NODE_XML}
  37. procedure XMLPrintNodeInfo(var t : text);override;
  38. {$endif DEBUG_NODE_XML}
  39. function pass_1 : tnode;override;
  40. function pass_typecheck:tnode;override;
  41. function pass_typecheck_cpu:tnode;virtual;
  42. function simplify(forinline : boolean): tnode;override;
  43. function docompare(p: tnode): boolean; override;
  44. procedure mark_write;override;
  45. { returns a node tree where the inc/dec are replaced by add/sub }
  46. function getaddsub_for_incdec : tnode;
  47. { pack and unpack are changed into for-loops by the compiler }
  48. function first_pack_unpack: tnode; virtual;
  49. property parameters : tnode read left write left;
  50. function may_have_sideeffect_norecurse: boolean;
  51. function may_ignore_result:boolean;
  52. protected
  53. { All the following routines currently
  54. call compilerprocs, unless they are
  55. overridden in which case, the code
  56. generator handles them.
  57. }
  58. function first_pi: tnode ; virtual;
  59. function first_arctan_real: tnode; virtual;
  60. function first_abs_real: tnode; virtual;
  61. function first_sqr_real: tnode; virtual;
  62. function first_sqrt_real: tnode; virtual;
  63. function first_ln_real: tnode; virtual;
  64. function first_cos_real: tnode; virtual;
  65. function first_sin_real: tnode; virtual;
  66. function first_exp_real: tnode; virtual;
  67. function first_frac_real: tnode; virtual;
  68. function first_round_real: tnode; virtual;
  69. function first_trunc_real: tnode; virtual;
  70. function first_int_real: tnode; virtual;
  71. function first_abs_long: tnode; virtual;
  72. function first_IncDec: tnode; virtual;
  73. function first_IncludeExclude: tnode; virtual;
  74. function first_get_frame: tnode; virtual;
  75. function first_setlength: tnode; virtual;
  76. function first_copy: tnode; virtual;
  77. { This one by default generates an internal error, because such
  78. nodes are not generated by the parser. It's however used internally
  79. by the JVM backend to create new dynamic arrays. }
  80. function first_new: tnode; virtual;
  81. function first_length: tnode; virtual;
  82. function first_high: tnode; virtual;
  83. function first_box: tnode; virtual; abstract;
  84. function first_unbox: tnode; virtual; abstract;
  85. function first_assigned: tnode; virtual;
  86. function first_assert: tnode; virtual;
  87. function first_popcnt: tnode; virtual;
  88. function first_bitscan: tnode; virtual;
  89. { override these for Seg() support }
  90. function typecheck_seg: tnode; virtual;
  91. function first_seg: tnode; virtual;
  92. function first_sar: tnode; virtual;
  93. function first_fma : tnode; virtual;
  94. function first_minmax: tnode; virtual;
  95. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  96. function first_ShiftRot_assign_64bitint: tnode; virtual;
  97. {$endif not cpu64bitalu and not cpuhighleveltarget}
  98. function first_AndOrXorShiftRot_assign: tnode; virtual;
  99. function first_NegNot_assign: tnode; virtual;
  100. function first_atomic:tnode;virtual;
  101. function first_cpu : tnode; virtual;
  102. procedure CheckParameters(count : integer);
  103. private
  104. function handle_str: tnode;
  105. function handle_reset_rewrite_typed: tnode;
  106. function handle_text_read_write(filepara,params:Ttertiarynode;var newstatement:Tnode):boolean;
  107. function handle_typed_read_write(filepara,params:Ttertiarynode;var newstatement:Tnode):boolean;
  108. function handle_read_write: tnode;
  109. function handle_val: tnode;
  110. function handle_default: tnode;
  111. function handle_setlength: tnode;
  112. function handle_copy: tnode;
  113. function handle_box: tnode;
  114. function handle_unbox: tnode;
  115. function handle_insert:tnode;
  116. function handle_delete:tnode;
  117. function handle_concat:tnode;
  118. end;
  119. tinlinenodeclass = class of tinlinenode;
  120. var
  121. cinlinenode : tinlinenodeclass = tinlinenode;
  122. function geninlinenode(number : tinlinenumber;is_const:boolean;l : tnode) : tinlinenode;
  123. implementation
  124. uses
  125. verbose,globals,systems,constexp,
  126. globtype,cutils,cclasses,fmodule,
  127. symconst,symdef,symsym,symcpu,symtable,paramgr,defcmp,defutil,symbase,
  128. cpuinfo,cpubase,
  129. pass_1,ppu,
  130. ncal,ncon,ncnv,nadd,nld,nbas,nflw,nmem,nmat,nutils,ngenutil,
  131. nobjc,objcdef,
  132. cgbase,procinfo;
  133. function geninlinenode(number : tinlinenumber;is_const:boolean;l : tnode) : tinlinenode;
  134. begin
  135. geninlinenode:=cinlinenode.create(number,is_const,l);
  136. end;
  137. {*****************************************************************************
  138. TINLINENODE
  139. *****************************************************************************}
  140. constructor tinlinenode.create(number : tinlinenumber;is_const:boolean;l : tnode);
  141. begin
  142. inherited create(inlinen,l);
  143. if is_const then
  144. inlinenodeflags:=[inf_inlineconst]
  145. else
  146. inlinenodeflags:=[];
  147. inlinenumber:=number;
  148. end;
  149. constructor tinlinenode.createintern(number : tinlinenumber; is_const : boolean;
  150. l : tnode);
  151. begin
  152. create(number,is_const,l);
  153. inlinenodeflags:=[];
  154. include(flags,nf_internal);
  155. end;
  156. constructor tinlinenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  157. begin
  158. inherited ppuload(t,ppufile);
  159. ppufile.getset(tppuset1(inlinenodeflags));
  160. inlinenumber:=tinlinenumber(ppufile.getlongint);
  161. end;
  162. procedure tinlinenode.ppuwrite(ppufile:tcompilerppufile);
  163. begin
  164. inherited ppuwrite(ppufile);
  165. ppufile.putset(tppuset1(inlinenodeflags));
  166. ppufile.putlongint(longint(inlinenumber));
  167. end;
  168. function tinlinenode.dogetcopy : tnode;
  169. var
  170. n : tinlinenode;
  171. begin
  172. n:=tinlinenode(inherited dogetcopy);
  173. n.inlinenodeflags:=inlinenodeflags;
  174. n.inlinenumber:=inlinenumber;
  175. result:=n;
  176. end;
  177. procedure tinlinenode.printnodeinfo(var t : text);
  178. begin
  179. inherited;
  180. write(t,', inlinenumber = ',inlinenumber);
  181. end;
  182. {$ifdef DEBUG_NODE_XML}
  183. procedure TInlineNode.XMLPrintNodeInfo(var T: Text);
  184. var
  185. i: TInlineNodeFlag;
  186. First: Boolean;
  187. begin
  188. inherited XMLPrintNodeInfo(T);
  189. First := True;
  190. for i in inlinenodeflags do
  191. begin
  192. if First then
  193. begin
  194. Write(T, ' inlinenodeflags="', i);
  195. First := False;
  196. end
  197. else
  198. Write(T, ',', i)
  199. end;
  200. if not First then
  201. Write(T, '"');
  202. Write(T, ' inlinenumber="', inlinenumber, '"');
  203. end;
  204. {$endif DEBUG_NODE_XML}
  205. function get_str_int_func(def: tdef): string;
  206. var
  207. ordtype: tordtype;
  208. begin
  209. ordtype := torddef(def).ordtype;
  210. if not (ordtype in [scurrency,s64bit,u64bit,s32bit,u32bit,s16bit,u16bit,s8bit,u8bit]) then
  211. internalerror(2013032603);
  212. if is_oversizedord(def) then
  213. begin
  214. case ordtype of
  215. scurrency,
  216. s64bit: exit('int64');
  217. u64bit: exit('qword');
  218. s32bit: exit('longint');
  219. u32bit: exit('longword');
  220. s16bit: exit('smallint');
  221. u16bit: exit('word');
  222. else
  223. internalerror(2013032604);
  224. end;
  225. end
  226. else
  227. begin
  228. if is_nativeuint(def) then
  229. exit('uint')
  230. else
  231. exit('sint');
  232. end;
  233. internalerror(2013032605);
  234. end;
  235. function tinlinenode.handle_str : tnode;
  236. var
  237. lenpara,
  238. fracpara,
  239. newparas,
  240. tmppara,
  241. dest,
  242. source : tcallparanode;
  243. procname: string;
  244. is_real,is_enum : boolean;
  245. rt : aint;
  246. intrinsiccode: TInlineNumber;
  247. begin
  248. result := cerrornode.create;
  249. { get destination string }
  250. dest := tcallparanode(left);
  251. { get source para (number) }
  252. source := dest;
  253. while assigned(source.right) do
  254. source := tcallparanode(source.right);
  255. { destination parameter must be a normal (not a colon) parameter, this
  256. check is needed because str(v:len) also has 2 parameters }
  257. if (source=dest) or
  258. (cpf_is_colon_para in tcallparanode(dest).callparaflags) then
  259. begin
  260. CGMessage1(parser_e_wrong_parameter_size,'Str');
  261. exit;
  262. end;
  263. { in case we are in a generic definition, we cannot
  264. do all checks, the parameters might be type parameters,
  265. bailout as well in case of an error before }
  266. if (df_generic in current_procinfo.procdef.defoptions) or
  267. (dest.resultdef.typ=errordef) or
  268. (source.resultdef.typ=errordef) then
  269. begin
  270. result.Free;
  271. result:=nil;
  272. resultdef:=voidtype;
  273. exit;
  274. end;
  275. is_real:=(source.resultdef.typ = floatdef) or is_currency(source.resultdef);
  276. is_enum:=source.left.resultdef.typ=enumdef;
  277. if ((dest.left.resultdef.typ<>stringdef) and
  278. not(is_chararray(dest.left.resultdef))) or
  279. not(is_real or is_enum or
  280. (source.left.resultdef.typ=orddef)) then
  281. begin
  282. CGMessagePos1(source.fileinfo,
  283. type_e_integer_expr_expected,source.resultdef.typename);
  284. exit;
  285. end;
  286. { get len/frac parameters }
  287. lenpara := nil;
  288. fracpara := nil;
  289. if (cpf_is_colon_para in tcallparanode(dest.right).callparaflags) then
  290. begin
  291. lenpara := tcallparanode(dest.right);
  292. { we can let the callnode do the type checking of these parameters too, }
  293. { but then the error messages aren't as nice }
  294. if not is_integer(lenpara.resultdef) then
  295. begin
  296. CGMessagePos1(lenpara.fileinfo,
  297. type_e_integer_expr_expected,lenpara.resultdef.typename);
  298. exit;
  299. end;
  300. if (cpf_is_colon_para in tcallparanode(lenpara.right).callparaflags) then
  301. begin
  302. { parameters are in reverse order! }
  303. fracpara := lenpara;
  304. lenpara := tcallparanode(lenpara.right);
  305. if not is_real then
  306. begin
  307. CGMessagePos(lenpara.fileinfo,parser_e_illegal_colon_qualifier);
  308. exit
  309. end;
  310. if not is_integer(lenpara.resultdef) then
  311. begin
  312. CGMessagePos1(lenpara.fileinfo,
  313. type_e_integer_expr_expected,lenpara.resultdef.typename);
  314. exit;
  315. end;
  316. end;
  317. end;
  318. { generate the parameter list for the compilerproc }
  319. newparas := dest;
  320. { if we have a float parameter, insert the realtype, len and fracpara parameters }
  321. if is_real then
  322. begin
  323. { insert realtype parameter }
  324. if not is_currency(source.resultdef) then
  325. begin
  326. rt:=ord(tfloatdef(source.left.resultdef).floattype);
  327. newparas.right := ccallparanode.create(cordconstnode.create(
  328. rt,s32inttype,true),newparas.right);
  329. tmppara:=tcallparanode(newparas.right);
  330. end
  331. else
  332. tmppara:=newparas;
  333. { if necessary, insert a fraction parameter }
  334. if not assigned(fracpara) then
  335. begin
  336. tmppara.right := ccallparanode.create(
  337. cordconstnode.create(int64(-1),s32inttype,false),
  338. tmppara.right);
  339. fracpara := tcallparanode(tmppara.right);
  340. end;
  341. { if necessary, insert a length para }
  342. if not assigned(lenpara) then
  343. fracpara.right := ccallparanode.create(
  344. cordconstnode.create(int64(-32767),s32inttype,false),
  345. fracpara.right);
  346. end
  347. else if is_enum then
  348. begin
  349. {Insert a reference to the ord2string index.}
  350. newparas.right:=Ccallparanode.create(
  351. Caddrnode.create_internal(
  352. Crttinode.create(Tenumdef(source.left.resultdef),fullrtti,rdt_normal)
  353. ),
  354. newparas.right);
  355. {Insert a reference to the typinfo.}
  356. newparas.right:=Ccallparanode.create(
  357. Caddrnode.create_internal(
  358. Crttinode.create(Tenumdef(source.left.resultdef),fullrtti,rdt_ord2str)
  359. ),
  360. newparas.right);
  361. {Insert a type conversion from the enumeration to longint.}
  362. source.left:=Ctypeconvnode.create_internal(source.left,s32inttype);
  363. typecheckpass(source.left);
  364. { if necessary, insert a length para }
  365. if not assigned(lenpara) then
  366. Tcallparanode(Tcallparanode(newparas.right).right).right:=
  367. Ccallparanode.create(
  368. cordconstnode.create(int64(-1),s32inttype,false),
  369. Tcallparanode(Tcallparanode(newparas.right).right).right
  370. );
  371. end
  372. else
  373. { for a normal parameter, insert a only length parameter if one is missing }
  374. if not assigned(lenpara) then
  375. newparas.right := ccallparanode.create(cordconstnode.create(int64(-1),s32inttype,false),
  376. newparas.right);
  377. { remove the parameters from the original node so they won't get disposed, }
  378. { since they're reused }
  379. left := nil;
  380. intrinsiccode := Default(TInlineNumber);
  381. { create procedure name }
  382. if is_chararray(dest.resultdef) then
  383. procname:='fpc_chararray_'
  384. else
  385. procname := 'fpc_' + tstringdef(dest.resultdef).stringtypname+'_';
  386. if is_real then
  387. if is_currency(source.resultdef) then
  388. procname := procname + 'currency'
  389. else
  390. procname := procname + 'float'
  391. else if is_enum then
  392. procname:=procname+'enum'
  393. else
  394. case torddef(source.resultdef).ordtype of
  395. pasbool1,pasbool8,pasbool16,pasbool32,pasbool64,
  396. bool8bit,bool16bit,bool32bit,bool64bit:
  397. procname := procname + 'bool';
  398. scurrency,s64bit,u64bit,s32bit,u32bit,s16bit,u16bit,s8bit,u8bit:
  399. begin
  400. intrinsiccode := in_str_x_string;
  401. procname := procname + get_str_int_func(source.resultdef);
  402. end;
  403. else
  404. begin
  405. CGMessagePos1(source.fileinfo,
  406. type_e_integer_expr_expected,torddef(source.resultdef).typename);
  407. exit;
  408. end;
  409. end;
  410. { for ansistrings insert the encoding argument }
  411. if is_ansistring(dest.resultdef) then
  412. newparas:=ccallparanode.create(cordconstnode.create(
  413. getparaencoding(dest.resultdef),u16inttype,true),newparas);
  414. { free the errornode we generated in the beginning }
  415. result.free;
  416. { create the call node, }
  417. result := ccallnode.createfromintrinsic(intrinsiccode,procname,newparas);
  418. end;
  419. function tinlinenode.handle_default: tnode;
  420. function getdefaultvarsym(def:tdef):tnode;
  421. var
  422. hashedid : thashedidstring;
  423. srsym : tsym;
  424. srsymtable : tsymtable;
  425. defaultname : tidstring;
  426. varspez : tvarspez;
  427. begin
  428. if not assigned(def) or
  429. not (def.typ in [arraydef,recorddef,variantdef,objectdef,procvardef]) or
  430. ((def.typ=objectdef) and not is_object(def)) then
  431. internalerror(201202101);
  432. { extra '$' prefix because on darwin the result of makemangledname
  433. is prefixed by '_' and hence adding a '$' at the start of the
  434. prefix passed to makemangledname doesn't help (the whole point of
  435. the copy() operation below is to ensure that the id does not start
  436. with a '$', because that is interpreted specially by the symtable
  437. routines -- that's also why we prefix with '$_', so it will still
  438. work if make_mangledname() would somehow return a name that already
  439. starts with '$' }
  440. defaultname:='$_'+make_mangledname('zero',def.owner,def.typesym.Name);
  441. { can't hardcode the position of the '$', e.g. on darwin an underscore
  442. is added }
  443. hashedid.id:=copy(defaultname,2,255);
  444. { in case of a previous error, current_procinfo might not be set
  445. so avoid a crash in this case }
  446. if assigned(current_procinfo) then
  447. begin
  448. { the default sym is always part of the current procedure/function }
  449. srsymtable:=current_module.localsymtable;
  450. srsym:=tsym(srsymtable.findwithhash(hashedid));
  451. if not assigned(srsym) then
  452. begin
  453. varspez:=vs_const;
  454. { if we have an initialize or finalize management operator then
  455. we may not declare this as const as the unit init-/finalization
  456. needs to be able to modify it }
  457. if (def.typ=recorddef) and (mop_initialize in trecordsymtable(trecorddef(def).symtable).managementoperators) then
  458. varspez:=vs_var;
  459. { no valid default variable found, so create it }
  460. srsym:=cstaticvarsym.create(defaultname,varspez,def,[]);
  461. { mark the staticvarsym as typedconst }
  462. if varspez=vs_const then
  463. include(tabstractvarsym(srsym).varoptions,vo_is_typed_const);
  464. include(tabstractvarsym(srsym).varoptions,vo_is_default_var);
  465. { There is no reliable way to be sure that this symbol will not be used
  466. later on inside some inlined code, so mark it as global }
  467. include(tabstractvarsym(srsym).varoptions,vo_is_public);
  468. { The variable has a value assigned }
  469. tabstractvarsym(srsym).varstate:=vs_initialised;
  470. srsymtable.insertsym(srsym);
  471. cnodeutils.insertbssdata(tstaticvarsym(srsym));
  472. end;
  473. result:=cloadnode.create(srsym,srsymtable);
  474. end
  475. else
  476. result:=cerrornode.create;
  477. end;
  478. var
  479. def : tdef;
  480. begin
  481. if not assigned(left) or (left.nodetype<>typen) then
  482. internalerror(2012032102);
  483. def:=ttypenode(left).typedef;
  484. if assigned(current_procinfo) and
  485. (df_generic in current_procinfo.procdef.defoptions) then
  486. begin
  487. { don't allow as a default parameter value, because it may not be valid
  488. when specialising }
  489. if block_type<>bt_const then
  490. result:=cpointerconstnode.create(0,def)
  491. else
  492. result:=cerrornode.create;
  493. exit;
  494. end;
  495. result:=nil;
  496. case def.typ of
  497. enumdef,
  498. orddef:
  499. { don't do a rangecheck as Default will also return 0
  500. for the following types (Delphi compatible):
  501. TRange1 = -10..-5;
  502. TRange2 = 5..10;
  503. TEnum = (a:=5;b:=10); }
  504. result:=cordconstnode.create(0,def,false);
  505. classrefdef,
  506. pointerdef:
  507. result:=cpointerconstnode.create(0,def);
  508. procvardef:
  509. if tprocvardef(def).size<>sizeof(pint) then
  510. result:=getdefaultvarsym(def)
  511. else
  512. result:=cpointerconstnode.create(0,def);
  513. stringdef:
  514. result:=cstringconstnode.createstr('');
  515. floatdef:
  516. result:=crealconstnode.create(0,def);
  517. objectdef:
  518. begin
  519. if is_implicit_pointer_object_type(def) then
  520. result:=cpointerconstnode.create(0,def)
  521. else
  522. if is_object(def) then
  523. begin
  524. { Delphi does not recursively check whether
  525. an object contains unsupported types }
  526. if not (m_delphi in current_settings.modeswitches) and
  527. not is_valid_for_default(def) then
  528. Message(type_e_type_not_allowed_for_default);
  529. result:=getdefaultvarsym(def);
  530. end
  531. else
  532. Message(type_e_type_not_allowed_for_default);
  533. end;
  534. variantdef,
  535. recorddef:
  536. begin
  537. { Delphi does not recursively check whether a record
  538. contains unsupported types }
  539. if (def.typ=recorddef) and not (m_delphi in current_settings.modeswitches) and
  540. not is_valid_for_default(def) then
  541. Message(type_e_type_not_allowed_for_default);
  542. result:=getdefaultvarsym(def);
  543. end;
  544. setdef:
  545. begin
  546. result:=csetconstnode.create(nil,def);
  547. New(tsetconstnode(result).value_set);
  548. tsetconstnode(result).value_set^:=[];
  549. end;
  550. arraydef:
  551. begin
  552. { can other array types be parsed by single_type? }
  553. if ado_isdynamicarray in tarraydef(def).arrayoptions then
  554. result:=cpointerconstnode.create(0,def)
  555. else
  556. begin
  557. result:=getdefaultvarsym(def);
  558. end;
  559. end;
  560. undefineddef:
  561. begin
  562. if sp_generic_dummy in def.typesym.symoptions then
  563. begin
  564. { this matches the error messages that are printed
  565. in case of non-Delphi modes }
  566. Message(parser_e_no_generics_as_types);
  567. Message(type_e_type_id_expected);
  568. end
  569. else
  570. result:=cpointerconstnode.create(0,def);
  571. end;
  572. else
  573. Message(type_e_type_not_allowed_for_default);
  574. end;
  575. if not assigned(result) then
  576. result:=cerrornode.create;
  577. end;
  578. function tinlinenode.handle_reset_rewrite_typed: tnode;
  579. begin
  580. { since this is a "in_xxxx_typedfile" node, we can be sure we have }
  581. { a typed file as argument and we don't have to check it again (JM) }
  582. { add the recsize parameter }
  583. { iso mode extension with name? }
  584. if inlinenumber in [in_reset_typedfile_name,in_rewrite_typedfile_name] then
  585. begin
  586. left := ccallparanode.create(cordconstnode.create(
  587. tfiledef(tcallparanode(tcallparanode(left).nextpara).paravalue.resultdef).typedfiledef.size,s32inttype,true),left);
  588. end
  589. else
  590. begin
  591. { note: for some reason, the parameter of intern procedures with only one }
  592. { parameter is gets lifted out of its original tcallparanode (see round }
  593. { line 1306 of ncal.pas), so recreate a tcallparanode here (JM) }
  594. left := ccallparanode.create(cordconstnode.create(
  595. tfiledef(left.resultdef).typedfiledef.size,s32inttype,true),
  596. ccallparanode.create(left,nil));
  597. end;
  598. { create the correct call }
  599. if m_isolike_io in current_settings.modeswitches then
  600. begin
  601. case inlinenumber of
  602. in_reset_typedfile:
  603. result := ccallnode.createintern('fpc_reset_typed_iso',left);
  604. in_reset_typedfile_name:
  605. result := ccallnode.createintern('fpc_reset_typed_name_iso',left);
  606. in_rewrite_typedfile:
  607. result := ccallnode.createintern('fpc_rewrite_typed_iso',left);
  608. in_rewrite_typedfile_name:
  609. result := ccallnode.createintern('fpc_rewrite_typed_name_iso',left);
  610. else
  611. internalerror(2016101502);
  612. end;
  613. end
  614. else
  615. begin
  616. if inlinenumber=in_reset_typedfile then
  617. result := ccallnode.createintern('fpc_reset_typed',left)
  618. else
  619. result := ccallnode.createintern('fpc_rewrite_typed',left);
  620. end;
  621. { make sure left doesn't get disposed, since we use it in the new call }
  622. left := nil;
  623. end;
  624. procedure maybe_convert_to_string(var n: tnode);
  625. begin
  626. { stringconstnodes are arrays of char. It's much more }
  627. { efficient to write a constant string, so convert }
  628. { either to shortstring or ansistring depending on }
  629. { length }
  630. if (n.nodetype=stringconstn) then
  631. if is_chararray(n.resultdef) then
  632. if (tstringconstnode(n).len<=255) then
  633. inserttypeconv(n,cshortstringtype)
  634. else
  635. inserttypeconv(n,getansistringdef)
  636. else if is_widechararray(n.resultdef) then
  637. inserttypeconv(n,cunicodestringtype);
  638. end;
  639. procedure get_read_write_int_func(def: tdef; out func_suffix: string; out readfunctype: tdef);
  640. var
  641. ordtype: tordtype;
  642. begin
  643. ordtype := torddef(def).ordtype;
  644. if is_oversizedint(def) then
  645. begin
  646. case ordtype of
  647. s64bit:
  648. begin
  649. func_suffix := 'int64';
  650. readfunctype:=s64inttype;
  651. end;
  652. u64bit :
  653. begin
  654. func_suffix := 'qword';
  655. readfunctype:=u64inttype;
  656. end;
  657. s32bit:
  658. begin
  659. func_suffix := 'longint';
  660. readfunctype:=s32inttype;
  661. end;
  662. u32bit :
  663. begin
  664. func_suffix := 'longword';
  665. readfunctype:=u32inttype;
  666. end;
  667. s16bit:
  668. begin
  669. func_suffix := 'smallint';
  670. readfunctype:=s16inttype;
  671. end;
  672. u16bit :
  673. begin
  674. func_suffix := 'word';
  675. readfunctype:=u16inttype;
  676. end;
  677. else
  678. internalerror(2013032602);
  679. end;
  680. end
  681. else
  682. begin
  683. case ordtype of
  684. s64bit,
  685. s32bit,
  686. s16bit,
  687. s8bit:
  688. begin
  689. func_suffix := 'sint';
  690. readfunctype := sinttype;
  691. end;
  692. u64bit,
  693. u32bit,
  694. u16bit,
  695. u8bit:
  696. begin
  697. func_suffix := 'uint';
  698. readfunctype := uinttype;
  699. end;
  700. else
  701. internalerror(2013032601);
  702. end;
  703. end;
  704. end;
  705. function Tinlinenode.handle_text_read_write(filepara,params:Ttertiarynode;var newstatement:Tnode):boolean;
  706. {Read(ln)/write(ln) for text files.}
  707. const procprefixes:array[boolean] of string[15]=('fpc_write_text_','fpc_read_text_');
  708. var error_para,is_real,special_handling,found_error,do_read:boolean;
  709. p1:Tnode;
  710. nextpara,
  711. indexpara,
  712. lenpara,
  713. para,
  714. fracpara:Tcallparanode;
  715. temp:Ttempcreatenode;
  716. readfunctype:Tdef;
  717. name:string[63];
  718. func_suffix:string[8];
  719. begin
  720. para:=Tcallparanode(params);
  721. found_error:=false;
  722. do_read:=inlinenumber in [in_read_x,in_readln_x,in_readstr_x];
  723. name:='';
  724. while assigned(para) do
  725. begin
  726. { is this parameter faulty? }
  727. error_para:=false;
  728. { is this parameter a real? }
  729. is_real:=false;
  730. { type used for the read(), this is used to check
  731. whether a temp is needed for range checking }
  732. readfunctype:=nil;
  733. { can't read/write types }
  734. if (para.left.nodetype=typen) and not(is_typeparam(ttypenode(para.left).typedef)) then
  735. begin
  736. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  737. error_para := true;
  738. end;
  739. { support writeln(procvar) }
  740. if para.left.resultdef.typ=procvardef then
  741. begin
  742. p1:=ccallnode.create_procvar(nil,para.left);
  743. typecheckpass(p1);
  744. para.left:=p1;
  745. end;
  746. if inlinenumber in [in_write_x,in_writeln_x] then
  747. { prefer strings to chararrays }
  748. maybe_convert_to_string(para.left);
  749. if is_typeparam(para.left.resultdef) then
  750. error_para:=true
  751. else
  752. case para.left.resultdef.typ of
  753. stringdef :
  754. begin
  755. name:=procprefixes[do_read]+tstringdef(para.left.resultdef).stringtypname;
  756. if (m_isolike_io in current_settings.modeswitches) and (tstringdef(para.left.resultdef).stringtype<>st_shortstring) then
  757. begin
  758. CGMessagePos(para.fileinfo,type_e_cant_read_write_type_in_iso_mode);
  759. error_para := true;
  760. end;
  761. end;
  762. pointerdef :
  763. begin
  764. if (not is_pchar(para.left.resultdef)) or do_read then
  765. begin
  766. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  767. error_para := true;
  768. end
  769. else
  770. name:=procprefixes[do_read]+'pchar_as_pointer';
  771. end;
  772. floatdef :
  773. begin
  774. is_real:=true;
  775. if Tfloatdef(para.left.resultdef).floattype=s64currency then
  776. name := procprefixes[do_read]+'currency'
  777. else
  778. begin
  779. name := procprefixes[do_read]+'float';
  780. readfunctype:=pbestrealtype^;
  781. end;
  782. { iso pascal needs a different handler }
  783. if (m_isolike_io in current_settings.modeswitches) and do_read then
  784. name:=name+'_iso';
  785. end;
  786. enumdef:
  787. begin
  788. if m_isolike_io in current_settings.modeswitches then
  789. begin
  790. error_para := true;
  791. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  792. end;
  793. name:=procprefixes[do_read]+'enum';
  794. if do_read then
  795. { read is done with a var parameter so we need the correct
  796. size for that }
  797. case para.left.resultdef.size of
  798. 1:
  799. begin
  800. name:=name+'_shortint';
  801. readfunctype:=s8inttype;
  802. end;
  803. 2:
  804. begin
  805. name:=name+'_smallint';
  806. readfunctype:=s16inttype;
  807. end;
  808. 4:
  809. begin
  810. name:=name+'_longint';
  811. readfunctype:=s32inttype;
  812. end;
  813. else
  814. internalerror(2022082601);
  815. end
  816. else
  817. readfunctype:=s32inttype;
  818. end;
  819. orddef :
  820. begin
  821. case Torddef(para.left.resultdef).ordtype of
  822. s8bit,
  823. s16bit,
  824. s32bit,
  825. s64bit,
  826. u8bit,
  827. u16bit,
  828. u32bit,
  829. u64bit:
  830. begin
  831. get_read_write_int_func(para.left.resultdef,func_suffix,readfunctype);
  832. name := procprefixes[do_read]+func_suffix;
  833. if (m_isolike_io in current_settings.modeswitches) and do_read then
  834. name:=name+'_iso';
  835. end;
  836. uchar :
  837. begin
  838. name := procprefixes[do_read]+'char';
  839. { iso pascal needs a different handler }
  840. if (m_isolike_io in current_settings.modeswitches) and do_read then
  841. name:=name+'_iso';
  842. readfunctype:=cansichartype;
  843. end;
  844. uwidechar :
  845. begin
  846. name := procprefixes[do_read]+'widechar';
  847. readfunctype:=cwidechartype;
  848. end;
  849. scurrency:
  850. begin
  851. name := procprefixes[do_read]+'currency';
  852. { iso pascal needs a different handler }
  853. if (m_isolike_io in current_settings.modeswitches) and do_read then
  854. name:=name+'_iso';
  855. readfunctype:=s64currencytype;
  856. is_real:=true;
  857. end;
  858. pasbool1,
  859. pasbool8,
  860. pasbool16,
  861. pasbool32,
  862. pasbool64,
  863. bool8bit,
  864. bool16bit,
  865. bool32bit,
  866. bool64bit:
  867. if do_read then
  868. begin
  869. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  870. error_para := true;
  871. end
  872. else
  873. begin
  874. name := procprefixes[do_read]+'boolean';
  875. readfunctype:=pasbool1type;
  876. end
  877. else
  878. begin
  879. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  880. error_para := true;
  881. end;
  882. end;
  883. end;
  884. variantdef :
  885. begin
  886. name:=procprefixes[do_read]+'variant';
  887. include(current_module.moduleflags,mf_uses_variants);
  888. end;
  889. arraydef :
  890. begin
  891. if is_chararray(para.left.resultdef) then
  892. name := procprefixes[do_read]+'pchar_as_array'
  893. else
  894. begin
  895. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  896. error_para := true;
  897. end
  898. end;
  899. else
  900. begin
  901. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  902. error_para := true;
  903. end;
  904. end;
  905. { iso pascal needs a different handler }
  906. if (m_isolike_io in current_settings.modeswitches) and not(do_read) then
  907. name:=name+'_iso';
  908. { check for length/fractional colon para's }
  909. fracpara:=nil;
  910. lenpara:=nil;
  911. indexpara:=nil;
  912. if assigned(para.right) and
  913. (cpf_is_colon_para in tcallparanode(para.right).callparaflags) then
  914. begin
  915. lenpara := tcallparanode(para.right);
  916. if assigned(lenpara.right) and
  917. (cpf_is_colon_para in tcallparanode(lenpara.right).callparaflags) then
  918. fracpara:=tcallparanode(lenpara.right);
  919. end;
  920. { get the next parameter now already, because we're going }
  921. { to muck around with the pointers }
  922. if assigned(fracpara) then
  923. nextpara := tcallparanode(fracpara.right)
  924. else if assigned(lenpara) then
  925. nextpara := tcallparanode(lenpara.right)
  926. else
  927. nextpara := tcallparanode(para.right);
  928. { check if a fracpara is allowed }
  929. if assigned(fracpara) and not is_real then
  930. begin
  931. CGMessagePos(fracpara.fileinfo,parser_e_illegal_colon_qualifier);
  932. error_para := true;
  933. end
  934. else if assigned(lenpara) and do_read then
  935. begin
  936. { I think this is already filtered out by parsing, but I'm not sure (JM) }
  937. CGMessagePos(lenpara.fileinfo,parser_e_illegal_colon_qualifier);
  938. error_para := true;
  939. end;
  940. { adjust found_error }
  941. found_error := found_error or error_para;
  942. if not error_para then
  943. begin
  944. special_handling:=false;
  945. { create dummy frac/len para's if necessary }
  946. if not do_read then
  947. begin
  948. { difference in default value for floats and the rest :( }
  949. if not is_real then
  950. begin
  951. if not assigned(lenpara) then
  952. begin
  953. if m_isolike_io in current_settings.modeswitches then
  954. lenpara := ccallparanode.create(
  955. cordconstnode.create(-1,s32inttype,false),nil)
  956. else
  957. lenpara := ccallparanode.create(
  958. cordconstnode.create(0,s32inttype,false),nil);
  959. end
  960. else
  961. { make sure we don't pass the successive }
  962. { parameters too. We also already have a }
  963. { reference to the next parameter in }
  964. { nextpara }
  965. lenpara.right := nil;
  966. end
  967. else
  968. begin
  969. if not assigned(lenpara) then
  970. lenpara := ccallparanode.create(
  971. cordconstnode.create(int64(-32767),s32inttype,false),nil);
  972. { also create a default fracpara if necessary }
  973. if not assigned(fracpara) then
  974. fracpara := ccallparanode.create(
  975. cordconstnode.create(int64(-1),s32inttype,false),nil);
  976. { add it to the lenpara }
  977. lenpara.right := fracpara;
  978. if not is_currency(para.left.resultdef) then
  979. begin
  980. { and add the realtype para (this also removes the link }
  981. { to any parameters coming after it) }
  982. fracpara.right := ccallparanode.create(
  983. cordconstnode.create(ord(tfloatdef(para.left.resultdef).floattype),
  984. s32inttype,true),nil);
  985. end
  986. else
  987. fracpara.right:=nil;
  988. end;
  989. if para.left.resultdef.typ=enumdef then
  990. begin
  991. {To write(ln) an enum we need a some extra parameters.}
  992. {Insert a reference to the ord2string index.}
  993. indexpara:=Ccallparanode.create(
  994. Caddrnode.create_internal(
  995. Crttinode.create(Tenumdef(para.left.resultdef),fullrtti,rdt_normal)
  996. ),
  997. nil);
  998. {Insert a reference to the typinfo.}
  999. indexpara:=Ccallparanode.create(
  1000. Caddrnode.create_internal(
  1001. Crttinode.create(Tenumdef(para.left.resultdef),fullrtti,rdt_ord2str)
  1002. ),
  1003. indexpara);
  1004. {Insert a type conversion to to convert the enum to longint.}
  1005. para.left:=Ctypeconvnode.create_internal(para.left,s32inttype);
  1006. typecheckpass(para.left);
  1007. end;
  1008. end
  1009. else
  1010. begin
  1011. {To read(ln) an enum we need a an extra parameter.}
  1012. if para.left.resultdef.typ=enumdef then
  1013. begin
  1014. {Insert a reference to the string2ord index.}
  1015. indexpara:=Ccallparanode.create(Caddrnode.create_internal(
  1016. Crttinode.create(Tenumdef(para.left.resultdef),fullrtti,rdt_str2ord)
  1017. ),nil);
  1018. {Insert a type conversion to to convert the enum to longint.}
  1019. para.left:=Ctypeconvnode.create_internal(para.left,readfunctype);
  1020. typecheckpass(para.left);
  1021. end;
  1022. { special handling of reading small numbers, because the helpers }
  1023. { expect a longint/card/bestreal var parameter. Use a temp. can't }
  1024. { use functions because then the call to FPC_IOCHECK destroys }
  1025. { their result before we can store it }
  1026. if (readfunctype<>nil) and (para.left.resultdef<>readfunctype) then
  1027. special_handling:=true;
  1028. end;
  1029. if special_handling then
  1030. begin
  1031. { since we're not going to pass the parameter as var-parameter }
  1032. { to the read function, manually check whether the parameter }
  1033. { can be used as var-parameter (e.g., whether it isn't a }
  1034. { property) }
  1035. valid_for_var(para.left,true);
  1036. { create the parameter list: the temp ... }
  1037. temp := ctempcreatenode.create(readfunctype,readfunctype.size,tt_persistent,false);
  1038. addstatement(Tstatementnode(newstatement),temp);
  1039. { ... and the file }
  1040. p1 := ccallparanode.create(ctemprefnode.create(temp),
  1041. filepara.getcopy);
  1042. Tcallparanode(Tcallparanode(p1).right).right:=indexpara;
  1043. { create the call to the helper }
  1044. addstatement(Tstatementnode(newstatement),
  1045. ccallnode.createintern(name,tcallparanode(p1)));
  1046. { assign the result to the original var (this automatically }
  1047. { takes care of range checking) }
  1048. addstatement(Tstatementnode(newstatement),
  1049. cassignmentnode.create(para.left,
  1050. ctemprefnode.create(temp)));
  1051. { release the temp location }
  1052. addstatement(Tstatementnode(newstatement),ctempdeletenode.create(temp));
  1053. { statement of para is used }
  1054. para.left := nil;
  1055. { free the enclosing tcallparanode, but not the }
  1056. { parameters coming after it }
  1057. para.right := nil;
  1058. para.free;
  1059. end
  1060. else
  1061. { read of non s/u-8/16bit, or a write }
  1062. begin
  1063. { add the filepara to the current parameter }
  1064. para.right := filepara.getcopy;
  1065. {Add the lenpara and the indexpara(s) (fracpara and realtype are
  1066. already linked with the lenpara if necessary).}
  1067. if indexpara=nil then
  1068. Tcallparanode(para.right).right:=lenpara
  1069. else
  1070. begin
  1071. if lenpara=nil then
  1072. Tcallparanode(para.right).right:=indexpara
  1073. else
  1074. begin
  1075. Tcallparanode(para.right).right:=lenpara;
  1076. lenpara.right:=indexpara;
  1077. end;
  1078. { indexpara.right:=lenpara;}
  1079. end;
  1080. { in case of writing a chararray, add whether it's zero-based }
  1081. if para.left.resultdef.typ=arraydef then
  1082. para := ccallparanode.create(cordconstnode.create(
  1083. ord(tarraydef(para.left.resultdef).lowrange=0),pasbool1type,false),para)
  1084. else
  1085. { in case of reading an ansistring pass a codepage argument }
  1086. if do_read and is_ansistring(para.left.resultdef) then
  1087. para:=ccallparanode.create(cordconstnode.create(
  1088. getparaencoding(para.left.resultdef),u16inttype,true),para);
  1089. { create the call statement }
  1090. addstatement(Tstatementnode(newstatement),
  1091. ccallnode.createintern(name,para));
  1092. end
  1093. end
  1094. else
  1095. { error_para = true }
  1096. begin
  1097. { free the parameter, since it isn't referenced anywhere anymore }
  1098. para.right := nil;
  1099. para.free;
  1100. if assigned(lenpara) then
  1101. begin
  1102. lenpara.right := nil;
  1103. lenpara.free;
  1104. end;
  1105. if assigned(fracpara) then
  1106. begin
  1107. fracpara.right := nil;
  1108. fracpara.free;
  1109. end;
  1110. end;
  1111. { process next parameter }
  1112. para := nextpara;
  1113. end;
  1114. { if no error, add the write(ln)/read(ln) end calls }
  1115. if not found_error then
  1116. begin
  1117. case inlinenumber of
  1118. in_read_x,
  1119. in_readstr_x:
  1120. name:='fpc_read_end';
  1121. in_write_x,
  1122. in_writestr_x:
  1123. name:='fpc_write_end';
  1124. in_readln_x:
  1125. begin
  1126. name:='fpc_readln_end';
  1127. if m_isolike_io in current_settings.modeswitches then
  1128. name:=name+'_iso';
  1129. end;
  1130. in_writeln_x:
  1131. name:='fpc_writeln_end';
  1132. else
  1133. internalerror(2019050501);
  1134. end;
  1135. addstatement(Tstatementnode(newstatement),ccallnode.createintern(name,filepara.getcopy));
  1136. end;
  1137. handle_text_read_write:=found_error;
  1138. end;
  1139. function Tinlinenode.handle_typed_read_write(filepara,params:Ttertiarynode;var newstatement:Tnode):boolean;
  1140. {Read/write for typed files.}
  1141. const procprefixes:array[boolean,boolean] of string[19]=(('fpc_typed_write','fpc_typed_read'),
  1142. ('fpc_typed_write','fpc_typed_read_iso'));
  1143. procnamesdisplay:array[boolean,boolean] of string[8] = (('Write','Read'),('WriteStr','ReadStr'));
  1144. var found_error,do_read,is_rwstr:boolean;
  1145. para,nextpara:Tcallparanode;
  1146. p1:Tnode;
  1147. temp:Ttempcreatenode;
  1148. begin
  1149. found_error:=false;
  1150. para:=Tcallparanode(params);
  1151. do_read:=inlinenumber in [in_read_x,in_readln_x,in_readstr_x];
  1152. is_rwstr := inlinenumber in [in_readstr_x,in_writestr_x];
  1153. temp:=nil;
  1154. { add the typesize to the filepara }
  1155. if filepara.resultdef.typ=filedef then
  1156. filepara.right := ccallparanode.create(cordconstnode.create(
  1157. tfiledef(filepara.resultdef).typedfiledef.size,s32inttype,true),nil);
  1158. { check for "no parameters" (you need at least one extra para for typed files) }
  1159. if not assigned(para) then
  1160. begin
  1161. CGMessage1(parser_e_wrong_parameter_size,procnamesdisplay[is_rwstr,do_read]);
  1162. found_error := true;
  1163. end;
  1164. { process all parameters }
  1165. while assigned(para) do
  1166. begin
  1167. { check if valid parameter }
  1168. if para.left.nodetype=typen then
  1169. begin
  1170. CGMessagePos(para.left.fileinfo,type_e_cant_read_write_type);
  1171. found_error := true;
  1172. end;
  1173. { support writeln(procvar) }
  1174. if (para.left.resultdef.typ=procvardef) then
  1175. begin
  1176. p1:=ccallnode.create_procvar(nil,para.left);
  1177. typecheckpass(p1);
  1178. para.left:=p1;
  1179. end;
  1180. if filepara.resultdef.typ=filedef then
  1181. inserttypeconv(para.left,tfiledef(filepara.resultdef).typedfiledef);
  1182. if assigned(para.right) and
  1183. (cpf_is_colon_para in tcallparanode(para.right).callparaflags) then
  1184. begin
  1185. CGMessagePos(para.right.fileinfo,parser_e_illegal_colon_qualifier);
  1186. { skip all colon para's }
  1187. nextpara := tcallparanode(tcallparanode(para.right).right);
  1188. while assigned(nextpara) and (cpf_is_colon_para in nextpara.callparaflags) do
  1189. nextpara := tcallparanode(nextpara.right);
  1190. found_error := true;
  1191. end
  1192. else
  1193. { get next parameter }
  1194. nextpara := tcallparanode(para.right);
  1195. { When we have a call, we have a problem: you can't pass the }
  1196. { result of a call as a formal const parameter. Solution: }
  1197. { assign the result to a temp and pass this temp as parameter }
  1198. { This is not very efficient, but write(typedfile,x) is }
  1199. { already slow by itself anyway (no buffering) (JM) }
  1200. { Actually, thge same goes for every non-simple expression }
  1201. { (such as an addition, ...) -> put everything but load nodes }
  1202. { into temps (JM) }
  1203. { of course, this must only be allowed for writes!!! (JM) }
  1204. if not(do_read) and (para.left.nodetype <> loadn) then
  1205. begin
  1206. { create temp for result }
  1207. temp := ctempcreatenode.create(para.left.resultdef,
  1208. para.left.resultdef.size,tt_persistent,false);
  1209. addstatement(Tstatementnode(newstatement),temp);
  1210. { assign result to temp }
  1211. addstatement(Tstatementnode(newstatement),
  1212. cassignmentnode.create(ctemprefnode.create(temp),
  1213. para.left));
  1214. { replace (reused) paranode with temp }
  1215. para.left := ctemprefnode.create(temp);
  1216. end;
  1217. { add fileparameter }
  1218. para.right := filepara.getcopy;
  1219. { create call statment }
  1220. { since the parameters are in the correct order, we have to insert }
  1221. { the statements always at the end of the current block }
  1222. addstatement(Tstatementnode(newstatement),
  1223. Ccallnode.createintern(procprefixes[m_isolike_io in current_settings.modeswitches,do_read],para
  1224. ));
  1225. { if we used a temp, free it }
  1226. if para.left.nodetype = temprefn then
  1227. addstatement(Tstatementnode(newstatement),ctempdeletenode.create(temp));
  1228. { process next parameter }
  1229. para := nextpara;
  1230. end;
  1231. handle_typed_read_write:=found_error;
  1232. end;
  1233. function tinlinenode.handle_read_write: tnode;
  1234. var
  1235. filepara,
  1236. nextpara,
  1237. params : tcallparanode;
  1238. newstatement : tstatementnode;
  1239. newblock : tblocknode;
  1240. filetemp : Ttempcreatenode;
  1241. name : string[31];
  1242. textsym : ttypesym;
  1243. is_typed,
  1244. do_read,
  1245. is_rwstr,
  1246. found_error : boolean;
  1247. begin
  1248. filepara := nil;
  1249. is_typed := false;
  1250. filetemp := nil;
  1251. do_read := inlinenumber in [in_read_x,in_readln_x,in_readstr_x];
  1252. is_rwstr := inlinenumber in [in_readstr_x,in_writestr_x];
  1253. { if we fail, we can quickly exit this way. We must generate something }
  1254. { instead of the inline node, because firstpass will bomb with an }
  1255. { internalerror if it encounters a read/write }
  1256. result := cerrornode.create;
  1257. { reverse the parameters (needed to get the colon parameters in the }
  1258. { correct order when processing write(ln) }
  1259. reverseparameters(tcallparanode(left));
  1260. if is_rwstr then
  1261. begin
  1262. filepara := tcallparanode(left);
  1263. { needs at least two parameters: source/dest string + min. 1 value }
  1264. if not(assigned(filepara)) or
  1265. not(assigned(filepara.right)) then
  1266. begin
  1267. CGMessagePos1(fileinfo,parser_e_wrong_parameter_size,'ReadStr/WriteStr');
  1268. exit;
  1269. end
  1270. else if (filepara.resultdef.typ <> stringdef) then
  1271. begin
  1272. { convert chararray to string, or give an appropriate error message }
  1273. { (if you want to optimize to use shortstring, keep in mind that }
  1274. { readstr internally always uses ansistring, and to account for }
  1275. { chararrays with > 255 characters) }
  1276. inserttypeconv(filepara.left,getansistringdef);
  1277. filepara.resultdef:=filepara.left.resultdef;
  1278. if codegenerror then
  1279. exit;
  1280. end
  1281. end
  1282. else if assigned(left) then
  1283. begin
  1284. { check if we have a file parameter and if yes, what kind it is }
  1285. filepara := tcallparanode(left);
  1286. if (filepara.resultdef.typ=filedef) then
  1287. begin
  1288. if (tfiledef(filepara.resultdef).filetyp=ft_untyped) then
  1289. begin
  1290. CGMessagePos(fileinfo,type_e_no_read_write_for_untyped_file);
  1291. exit;
  1292. end
  1293. else
  1294. begin
  1295. if (tfiledef(filepara.resultdef).filetyp=ft_typed) then
  1296. begin
  1297. if (inlinenumber in [in_readln_x,in_writeln_x]) then
  1298. begin
  1299. CGMessagePos(fileinfo,type_e_no_readln_writeln_for_typed_file);
  1300. exit;
  1301. end;
  1302. is_typed := true;
  1303. end
  1304. end;
  1305. end
  1306. else
  1307. filepara := nil;
  1308. end;
  1309. if assigned(filepara) and
  1310. assigned(filepara.right) and
  1311. (cpf_is_colon_para in tcallparanode(filepara.right).callparaflags) then
  1312. begin
  1313. CGMessagePos(filepara.fileinfo,parser_e_illegal_colon_qualifier);
  1314. { for recovery we can simply continue, because the compiler will
  1315. simply treat the next parameters as normal parameters }
  1316. end;
  1317. { create a blocknode in which the successive write/read statements will be }
  1318. { put, since they belong together. Also create a dummy statement already to }
  1319. { make inserting of additional statements easier }
  1320. newblock:=internalstatements(newstatement);
  1321. if is_rwstr then
  1322. begin
  1323. { create a dummy temp text file that will be used to cache the
  1324. readstr/writestr state. Can't use a global variable in the system
  1325. unit because these can be nested (in case of parameters to
  1326. writestr that are function calls to functions that also call
  1327. readstr/writestr) }
  1328. textsym:=search_system_type('TEXT');
  1329. filetemp:=ctempcreatenode.create(textsym.typedef,textsym.typedef.size,tt_persistent,false);
  1330. addstatement(newstatement,filetemp);
  1331. if (do_read) then
  1332. name:='fpc_setupreadstr_'
  1333. else
  1334. name:='fpc_setupwritestr_';
  1335. name:=name+tstringdef(filepara.resultdef).stringtypname;
  1336. { the file para is a var parameter, but it is properly initialized,
  1337. so it should be actually an out parameter }
  1338. if not(do_read) then
  1339. set_varstate(filepara.left,vs_written,[]);
  1340. { remove the source/destination string parameter from the }
  1341. { parameter chain }
  1342. left:=filepara.right;
  1343. filepara.right:=ccallparanode.create(ctemprefnode.create(filetemp),nil);
  1344. { in case of a writestr() to an ansistring, also pass the string's
  1345. code page }
  1346. if not do_read and
  1347. is_ansistring(filepara.left.resultdef) then
  1348. filepara:=ccallparanode.create(genintconstnode(tstringdef(filepara.left.resultdef).encoding),filepara);
  1349. { pass the temp text file and the source/destination string to the
  1350. setup routine, which will store the string's address in the
  1351. textrec }
  1352. addstatement(newstatement,ccallnode.createintern(name,filepara));
  1353. filepara:=ccallparanode.create(ctemprefnode.create(filetemp),nil);
  1354. end
  1355. { if we don't have a filepara, create one containing the default }
  1356. else if not assigned(filepara) then
  1357. begin
  1358. { since the input/output variables are threadvars loading them into
  1359. a temp once is faster. Create a temp which will hold a pointer to the file }
  1360. filetemp := ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  1361. addstatement(newstatement,filetemp);
  1362. { make sure the resultdef of the temp (and as such of the }
  1363. { temprefs coming after it) is set (necessary because the }
  1364. { temprefs will be part of the filepara, of which we need }
  1365. { the resultdef later on and temprefs can only be }
  1366. { typecheckpassed if the resultdef of the temp is known) }
  1367. typecheckpass(tnode(filetemp));
  1368. { assign the address of the file to the temp }
  1369. if do_read then
  1370. name := 'input'
  1371. else
  1372. name := 'output';
  1373. addstatement(newstatement,
  1374. cassignmentnode.create(ctemprefnode.create(filetemp),
  1375. ccallnode.createintern('fpc_get_'+name,nil)));
  1376. { create a new fileparameter as follows: file_type(temp^) }
  1377. { (so that we pass the value and not the address of the temp }
  1378. { to the read/write routine) }
  1379. textsym:=search_system_type('TEXT');
  1380. filepara := ccallparanode.create(ctypeconvnode.create_internal(
  1381. cderefnode.create(ctemprefnode.create(filetemp)),textsym.typedef),nil);
  1382. end
  1383. else
  1384. { remove filepara from the parameter chain }
  1385. begin
  1386. left := filepara.right;
  1387. filepara.right := nil;
  1388. { the file para is a var parameter, but it must be valid already }
  1389. set_varstate(filepara.left,vs_readwritten,[vsf_must_be_valid]);
  1390. { check if we should make a temp to store the result of a complex }
  1391. { expression (better heuristics, anyone?) (JM) }
  1392. if (filepara.left.nodetype <> loadn) then
  1393. begin
  1394. { create a temp which will hold a pointer to the file }
  1395. filetemp := ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  1396. { add it to the statements }
  1397. addstatement(newstatement,filetemp);
  1398. { make sure the resultdef of the temp (and as such of the }
  1399. { temprefs coming after it) is set (necessary because the }
  1400. { temprefs will be part of the filepara, of which we need }
  1401. { the resultdef later on and temprefs can only be }
  1402. { typecheckpassed if the resultdef of the temp is known) }
  1403. typecheckpass(tnode(filetemp));
  1404. { assign the address of the file to the temp }
  1405. addstatement(newstatement,
  1406. cassignmentnode.create(ctemprefnode.create(filetemp),
  1407. caddrnode.create_internal(filepara.left)));
  1408. typecheckpass(newstatement.left);
  1409. { create a new fileparameter as follows: file_type(temp^) }
  1410. { (so that we pass the value and not the address of the temp }
  1411. { to the read/write routine) }
  1412. nextpara := ccallparanode.create(ctypeconvnode.create_internal(
  1413. cderefnode.create(ctemprefnode.create(filetemp)),filepara.left.resultdef),nil);
  1414. { replace the old file para with the new one }
  1415. filepara.left := nil;
  1416. filepara.free;
  1417. filepara := nextpara;
  1418. end;
  1419. end;
  1420. { the resultdef of the filepara must be set since it's }
  1421. { used below }
  1422. filepara.get_paratype;
  1423. { now, filepara is nowhere referenced anymore, so we can safely dispose it }
  1424. { if something goes wrong or at the end of the procedure }
  1425. { we're going to reuse the paranodes, so make sure they don't get freed }
  1426. { twice }
  1427. params:=Tcallparanode(left);
  1428. left := nil;
  1429. if is_typed then
  1430. found_error:=handle_typed_read_write(filepara,Ttertiarynode(params),tnode(newstatement))
  1431. else
  1432. found_error:=handle_text_read_write(filepara,Ttertiarynode(params),tnode(newstatement));
  1433. { free the file parameter (it's copied inside the handle_*_read_write methods) }
  1434. filepara.free;
  1435. { if we found an error, simply delete the generated blocknode }
  1436. if found_error then
  1437. begin
  1438. { ensure that the tempinfo is freed correctly by destroying a
  1439. delete node for it
  1440. Note: this might happen legitimately whe parsing a generic that
  1441. passes a undefined type to Write/Read }
  1442. if assigned(filetemp) then
  1443. ctempdeletenode.create(filetemp).free;
  1444. newblock.free
  1445. end
  1446. else
  1447. begin
  1448. { deallocate the temp for the file para if we used one }
  1449. if assigned(filetemp) then
  1450. addstatement(newstatement,ctempdeletenode.create(filetemp));
  1451. { otherwise return the newly generated block of instructions, }
  1452. { but first free the errornode we generated at the beginning }
  1453. result.free;
  1454. result := newblock
  1455. end;
  1456. end;
  1457. function get_val_int_func(def: tdef): string;
  1458. var
  1459. ordtype: tordtype;
  1460. begin
  1461. ordtype := torddef(def).ordtype;
  1462. if not (ordtype in [s64bit,u64bit,s32bit,u32bit,s16bit,u16bit,s8bit,u8bit]) then
  1463. internalerror(2020080101);
  1464. if is_oversizedint(def) then
  1465. begin
  1466. case ordtype of
  1467. s64bit: exit('int64');
  1468. u64bit: exit('qword');
  1469. s32bit: exit('longint');
  1470. u32bit: exit('longword');
  1471. s16bit: exit('smallint');
  1472. u16bit: exit('word');
  1473. else
  1474. internalerror(2013032606);
  1475. end;
  1476. end
  1477. else
  1478. begin
  1479. case ordtype of
  1480. s64bit,s32bit,s16bit,s8bit: exit('sint');
  1481. u64bit,u32bit,u16bit,u8bit: exit('uint');
  1482. else
  1483. internalerror(2013032607);
  1484. end;
  1485. end;
  1486. internalerror(2013032608);
  1487. end;
  1488. function tinlinenode.handle_val: tnode;
  1489. var
  1490. procname,
  1491. suffix : string[31];
  1492. sourcepara,
  1493. destpara,
  1494. codepara,
  1495. sizepara,
  1496. newparas : tcallparanode;
  1497. orgcode,tc : tnode;
  1498. newstatement : tstatementnode;
  1499. newblock : tblocknode;
  1500. tempcode : ttempcreatenode;
  1501. valsinttype : tdef;
  1502. begin
  1503. { for easy exiting if something goes wrong }
  1504. result := cerrornode.create;
  1505. { check the amount of parameters }
  1506. if not(assigned(left)) or
  1507. not(assigned(tcallparanode(left).right)) then
  1508. begin
  1509. CGMessage1(parser_e_wrong_parameter_size,'Val');
  1510. exit;
  1511. end;
  1512. suffix:='';
  1513. { in case we are in a generic definition, we cannot
  1514. do all checks, the parameters might be type parameters }
  1515. if df_generic in current_procinfo.procdef.defoptions then
  1516. begin
  1517. result.Free;
  1518. result:=nil;
  1519. resultdef:=voidtype;
  1520. exit;
  1521. end;
  1522. { retrieve the ValSInt type }
  1523. valsinttype:=search_system_type('VALSINT').typedef;
  1524. { reverse parameters for easier processing }
  1525. reverseparameters(tcallparanode(left));
  1526. { get the parameters }
  1527. tempcode := nil;
  1528. orgcode := nil;
  1529. sizepara := nil;
  1530. sourcepara := tcallparanode(left);
  1531. destpara := tcallparanode(sourcepara.right);
  1532. codepara := tcallparanode(destpara.right);
  1533. { check if codepara is valid }
  1534. if assigned(codepara) and
  1535. (
  1536. not is_integer(codepara.resultdef)
  1537. {$ifndef cpu64bitaddr}
  1538. or is_64bitint(codepara.resultdef)
  1539. {$endif not cpu64bitaddr}
  1540. ) then
  1541. begin
  1542. CGMessagePos1(codepara.fileinfo,type_e_integer_expr_expected,codepara.resultdef.typename);
  1543. exit;
  1544. end;
  1545. { check if dest para is valid }
  1546. if not is_integer(destpara.resultdef) and
  1547. not is_currency(destpara.resultdef) and
  1548. not(destpara.resultdef.typ in [floatdef,enumdef]) then
  1549. begin
  1550. CGMessagePos(destpara.fileinfo,type_e_integer_or_real_expr_expected);
  1551. exit;
  1552. end;
  1553. { we're going to reuse the exisiting para's, so make sure they }
  1554. { won't be disposed }
  1555. left := nil;
  1556. { create the blocknode which will hold the generated statements + }
  1557. { an initial dummy statement }
  1558. newblock:=internalstatements(newstatement);
  1559. { do we need a temp for code? Yes, if no code specified, or if }
  1560. { code is not a valsinttype sized parameter (we already checked }
  1561. { whether the code para, if specified, was an orddef) }
  1562. if not assigned(codepara) or
  1563. (codepara.resultdef.size<>valsinttype.size) then
  1564. begin
  1565. tempcode := ctempcreatenode.create(valsinttype,valsinttype.size,tt_persistent,false);
  1566. addstatement(newstatement,tempcode);
  1567. { set the resultdef of the temp (needed to be able to get }
  1568. { the resultdef of the tempref used in the new code para) }
  1569. typecheckpass(tnode(tempcode));
  1570. { create a temp codepara, but save the original code para to }
  1571. { assign the result to later on }
  1572. if assigned(codepara) then
  1573. begin
  1574. orgcode := codepara.left;
  1575. codepara.left := ctemprefnode.create(tempcode);
  1576. end
  1577. else
  1578. codepara := ccallparanode.create(ctemprefnode.create(tempcode),nil);
  1579. { we need its resultdef later on }
  1580. codepara.get_paratype;
  1581. end
  1582. else if (torddef(codepara.resultdef).ordtype <> torddef(valsinttype).ordtype) then
  1583. { because code is a var parameter, it must match types exactly }
  1584. { however, since it will return values >= 0, both signed and }
  1585. { and unsigned ints of the same size are fine. Since the formal }
  1586. { code para type is sinttype, insert a typecoversion to sint for }
  1587. { unsigned para's }
  1588. begin
  1589. codepara.left := ctypeconvnode.create_internal(codepara.left,valsinttype);
  1590. { make it explicit, oterwise you may get a nonsense range }
  1591. { check error if the cardinal already contained a value }
  1592. { > $7fffffff }
  1593. codepara.get_paratype;
  1594. end;
  1595. { create the procedure name }
  1596. procname := 'fpc_val_';
  1597. case destpara.resultdef.typ of
  1598. orddef:
  1599. begin
  1600. case torddef(destpara.resultdef).ordtype of
  1601. s8bit,s16bit,s32bit,s64bit,
  1602. u8bit,u16bit,u32bit,u64bit:
  1603. begin
  1604. suffix := get_val_int_func(destpara.resultdef) + '_';
  1605. { we also need a destsize para in the case of sint or uint }
  1606. if (suffix = 'sint_') or (suffix = 'uint_') then
  1607. sizepara := ccallparanode.create(cordconstnode.create
  1608. (destpara.resultdef.size,s32inttype,true),nil);
  1609. end;
  1610. scurrency: suffix := 'currency_';
  1611. else
  1612. internalerror(200304225);
  1613. end;
  1614. end;
  1615. floatdef:
  1616. suffix:='real_';
  1617. enumdef:
  1618. begin
  1619. suffix:='enum_';
  1620. sizepara:=Ccallparanode.create(Caddrnode.create_internal(
  1621. Crttinode.create(Tenumdef(destpara.resultdef),fullrtti,rdt_str2ord)
  1622. ),nil);
  1623. end;
  1624. else
  1625. internalerror(2019050515);
  1626. end;
  1627. procname := procname + suffix;
  1628. { play a trick to have tcallnode handle invalid source parameters: }
  1629. { the shortstring-longint val routine by default }
  1630. if (sourcepara.resultdef.typ = stringdef) then
  1631. procname := procname + tstringdef(sourcepara.resultdef).stringtypname
  1632. { zero-based arrays (of char) can be implicitely converted to ansistring, but don't do
  1633. so if not needed because the array is too short }
  1634. else if is_zero_based_array(sourcepara.resultdef) and (sourcepara.resultdef.size>255) then
  1635. procname := procname + 'ansistr'
  1636. else
  1637. procname := procname + 'shortstr';
  1638. { set up the correct parameters for the call: the code para... }
  1639. newparas := codepara;
  1640. { and the source para }
  1641. codepara.right := sourcepara;
  1642. { sizepara either contains nil if none is needed (which is ok, since }
  1643. { then the next statement severes any possible links with other paras }
  1644. { that sourcepara may have) or it contains the necessary size para and }
  1645. { its right field is nil }
  1646. sourcepara.right := sizepara;
  1647. { create the call and assign the result to dest (val helpers are functions).
  1648. Use a trick to prevent a type size mismatch warning to be generated by the
  1649. assignment node. First convert implicitly to the resultdef. This will insert
  1650. the range check. The Second conversion is done explicitly to hide the implicit conversion
  1651. for the assignment node and therefor preventing the warning (PFV)
  1652. The implicit conversion is avoided for enums because implicit conversion between
  1653. longint (which is what fpc_val_enum_shortstr returns) and enumerations is not
  1654. possible. (DM).
  1655. The implicit conversion is also avoided for COMP type if it is handled by FPU (x86)
  1656. to prevent warning about automatic type conversion. }
  1657. if (destpara.resultdef.typ=enumdef) or
  1658. ((destpara.resultdef.typ=floatdef) and (tfloatdef(destpara.resultdef).floattype=s64comp))
  1659. then
  1660. tc:=ccallnode.createfromintrinsic(in_val_x,procname,newparas)
  1661. else
  1662. tc:=ctypeconvnode.create(ccallnode.createfromintrinsic(in_val_x,procname,newparas),destpara.left.resultdef);
  1663. addstatement(newstatement,cassignmentnode.create(
  1664. destpara.left,ctypeconvnode.create_internal(tc,destpara.left.resultdef)));
  1665. { dispose of the enclosing paranode of the destination }
  1666. destpara.left := nil;
  1667. destpara.right := nil;
  1668. destpara.free;
  1669. { check if we used a temp for code and whether we have to store }
  1670. { it to the real code parameter }
  1671. if assigned(orgcode) then
  1672. addstatement(newstatement,cassignmentnode.create(
  1673. orgcode,
  1674. ctypeconvnode.create_internal(
  1675. ctemprefnode.create(tempcode),orgcode.resultdef)));
  1676. { release the temp if we allocated one }
  1677. if assigned(tempcode) then
  1678. addstatement(newstatement,ctempdeletenode.create(tempcode));
  1679. { free the errornode }
  1680. result.free;
  1681. { and return it }
  1682. result := newblock;
  1683. end;
  1684. function tinlinenode.handle_setlength: tnode;
  1685. var
  1686. def: tdef;
  1687. destppn,
  1688. paras: tnode;
  1689. newstatement: tstatementnode;
  1690. ppn: tcallparanode;
  1691. counter,
  1692. dims: longint;
  1693. isarray: boolean;
  1694. begin
  1695. { for easy exiting if something goes wrong }
  1696. result:=cerrornode.create;
  1697. resultdef:=voidtype;
  1698. paras:=left;
  1699. dims:=0;
  1700. if assigned(paras) then
  1701. begin
  1702. { check type of lengths }
  1703. ppn:=tcallparanode(paras);
  1704. while assigned(ppn.right) do
  1705. begin
  1706. set_varstate(ppn.left,vs_read,[vsf_must_be_valid]);
  1707. inserttypeconv(ppn.left,sinttype);
  1708. inc(dims);
  1709. ppn:=tcallparanode(ppn.right);
  1710. end;
  1711. end
  1712. else
  1713. internalerror(2013112912);
  1714. if dims=0 then
  1715. begin
  1716. CGMessage1(parser_e_wrong_parameter_size,'SetLength');
  1717. exit;
  1718. end;
  1719. { last param must be var }
  1720. destppn:=ppn.left;
  1721. valid_for_var(destppn,true);
  1722. set_varstate(destppn,vs_written,[vsf_must_be_valid,vsf_use_hints,vsf_use_hint_for_string_result]);
  1723. { first param must be a string or dynamic array ...}
  1724. isarray:=is_dynamic_array(destppn.resultdef);
  1725. if not((destppn.resultdef.typ=stringdef) or
  1726. isarray) then
  1727. begin
  1728. { possibly generic involved? }
  1729. if df_generic in current_procinfo.procdef.defoptions then
  1730. result:=internalstatements(newstatement)
  1731. else
  1732. CGMessage(type_e_mismatch);
  1733. exit;
  1734. end;
  1735. { only dynamic arrays accept more dimensions }
  1736. if (dims>1) then
  1737. begin
  1738. if (not isarray) then
  1739. CGMessage(type_e_mismatch)
  1740. else
  1741. begin
  1742. { check if the amount of dimensions is valid }
  1743. def:=tarraydef(destppn.resultdef).elementdef;
  1744. counter:=dims;
  1745. while counter > 1 do
  1746. begin
  1747. if not(is_dynamic_array(def)) then
  1748. begin
  1749. CGMessage1(parser_e_wrong_parameter_size,'SetLength');
  1750. break;
  1751. end;
  1752. dec(counter);
  1753. def:=tarraydef(def).elementdef;
  1754. end;
  1755. end;
  1756. end;
  1757. result.free;
  1758. result:=nil;
  1759. end;
  1760. function tinlinenode.handle_copy: tnode;
  1761. procedure do_error(typemismatch:boolean;func:string;fi:tfileposinfo);
  1762. procedure write_dynarray_copy;
  1763. begin
  1764. MessagePos1(fileinfo,sym_e_param_list,'Copy(Dynamic Array;'+sizesinttype.typename+'=`<low>`;'+sizesinttype.typename+'=`<length>`);');
  1765. end;
  1766. begin
  1767. if typemismatch then
  1768. CGMessagePos(fi,type_e_mismatch)
  1769. else
  1770. CGMessagePos1(fi,parser_e_wrong_parameter_size,'Copy');
  1771. if func='' then
  1772. begin
  1773. write_system_parameter_lists('fpc_shortstr_copy');
  1774. write_system_parameter_lists('fpc_char_copy');
  1775. write_system_parameter_lists('fpc_unicodestr_copy');
  1776. if tf_winlikewidestring in target_info.flags then
  1777. write_system_parameter_lists('fpc_widestr_copy');
  1778. write_system_parameter_lists('fpc_ansistr_copy');
  1779. write_dynarray_copy;
  1780. end
  1781. else if func='fpc_dynarray_copy' then
  1782. write_dynarray_copy
  1783. else
  1784. write_system_parameter_lists(func);
  1785. end;
  1786. var
  1787. paras : tnode;
  1788. ppn : tcallparanode;
  1789. paradef : tdef;
  1790. counter : integer;
  1791. minargs,
  1792. maxargs : longint;
  1793. func : string;
  1794. begin
  1795. if not assigned(left) then
  1796. begin
  1797. do_error(false,'',fileinfo);
  1798. exit(cerrornode.create);
  1799. end;
  1800. result:=nil;
  1801. { determine copy function to use based on the first argument,
  1802. also count the number of arguments in this loop }
  1803. counter:=1;
  1804. paras:=left;
  1805. ppn:=tcallparanode(paras);
  1806. while assigned(ppn.right) do
  1807. begin
  1808. inc(counter);
  1809. set_varstate(ppn.left,vs_read,[vsf_must_be_valid]);
  1810. ppn:=tcallparanode(ppn.right);
  1811. end;
  1812. set_varstate(ppn.left,vs_read,[vsf_must_be_valid]);
  1813. paradef:=ppn.left.resultdef;
  1814. { the string variants all require 2 or 3 args, only the array one allows less }
  1815. minargs:=2;
  1816. maxargs:=3;
  1817. func:='';
  1818. if is_ansistring(paradef) then
  1819. begin
  1820. // set resultdef to argument def
  1821. resultdef:=paradef;
  1822. func:='fpc_ansistr_copy';
  1823. end
  1824. else if (is_chararray(paradef) and (paradef.size>255)) or
  1825. ((cs_refcountedstrings in current_settings.localswitches) and is_pchar(paradef)) then
  1826. begin
  1827. // set resultdef to ansistring type since result will be in ansistring codepage
  1828. resultdef:=getansistringdef;
  1829. func:='fpc_ansistr_copy';
  1830. end
  1831. else if is_widestring(paradef) then
  1832. begin
  1833. resultdef:=cwidestringtype;
  1834. func:='fpc_widestr_copy';
  1835. end
  1836. else if is_unicodestring(paradef) or
  1837. is_widechararray(paradef) or
  1838. is_pwidechar(paradef) then
  1839. begin
  1840. resultdef:=cunicodestringtype;
  1841. func:='fpc_unicodestr_copy';
  1842. end
  1843. else
  1844. if is_char(paradef) then
  1845. begin
  1846. resultdef:=cshortstringtype;
  1847. func:='fpc_char_copy';
  1848. end
  1849. else
  1850. if is_dynamic_array(paradef) then
  1851. begin
  1852. minargs:=1;
  1853. resultdef:=paradef;
  1854. func:='fpc_array_to_dynarray_copy';
  1855. end
  1856. else
  1857. if is_open_array(paradef) then
  1858. begin
  1859. minargs:=1;
  1860. resultdef:=carraydef.create(0,-1,tarraydef(paradef).rangedef);
  1861. tarraydef(resultdef).arrayoptions:=tarraydef(resultdef).arrayoptions+[ado_IsDynamicArray];
  1862. tarraydef(resultdef).elementdef:=tarraydef(paradef).elementdef;
  1863. func:='fpc_array_to_dynarray_copy';
  1864. end
  1865. else if counter in [2..3] then
  1866. begin
  1867. resultdef:=cshortstringtype;
  1868. func:='fpc_shortstr_copy';
  1869. end
  1870. else if counter<=maxargs then
  1871. begin
  1872. do_error(true,'',ppn.left.fileinfo);
  1873. exit(cerrornode.create);
  1874. end;
  1875. if (counter<minargs) or (counter>maxargs) then
  1876. begin
  1877. do_error(false,func,fileinfo);
  1878. exit(cerrornode.create);
  1879. end;
  1880. end;
  1881. {$maxfpuregisters 0}
  1882. function getpi : bestreal;
  1883. begin
  1884. {$ifdef x86}
  1885. { x86 has pi in hardware }
  1886. result:=pi;
  1887. {$else x86}
  1888. {$ifdef cpuextended}
  1889. result:=MathPiExtended.Value;
  1890. {$else cpuextended}
  1891. result:=MathPi.Value;
  1892. {$endif cpuextended}
  1893. {$endif x86}
  1894. end;
  1895. function tinlinenode.simplify(forinline : boolean): tnode;
  1896. function do_lowhigh(def:tdef) : tnode;
  1897. var
  1898. v : tconstexprint;
  1899. enum : tenumsym;
  1900. hp : tnode;
  1901. i : integer;
  1902. begin
  1903. case def.typ of
  1904. orddef:
  1905. begin
  1906. set_varstate(left,vs_read,[]);
  1907. if inlinenumber=in_low_x then
  1908. v:=torddef(def).low
  1909. else
  1910. v:=torddef(def).high;
  1911. hp:=cordconstnode.create(v,def,true);
  1912. typecheckpass(hp);
  1913. do_lowhigh:=hp;
  1914. end;
  1915. enumdef:
  1916. begin
  1917. set_varstate(left,vs_read,[]);
  1918. if inlinenumber=in_high_x then
  1919. v:=tenumdef(def).maxval
  1920. else
  1921. v:=tenumdef(def).minval;
  1922. enum:=nil;
  1923. for i := 0 to tenumdef(def).symtable.SymList.Count - 1 do
  1924. if tenumsym(tenumdef(def).symtable.SymList[i]).value=v then
  1925. begin
  1926. enum:=tenumsym(tenumdef(def).symtable.SymList[i]);
  1927. break;
  1928. end;
  1929. if not assigned(enum) then
  1930. internalerror(309993)
  1931. else
  1932. hp:=genenumnode(enum);
  1933. do_lowhigh:=hp;
  1934. end;
  1935. else
  1936. internalerror(87);
  1937. end;
  1938. end;
  1939. function getconstrealvalue : bestreal;
  1940. begin
  1941. case left.nodetype of
  1942. ordconstn:
  1943. getconstrealvalue:=tordconstnode(left).value;
  1944. realconstn:
  1945. getconstrealvalue:=trealconstnode(left).value_real;
  1946. else
  1947. internalerror(309992);
  1948. end;
  1949. end;
  1950. procedure setconstrealvalue(r : bestreal);
  1951. begin
  1952. result:=crealconstnode.create(r,pbestrealtype^);
  1953. end;
  1954. function handle_ln_const(r : bestreal) : tnode;
  1955. begin
  1956. if r<=0.0 then
  1957. if floating_point_range_check_error then
  1958. begin
  1959. result:=crealconstnode.create(0,pbestrealtype^);
  1960. CGMessage(type_e_wrong_math_argument)
  1961. end
  1962. else
  1963. begin
  1964. if r=0.0 then
  1965. result:=crealconstnode.create(MathNegInf.Value,pbestrealtype^)
  1966. else
  1967. result:=crealconstnode.create(MathQNaN.Value,pbestrealtype^)
  1968. end
  1969. else
  1970. result:=crealconstnode.create(ln(r),pbestrealtype^)
  1971. end;
  1972. function handle_sqrt_const(r : bestreal) : tnode;
  1973. begin
  1974. if r<0.0 then
  1975. if floating_point_range_check_error then
  1976. begin
  1977. result:=crealconstnode.create(0,pbestrealtype^);
  1978. CGMessage(type_e_wrong_math_argument)
  1979. end
  1980. else
  1981. result:=crealconstnode.create(MathQNaN.Value,pbestrealtype^)
  1982. else
  1983. result:=crealconstnode.create(sqrt(r),pbestrealtype^)
  1984. end;
  1985. function handle_const_sar : tnode;
  1986. var
  1987. vl,vl2 : TConstExprInt;
  1988. bits,shift: integer;
  1989. mask : qword;
  1990. def : tdef;
  1991. begin
  1992. result:=nil;
  1993. if (left.nodetype=ordconstn) or ((left.nodetype=callparan) and (tcallparanode(left).left.nodetype=ordconstn)) then
  1994. begin
  1995. if (left.nodetype=callparan) and
  1996. assigned(tcallparanode(left).right) then
  1997. begin
  1998. vl:=tordconstnode(tcallparanode(left).left).value;
  1999. if forinline then
  2000. case resultdef.size of
  2001. 1,2,4:
  2002. vl:=vl and byte($1f);
  2003. 8:
  2004. vl:=vl and byte($3f);
  2005. else
  2006. internalerror(2013122303);
  2007. end;
  2008. if (tcallparanode(tcallparanode(left).right).left.nodetype=ordconstn) then
  2009. begin
  2010. def:=tcallparanode(tcallparanode(left).right).left.resultdef;
  2011. vl2:=tordconstnode(tcallparanode(tcallparanode(left).right).left).value;
  2012. end
  2013. else if vl=0 then
  2014. begin
  2015. result:=tcallparanode(tcallparanode(left).right).left;
  2016. tcallparanode(tcallparanode(left).right).left:=nil;
  2017. exit;
  2018. end
  2019. else
  2020. exit;
  2021. end
  2022. else
  2023. begin
  2024. def:=left.resultdef;
  2025. vl:=1;
  2026. vl2:=tordconstnode(left).value;
  2027. end;
  2028. bits:=def.size*8;
  2029. shift:=vl.svalue and (bits-1);
  2030. case bits of
  2031. 8:
  2032. mask:=$ff;
  2033. 16:
  2034. mask:=$ffff;
  2035. 32:
  2036. mask:=$ffffffff;
  2037. 64:
  2038. mask:=qword($ffffffffffffffff);
  2039. else
  2040. mask:=qword(1 shl bits)-1;
  2041. end;
  2042. {$push}
  2043. {$r-,q-}
  2044. if shift=0 then
  2045. result:=cordconstnode.create(vl2.svalue,def,false)
  2046. else if vl2.svalue<0 then
  2047. result:=cordconstnode.create(((vl2.svalue shr shift) or (mask shl (bits-shift))) and mask,def,false)
  2048. else
  2049. result:=cordconstnode.create((vl2.svalue shr shift) and mask,def,false);
  2050. {$pop}
  2051. end
  2052. else if (left.nodetype=callparan) and assigned(tcallparanode(left).right) and
  2053. (tcallparanode(tcallparanode(left).right).left.nodetype=ordconstn) then
  2054. begin
  2055. def:=tcallparanode(tcallparanode(left).right).left.resultdef;
  2056. vl2:=tordconstnode(tcallparanode(tcallparanode(left).right).left).value;
  2057. { sar(0,x) is 0 }
  2058. { sar32(ffffffff,x) is ffffffff, etc. }
  2059. if ((vl2=0) or
  2060. ((resultdef.size=1) and (shortint(vl2.svalue)=-1)) or
  2061. ((resultdef.size=2) and (smallint(vl2.svalue)=-1)) or
  2062. ((resultdef.size=4) and (longint(vl2.svalue)=-1)) or
  2063. ((resultdef.size=8) and (int64(vl2.svalue)=-1))) and
  2064. ((cs_opt_level4 in current_settings.optimizerswitches) or
  2065. not might_have_sideeffects(tcallparanode(left).left)) then
  2066. begin
  2067. if vl2=0 then
  2068. result:=cordconstnode.create(0,resultdef,true)
  2069. else
  2070. result:=cordconstnode.create(-1,resultdef,true);
  2071. end;
  2072. end;
  2073. end;
  2074. function handle_const_rox : tnode;
  2075. var
  2076. vl,vl2 : TConstExprInt;
  2077. bits,shift: integer;
  2078. def : tdef;
  2079. begin
  2080. result:=nil;
  2081. if (left.nodetype=ordconstn) or ((left.nodetype=callparan) and (tcallparanode(left).left.nodetype=ordconstn)) then
  2082. begin
  2083. if (left.nodetype=callparan) and
  2084. assigned(tcallparanode(left).right) then
  2085. begin
  2086. vl:=tordconstnode(tcallparanode(left).left).value;
  2087. if forinline then
  2088. case resultdef.size of
  2089. { unlike shifts, for rotates, when masking out the higher bits
  2090. of the rotate count, we go all the way down to byte, because
  2091. it doesn't matter, it produces the same result, since it's a rotate }
  2092. 1:
  2093. vl:=vl and byte($07);
  2094. 2:
  2095. vl:=vl and byte($0f);
  2096. 4:
  2097. vl:=vl and byte($1f);
  2098. 8:
  2099. vl:=vl and byte($3f);
  2100. else
  2101. internalerror(2013122304);
  2102. end;
  2103. if (tcallparanode(tcallparanode(left).right).left.nodetype=ordconstn) then
  2104. begin
  2105. def:=tcallparanode(tcallparanode(left).right).left.resultdef;
  2106. vl2:=tordconstnode(tcallparanode(tcallparanode(left).right).left).value;
  2107. end
  2108. else if vl=0 then
  2109. begin
  2110. result:=tcallparanode(tcallparanode(left).right).left;
  2111. tcallparanode(tcallparanode(left).right).left:=nil;
  2112. exit;
  2113. end
  2114. else
  2115. exit;
  2116. end
  2117. else
  2118. begin
  2119. def:=left.resultdef;
  2120. vl:=1;
  2121. vl2:=tordconstnode(left).value;
  2122. end;
  2123. bits:=def.size*8;
  2124. shift:=vl.svalue and (bits-1);
  2125. {$push}
  2126. {$r-,q-}
  2127. if shift=0 then
  2128. result:=cordconstnode.create(vl2.svalue,def,false)
  2129. else
  2130. case inlinenumber of
  2131. in_ror_x,in_ror_x_y:
  2132. case def.size of
  2133. 1:
  2134. result:=cordconstnode.create(RorByte(Byte(vl2.svalue),shift),def,false);
  2135. 2:
  2136. result:=cordconstnode.create(RorWord(Word(vl2.svalue),shift),def,false);
  2137. 4:
  2138. result:=cordconstnode.create(RorDWord(DWord(vl2.svalue),shift),def,false);
  2139. 8:
  2140. result:=cordconstnode.create(RorQWord(QWord(vl2.svalue),shift),def,false);
  2141. else
  2142. internalerror(2011061903);
  2143. end;
  2144. in_rol_x,in_rol_x_y:
  2145. case def.size of
  2146. 1:
  2147. result:=cordconstnode.create(RolByte(Byte(vl2.svalue),shift),def,false);
  2148. 2:
  2149. result:=cordconstnode.create(RolWord(Word(vl2.svalue),shift),def,false);
  2150. 4:
  2151. result:=cordconstnode.create(RolDWord(DWord(vl2.svalue),shift),def,false);
  2152. 8:
  2153. result:=cordconstnode.create(RolQWord(QWord(vl2.svalue),shift),def,false);
  2154. else
  2155. internalerror(2011061902);
  2156. end;
  2157. else
  2158. internalerror(2011061901);
  2159. end;
  2160. {$pop}
  2161. end
  2162. else if (left.nodetype=callparan) and assigned(tcallparanode(left).right) and
  2163. (tcallparanode(tcallparanode(left).right).left.nodetype=ordconstn) then
  2164. begin
  2165. def:=tcallparanode(tcallparanode(left).right).left.resultdef;
  2166. vl2:=tordconstnode(tcallparanode(tcallparanode(left).right).left).value;
  2167. { rol/ror are unsigned operations, so cut off upper bits }
  2168. case resultdef.size of
  2169. 1:
  2170. vl2:=vl2 and byte($ff);
  2171. 2:
  2172. vl2:=vl2 and word($ffff);
  2173. 4:
  2174. vl2:=vl2 and dword($ffffffff);
  2175. 8:
  2176. vl2:=vl2 and qword($ffffffffffffffff);
  2177. else
  2178. internalerror(2017050101);
  2179. end;
  2180. { rol(0,x) and ror(0,x) are 0 }
  2181. { rol32(ffffffff,x) and ror32(ffffffff,x) are ffffffff, etc. }
  2182. if ((vl2=0) or
  2183. ((resultdef.size=1) and (vl2=$ff)) or
  2184. ((resultdef.size=2) and (vl2=$ffff)) or
  2185. ((resultdef.size=4) and (vl2=$ffffffff)) or
  2186. ((resultdef.size=8) and (vl2.uvalue=qword($ffffffffffffffff)))) and
  2187. ((cs_opt_level4 in current_settings.optimizerswitches) or
  2188. not might_have_sideeffects(tcallparanode(left).left)) then
  2189. result:=cordconstnode.create(vl2,resultdef,true);
  2190. end;
  2191. end;
  2192. function is_minmax_deterministic(var node: tordconstnode; const DoMax: Boolean; out res: Boolean): Boolean;
  2193. begin
  2194. Result := False;
  2195. if not is_integer(resultdef) then
  2196. InternalError(2024031501);
  2197. if (node.value <= torddef(resultdef).low) then
  2198. begin
  2199. res := not DoMax;
  2200. Exit(True);
  2201. end
  2202. else if (node.value >= torddef(resultdef).high) then
  2203. begin
  2204. res := DoMax;
  2205. Exit(True);
  2206. end
  2207. end;
  2208. var
  2209. hp,hp2 : tnode;
  2210. vl,vl2 : TConstExprInt;
  2211. vr : bestreal;
  2212. helperres : Boolean;
  2213. begin { simplify }
  2214. result:=nil;
  2215. { handle intern constant functions in separate case }
  2216. if inf_inlineconst in inlinenodeflags then
  2217. begin
  2218. { no parameters? }
  2219. if not assigned(left) then
  2220. internalerror(200501231)
  2221. else
  2222. begin
  2223. vl:=0;
  2224. vl2:=0; { second parameter Ex: ptr(vl,vl2) }
  2225. case left.nodetype of
  2226. ordconstn :
  2227. vl:=tordconstnode(left).value;
  2228. callparan :
  2229. begin
  2230. { both exists, else it was not generated }
  2231. vl:=tordconstnode(tcallparanode(left).left).value;
  2232. vl2:=tordconstnode(tcallparanode(tcallparanode(left).right).left).value;
  2233. end;
  2234. else
  2235. begin
  2236. { Real functions are all handled with internproc below, and
  2237. unsupported typex are also trapped here }
  2238. if is_integer(left.resultdef) then
  2239. { Not as informative, but less confusing }
  2240. CGMessagePos(left.fileinfo,parser_e_illegal_expression)
  2241. else
  2242. CGMessagePos1(left.fileinfo,type_e_integer_expr_expected,left.resultdef.typename);
  2243. result:=cerrornode.create;
  2244. exit;
  2245. end;
  2246. end;
  2247. case inlinenumber of
  2248. in_const_abs :
  2249. if vl.signed then
  2250. hp:=create_simplified_ord_const(abs(vl.svalue),resultdef,forinline,false)
  2251. else
  2252. hp:=create_simplified_ord_const(vl.uvalue,resultdef,forinline,false);
  2253. in_const_sqr:
  2254. if vl.signed then
  2255. hp:=create_simplified_ord_const(sqr(vl.svalue),resultdef,forinline,false)
  2256. else
  2257. hp:=create_simplified_ord_const(sqr(vl.uvalue),resultdef,forinline,false);
  2258. in_const_odd :
  2259. hp:=cordconstnode.create(qword(odd(int64(vl))),pasbool1type,true);
  2260. in_const_swap_word :
  2261. hp:=cordconstnode.create((vl and $ff) shl 8+(vl shr 8),left.resultdef,true);
  2262. in_const_swap_long :
  2263. hp:=cordconstnode.create((vl and $ffff) shl 16+(vl shr 16),left.resultdef,true);
  2264. in_const_swap_qword :
  2265. hp:=cordconstnode.create((vl and $ffffffff) shl 32+(vl shr 32),left.resultdef,true);
  2266. in_const_ptr:
  2267. begin
  2268. {Don't construct pointers from negative values.}
  2269. if (vl.signed and (vl.svalue<0)) or (vl2.signed and (vl2.svalue<0)) then
  2270. cgmessage(parser_e_range_check_error);
  2271. {$if defined(i8086)}
  2272. hp:=cpointerconstnode.create((vl2.uvalue shl 16)+vl.uvalue,voidfarpointertype);
  2273. {$elseif defined(i386)}
  2274. hp:=cpointerconstnode.create((vl2.uvalue shl 4)+vl.uvalue,voidnearfspointertype);
  2275. {$else}
  2276. hp:=cpointerconstnode.create((vl2.uvalue shl 4)+vl.uvalue,voidpointertype);
  2277. {$endif}
  2278. end;
  2279. in_const_eh_return_data_regno:
  2280. begin
  2281. vl:=eh_return_data_regno(vl.svalue);
  2282. if vl=-1 then
  2283. CGMessagePos(left.fileinfo,type_e_range_check_error_bounds);
  2284. hp:=genintconstnode(vl);
  2285. end;
  2286. else
  2287. internalerror(88);
  2288. end;
  2289. end;
  2290. if hp=nil then
  2291. hp:=cerrornode.create;
  2292. result:=hp;
  2293. end
  2294. else
  2295. begin
  2296. case inlinenumber of
  2297. in_lo_long,
  2298. in_hi_long,
  2299. in_lo_qword,
  2300. in_hi_qword,
  2301. in_lo_word,
  2302. in_hi_word :
  2303. begin
  2304. if left.nodetype=ordconstn then
  2305. begin
  2306. case inlinenumber of
  2307. in_lo_word :
  2308. result:=cordconstnode.create(tordconstnode(left).value and $ff,u8inttype,true);
  2309. in_hi_word :
  2310. result:=cordconstnode.create(tordconstnode(left).value shr 8,u8inttype,true);
  2311. in_lo_long :
  2312. result:=cordconstnode.create(tordconstnode(left).value and $ffff,u16inttype,true);
  2313. in_hi_long :
  2314. result:=cordconstnode.create(tordconstnode(left).value shr 16,u16inttype,true);
  2315. in_lo_qword :
  2316. result:=cordconstnode.create(tordconstnode(left).value and $ffffffff,u32inttype,true);
  2317. in_hi_qword :
  2318. result:=cordconstnode.create(tordconstnode(left).value shr 32,u32inttype,true);
  2319. else
  2320. internalerror(2019050514);
  2321. end;
  2322. end;
  2323. end;
  2324. in_ord_x:
  2325. begin
  2326. case left.resultdef.typ of
  2327. orddef :
  2328. begin
  2329. case torddef(left.resultdef).ordtype of
  2330. pasbool1,
  2331. pasbool8,
  2332. uchar:
  2333. begin
  2334. { change to byte() }
  2335. result:=ctypeconvnode.create_internal(left,u8inttype);
  2336. left:=nil;
  2337. end;
  2338. pasbool16,
  2339. uwidechar :
  2340. begin
  2341. { change to word() }
  2342. result:=ctypeconvnode.create_internal(left,u16inttype);
  2343. left:=nil;
  2344. end;
  2345. pasbool32 :
  2346. begin
  2347. { change to dword() }
  2348. result:=ctypeconvnode.create_internal(left,u32inttype);
  2349. left:=nil;
  2350. end;
  2351. pasbool64 :
  2352. begin
  2353. { change to qword() }
  2354. result:=ctypeconvnode.create_internal(left,u64inttype);
  2355. left:=nil;
  2356. end;
  2357. bool8bit:
  2358. begin
  2359. { change to shortint() }
  2360. result:=ctypeconvnode.create_internal(left,s8inttype);
  2361. left:=nil;
  2362. end;
  2363. bool16bit :
  2364. begin
  2365. { change to smallint() }
  2366. result:=ctypeconvnode.create_internal(left,s16inttype);
  2367. left:=nil;
  2368. end;
  2369. bool32bit :
  2370. begin
  2371. { change to longint() }
  2372. result:=ctypeconvnode.create_internal(left,s32inttype);
  2373. left:=nil;
  2374. end;
  2375. bool64bit :
  2376. begin
  2377. { change to int64() }
  2378. result:=ctypeconvnode.create_internal(left,s64inttype);
  2379. left:=nil;
  2380. end;
  2381. uvoid :
  2382. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  2383. else
  2384. begin
  2385. { all other orddef need no transformation }
  2386. result:=left;
  2387. left:=nil;
  2388. end;
  2389. end;
  2390. end;
  2391. enumdef :
  2392. begin
  2393. result:=ctypeconvnode.create_internal(left,s32inttype);
  2394. left:=nil;
  2395. end;
  2396. undefineddef :
  2397. begin
  2398. { we just create a constant 0 here, that's marked as a
  2399. parameter }
  2400. result:=cordconstnode.create(0,s32inttype,false);
  2401. include(result.flags,nf_generic_para);
  2402. left:=nil;
  2403. end;
  2404. pointerdef :
  2405. begin
  2406. if m_mac in current_settings.modeswitches then
  2407. begin
  2408. result:=ctypeconvnode.create_internal(left,ptruinttype);
  2409. left:=nil;
  2410. end
  2411. end;
  2412. else
  2413. internalerror(2019050513);
  2414. end;
  2415. (*
  2416. if (left.nodetype=ordconstn) then
  2417. begin
  2418. result:=cordconstnode.create(
  2419. tordconstnode(left).value,sinttype,true);
  2420. end
  2421. else if (m_mac in current_settings.modeswitches) and
  2422. (left.ndoetype=pointerconstn) then
  2423. result:=cordconstnode.create(
  2424. tpointerconstnode(left).value,ptruinttype,true);
  2425. *)
  2426. end;
  2427. in_chr_byte:
  2428. begin
  2429. { convert to explicit char() }
  2430. result:=ctypeconvnode.create_internal(left,cansichartype);
  2431. left:=nil;
  2432. end;
  2433. in_length_x:
  2434. begin
  2435. case left.resultdef.typ of
  2436. stringdef :
  2437. begin
  2438. if (left.nodetype=stringconstn) then
  2439. begin
  2440. result:=cordconstnode.create(
  2441. tstringconstnode(left).len,sinttype,true);
  2442. end;
  2443. end;
  2444. orddef :
  2445. begin
  2446. { length of char is always one }
  2447. if is_char(left.resultdef) or
  2448. is_widechar(left.resultdef) then
  2449. begin
  2450. result:=cordconstnode.create(1,sinttype,false);
  2451. end
  2452. end;
  2453. arraydef :
  2454. begin
  2455. if (left.nodetype=stringconstn) then
  2456. begin
  2457. result:=cordconstnode.create(
  2458. tstringconstnode(left).len,sinttype,true);
  2459. end
  2460. else if not is_open_array(left.resultdef) and
  2461. not is_array_of_const(left.resultdef) and
  2462. not is_dynamic_array(left.resultdef) then
  2463. result:=genintconstnode(tarraydef(left.resultdef).highrange-
  2464. tarraydef(left.resultdef).lowrange+1,sizesinttype);
  2465. end;
  2466. else
  2467. ;
  2468. end;
  2469. end;
  2470. in_assigned_x:
  2471. begin
  2472. if is_constnode(tcallparanode(left).left) or
  2473. (tcallparanode(left).left.nodetype = pointerconstn) then
  2474. begin
  2475. { let an add node figure it out }
  2476. result:=caddnode.create(unequaln,tcallparanode(left).left,cnilnode.create);
  2477. tcallparanode(left).left := nil;
  2478. end;
  2479. end;
  2480. in_pred_x,
  2481. in_succ_x:
  2482. begin
  2483. case left.nodetype of
  2484. ordconstn:
  2485. begin
  2486. if inlinenumber=in_succ_x then
  2487. vl:=tordconstnode(left).value+1
  2488. else
  2489. vl:=tordconstnode(left).value-1;
  2490. if is_integer(left.resultdef) then
  2491. { the type of the original integer constant is irrelevant,
  2492. it should be automatically adapted to the new value
  2493. (except when inlining) }
  2494. result:=create_simplified_ord_const(vl,resultdef,forinline,cs_check_range in localswitches)
  2495. else
  2496. { check the range for enums, chars, booleans }
  2497. result:=cordconstnode.create(vl,left.resultdef,not(nf_internal in flags));
  2498. result.flags:=result.flags+(flags*[nf_internal]);
  2499. end;
  2500. addn,
  2501. subn:
  2502. begin
  2503. { fold succ/pred in child add/sub nodes with a constant if possible:
  2504. - no overflow/range checking
  2505. - equal types
  2506. }
  2507. if ([cs_check_overflow,cs_check_range]*current_settings.localswitches)=[] then
  2508. begin
  2509. if inlinenumber=in_succ_x then
  2510. vl:=1
  2511. else
  2512. vl:=-1;
  2513. if (taddnode(left).left.nodetype=ordconstn) and equal_defs(resultdef,taddnode(left).left.resultdef) then
  2514. begin
  2515. tordconstnode(taddnode(left).left).value:=tordconstnode(taddnode(left).left).value+vl;
  2516. result:=left;
  2517. left:=nil;
  2518. end
  2519. else if (taddnode(left).right.nodetype=ordconstn) and equal_defs(resultdef,taddnode(left).right.resultdef) then
  2520. begin
  2521. if left.nodetype=subn then
  2522. tordconstnode(taddnode(left).right).value:=tordconstnode(taddnode(left).right).value-vl
  2523. else
  2524. tordconstnode(taddnode(left).right).value:=tordconstnode(taddnode(left).right).value+vl;
  2525. result:=left;
  2526. left:=nil;
  2527. end;
  2528. end;
  2529. end;
  2530. else
  2531. ;
  2532. end;
  2533. end;
  2534. in_low_x,
  2535. in_high_x:
  2536. begin
  2537. case left.resultdef.typ of
  2538. orddef,
  2539. enumdef:
  2540. begin
  2541. result:=do_lowhigh(left.resultdef);
  2542. end;
  2543. setdef:
  2544. begin
  2545. result:=do_lowhigh(tsetdef(left.resultdef).elementdef);
  2546. end;
  2547. arraydef:
  2548. begin
  2549. if (inlinenumber=in_low_x) then
  2550. begin
  2551. result:=cordconstnode.create(int64(tarraydef(
  2552. left.resultdef).lowrange),tarraydef(left.resultdef).rangedef,true);
  2553. end
  2554. else if not is_open_array(left.resultdef) and
  2555. not is_array_of_const(left.resultdef) and
  2556. not is_dynamic_array(left.resultdef) then
  2557. result:=cordconstnode.create(int64(tarraydef(left.resultdef).highrange),
  2558. tarraydef(left.resultdef).rangedef,true);
  2559. end;
  2560. stringdef:
  2561. begin
  2562. if inlinenumber=in_low_x then
  2563. begin
  2564. if is_dynamicstring(left.resultdef) and
  2565. not(cs_zerobasedstrings in current_settings.localswitches) then
  2566. result:=cordconstnode.create(1,u8inttype,false)
  2567. else
  2568. result:=cordconstnode.create(0,u8inttype,false);
  2569. end
  2570. else if not is_dynamicstring(left.resultdef) then
  2571. result:=cordconstnode.create(tstringdef(left.resultdef).len,u8inttype,true)
  2572. end;
  2573. undefineddef:
  2574. begin
  2575. result:=cordconstnode.create(0,u8inttype,false);
  2576. end;
  2577. errordef:
  2578. ;
  2579. else
  2580. internalerror(2019050512);
  2581. end;
  2582. end;
  2583. in_exp_real :
  2584. begin
  2585. if left.nodetype in [ordconstn,realconstn] then
  2586. begin
  2587. result:=crealconstnode.create(exp(getconstrealvalue),pbestrealtype^);
  2588. if (trealconstnode(result).value_real=MathInf.Value) and
  2589. floating_point_range_check_error then
  2590. begin
  2591. result:=crealconstnode.create(0,pbestrealtype^);
  2592. CGMessage(parser_e_range_check_error);
  2593. end;
  2594. end
  2595. end;
  2596. in_trunc_real :
  2597. begin
  2598. if left.nodetype in [ordconstn,realconstn] then
  2599. begin
  2600. vr:=getconstrealvalue;
  2601. if (vr>=9223372036854775807.99) or (vr<=-9223372036854775808.0) then
  2602. begin
  2603. message3(type_e_range_check_error_bounds,realtostr(vr),'-9223372036854775808.0','9223372036854775807.99..');
  2604. result:=cordconstnode.create(1,s64inttype,false)
  2605. end
  2606. else
  2607. result:=cordconstnode.create(trunc(vr),s64inttype,true)
  2608. end
  2609. end;
  2610. in_round_real :
  2611. begin
  2612. { can't evaluate while inlining, may depend on fpu setting }
  2613. if (not forinline) and
  2614. (left.nodetype in [ordconstn,realconstn]) then
  2615. begin
  2616. vr:=getconstrealvalue;
  2617. if (vr>=9223372036854775807.5) or (vr<=-9223372036854775808.5) then
  2618. begin
  2619. message3(type_e_range_check_error_bounds,realtostr(vr),'-9223372036854775808.49..','9223372036854775807.49..');
  2620. result:=cordconstnode.create(1,s64inttype,false)
  2621. end
  2622. else
  2623. result:=cordconstnode.create(round(vr),s64inttype,true)
  2624. end
  2625. end;
  2626. in_frac_real :
  2627. begin
  2628. if left.nodetype in [ordconstn,realconstn] then
  2629. setconstrealvalue(frac(getconstrealvalue))
  2630. end;
  2631. in_int_real :
  2632. begin
  2633. if left.nodetype in [ordconstn,realconstn] then
  2634. setconstrealvalue(int(getconstrealvalue));
  2635. end;
  2636. in_pi_real :
  2637. begin
  2638. if block_type=bt_const then
  2639. setconstrealvalue(getpi)
  2640. end;
  2641. in_cos_real :
  2642. begin
  2643. if left.nodetype in [ordconstn,realconstn] then
  2644. setconstrealvalue(cos(getconstrealvalue))
  2645. end;
  2646. in_sin_real :
  2647. begin
  2648. if left.nodetype in [ordconstn,realconstn] then
  2649. setconstrealvalue(sin(getconstrealvalue))
  2650. end;
  2651. in_arctan_real :
  2652. begin
  2653. if left.nodetype in [ordconstn,realconstn] then
  2654. setconstrealvalue(arctan(getconstrealvalue))
  2655. end;
  2656. in_abs_real :
  2657. begin
  2658. if left.nodetype in [ordconstn,realconstn] then
  2659. setconstrealvalue(abs(getconstrealvalue))
  2660. end;
  2661. in_abs_long:
  2662. begin
  2663. if left.nodetype=ordconstn then
  2664. begin
  2665. if tordconstnode(left).value<0 then
  2666. result:=cordconstnode.create((-tordconstnode(left).value),resultdef,false)
  2667. else
  2668. result:=cordconstnode.create((tordconstnode(left).value),resultdef,false);
  2669. end
  2670. end;
  2671. in_sqr_real :
  2672. begin
  2673. if left.nodetype in [ordconstn,realconstn] then
  2674. setconstrealvalue(sqr(getconstrealvalue))
  2675. end;
  2676. in_sqrt_real :
  2677. begin
  2678. if left.nodetype in [ordconstn,realconstn] then
  2679. result:=handle_sqrt_const(getconstrealvalue);
  2680. end;
  2681. in_ln_real :
  2682. begin
  2683. if left.nodetype in [ordconstn,realconstn] then
  2684. result:=handle_ln_const(getconstrealvalue);
  2685. end;
  2686. in_assert_x_y :
  2687. begin
  2688. if not(cs_do_assertion in current_settings.localswitches) then
  2689. { we need a valid node, so insert a nothingn }
  2690. result:=cnothingnode.create;
  2691. end;
  2692. in_sar_x,
  2693. in_sar_x_y :
  2694. begin
  2695. result:=handle_const_sar;
  2696. end;
  2697. in_rol_x,
  2698. in_rol_x_y,
  2699. in_ror_x,
  2700. in_ror_x_y :
  2701. result:=handle_const_rox;
  2702. in_bsf_x:
  2703. begin
  2704. if left.nodetype=ordconstn then
  2705. begin
  2706. case left.resultdef.size of
  2707. 1:
  2708. result:=cordconstnode.create(BsfByte(Byte(tordconstnode(left).value.uvalue)),resultdef,false);
  2709. 2:
  2710. result:=cordconstnode.create(BsfWord(Word(tordconstnode(left).value.uvalue)),resultdef,false);
  2711. 4:
  2712. result:=cordconstnode.create(BsfDWord(DWord(tordconstnode(left).value.uvalue)),resultdef,false);
  2713. 8:
  2714. result:=cordconstnode.create(BsfQWord(QWord(tordconstnode(left).value.uvalue)),resultdef,false);
  2715. else
  2716. internalerror(2017042401);
  2717. end;
  2718. end;
  2719. end;
  2720. in_bsr_x :
  2721. begin
  2722. if left.nodetype=ordconstn then
  2723. begin
  2724. case left.resultdef.size of
  2725. 1:
  2726. result:=cordconstnode.create(BsrByte(Byte(tordconstnode(left).value.uvalue)),resultdef,false);
  2727. 2:
  2728. result:=cordconstnode.create(BsrWord(Word(tordconstnode(left).value.uvalue)),resultdef,false);
  2729. 4:
  2730. result:=cordconstnode.create(BsrDWord(DWord(tordconstnode(left).value.uvalue)),resultdef,false);
  2731. 8:
  2732. result:=cordconstnode.create(BsrQWord(QWord(tordconstnode(left).value.uvalue)),resultdef,false);
  2733. else
  2734. internalerror(2017042402);
  2735. end;
  2736. end;
  2737. end;
  2738. in_popcnt_x :
  2739. begin
  2740. if left.nodetype=ordconstn then
  2741. begin
  2742. result:=cordconstnode.create(PopCnt(tordconstnode(left).value),resultdef,false);
  2743. end;
  2744. end;
  2745. in_min_single,
  2746. in_min_double:
  2747. begin
  2748. { Check to see if the result is deterministic }
  2749. if left.nodetype=callparan then
  2750. begin
  2751. hp:=tcallparanode(tcallparanode(left).nextpara).paravalue;
  2752. hp2:=tcallparanode(left).paravalue;
  2753. if (hp.nodetype=realconstn) then
  2754. begin
  2755. if (trealconstnode(hp).value_real = MathQNaN.value) then
  2756. { If one of the inputs is NaN, the second parameter
  2757. is taken }
  2758. result:=hp2.getcopy()
  2759. else if (trealconstnode(hp).value_real = MathNegInf.value) then
  2760. { Nothing is less than than -oo }
  2761. result:=crealconstnode.create(MathNegInf.value,resultdef)
  2762. else if (trealconstnode(hp).value_real = MathInf.value) then
  2763. { Everything is less than +oo }
  2764. result:=hp2.getcopy()
  2765. else if (hp2.nodetype=realconstn) then
  2766. begin
  2767. { Both actual parameters are constants, so take
  2768. the smaller of the two right now }
  2769. if (trealconstnode(hp).value_real < trealconstnode(hp2).value_real) then
  2770. result:=crealconstnode.create(trealconstnode(hp).value_real,resultdef)
  2771. else
  2772. result:=crealconstnode.create(trealconstnode(hp2).value_real,resultdef);
  2773. end;
  2774. end
  2775. else if (hp2.nodetype=realconstn) then
  2776. begin
  2777. if (trealconstnode(hp2).value_real = MathQNaN.value) then
  2778. { If one of the inputs is NaN, the second parameter
  2779. is taken (even if it is NaN) }
  2780. result:=crealconstnode.create(MathQNaN.value,resultdef)
  2781. else if (trealconstnode(hp2).value_real = MathNegInf.value) then
  2782. { Nothing is less than than -oo }
  2783. result:=crealconstnode.create(MathNegInf.value,resultdef)
  2784. else if (trealconstnode(hp2).value_real = MathInf.value) then
  2785. { Everything is less than +oo }
  2786. result:=hp.getcopy();
  2787. end;
  2788. end;
  2789. end;
  2790. in_max_single,
  2791. in_max_double:
  2792. begin
  2793. { Check to see if the result is deterministic }
  2794. if left.nodetype=callparan then
  2795. begin
  2796. hp:=tcallparanode(tcallparanode(left).nextpara).paravalue;
  2797. hp2:=tcallparanode(left).paravalue;
  2798. if (hp.nodetype=realconstn) then
  2799. begin
  2800. if (trealconstnode(hp).value_real = MathQNaN.value) then
  2801. { If one of the inputs is NaN, the second parameter
  2802. is taken }
  2803. result:=hp2.getcopy()
  2804. else if (trealconstnode(hp).value_real = MathNegInf.value) then
  2805. { Everything is greater than than -oo }
  2806. result:=hp2.getcopy()
  2807. else if (trealconstnode(hp).value_real = MathInf.value) then
  2808. { Nothing is greater than +oo }
  2809. result:=crealconstnode.create(MathInf.value,resultdef)
  2810. else if (hp2.nodetype=realconstn) then
  2811. begin
  2812. { Both actual parameters are constants, so take
  2813. the larger of the two right now }
  2814. if (trealconstnode(hp).value_real > trealconstnode(hp2).value_real) then
  2815. result:=crealconstnode.create(trealconstnode(hp).value_real,resultdef)
  2816. else
  2817. result:=crealconstnode.create(trealconstnode(hp2).value_real,resultdef)
  2818. end;
  2819. end
  2820. else if (hp2.nodetype=realconstn) then
  2821. begin
  2822. if (trealconstnode(hp2).value_real = MathQNaN.value) then
  2823. { If one of the inputs is NaN, the second parameter
  2824. is taken (even if it is NaN) }
  2825. result:=crealconstnode.create(MathQNaN.value,resultdef)
  2826. else if (trealconstnode(hp2).value_real = MathNegInf.value) then
  2827. { Everything is greater than than -oo }
  2828. result:=hp.getcopy()
  2829. else if (trealconstnode(hp2).value_real = MathInf.value) then
  2830. { Nothing is greater than +oo }
  2831. result:=crealconstnode.create(MathInf.value,resultdef);
  2832. end;
  2833. end;
  2834. end;
  2835. in_min_longint,
  2836. in_min_int64:
  2837. begin
  2838. if left.nodetype=callparan then
  2839. begin
  2840. { Check to see if the result is deterministic }
  2841. hp:=tcallparanode(tcallparanode(left).nextpara).paravalue;
  2842. hp2:=tcallparanode(left).paravalue;
  2843. if (hp.nodetype=ordconstn) then
  2844. begin
  2845. if (hp2.nodetype=ordconstn) then
  2846. begin
  2847. { Both actual parameters are constants, so take
  2848. the smaller of the two right now }
  2849. if inlinenumber=in_min_longint then
  2850. result:=cordconstnode.create(min(LongInt(tordconstnode(hp).value.svalue),LongInt(tordconstnode(hp2).value.svalue)),resultdef,false)
  2851. else
  2852. result:=cordconstnode.create(min(tordconstnode(hp).value,tordconstnode(hp2).value),resultdef,false);
  2853. end;
  2854. if is_minmax_deterministic(tordconstnode(hp), False, helperres) then
  2855. begin
  2856. if helperres then
  2857. result:=cordconstnode.create(tordconstnode(hp).value,resultdef,false)
  2858. else
  2859. result:=hp2.getcopy();
  2860. end;
  2861. end
  2862. else if (hp2.nodetype=ordconstn) then
  2863. begin
  2864. if is_minmax_deterministic(tordconstnode(hp2), False, helperres) then
  2865. begin
  2866. if helperres then
  2867. result:=cordconstnode.create(tordconstnode(hp2).value,resultdef,false)
  2868. else
  2869. result:=hp.getcopy();
  2870. end;
  2871. end;
  2872. end;
  2873. end;
  2874. in_max_longint,
  2875. in_max_int64:
  2876. begin
  2877. if left.nodetype=callparan then
  2878. begin
  2879. { Check to see if the result is deterministic }
  2880. hp:=tcallparanode(left).paravalue;
  2881. hp2:=tcallparanode(tcallparanode(left).nextpara).paravalue;
  2882. if (hp.nodetype=ordconstn) then
  2883. begin
  2884. if (hp2.nodetype=ordconstn) then
  2885. begin
  2886. { Both actual parameters are constants, so take
  2887. the larger of the two right now }
  2888. if inlinenumber=in_max_longint then
  2889. result:=cordconstnode.create(max(LongInt(tordconstnode(hp).value.svalue),LongInt(tordconstnode(hp2).value.svalue)),resultdef,false)
  2890. else
  2891. result:=cordconstnode.create(max(tordconstnode(hp).value,tordconstnode(hp2).value),resultdef,false);
  2892. end;
  2893. if is_minmax_deterministic(tordconstnode(hp), True, helperres) then
  2894. begin
  2895. if helperres then
  2896. result:=cordconstnode.create(tordconstnode(hp).value,resultdef,false)
  2897. else
  2898. result:=hp2.getcopy();
  2899. end;
  2900. end
  2901. else if (hp2.nodetype=ordconstn) then
  2902. begin
  2903. if is_minmax_deterministic(tordconstnode(hp2), True, helperres) then
  2904. begin
  2905. if helperres then
  2906. result:=cordconstnode.create(tordconstnode(hp2).value,resultdef,false)
  2907. else
  2908. result:=hp.getcopy();
  2909. end;
  2910. end;
  2911. end;
  2912. end;
  2913. in_min_dword,
  2914. in_min_qword:
  2915. begin
  2916. if left.nodetype=callparan then
  2917. begin
  2918. { Check to see if the result is deterministic }
  2919. hp:=tcallparanode(tcallparanode(left).nextpara).paravalue;
  2920. hp2:=tcallparanode(left).paravalue;
  2921. if (hp.nodetype=ordconstn) then
  2922. begin
  2923. if (hp2.nodetype=ordconstn) then
  2924. begin
  2925. { Both actual parameters are constants, so take
  2926. the smaller of the two right now }
  2927. if inlinenumber=in_min_dword then
  2928. result:=cordconstnode.create(min(DWord(tordconstnode(hp).value.uvalue),DWord(tordconstnode(hp2).value.uvalue)),resultdef,false)
  2929. else
  2930. result:=cordconstnode.create(min(tordconstnode(hp).value,tordconstnode(hp2).value),resultdef,false);
  2931. end;
  2932. if is_minmax_deterministic(tordconstnode(hp), False, helperres) then
  2933. begin
  2934. if helperres then
  2935. result:=cordconstnode.create(tordconstnode(hp).value,resultdef,false)
  2936. else
  2937. result:=hp2.getcopy();
  2938. end;
  2939. end
  2940. else if (hp2.nodetype=ordconstn) then
  2941. begin
  2942. if is_minmax_deterministic(tordconstnode(hp2), False, helperres) then
  2943. begin
  2944. if helperres then
  2945. result:=cordconstnode.create(tordconstnode(hp2).value,resultdef,false)
  2946. else
  2947. result:=hp.getcopy();
  2948. end;
  2949. end;
  2950. end;
  2951. end;
  2952. in_max_dword,
  2953. in_max_qword:
  2954. begin
  2955. if left.nodetype=callparan then
  2956. begin
  2957. { Check to see if the result is deterministic }
  2958. hp:=tcallparanode(left).paravalue;
  2959. hp2:=tcallparanode(tcallparanode(left).nextpara).paravalue;
  2960. if (hp.nodetype=ordconstn) then
  2961. begin
  2962. if (hp2.nodetype=ordconstn) then
  2963. begin
  2964. { Both actual parameters are constants, so take
  2965. the larger of the two right now }
  2966. if inlinenumber=in_max_dword then
  2967. result:=cordconstnode.create(max(DWord(tordconstnode(hp).value.uvalue),DWord(tordconstnode(hp2).value.uvalue)),resultdef,false)
  2968. else
  2969. result:=cordconstnode.create(max(tordconstnode(hp).value,tordconstnode(hp2).value),resultdef,false);
  2970. end;
  2971. if is_minmax_deterministic(tordconstnode(hp), True, helperres) then
  2972. begin
  2973. if helperres then
  2974. result:=cordconstnode.create(tordconstnode(hp).value,resultdef,false)
  2975. else
  2976. result:=hp2.getcopy();
  2977. end;
  2978. end
  2979. else if (hp2.nodetype=ordconstn) then
  2980. begin
  2981. if is_minmax_deterministic(tordconstnode(hp2), True, helperres) then
  2982. begin
  2983. if helperres then
  2984. result:=cordconstnode.create(tordconstnode(hp2).value,resultdef,false)
  2985. else
  2986. result:=hp.getcopy();
  2987. end;
  2988. end;
  2989. end;
  2990. end;
  2991. else
  2992. ;
  2993. end;
  2994. end;
  2995. end;
  2996. function tinlinenode.pass_typecheck:tnode;
  2997. type
  2998. tfloattypeset = set of tfloattype;
  2999. function removefloatupcasts(var p: tnode; const floattypes: tfloattypeset): tdef;
  3000. var
  3001. hnode: tnode;
  3002. begin
  3003. { System unit declares internal functions like this:
  3004. function foo(x: valreal): valreal; [internproc: number];
  3005. Calls to such functions are initially processed by callnode,
  3006. which typechecks the arguments, possibly inserting conversion to valreal.
  3007. To handle smaller types without excess precision, we need to remove
  3008. these extra typecasts. }
  3009. if not(cs_excessprecision in current_settings.localswitches) and
  3010. (p.nodetype=typeconvn) and
  3011. (ttypeconvnode(p).left.resultdef.typ=floatdef) and
  3012. (p.flags*[nf_explicit,nf_internal]=[]) and
  3013. (tfloatdef(ttypeconvnode(p).left.resultdef).floattype in (floattypes*[s32real,s64real,s80real,sc80real,s128real])) then
  3014. begin
  3015. hnode:=ttypeconvnode(p).left;
  3016. ttypeconvnode(p).left:=nil;
  3017. p.free;
  3018. p:=hnode;
  3019. result:=p.resultdef;
  3020. end
  3021. else if (p.nodetype=typeconvn) and
  3022. (p.flags*[nf_explicit,nf_internal]=[]) and
  3023. (ttypeconvnode(p).left.resultdef.typ=floatdef) and
  3024. (tfloatdef(ttypeconvnode(p).left.resultdef).floattype in (floattypes*[s64currency,s64comp])) then
  3025. begin
  3026. hnode:=ttypeconvnode(p).left;
  3027. ttypeconvnode(p).left:=nil;
  3028. p.free;
  3029. p:=hnode;
  3030. if is_currency(p.resultdef) then
  3031. begin
  3032. if (nf_is_currency in p.flags) and
  3033. (p.nodetype=slashn) and
  3034. (taddnode(p).right.nodetype=realconstn) and
  3035. (trealconstnode(taddnode(p).right).value_real=10000.0) and
  3036. not(nf_is_currency in taddnode(p).left.flags) then
  3037. begin
  3038. hnode:=taddnode(p).left;
  3039. taddnode(p).left:=nil;
  3040. p.free;
  3041. p:=hnode;
  3042. end;
  3043. end;
  3044. result:=p.resultdef;
  3045. end
  3046. { in case the system helper was declared with overloads for different types,
  3047. keep those }
  3048. else if (p.resultdef.typ=floatdef) and
  3049. (tfloatdef(p.resultdef).floattype in (floattypes*[s32real,s64real,s80real,sc80real,s128real])) then
  3050. result:=p.resultdef
  3051. else
  3052. begin
  3053. { for variant parameters; the rest has been converted by the
  3054. call node already }
  3055. if not(p.nodetype in [ordconstn,realconstn]) then
  3056. inserttypeconv(P,pbestrealtype^);
  3057. result:=p.resultdef
  3058. end;
  3059. end;
  3060. procedure handle_pack_unpack;
  3061. var
  3062. source, target, index: tcallparanode;
  3063. unpackedarraydef, packedarraydef: tarraydef;
  3064. tempindex: TConstExprInt;
  3065. begin
  3066. resultdef:=voidtype;
  3067. unpackedarraydef := nil;
  3068. packedarraydef := nil;
  3069. source := tcallparanode(left);
  3070. if (inlinenumber = in_unpack_x_y_z) then
  3071. begin
  3072. target := tcallparanode(source.right);
  3073. index := tcallparanode(target.right);
  3074. { source must be a packed array }
  3075. if not is_packed_array(source.left.resultdef) then
  3076. CGMessagePos2(source.left.fileinfo,type_e_got_expected_packed_array,'1',source.left.resultdef.typename)
  3077. else
  3078. packedarraydef := tarraydef(source.left.resultdef);
  3079. { target can be any kind of array, as long as it's not packed }
  3080. if (target.left.resultdef.typ <> arraydef) or
  3081. is_packed_array(target.left.resultdef) then
  3082. CGMessagePos2(target.left.fileinfo,type_e_got_expected_unpacked_array,'2',target.left.resultdef.typename)
  3083. else
  3084. unpackedarraydef := tarraydef(target.left.resultdef);
  3085. end
  3086. else
  3087. begin
  3088. index := tcallparanode(source.right);
  3089. target := tcallparanode(index.right);
  3090. { source can be any kind of array, as long as it's not packed }
  3091. if (source.left.resultdef.typ <> arraydef) or
  3092. is_packed_array(source.left.resultdef) then
  3093. CGMessagePos2(source.left.fileinfo,type_e_got_expected_unpacked_array,'1',source.left.resultdef.typename)
  3094. else
  3095. unpackedarraydef := tarraydef(source.left.resultdef);
  3096. { target must be a packed array }
  3097. if not is_packed_array(target.left.resultdef) then
  3098. CGMessagePos2(target.left.fileinfo,type_e_got_expected_packed_array,'3',target.left.resultdef.typename)
  3099. else
  3100. packedarraydef := tarraydef(target.left.resultdef);
  3101. end;
  3102. if assigned(unpackedarraydef) then
  3103. begin
  3104. { index must be compatible with the unpacked array's indextype }
  3105. inserttypeconv(index.left,unpackedarraydef.rangedef);
  3106. { range check at compile time if possible }
  3107. if assigned(packedarraydef) and
  3108. (index.left.nodetype = ordconstn) and
  3109. not is_special_array(unpackedarraydef) then
  3110. begin
  3111. adaptrange(unpackedarraydef,tordconstnode(index.left).value,false,false,cs_check_range in current_settings.localswitches);
  3112. tempindex := tordconstnode(index.left).value + packedarraydef.highrange-packedarraydef.lowrange;
  3113. adaptrange(unpackedarraydef,tempindex,false,false,cs_check_range in current_settings.localswitches);
  3114. end;
  3115. end;
  3116. { source array is read and must be valid }
  3117. set_varstate(source.left,vs_read,[vsf_must_be_valid]);
  3118. { target array is written }
  3119. valid_for_assignment(target.left,true);
  3120. set_varstate(target.left,vs_written,[]);
  3121. { index in the unpacked array is read and must be valid }
  3122. set_varstate(index.left,vs_read,[vsf_must_be_valid]);
  3123. { if the size of the arrays is 0 (array of empty records), }
  3124. { do nothing }
  3125. if (source.resultdef.size = 0) then
  3126. result:=cnothingnode.create;
  3127. end;
  3128. function handle_objc_encode: tnode;
  3129. var
  3130. encodedtype: ansistring;
  3131. errordef: tdef;
  3132. begin
  3133. encodedtype:='';
  3134. if not objctryencodetype(left.resultdef,encodedtype,errordef) then
  3135. Message1(type_e_objc_type_unsupported,errordef.typename);
  3136. result:=cstringconstnode.createpchar(pchar(encodedtype),length(encodedtype),nil);
  3137. end;
  3138. var
  3139. hightree,
  3140. hp : tnode;
  3141. temp_pnode: pnode;
  3142. convdef : tdef;
  3143. begin
  3144. result:=nil;
  3145. { when handling writeln "left" contains no valid address }
  3146. if assigned(left) then
  3147. begin
  3148. if left.nodetype=callparan then
  3149. tcallparanode(left).get_paratype
  3150. else
  3151. typecheckpass(left);
  3152. end;
  3153. if not(inf_inlineconst in inlinenodeflags) then
  3154. begin
  3155. case inlinenumber of
  3156. in_lo_long,
  3157. in_hi_long,
  3158. in_lo_qword,
  3159. in_hi_qword,
  3160. in_lo_word,
  3161. in_hi_word :
  3162. begin
  3163. { give warning for incompatibility with tp and delphi }
  3164. if (inlinenumber in [in_lo_long,in_hi_long,in_lo_qword,in_hi_qword]) and
  3165. ((m_tp7 in current_settings.modeswitches) or
  3166. (m_delphi in current_settings.modeswitches)) then
  3167. CGMessage(type_w_maybe_wrong_hi_lo);
  3168. set_varstate(left,vs_read,[vsf_must_be_valid]);
  3169. if not is_integer(left.resultdef) then
  3170. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename);
  3171. case inlinenumber of
  3172. in_lo_word,
  3173. in_hi_word :
  3174. resultdef:=u8inttype;
  3175. in_lo_long,
  3176. in_hi_long :
  3177. resultdef:=u16inttype;
  3178. in_lo_qword,
  3179. in_hi_qword :
  3180. resultdef:=u32inttype;
  3181. else
  3182. ;
  3183. end;
  3184. end;
  3185. in_sizeof_x:
  3186. begin
  3187. { the constant evaluation of in_sizeof_x happens in pexpr where possible,
  3188. though for generics it can reach here as well }
  3189. set_varstate(left,vs_read,[]);
  3190. if (left.resultdef.typ<>undefineddef) and
  3191. assigned(current_procinfo) and
  3192. paramanager.push_high_param(vs_value,left.resultdef,current_procinfo.procdef.proccalloption) then
  3193. begin
  3194. { this should be an open array or array of const, both of
  3195. which can only be simple load nodes of parameters }
  3196. if left.nodetype<>loadn then
  3197. internalerror(2014120701);
  3198. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  3199. if assigned(hightree) then
  3200. begin
  3201. hp:=caddnode.create(addn,hightree,
  3202. cordconstnode.create(1,sizesinttype,false));
  3203. if (left.resultdef.typ=arraydef) then
  3204. if not is_packed_array(tarraydef(left.resultdef)) then
  3205. begin
  3206. if (tarraydef(left.resultdef).elesize<>1) then
  3207. hp:=caddnode.create(muln,hp,cordconstnode.create(tarraydef(
  3208. left.resultdef).elesize,sizesinttype,true));
  3209. end
  3210. else if (tarraydef(left.resultdef).elepackedbitsize <> 8) then
  3211. begin
  3212. { no packed open array support yet }
  3213. if (hp.nodetype <> ordconstn) then
  3214. internalerror(2006081511);
  3215. hp.free;
  3216. hp := cordconstnode.create(left.resultdef.size,sizesinttype,true);
  3217. {
  3218. hp:=
  3219. ctypeconvnode.create_explicit(sizesinttype,
  3220. cmoddivnode.create(divn,
  3221. caddnode.create(addn,
  3222. caddnode.create(muln,hp,cordconstnode.create(tarraydef(
  3223. left.resultdef).elepackedbitsize,s64inttype,true)),
  3224. cordconstnode.create(a,s64inttype,true)),
  3225. cordconstnode.create(8,s64inttype,true)),
  3226. sizesinttype);
  3227. }
  3228. end;
  3229. result:=hp;
  3230. end;
  3231. end
  3232. else
  3233. resultdef:=sizesinttype;
  3234. end;
  3235. in_typeof_x:
  3236. begin
  3237. if target_info.system in systems_managed_vm then
  3238. message(parser_e_feature_unsupported_for_vm);
  3239. typecheckpass(left);
  3240. set_varstate(left,vs_read,[]);
  3241. if (left.resultdef.typ=objectdef) and
  3242. not(oo_has_vmt in tobjectdef(left.resultdef).objectoptions) then
  3243. message(type_e_typeof_requires_vmt);
  3244. resultdef:=voidpointertype;
  3245. end;
  3246. in_ord_x:
  3247. begin
  3248. set_varstate(left,vs_read,[vsf_must_be_valid]);
  3249. case left.resultdef.typ of
  3250. orddef,
  3251. enumdef,
  3252. undefineddef :
  3253. ;
  3254. pointerdef :
  3255. begin
  3256. if not(m_mac in current_settings.modeswitches) then
  3257. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  3258. end
  3259. else
  3260. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  3261. end;
  3262. end;
  3263. in_chr_byte:
  3264. begin
  3265. set_varstate(left,vs_read,[vsf_must_be_valid]);
  3266. end;
  3267. in_length_x:
  3268. begin
  3269. if ((left.resultdef.typ=arraydef) and
  3270. (not is_special_array(left.resultdef) or
  3271. is_open_array(left.resultdef))) or
  3272. (left.resultdef.typ=orddef) then
  3273. set_varstate(left,vs_read,[])
  3274. else
  3275. set_varstate(left,vs_read,[vsf_must_be_valid]);
  3276. case left.resultdef.typ of
  3277. variantdef:
  3278. begin
  3279. inserttypeconv(left,getansistringdef);
  3280. end;
  3281. stringdef :
  3282. begin
  3283. { we don't need string convertions here, }
  3284. { except if from widestring to ansistring }
  3285. { and vice versa (that can change the }
  3286. { length) }
  3287. if (left.nodetype=typeconvn) and
  3288. (ttypeconvnode(left).left.resultdef.typ=stringdef) and
  3289. not(is_wide_or_unicode_string(left.resultdef) xor
  3290. is_wide_or_unicode_string(ttypeconvnode(left).left.resultdef)) then
  3291. begin
  3292. hp:=ttypeconvnode(left).left;
  3293. ttypeconvnode(left).left:=nil;
  3294. left.free;
  3295. left:=hp;
  3296. end;
  3297. end;
  3298. orddef :
  3299. begin
  3300. { will be handled in simplify }
  3301. if not is_char(left.resultdef) and
  3302. not is_widechar(left.resultdef) then
  3303. CGMessage(type_e_mismatch);
  3304. end;
  3305. pointerdef :
  3306. begin
  3307. if is_pchar(left.resultdef) then
  3308. begin
  3309. hp := ccallparanode.create(left,nil);
  3310. result := ccallnode.createintern('fpc_pchar_length',hp);
  3311. { make sure the left node doesn't get disposed, since it's }
  3312. { reused in the new node (JM) }
  3313. left:=nil;
  3314. exit;
  3315. end
  3316. else if is_pwidechar(left.resultdef) then
  3317. begin
  3318. hp := ccallparanode.create(left,nil);
  3319. result := ccallnode.createintern('fpc_pwidechar_length',hp);
  3320. { make sure the left node doesn't get disposed, since it's }
  3321. { reused in the new node (JM) }
  3322. left:=nil;
  3323. exit;
  3324. end
  3325. else
  3326. CGMessage(type_e_mismatch);
  3327. end;
  3328. arraydef :
  3329. begin
  3330. if is_open_array(left.resultdef) or
  3331. is_array_of_const(left.resultdef) then
  3332. begin
  3333. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  3334. if assigned(hightree) then
  3335. result:=caddnode.create(addn,hightree,
  3336. cordconstnode.create(1,sinttype,false));
  3337. exit;
  3338. end
  3339. { Length() for dynamic arrays is inlined }
  3340. else
  3341. begin
  3342. { will be handled in simplify }
  3343. end;
  3344. end;
  3345. undefineddef :
  3346. begin
  3347. if not (df_generic in current_procinfo.procdef.defoptions) then
  3348. CGMessage(type_e_mismatch);
  3349. { otherwise nothing }
  3350. end;
  3351. else
  3352. CGMessage(type_e_mismatch);
  3353. end;
  3354. { shortstring return an 8 bit value as the length
  3355. is the first byte of the string }
  3356. if is_shortstring(left.resultdef) then
  3357. resultdef:=u8inttype
  3358. else
  3359. resultdef:=sizesinttype;
  3360. end;
  3361. in_typeinfo_x:
  3362. begin
  3363. if target_info.system in systems_managed_vm then
  3364. message(parser_e_feature_unsupported_for_vm);
  3365. if (left.resultdef.typ=enumdef) and
  3366. (tenumdef(left.resultdef).has_jumps) and
  3367. (
  3368. (left.nodetype<>typen) or
  3369. not (sp_generic_para in ttypenode(left).typesym.symoptions)
  3370. ) then
  3371. CGMessage(type_e_no_type_info);
  3372. set_varstate(left,vs_read,[vsf_must_be_valid]);
  3373. resultdef:=voidpointertype;
  3374. end;
  3375. in_gettypekind_x:
  3376. begin
  3377. if target_info.system in systems_managed_vm then
  3378. message(parser_e_feature_unsupported_for_vm);
  3379. set_varstate(left,vs_read,[vsf_must_be_valid]);
  3380. resultdef:=typekindtype;
  3381. end;
  3382. in_ismanagedtype_x:
  3383. begin
  3384. if target_info.system in systems_managed_vm then
  3385. message(parser_e_feature_unsupported_for_vm);
  3386. set_varstate(left,vs_read,[vsf_must_be_valid]);
  3387. resultdef:=pasbool1type;
  3388. end;
  3389. in_isconstvalue_x:
  3390. begin
  3391. set_varstate(left,vs_read,[vsf_must_be_valid]);
  3392. resultdef:=pasbool1type;
  3393. end;
  3394. in_assigned_x:
  3395. begin
  3396. { the parser has already made sure the expression is valid }
  3397. { in case of a complex procvar, only check the "code" pointer }
  3398. if (tcallparanode(left).left.resultdef.typ=procvardef) and
  3399. not tprocvardef(tcallparanode(left).left.resultdef).is_addressonly then
  3400. begin
  3401. inserttypeconv_explicit(tcallparanode(left).left,search_system_type('TMETHOD').typedef);
  3402. tcallparanode(left).left:=csubscriptnode.create(tsym(tabstractrecorddef(tcallparanode(left).left.resultdef).symtable.find('CODE')),tcallparanode(left).left);
  3403. tcallparanode(left).get_paratype;
  3404. end;
  3405. { Postpone conversion into addnode until firstpass, so targets
  3406. may override first_assigned and insert specific code. }
  3407. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  3408. resultdef:=pasbool1type;
  3409. end;
  3410. in_ofs_x :
  3411. internalerror(2000101001);
  3412. in_seg_x :
  3413. begin
  3414. result := typecheck_seg;
  3415. end;
  3416. in_pred_x,
  3417. in_succ_x:
  3418. begin
  3419. set_varstate(left,vs_read,[vsf_must_be_valid]);
  3420. resultdef:=left.resultdef;
  3421. if is_ordinal(resultdef) or is_typeparam(resultdef) then
  3422. begin
  3423. if (resultdef.typ=enumdef) and
  3424. (tenumdef(resultdef).has_jumps) and
  3425. not(m_delphi in current_settings.modeswitches) and
  3426. not(nf_internal in flags) then
  3427. CGMessage(type_e_succ_and_pred_enums_with_assign_not_possible);
  3428. end
  3429. else
  3430. CGMessage(type_e_ordinal_expr_expected)
  3431. end;
  3432. in_copy_x:
  3433. result:=handle_copy;
  3434. in_initialize_x,
  3435. in_finalize_x:
  3436. begin
  3437. { inlined from pinline }
  3438. internalerror(200204231);
  3439. end;
  3440. in_setlength_x:
  3441. begin
  3442. result:=handle_setlength;
  3443. end;
  3444. in_inc_x,
  3445. in_dec_x:
  3446. begin
  3447. resultdef:=voidtype;
  3448. if not(df_generic in current_procinfo.procdef.defoptions) then
  3449. begin
  3450. if assigned(left) then
  3451. begin
  3452. { first param must be var }
  3453. valid_for_var(tcallparanode(left).left,true);
  3454. set_varstate(tcallparanode(left).left,vs_readwritten,[vsf_must_be_valid]);
  3455. if (left.resultdef.typ in [enumdef,pointerdef]) or
  3456. is_ordinal(left.resultdef) or
  3457. is_currency(left.resultdef) then
  3458. begin
  3459. { value of left gets changed -> must be unique }
  3460. set_unique(tcallparanode(left).left);
  3461. { two paras ? }
  3462. if assigned(tcallparanode(left).right) then
  3463. begin
  3464. if is_integer(tcallparanode(left).right.resultdef) then
  3465. begin
  3466. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  3467. { when range/overflow checking is on, we
  3468. convert this to a regular add, and for proper
  3469. checking we need the original type }
  3470. if ([cs_check_range,cs_check_overflow]*current_settings.localswitches=[]) then
  3471. if (tcallparanode(left).left.resultdef.typ=pointerdef) then
  3472. begin
  3473. { don't convert values added to pointers into the pointer types themselves,
  3474. because that will turn signed values into unsigned ones, which then
  3475. goes wrong when they have to be multiplied with the size of the elements
  3476. to which the pointer points in ncginl (mantis #17342) }
  3477. if is_signed(tcallparanode(tcallparanode(left).right).left.resultdef) then
  3478. inserttypeconv(tcallparanode(tcallparanode(left).right).left,tpointerdef(tcallparanode(left).left.resultdef).pointer_arithmetic_int_type)
  3479. else
  3480. inserttypeconv(tcallparanode(tcallparanode(left).right).left,tpointerdef(tcallparanode(left).left.resultdef).pointer_arithmetic_uint_type)
  3481. end
  3482. else if is_integer(tcallparanode(left).left.resultdef) then
  3483. inserttypeconv(tcallparanode(tcallparanode(left).right).left,tcallparanode(left).left.resultdef)
  3484. else
  3485. inserttypeconv_internal(tcallparanode(tcallparanode(left).right).left,tcallparanode(left).left.resultdef);
  3486. if assigned(tcallparanode(tcallparanode(left).right).right) then
  3487. { should be handled in the parser (JM) }
  3488. internalerror(2006020901);
  3489. end
  3490. else
  3491. CGMessagePos(tcallparanode(left).right.fileinfo,type_e_ordinal_expr_expected);
  3492. end;
  3493. end
  3494. { generic type parameter? }
  3495. else if is_typeparam(left.resultdef) then
  3496. begin
  3497. result:=cnothingnode.create;
  3498. exit;
  3499. end
  3500. else
  3501. begin
  3502. hp:=self;
  3503. if isunaryoverloaded(hp,[]) then
  3504. begin
  3505. { inc(rec) and dec(rec) assigns result value to argument }
  3506. result:=cassignmentnode.create(tcallparanode(left).left.getcopy,hp);
  3507. exit;
  3508. end
  3509. else
  3510. CGMessagePos(left.fileinfo,type_e_ordinal_expr_expected);
  3511. end;
  3512. end
  3513. else
  3514. CGMessagePos(fileinfo,type_e_mismatch);
  3515. end;
  3516. end;
  3517. in_and_assign_x_y,
  3518. in_or_assign_x_y,
  3519. in_xor_assign_x_y,
  3520. in_sar_assign_x_y,
  3521. in_shl_assign_x_y,
  3522. in_shr_assign_x_y,
  3523. in_rol_assign_x_y,
  3524. in_ror_assign_x_y:
  3525. begin
  3526. resultdef:=voidtype;
  3527. if not(df_generic in current_procinfo.procdef.defoptions) then
  3528. begin
  3529. { first parameter must exist }
  3530. if not assigned(left) or (left.nodetype<>callparan) then
  3531. internalerror(2017032501);
  3532. { second parameter must exist }
  3533. if not assigned(tcallparanode(left).right) or (tcallparanode(left).right.nodetype<>callparan) then
  3534. internalerror(2017032502);
  3535. { third parameter must NOT exist }
  3536. if assigned(tcallparanode(tcallparanode(left).right).right) then
  3537. internalerror(2017032503);
  3538. valid_for_var(tcallparanode(tcallparanode(left).right).left,true);
  3539. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_readwritten,[vsf_must_be_valid]);
  3540. if is_integer(tcallparanode(left).right.resultdef) then
  3541. begin
  3542. { value of right gets changed -> must be unique }
  3543. set_unique(tcallparanode(tcallparanode(left).right).left);
  3544. if is_integer(left.resultdef) then
  3545. begin
  3546. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  3547. { these nodes shouldn't be created, when range checking is on }
  3548. if [cs_check_range,cs_check_overflow]*localswitches<>[] then
  3549. internalerror(2017032701);
  3550. if inlinenumber in [in_sar_assign_x_y,in_shl_assign_x_y,in_shr_assign_x_y,in_rol_assign_x_y,in_ror_assign_x_y] then
  3551. inserttypeconv(tcallparanode(left).left,sinttype)
  3552. else
  3553. inserttypeconv(tcallparanode(left).left,tcallparanode(tcallparanode(left).right).left.resultdef);
  3554. end
  3555. else
  3556. CGMessagePos(left.fileinfo,type_e_ordinal_expr_expected);
  3557. end
  3558. { generic type parameter? }
  3559. else if is_typeparam(tcallparanode(left).right.resultdef) then
  3560. begin
  3561. result:=cnothingnode.create;
  3562. exit;
  3563. end
  3564. else
  3565. CGMessagePos(tcallparanode(left).right.fileinfo,type_e_ordinal_expr_expected);
  3566. end;
  3567. end;
  3568. in_neg_assign_x,
  3569. in_not_assign_x:
  3570. begin
  3571. resultdef:=voidtype;
  3572. if not(df_generic in current_procinfo.procdef.defoptions) then
  3573. begin
  3574. valid_for_var(left,true);
  3575. set_varstate(left,vs_readwritten,[vsf_must_be_valid]);
  3576. if is_integer(left.resultdef) then
  3577. begin
  3578. { value of left gets changed -> must be unique }
  3579. set_unique(left);
  3580. { these nodes shouldn't be created, when range checking is on }
  3581. if [cs_check_range,cs_check_overflow]*current_settings.localswitches<>[] then
  3582. internalerror(2017040703);
  3583. end
  3584. { generic type parameter? }
  3585. else if is_typeparam(left.resultdef) then
  3586. begin
  3587. result:=cnothingnode.create;
  3588. exit;
  3589. end
  3590. else
  3591. CGMessagePos(left.fileinfo,type_e_ordinal_expr_expected);
  3592. end;
  3593. end;
  3594. in_read_x,
  3595. in_readln_x,
  3596. in_readstr_x,
  3597. in_write_x,
  3598. in_writeln_x,
  3599. in_writestr_x :
  3600. begin
  3601. result := handle_read_write;
  3602. end;
  3603. in_settextbuf_file_x :
  3604. begin
  3605. if target_info.system in systems_managed_vm then
  3606. message(parser_e_feature_unsupported_for_vm);
  3607. resultdef:=voidtype;
  3608. { now we know the type of buffer }
  3609. hp:=ccallparanode.create(cordconstnode.create(
  3610. tcallparanode(left).left.resultdef.size,s32inttype,true),left);
  3611. result:=ccallnode.createintern('SETTEXTBUF',hp);
  3612. left:=nil;
  3613. end;
  3614. { the firstpass of the arg has been done in firstcalln ? }
  3615. in_reset_typedfile,
  3616. in_rewrite_typedfile,
  3617. in_reset_typedfile_name,
  3618. in_rewrite_typedfile_name :
  3619. begin
  3620. result := handle_reset_rewrite_typed;
  3621. end;
  3622. in_str_x_string :
  3623. begin
  3624. result:=handle_str;
  3625. end;
  3626. in_val_x :
  3627. begin
  3628. result:=handle_val;
  3629. end;
  3630. in_include_x_y,
  3631. in_exclude_x_y:
  3632. begin
  3633. resultdef:=voidtype;
  3634. { the parser already checks whether we have two (and exactly two) }
  3635. { parameters (JM) }
  3636. { first param must be var }
  3637. valid_for_var(tcallparanode(left).left,true);
  3638. set_varstate(tcallparanode(left).left,vs_readwritten,[vsf_must_be_valid]);
  3639. { check type }
  3640. if (left.resultdef.typ=setdef) then
  3641. begin
  3642. { insert a type conversion }
  3643. { to the type of the set elements }
  3644. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  3645. inserttypeconv(tcallparanode(tcallparanode(left).right).left,
  3646. tsetdef(left.resultdef).elementdef);
  3647. end
  3648. else if left.resultdef.typ<>undefineddef then
  3649. CGMessage(type_e_mismatch);
  3650. end;
  3651. in_pack_x_y_z,
  3652. in_unpack_x_y_z :
  3653. begin
  3654. handle_pack_unpack;
  3655. end;
  3656. in_slice_x:
  3657. begin
  3658. if target_info.system in systems_managed_vm then
  3659. message(parser_e_feature_unsupported_for_vm);
  3660. result:=nil;
  3661. resultdef:=tcallparanode(left).left.resultdef;
  3662. if (resultdef.typ <> arraydef) then
  3663. CGMessagePos(left.fileinfo,type_e_mismatch)
  3664. else if is_packed_array(resultdef) then
  3665. CGMessagePos2(left.fileinfo,type_e_got_expected_unpacked_array,'1',resultdef.typename);
  3666. if not(is_integer(tcallparanode(tcallparanode(left).right).left.resultdef)) then
  3667. CGMessagePos1(tcallparanode(left).right.fileinfo,
  3668. type_e_integer_expr_expected,
  3669. tcallparanode(tcallparanode(left).right).left.resultdef.typename);
  3670. end;
  3671. in_new_x:
  3672. resultdef:=left.resultdef;
  3673. in_low_x,
  3674. in_high_x:
  3675. begin
  3676. case left.resultdef.typ of
  3677. undefineddef,
  3678. orddef,
  3679. enumdef,
  3680. setdef:
  3681. ;
  3682. arraydef:
  3683. begin
  3684. if (inlinenumber=in_low_x) then
  3685. set_varstate(left,vs_read,[])
  3686. else
  3687. begin
  3688. if is_open_array(left.resultdef) or
  3689. is_array_of_const(left.resultdef) then
  3690. begin
  3691. set_varstate(left,vs_read,[]);
  3692. result:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  3693. end
  3694. else
  3695. begin
  3696. set_varstate(left,vs_read,[]);
  3697. resultdef:=sizesinttype;
  3698. end;
  3699. end;
  3700. end;
  3701. stringdef:
  3702. begin
  3703. if inlinenumber=in_low_x then
  3704. begin
  3705. set_varstate(left,vs_read,[]);
  3706. end
  3707. else
  3708. begin
  3709. if is_open_string(left.resultdef) then
  3710. begin
  3711. set_varstate(left,vs_read,[]);
  3712. result:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry))
  3713. end
  3714. else if is_dynamicstring(left.resultdef) then
  3715. begin
  3716. result:=cinlinenode.create(in_length_x,false,left);
  3717. if cs_zerobasedstrings in current_settings.localswitches then
  3718. result:=caddnode.create(subn,result,cordconstnode.create(1,sinttype,false));
  3719. { make sure the left node doesn't get disposed, since it's }
  3720. { reused in the new node (JM) }
  3721. left:=nil;
  3722. end
  3723. end;
  3724. end;
  3725. else
  3726. CGMessage(type_e_mismatch);
  3727. end;
  3728. end;
  3729. in_exp_real,
  3730. in_frac_real,
  3731. in_int_real,
  3732. in_cos_real,
  3733. in_sin_real,
  3734. in_arctan_real,
  3735. in_ln_real :
  3736. begin
  3737. { on the Z80, the double result is returned in a var param, because
  3738. it's too big to fit in registers. In that case we have 2 parameters
  3739. and left.nodetype is a callparan. }
  3740. if left.nodetype = callparan then
  3741. temp_pnode := @tcallparanode(left).left
  3742. else
  3743. temp_pnode := @left;
  3744. set_varstate(temp_pnode^,vs_read,[vsf_must_be_valid]);
  3745. { converting an int64 to double on platforms without }
  3746. { extended can cause precision loss }
  3747. if not(temp_pnode^.nodetype in [ordconstn,realconstn]) then
  3748. inserttypeconv(temp_pnode^,pbestrealtype^);
  3749. resultdef:=pbestrealtype^;
  3750. end;
  3751. in_trunc_real,
  3752. in_round_real :
  3753. begin
  3754. { on i8086, the int64 result is returned in a var param, because
  3755. it's too big to fit in a register or a pair of registers. In
  3756. that case we have 2 parameters and left.nodetype is a callparan. }
  3757. if left.nodetype=callparan then
  3758. temp_pnode:=@tcallparanode(left).left
  3759. else
  3760. temp_pnode:=@left;
  3761. set_varstate(temp_pnode^,vs_read,[vsf_must_be_valid]);
  3762. { on platforms where comp and currency are "type int64", this is
  3763. handled via inlined system helpers (-> no need for special
  3764. handling of s64currency/s64comp for them) }
  3765. if inlinenumber=in_trunc_real then
  3766. removefloatupcasts(temp_pnode^,[s32real,s64real,s80real,sc80real,s128real,s64currency,s64comp])
  3767. else
  3768. removefloatupcasts(temp_pnode^,[s32real,s64real,s80real,sc80real,s128real,s64comp]);
  3769. if (inlinenumber=in_trunc_real) and
  3770. is_currency(temp_pnode^.resultdef) then
  3771. begin
  3772. result:=cmoddivnode.create(divn,ctypeconvnode.create_internal(temp_pnode^.getcopy,s64inttype),genintconstnode(10000));
  3773. exit;
  3774. end
  3775. else if is_fpucomp(temp_pnode^.resultdef) then
  3776. begin
  3777. result:=ctypeconvnode.create_internal(temp_pnode^.getcopy,s64inttype);
  3778. exit;
  3779. end;
  3780. resultdef:=s64inttype;
  3781. end;
  3782. in_pi_real :
  3783. begin
  3784. resultdef:=pbestrealtype^;
  3785. end;
  3786. in_abs_long:
  3787. begin
  3788. set_varstate(left,vs_read,[vsf_must_be_valid]);
  3789. resultdef:=left.resultdef;
  3790. end;
  3791. in_abs_real,
  3792. in_sqr_real,
  3793. in_sqrt_real :
  3794. begin
  3795. { on the Z80, the double result is returned in a var param, because
  3796. it's too big to fit in registers. In that case we have 2 parameters
  3797. and left.nodetype is a callparan. }
  3798. if left.nodetype = callparan then
  3799. temp_pnode := @tcallparanode(left).left
  3800. else
  3801. temp_pnode := @left;
  3802. set_varstate(temp_pnode^,vs_read,[vsf_must_be_valid]);
  3803. resultdef:=removefloatupcasts(temp_pnode^,[s32real,s64real,s80real,sc80real,s128real]);
  3804. end;
  3805. {$ifdef SUPPORT_MMX}
  3806. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  3807. begin
  3808. end;
  3809. {$endif SUPPORT_MMX}
  3810. in_aligned_x,
  3811. in_unaligned_x:
  3812. begin
  3813. resultdef:=left.resultdef;
  3814. end;
  3815. in_volatile_x:
  3816. begin
  3817. resultdef:=left.resultdef;
  3818. { volatile only makes sense if the value is in memory }
  3819. make_not_regable(left,[ra_addr_regable]);
  3820. end;
  3821. in_assert_x_y :
  3822. begin
  3823. resultdef:=voidtype;
  3824. if assigned(left) then
  3825. begin
  3826. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  3827. { check type }
  3828. if is_boolean(left.resultdef) or
  3829. (
  3830. (left.resultdef.typ=undefineddef) and
  3831. (df_generic in current_procinfo.procdef.defoptions)
  3832. ) then
  3833. begin
  3834. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  3835. { must always be a string }
  3836. inserttypeconv(tcallparanode(tcallparanode(left).right).left,cshortstringtype);
  3837. end
  3838. else
  3839. CGMessage1(type_e_boolean_expr_expected,left.resultdef.typename);
  3840. end
  3841. else
  3842. CGMessage(type_e_mismatch);
  3843. if (cs_do_assertion in current_settings.localswitches) then
  3844. include(current_procinfo.flags,pi_do_call);
  3845. end;
  3846. in_prefetch_var:
  3847. resultdef:=voidtype;
  3848. in_get_frame,
  3849. in_get_caller_frame,
  3850. in_get_caller_addr:
  3851. begin
  3852. resultdef:=voidpointertype;
  3853. end;
  3854. in_rol_x,
  3855. in_ror_x,
  3856. in_sar_x:
  3857. begin
  3858. set_varstate(left,vs_read,[vsf_must_be_valid]);
  3859. resultdef:=left.resultdef;
  3860. end;
  3861. in_rol_x_y,
  3862. in_ror_x_y,
  3863. in_sar_x_y:
  3864. begin
  3865. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  3866. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  3867. resultdef:=tcallparanode(tcallparanode(left).right).left.resultdef;
  3868. end;
  3869. in_bsf_x,
  3870. in_bsr_x:
  3871. begin
  3872. set_varstate(left,vs_read,[vsf_must_be_valid]);
  3873. if not is_integer(left.resultdef) then
  3874. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename);
  3875. if torddef(left.resultdef).ordtype in [u64bit, s64bit] then
  3876. resultdef:=u64inttype
  3877. else
  3878. resultdef:=u32inttype
  3879. end;
  3880. in_popcnt_x:
  3881. begin
  3882. set_varstate(left,vs_read,[vsf_must_be_valid]);
  3883. if not is_integer(left.resultdef) then
  3884. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename);
  3885. resultdef:=left.resultdef;
  3886. end;
  3887. in_objc_selector_x:
  3888. begin
  3889. result:=cobjcselectornode.create(left);
  3890. { reused }
  3891. left:=nil;
  3892. end;
  3893. in_objc_protocol_x:
  3894. begin
  3895. result:=cobjcprotocolnode.create(left);
  3896. { reused }
  3897. left:=nil;
  3898. end;
  3899. in_objc_encode_x:
  3900. begin
  3901. result:=handle_objc_encode;
  3902. end;
  3903. in_default_x:
  3904. begin
  3905. result:=handle_default;
  3906. end;
  3907. in_box_x:
  3908. begin
  3909. result:=handle_box;
  3910. end;
  3911. in_unbox_x_y:
  3912. begin
  3913. result:=handle_unbox;
  3914. end;
  3915. in_fma_single,
  3916. in_fma_double,
  3917. in_fma_extended,
  3918. in_fma_float128:
  3919. begin
  3920. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  3921. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  3922. set_varstate(tcallparanode(tcallparanode(tcallparanode(left).right).right).left,vs_read,[vsf_must_be_valid]);
  3923. resultdef:=tcallparanode(left).left.resultdef;
  3924. end;
  3925. in_max_longint,
  3926. in_max_dword,
  3927. in_min_longint,
  3928. in_min_dword,
  3929. in_max_int64,
  3930. in_max_qword,
  3931. in_min_int64,
  3932. in_min_qword,
  3933. in_max_single,
  3934. in_max_double,
  3935. in_min_single,
  3936. in_min_double:
  3937. begin
  3938. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  3939. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  3940. resultdef:=tcallparanode(left).left.resultdef;
  3941. end;
  3942. in_delete_x_y_z:
  3943. begin
  3944. result:=handle_delete;
  3945. end;
  3946. in_insert_x_y_z:
  3947. begin
  3948. result:=handle_insert;
  3949. end;
  3950. in_concat_x:
  3951. begin
  3952. result:=handle_concat;
  3953. end;
  3954. in_atomic_dec,
  3955. in_atomic_inc,
  3956. in_atomic_xchg,
  3957. in_atomic_cmp_xchg:
  3958. begin
  3959. { first parameter must exist for all }
  3960. if not assigned(left) or (left.nodetype<>callparan) then
  3961. internalerror(2022093001);
  3962. { second parameter must exist for xchg and cmp_xchg }
  3963. if (inlinenumber=in_atomic_xchg) or (inlinenumber=in_atomic_cmp_xchg) then
  3964. begin
  3965. if not assigned(tcallparanode(left).right) or (tcallparanode(left).right.nodetype<>callparan) then
  3966. internalerror(2022093002);
  3967. if inlinenumber=in_atomic_cmp_xchg then
  3968. begin
  3969. { third parameter must exist }
  3970. if not assigned(tcallparanode(tcallparanode(left).right).right) or (tcallparanode(tcallparanode(left).right).right.nodetype<>callparan) then
  3971. internalerror(2022093004);
  3972. { fourth parameter may exist }
  3973. if assigned(tcallparanode(tcallparanode(tcallparanode(left).right).right).right) then
  3974. begin
  3975. if tcallparanode(tcallparanode(tcallparanode(left).right).right).right.nodetype<>callparan then
  3976. internalerror(2022093005);
  3977. { fifth parameter must NOT exist }
  3978. if assigned(tcallparanode(tcallparanode(tcallparanode(tcallparanode(left).right).right).right).right) then
  3979. internalerror(2022093006);
  3980. end;
  3981. end
  3982. { third parameter must NOT exist }
  3983. else if assigned(tcallparanode(tcallparanode(left).right).right) then
  3984. internalerror(2022093003);
  3985. end
  3986. else if assigned(tcallparanode(left).right) then
  3987. begin
  3988. { if the second parameter exists, it must be a callparan }
  3989. if tcallparanode(left).right.nodetype<>callparan then
  3990. internalerror(2022093004);
  3991. { a third parameter must not exist }
  3992. if assigned(tcallparanode(tcallparanode(left).right).right) then
  3993. internalerror(2022093005);
  3994. end;
  3995. valid_for_var(tcallparanode(left).left,true);
  3996. set_varstate(tcallparanode(left).left,vs_readwritten,[vsf_must_be_valid]);
  3997. if is_integer(tcallparanode(left).resultdef) or is_pointer(tcallparanode(left).resultdef) then
  3998. begin
  3999. if not is_pointer(tcallparanode(left).resultdef) then
  4000. begin
  4001. resultdef:=get_signed_inttype(tcallparanode(left).left.resultdef);
  4002. convdef:=resultdef;
  4003. end
  4004. else
  4005. begin
  4006. { pointer is only allowed for Exchange and CmpExchange }
  4007. if (inlinenumber<>in_atomic_xchg) and (inlinenumber<>in_atomic_cmp_xchg) then
  4008. cgmessagepos(fileinfo,type_e_ordinal_expr_expected);
  4009. resultdef:=voidpointertype;
  4010. convdef:=ptrsinttype;
  4011. end;
  4012. { left gets changed -> must be unique }
  4013. set_unique(tcallparanode(left).left);
  4014. inserttypeconv_internal(tcallparanode(left).left,convdef);
  4015. if assigned(tcallparanode(left).right) then
  4016. begin
  4017. inserttypeconv(tcallparanode(tcallparanode(left).right).left,resultdef);
  4018. if resultdef<>convdef then
  4019. inserttypeconv_internal(tcallparanode(tcallparanode(left).right).left,convdef);
  4020. if assigned(tcallparanode(tcallparanode(left).right).right) then
  4021. begin
  4022. inserttypeconv(tcallparanode(tcallparanode(tcallparanode(left).right).right).left,resultdef);
  4023. if resultdef<>convdef then
  4024. inserttypeconv_internal(tcallparanode(tcallparanode(tcallparanode(left).right).right).left,convdef);
  4025. if assigned(tcallparanode(tcallparanode(tcallparanode(left).right).right).right) then
  4026. begin
  4027. { the boolean parameter must be assignable }
  4028. valid_for_var(tcallparanode(tcallparanode(tcallparanode(tcallparanode(left).right).right).right).left,true);
  4029. set_varstate(tcallparanode(tcallparanode(tcallparanode(tcallparanode(left).right).right).right).left,vs_written,[]);
  4030. if not is_boolean(tcallparanode(tcallparanode(tcallparanode(tcallparanode(left).right).right).right).left.resultdef) then
  4031. inserttypeconv(tcallparanode(tcallparanode(tcallparanode(tcallparanode(left).right).right).right).left,pasbool1type);
  4032. end;
  4033. end;
  4034. end;
  4035. end
  4036. else if is_typeparam(tcallparanode(left).left.resultdef) then
  4037. begin
  4038. resultdef:=tcallparanode(left).left.resultdef;
  4039. end
  4040. else if (inlinenumber=in_atomic_xchg) or (inlinenumber=in_atomic_cmp_xchg) then
  4041. CGMessagePos(tcallparanode(left).left.fileinfo,type_e_ordinal_or_pointer_expr_expected)
  4042. else
  4043. CGMessagePos(tcallparanode(left).left.fileinfo,type_e_ordinal_expr_expected);
  4044. end;
  4045. else
  4046. result:=pass_typecheck_cpu;
  4047. end;
  4048. end;
  4049. if not assigned(result) and not
  4050. codegenerror then
  4051. result:=simplify(false);
  4052. end;
  4053. function tinlinenode.pass_typecheck_cpu : tnode;
  4054. begin
  4055. Result:=nil;
  4056. Message1(cg_f_unknown_internal_procedure_number,tostr(ord(inlinenumber)));
  4057. end;
  4058. function tinlinenode.pass_1 : tnode;
  4059. var
  4060. hp: tnode;
  4061. shiftconst: longint;
  4062. objdef: tobjectdef;
  4063. sym : tsym;
  4064. begin
  4065. result:=nil;
  4066. { if we handle writeln; left contains no valid address }
  4067. if assigned(left) then
  4068. begin
  4069. if left.nodetype=callparan then
  4070. tcallparanode(left).firstcallparan
  4071. else
  4072. firstpass(left);
  4073. end;
  4074. { intern const should already be handled }
  4075. if inf_inlineconst in inlinenodeflags then
  4076. internalerror(200104044);
  4077. case inlinenumber of
  4078. in_lo_qword,
  4079. in_hi_qword,
  4080. in_lo_long,
  4081. in_hi_long,
  4082. in_lo_word,
  4083. in_hi_word:
  4084. begin
  4085. shiftconst := 0;
  4086. case inlinenumber of
  4087. in_hi_qword:
  4088. shiftconst := 32;
  4089. in_hi_long:
  4090. shiftconst := 16;
  4091. in_hi_word:
  4092. shiftconst := 8;
  4093. else
  4094. ;
  4095. end;
  4096. if shiftconst <> 0 then
  4097. result := ctypeconvnode.create_internal(cshlshrnode.create(shrn,left,
  4098. cordconstnode.create(shiftconst,sinttype,false)),resultdef)
  4099. else
  4100. result := ctypeconvnode.create_internal(left,resultdef);
  4101. left := nil;
  4102. firstpass(result);
  4103. end;
  4104. in_sizeof_x,
  4105. in_typeof_x:
  4106. begin
  4107. expectloc:=LOC_REGISTER;
  4108. case left.resultdef.typ of
  4109. objectdef,classrefdef:
  4110. begin
  4111. if left.resultdef.typ=objectdef then
  4112. begin
  4113. result:=cloadvmtaddrnode.create(left);
  4114. objdef:=tobjectdef(left.resultdef);
  4115. end
  4116. else
  4117. begin
  4118. result:=left;
  4119. objdef:=tobjectdef(tclassrefdef(left.resultdef).pointeddef);
  4120. end;
  4121. left:=nil;
  4122. if inlinenumber=in_sizeof_x then
  4123. begin
  4124. inserttypeconv_explicit(result,cpointerdef.getreusable(objdef.vmt_def));
  4125. result:=cderefnode.create(result);
  4126. result:=genloadfield(result,'VINSTANCESIZE');
  4127. end
  4128. else
  4129. inserttypeconv_explicit(result,voidpointertype);
  4130. end;
  4131. undefineddef:
  4132. ;
  4133. else
  4134. internalerror(2015122702);
  4135. end;
  4136. end;
  4137. in_length_x:
  4138. begin
  4139. result:=first_length;
  4140. end;
  4141. in_typeinfo_x:
  4142. begin
  4143. if (left.resultdef.typ=enumdef) and
  4144. (tenumdef(left.resultdef).has_jumps) then
  4145. begin
  4146. if (left.nodetype=typen) and (sp_generic_para in ttypenode(left).typesym.symoptions) then
  4147. result:=cnilnode.create
  4148. else
  4149. internalerror(2021032601);
  4150. end
  4151. else
  4152. result:=caddrnode.create_internal(
  4153. crttinode.create(tstoreddef(left.resultdef),fullrtti,rdt_normal)
  4154. );
  4155. end;
  4156. in_gettypekind_x:
  4157. begin
  4158. sym:=tenumdef(typekindtype).int2enumsym(get_typekind(left.resultdef));
  4159. if not assigned(sym) then
  4160. internalerror(2017081101);
  4161. if sym.typ<>enumsym then
  4162. internalerror(2017081102);
  4163. result:=genenumnode(tenumsym(sym));
  4164. end;
  4165. in_ismanagedtype_x:
  4166. begin
  4167. if left.resultdef.needs_inittable then
  4168. result:=cordconstnode.create(1,resultdef,false)
  4169. else
  4170. result:=cordconstnode.create(0,resultdef,false);
  4171. end;
  4172. in_isconstvalue_x:
  4173. begin
  4174. if is_constnode(left) then
  4175. result:=cordconstnode.create(1,resultdef,false)
  4176. else
  4177. result:=cordconstnode.create(0,resultdef,false);
  4178. end;
  4179. in_assigned_x:
  4180. begin
  4181. result:=first_assigned;
  4182. end;
  4183. in_pred_x,
  4184. in_succ_x:
  4185. begin
  4186. expectloc:=LOC_REGISTER;
  4187. { in case of range/overflow checking, use a regular addnode
  4188. because it's too complex to handle correctly otherwise }
  4189. {$ifndef jvm}
  4190. { enums are class instances in the JVM -> always need conversion }
  4191. if (([cs_check_overflow,cs_check_range]*current_settings.localswitches)<>[]) and not(nf_internal in flags) then
  4192. {$endif}
  4193. begin
  4194. { create constant 1 }
  4195. hp:=cordconstnode.create(1,left.resultdef,false);
  4196. typecheckpass(hp);
  4197. if not is_integer(hp.resultdef) then
  4198. inserttypeconv_internal(hp,sinttype);
  4199. { avoid type errors from the addn/subn }
  4200. if not is_integer(left.resultdef) then
  4201. inserttypeconv_internal(left,sinttype);
  4202. { addition/substraction depending on succ/pred }
  4203. if inlinenumber=in_succ_x then
  4204. hp:=caddnode.create(addn,left,hp)
  4205. else
  4206. hp:=caddnode.create(subn,left,hp);
  4207. { the condition above is not tested for jvm, so we need to avoid overflow checks here
  4208. by setting nf_internal for the add/sub node as well }
  4209. if nf_internal in flags then
  4210. include(hp.flags,nf_internal);
  4211. { assign result of addition }
  4212. if not(is_integer(resultdef)) then
  4213. inserttypeconv(hp,corddef.create(
  4214. {$ifdef cpu64bitaddr}
  4215. s64bit,
  4216. {$else cpu64bitaddr}
  4217. s32bit,
  4218. {$endif cpu64bitaddr}
  4219. get_min_value(resultdef),
  4220. get_max_value(resultdef),
  4221. true))
  4222. else
  4223. inserttypeconv(hp,resultdef);
  4224. if nf_internal in flags then
  4225. include(hp.flags,nf_internal);
  4226. { avoid any possible errors/warnings }
  4227. inserttypeconv_internal(hp,resultdef);
  4228. { firstpass it }
  4229. firstpass(hp);
  4230. { left is reused }
  4231. left:=nil;
  4232. { return new node }
  4233. result:=hp;
  4234. end;
  4235. end;
  4236. in_setlength_x:
  4237. result:=first_setlength;
  4238. in_copy_x:
  4239. result:=first_copy;
  4240. in_initialize_x,
  4241. in_finalize_x:
  4242. begin
  4243. expectloc:=LOC_VOID;
  4244. end;
  4245. in_inc_x,
  4246. in_dec_x:
  4247. begin
  4248. result:=first_IncDec;
  4249. end;
  4250. in_and_assign_x_y,
  4251. in_or_assign_x_y,
  4252. in_xor_assign_x_y,
  4253. in_sar_assign_x_y,
  4254. in_shl_assign_x_y,
  4255. in_shr_assign_x_y,
  4256. in_rol_assign_x_y,
  4257. in_ror_assign_x_y:
  4258. begin
  4259. result:=first_AndOrXorShiftRot_assign;
  4260. end;
  4261. in_neg_assign_x,
  4262. in_not_assign_x:
  4263. begin
  4264. result:=first_NegNot_assign;
  4265. end;
  4266. in_include_x_y,
  4267. in_exclude_x_y:
  4268. begin
  4269. result:=first_IncludeExclude;
  4270. end;
  4271. in_pack_x_y_z,
  4272. in_unpack_x_y_z:
  4273. begin
  4274. result:=first_pack_unpack;
  4275. end;
  4276. in_exp_real:
  4277. begin
  4278. result:= first_exp_real;
  4279. end;
  4280. in_round_real:
  4281. begin
  4282. result:= first_round_real;
  4283. end;
  4284. in_trunc_real:
  4285. begin
  4286. result:= first_trunc_real;
  4287. end;
  4288. in_int_real:
  4289. begin
  4290. result:= first_int_real;
  4291. end;
  4292. in_frac_real:
  4293. begin
  4294. result:= first_frac_real;
  4295. end;
  4296. in_cos_real:
  4297. begin
  4298. result:= first_cos_real;
  4299. end;
  4300. in_sin_real:
  4301. begin
  4302. result := first_sin_real;
  4303. end;
  4304. in_arctan_real:
  4305. begin
  4306. result := first_arctan_real;
  4307. end;
  4308. in_pi_real :
  4309. begin
  4310. result := first_pi;
  4311. end;
  4312. in_abs_real:
  4313. begin
  4314. result := first_abs_real;
  4315. end;
  4316. in_abs_long:
  4317. begin
  4318. result := first_abs_long;
  4319. end;
  4320. in_sqr_real:
  4321. begin
  4322. result := first_sqr_real;
  4323. end;
  4324. in_sqrt_real:
  4325. begin
  4326. result := first_sqrt_real;
  4327. end;
  4328. in_ln_real:
  4329. begin
  4330. result := first_ln_real;
  4331. end;
  4332. {$ifdef SUPPORT_MMX}
  4333. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  4334. begin
  4335. end;
  4336. {$endif SUPPORT_MMX}
  4337. in_assert_x_y :
  4338. begin
  4339. result:=first_assert;
  4340. end;
  4341. in_low_x:
  4342. internalerror(200104047);
  4343. in_high_x:
  4344. begin
  4345. result:=first_high;
  4346. end;
  4347. in_slice_x:
  4348. { slice can be used only in calls for open array parameters, so it has to be converted appropriatly before
  4349. if we get here, the array could not be passed to an open array parameter so it is an error }
  4350. CGMessagePos(left.fileinfo,type_e_mismatch);
  4351. in_ord_x,
  4352. in_chr_byte:
  4353. begin
  4354. { should not happend as it's converted to typeconv }
  4355. internalerror(200104045);
  4356. end;
  4357. in_ofs_x :
  4358. internalerror(2000101002);
  4359. in_seg_x :
  4360. begin
  4361. result:=first_seg;
  4362. end;
  4363. in_settextbuf_file_x,
  4364. in_reset_typedfile,
  4365. in_rewrite_typedfile,
  4366. in_reset_typedfile_name,
  4367. in_rewrite_typedfile_name,
  4368. in_str_x_string,
  4369. in_val_x,
  4370. in_read_x,
  4371. in_readln_x,
  4372. in_write_x,
  4373. in_writeln_x :
  4374. begin
  4375. { should be handled by pass_typecheck }
  4376. internalerror(2001082302);
  4377. end;
  4378. in_get_frame:
  4379. begin
  4380. result:=first_get_frame;
  4381. end;
  4382. in_get_caller_frame:
  4383. begin
  4384. expectloc:=LOC_REGISTER;
  4385. end;
  4386. in_get_caller_addr:
  4387. begin
  4388. expectloc:=LOC_REGISTER;
  4389. end;
  4390. in_prefetch_var:
  4391. begin
  4392. expectloc:=LOC_VOID;
  4393. end;
  4394. in_aligned_x,
  4395. in_unaligned_x,
  4396. in_volatile_x:
  4397. begin
  4398. expectloc:=tcallparanode(left).left.expectloc;
  4399. end;
  4400. in_rol_x,
  4401. in_rol_x_y,
  4402. in_ror_x,
  4403. in_ror_x_y:
  4404. expectloc:=LOC_REGISTER;
  4405. in_bsf_x,
  4406. in_bsr_x:
  4407. result:=first_bitscan;
  4408. in_sar_x,
  4409. in_sar_x_y:
  4410. result:=first_sar;
  4411. in_popcnt_x:
  4412. result:=first_popcnt;
  4413. in_new_x:
  4414. result:=first_new;
  4415. in_box_x:
  4416. result:=first_box;
  4417. in_unbox_x_y:
  4418. result:=first_unbox;
  4419. in_fma_single,
  4420. in_fma_double,
  4421. in_fma_extended,
  4422. in_fma_float128:
  4423. result:=first_fma;
  4424. in_max_longint,
  4425. in_max_dword,
  4426. in_min_longint,
  4427. in_min_dword,
  4428. in_max_int64,
  4429. in_max_qword,
  4430. in_min_int64,
  4431. in_min_qword,
  4432. in_min_single,
  4433. in_min_double,
  4434. in_max_single,
  4435. in_max_double:
  4436. result:=first_minmax;
  4437. in_atomic_inc,
  4438. in_atomic_dec,
  4439. in_atomic_xchg,
  4440. in_atomic_cmp_xchg:
  4441. result:=first_atomic;
  4442. else
  4443. result:=first_cpu;
  4444. end;
  4445. end;
  4446. {$maxfpuregisters default}
  4447. function tinlinenode.docompare(p: tnode): boolean;
  4448. begin
  4449. docompare :=
  4450. inherited docompare(p) and
  4451. (inlinenumber = tinlinenode(p).inlinenumber);
  4452. end;
  4453. procedure tinlinenode.mark_write;
  4454. begin
  4455. case inlinenumber of
  4456. in_aligned_x, in_unaligned_x:
  4457. tcallparanode(left).left.mark_write;
  4458. else
  4459. inherited mark_write;
  4460. end;
  4461. end;
  4462. function tinlinenode.first_pi : tnode;
  4463. begin
  4464. result:=crealconstnode.create(getpi,pbestrealtype^);
  4465. end;
  4466. function tinlinenode.first_arctan_real : tnode;
  4467. var
  4468. temp_pnode: pnode;
  4469. begin
  4470. { create the call to the helper }
  4471. { on entry left node contains the parameter }
  4472. if left.nodetype = callparan then
  4473. temp_pnode := @tcallparanode(left).left
  4474. else
  4475. temp_pnode := @left;
  4476. result := ccallnode.createintern('fpc_arctan_real',
  4477. ccallparanode.create(temp_pnode^,nil));
  4478. temp_pnode^ := nil;
  4479. end;
  4480. function tinlinenode.first_abs_real : tnode;
  4481. var
  4482. callnode : tcallnode;
  4483. temp_pnode: pnode;
  4484. begin
  4485. { create the call to the helper }
  4486. { on entry left node contains the parameter }
  4487. if left.nodetype = callparan then
  4488. temp_pnode := @tcallparanode(left).left
  4489. else
  4490. temp_pnode := @left;
  4491. callnode:=ccallnode.createintern('fpc_abs_real',
  4492. ccallparanode.create(temp_pnode^,nil));
  4493. result := ctypeconvnode.create(callnode,resultdef);
  4494. include(callnode.callnodeflags,cnf_check_fpu_exceptions);
  4495. temp_pnode^ := nil;
  4496. end;
  4497. function tinlinenode.first_sqr_real : tnode;
  4498. var
  4499. callnode : tcallnode;
  4500. temp_pnode: pnode;
  4501. begin
  4502. {$ifndef cpufpemu}
  4503. { this procedure might be only used for cpus definining cpufpemu else
  4504. the optimizer might go into an endless loop when doing x*x -> changes }
  4505. internalerror(2011092401);
  4506. {$endif cpufpemu}
  4507. { create the call to the helper }
  4508. { on entry left node contains the parameter }
  4509. if left.nodetype = callparan then
  4510. temp_pnode := @tcallparanode(left).left
  4511. else
  4512. temp_pnode := @left;
  4513. callnode:=ccallnode.createintern('fpc_sqr_real',
  4514. ccallparanode.create(temp_pnode^,nil));
  4515. result := ctypeconvnode.create(callnode,resultdef);
  4516. include(callnode.callnodeflags,cnf_check_fpu_exceptions);
  4517. temp_pnode^ := nil;
  4518. end;
  4519. function tinlinenode.first_sqrt_real : tnode;
  4520. var
  4521. fdef: tdef;
  4522. procname: string[31];
  4523. callnode: tcallnode;
  4524. temp_pnode: pnode;
  4525. begin
  4526. if left.nodetype = callparan then
  4527. temp_pnode := @tcallparanode(left).left
  4528. else
  4529. temp_pnode := @left;
  4530. if ((cs_fp_emulation in current_settings.moduleswitches)
  4531. {$ifdef cpufpemu}
  4532. or (current_settings.fputype=fpu_soft)
  4533. {$endif cpufpemu}
  4534. ) and not (target_info.system in systems_wince) then
  4535. begin
  4536. case tfloatdef(temp_pnode^.resultdef).floattype of
  4537. s32real:
  4538. begin
  4539. fdef:=search_system_type('FLOAT32REC').typedef;
  4540. procname:='float32_sqrt';
  4541. end;
  4542. s64real:
  4543. begin
  4544. fdef:=search_system_type('FLOAT64').typedef;
  4545. procname:='float64_sqrt';
  4546. end;
  4547. {!!! not yet implemented
  4548. s128real:
  4549. }
  4550. else
  4551. internalerror(2014052101);
  4552. end;
  4553. result:=ctypeconvnode.create_internal(ccallnode.createintern(procname,ccallparanode.create(
  4554. ctypeconvnode.create_internal(temp_pnode^,fdef),nil)),resultdef);
  4555. end
  4556. else
  4557. begin
  4558. { create the call to the helper }
  4559. { on entry left node contains the parameter }
  4560. callnode := ccallnode.createintern('fpc_sqrt_real',
  4561. ccallparanode.create(temp_pnode^,nil));
  4562. result := ctypeconvnode.create(callnode,resultdef);
  4563. include(callnode.callnodeflags,cnf_check_fpu_exceptions);
  4564. end;
  4565. temp_pnode^ := nil;
  4566. end;
  4567. function tinlinenode.first_ln_real : tnode;
  4568. var
  4569. temp_pnode: pnode;
  4570. begin
  4571. { create the call to the helper }
  4572. { on entry left node contains the parameter }
  4573. if left.nodetype = callparan then
  4574. temp_pnode := @tcallparanode(left).left
  4575. else
  4576. temp_pnode := @left;
  4577. result := ccallnode.createintern('fpc_ln_real',
  4578. ccallparanode.create(temp_pnode^,nil));
  4579. include(tcallnode(result).callnodeflags,cnf_check_fpu_exceptions);
  4580. temp_pnode^ := nil;
  4581. end;
  4582. function tinlinenode.first_cos_real : tnode;
  4583. var
  4584. temp_pnode: pnode;
  4585. begin
  4586. { create the call to the helper }
  4587. { on entry left node contains the parameter }
  4588. if left.nodetype = callparan then
  4589. temp_pnode := @tcallparanode(left).left
  4590. else
  4591. temp_pnode := @left;
  4592. result := ccallnode.createintern('fpc_cos_real',
  4593. ccallparanode.create(temp_pnode^,nil));
  4594. include(tcallnode(result).callnodeflags,cnf_check_fpu_exceptions);
  4595. temp_pnode^ := nil;
  4596. end;
  4597. function tinlinenode.first_sin_real : tnode;
  4598. var
  4599. temp_pnode: pnode;
  4600. begin
  4601. { create the call to the helper }
  4602. { on entry left node contains the parameter }
  4603. if left.nodetype = callparan then
  4604. temp_pnode := @tcallparanode(left).left
  4605. else
  4606. temp_pnode := @left;
  4607. result := ccallnode.createintern('fpc_sin_real',
  4608. ccallparanode.create(temp_pnode^,nil));
  4609. include(tcallnode(result).callnodeflags,cnf_check_fpu_exceptions);
  4610. temp_pnode^ := nil;
  4611. end;
  4612. function tinlinenode.first_exp_real : tnode;
  4613. var
  4614. temp_pnode: pnode;
  4615. begin
  4616. { create the call to the helper }
  4617. { on entry left node contains the parameter }
  4618. if left.nodetype = callparan then
  4619. temp_pnode := @tcallparanode(left).left
  4620. else
  4621. temp_pnode := @left;
  4622. result := ccallnode.createintern('fpc_exp_real',ccallparanode.create(temp_pnode^,nil));
  4623. include(tcallnode(result).callnodeflags,cnf_check_fpu_exceptions);
  4624. temp_pnode^ := nil;
  4625. end;
  4626. function tinlinenode.first_int_real : tnode;
  4627. var
  4628. temp_pnode: pnode;
  4629. begin
  4630. { create the call to the helper }
  4631. { on entry left node contains the parameter }
  4632. if left.nodetype = callparan then
  4633. temp_pnode := @tcallparanode(left).left
  4634. else
  4635. temp_pnode := @left;
  4636. result := ccallnode.createintern('fpc_int_real',ccallparanode.create(temp_pnode^,nil));
  4637. include(tcallnode(result).callnodeflags,cnf_check_fpu_exceptions);
  4638. temp_pnode^ := nil;
  4639. end;
  4640. function tinlinenode.first_frac_real : tnode;
  4641. var
  4642. temp_pnode: pnode;
  4643. begin
  4644. { create the call to the helper }
  4645. { on entry left node contains the parameter }
  4646. if left.nodetype = callparan then
  4647. temp_pnode := @tcallparanode(left).left
  4648. else
  4649. temp_pnode := @left;
  4650. result := ccallnode.createintern('fpc_frac_real',ccallparanode.create(temp_pnode^,nil));
  4651. include(tcallnode(result).callnodeflags,cnf_check_fpu_exceptions);
  4652. temp_pnode^ := nil;
  4653. end;
  4654. function tinlinenode.first_round_real : tnode;
  4655. var
  4656. temp_pnode: pnode;
  4657. begin
  4658. { create the call to the helper }
  4659. { on entry left node contains the parameter }
  4660. if left.nodetype = callparan then
  4661. temp_pnode := @tcallparanode(left).left
  4662. else
  4663. temp_pnode := @left;
  4664. result := ccallnode.createintern('fpc_round_real',ccallparanode.create(temp_pnode^,nil));
  4665. include(tcallnode(result).callnodeflags,cnf_check_fpu_exceptions);
  4666. temp_pnode^ := nil;
  4667. end;
  4668. function tinlinenode.first_trunc_real : tnode;
  4669. var
  4670. temp_pnode: pnode;
  4671. begin
  4672. { create the call to the helper }
  4673. { on entry left node contains the parameter }
  4674. if left.nodetype = callparan then
  4675. temp_pnode := @tcallparanode(left).left
  4676. else
  4677. temp_pnode := @left;
  4678. result := ccallnode.createintern('fpc_trunc_real',ccallparanode.create(temp_pnode^,nil));
  4679. include(tcallnode(result).callnodeflags,cnf_check_fpu_exceptions);
  4680. temp_pnode^ := nil;
  4681. end;
  4682. function tinlinenode.first_abs_long : tnode;
  4683. begin
  4684. expectloc:=LOC_REGISTER;
  4685. result:=nil;
  4686. end;
  4687. function tinlinenode.getaddsub_for_incdec : tnode;
  4688. var
  4689. hp,hpp,resultnode : tnode;
  4690. tempnode: ttempcreatenode;
  4691. newstatement: tstatementnode;
  4692. newblock: tblocknode;
  4693. begin
  4694. newblock := internalstatements(newstatement);
  4695. { extra parameter? }
  4696. if assigned(tcallparanode(left).right) then
  4697. begin
  4698. { Yes, use for add node }
  4699. hpp := tcallparanode(tcallparanode(left).right).left;
  4700. tcallparanode(tcallparanode(left).right).left := nil;
  4701. if assigned(tcallparanode(tcallparanode(left).right).right) then
  4702. { A syntax error should have already been raised }
  4703. InternalError(2025050601);
  4704. end
  4705. else
  4706. begin
  4707. { no, create constant 1 }
  4708. hpp := cordconstnode.create(1,tcallparanode(left).left.resultdef,false);
  4709. end;
  4710. typecheckpass(hpp);
  4711. { make sure we don't call functions part of the left node twice (and generally }
  4712. { optimize the code generation) }
  4713. { Storing address is not always an optimization: alignment of left is not known
  4714. at this point, so we must assume the worst and use an unaligned pointer.
  4715. This results in larger and slower code on alignment-sensitive targets.
  4716. Therefore the complexity condition below is questionable, maybe just filtering
  4717. out calls with "= NODE_COMPLEXITY_INF" is sufficient.
  4718. Value of 3 corresponds to subscript nodes, i.e. record field. }
  4719. if node_complexity(tcallparanode(left).left) > 3 then
  4720. begin
  4721. tempnode := ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  4722. addstatement(newstatement,tempnode);
  4723. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  4724. caddrnode.create_internal(tcallparanode(left).left.getcopy)));
  4725. hp := cderefnode.create(ctemprefnode.create(tempnode));
  4726. inserttypeconv_internal(hp,tcallparanode(left).left.resultdef);
  4727. end
  4728. else
  4729. begin
  4730. hp := tcallparanode(left).left.getcopy;
  4731. tempnode := nil;
  4732. end;
  4733. resultnode := hp.getcopy;
  4734. { Remove "modify" flag from what will be an assignment destination }
  4735. Exclude(resultnode.flags, nf_modify);
  4736. { avoid type errors from the addn/subn }
  4737. if not is_integer(resultnode.resultdef) then
  4738. begin
  4739. inserttypeconv_internal(hp,sinttype);
  4740. inserttypeconv_internal(hpp,sinttype);
  4741. end;
  4742. { addition/subtraction depending on inc/dec }
  4743. if inlinenumber = in_inc_x then
  4744. hpp := caddnode.create_internal(addn,hp,hpp)
  4745. else
  4746. hpp := caddnode.create_internal(subn,hp,hpp);
  4747. { assign result of addition }
  4748. { inherit internal flag }
  4749. if not(is_integer(resultnode.resultdef)) then
  4750. begin
  4751. if nf_internal in flags then
  4752. inserttypeconv_internal(hpp,corddef.create(
  4753. {$ifdef cpu64bitaddr}
  4754. s64bit,
  4755. {$else cpu64bitaddr}
  4756. s32bit,
  4757. {$endif cpu64bitaddr}
  4758. get_min_value(resultnode.resultdef),
  4759. get_max_value(resultnode.resultdef),
  4760. true))
  4761. else
  4762. inserttypeconv(hpp,corddef.create(
  4763. {$ifdef cpu64bitaddr}
  4764. s64bit,
  4765. {$else cpu64bitaddr}
  4766. s32bit,
  4767. {$endif cpu64bitaddr}
  4768. get_min_value(resultnode.resultdef),
  4769. get_max_value(resultnode.resultdef),
  4770. true))
  4771. end
  4772. else
  4773. begin
  4774. if nf_internal in flags then
  4775. inserttypeconv_internal(hpp,resultnode.resultdef)
  4776. else
  4777. inserttypeconv(hpp,resultnode.resultdef);
  4778. end;
  4779. { avoid any possible warnings }
  4780. inserttypeconv_internal(hpp,resultnode.resultdef);
  4781. { force pass 1, so copied trees get first pass'ed as well and flags like
  4782. nf_call_unique get set right }
  4783. node_reset_pass1_write(hpp);
  4784. do_typecheckpass(hpp);
  4785. addstatement(newstatement,cassignmentnode.create(resultnode,hpp));
  4786. { firstpass it }
  4787. firstpass(tnode(newstatement.left));
  4788. { deallocate the temp }
  4789. if assigned(tempnode) then
  4790. addstatement(newstatement,ctempdeletenode.create(tempnode));
  4791. { firstpass it }
  4792. firstpass(tnode(newblock));
  4793. { return new node }
  4794. result := newblock;
  4795. end;
  4796. function tinlinenode.first_IncDec: tnode;
  4797. begin
  4798. expectloc:=LOC_VOID;
  4799. result:=nil;
  4800. { range/overflow checking doesn't work properly }
  4801. { with the inc/dec code that's generated (JM) }
  4802. if ((localswitches * [cs_check_overflow,cs_check_range] <> []) and
  4803. { No overflow check for pointer operations, because inc(pointer,-1) will always
  4804. trigger an overflow. For uint32 it works because then the operation is done
  4805. in 64bit. Range checking is not applicable to pointers either }
  4806. (tcallparanode(left).left.resultdef.typ<>pointerdef))
  4807. {$ifdef jvm}
  4808. { enums are class instances on the JVM -> special treatment }
  4809. or (tcallparanode(left).left.resultdef.typ=enumdef)
  4810. {$endif}
  4811. then
  4812. { convert to simple add (JM) }
  4813. result:=getaddsub_for_incdec
  4814. end;
  4815. function tinlinenode.first_IncludeExclude: tnode;
  4816. begin
  4817. result:=nil;
  4818. expectloc:=LOC_VOID;
  4819. end;
  4820. function tinlinenode.first_get_frame: tnode;
  4821. begin
  4822. include(current_procinfo.flags,pi_needs_stackframe);
  4823. include(current_procinfo.flags,pi_uses_get_frame);
  4824. expectloc:=LOC_CREGISTER;
  4825. result:=nil;
  4826. end;
  4827. function tinlinenode.first_setlength: tnode;
  4828. var
  4829. paras : tnode;
  4830. npara,
  4831. ppn : tcallparanode;
  4832. dims,
  4833. counter : integer;
  4834. isarray : boolean;
  4835. destppn : tnode;
  4836. newstatement : tstatementnode;
  4837. temp : ttempcreatenode;
  4838. newblock : tnode;
  4839. begin
  4840. paras:=left;
  4841. ppn:=tcallparanode(paras);
  4842. dims:=0;
  4843. while assigned(ppn.right) do
  4844. begin
  4845. inc(dims);
  4846. ppn:=tcallparanode(ppn.right);
  4847. end;
  4848. destppn:=ppn.left;
  4849. isarray:=is_dynamic_array(destppn.resultdef);
  4850. { first param must be a string or dynamic array ...}
  4851. if isarray then
  4852. begin
  4853. { SetLength(Arr, 0), Arr := nil, Arr := [] }
  4854. if (dims=1) and is_constintvalue(tcallparanode(paras).left, 0) then
  4855. begin
  4856. ppn.left:=nil; { unlink destppn }
  4857. result:=ccallparanode.create(ctypeconvnode.create_internal(destppn,voidpointertype),nil);
  4858. result:=ccallnode.createintern('fpc_dynarray_clear',
  4859. ccallparanode.create(caddrnode.create_internal(
  4860. crttinode.create(tstoreddef(destppn.resultdef),initrtti,rdt_normal)),
  4861. result));
  4862. exit;
  4863. end;
  4864. { create statements with call initialize the arguments and
  4865. call fpc_dynarr_setlength }
  4866. newblock:=internalstatements(newstatement);
  4867. { get temp for array of lengths }
  4868. temp:=ctempcreatenode.create(carraydef.getreusable(sinttype,dims),dims*sinttype.size,tt_persistent,false);
  4869. addstatement(newstatement,temp);
  4870. { load array of lengths }
  4871. ppn:=tcallparanode(paras);
  4872. counter:=dims-1;
  4873. while assigned(ppn.right) do
  4874. begin
  4875. addstatement(newstatement,cassignmentnode.create(
  4876. cvecnode.create(
  4877. ctemprefnode.create(temp),
  4878. genintconstnode(counter)
  4879. ),
  4880. ppn.left));
  4881. ppn.left:=nil;
  4882. dec(counter);
  4883. ppn:=tcallparanode(ppn.right);
  4884. end;
  4885. { destppn is also reused }
  4886. ppn.left:=nil;
  4887. { create call to fpc_dynarr_setlength }
  4888. npara:=ccallparanode.create(caddrnode.create_internal(
  4889. cvecnode.create(
  4890. ctemprefnode.create(temp),
  4891. genintconstnode(0)
  4892. )),
  4893. ccallparanode.create(cordconstnode.create
  4894. (dims,sinttype,true),
  4895. ccallparanode.create(caddrnode.create_internal
  4896. (crttinode.create(tstoreddef(destppn.resultdef),initrtti,rdt_normal)),
  4897. ccallparanode.create(ctypeconvnode.create_internal(destppn,voidpointertype),nil))));
  4898. addstatement(newstatement,ccallnode.createintern('fpc_dynarray_setlength',npara));
  4899. addstatement(newstatement,ctempdeletenode.create(temp));
  4900. end
  4901. else if is_ansistring(destppn.resultdef) then
  4902. begin
  4903. newblock:=ccallnode.createintern(
  4904. 'fpc_'+tstringdef(destppn.resultdef).stringtypname+'_setlength',
  4905. ccallparanode.create(
  4906. cordconstnode.create(getparaencoding(destppn.resultdef),u16inttype,true),
  4907. paras
  4908. )
  4909. );
  4910. { we reused the parameters, make sure we don't release them }
  4911. left:=nil;
  4912. end
  4913. else
  4914. begin
  4915. { we can reuse the supplied parameters }
  4916. newblock:=ccallnode.createintern(
  4917. 'fpc_'+tstringdef(destppn.resultdef).stringtypname+'_setlength',paras);
  4918. { we reused the parameters, make sure we don't release them }
  4919. left:=nil;
  4920. end;
  4921. result:=newblock;
  4922. end;
  4923. function tinlinenode.first_copy: tnode;
  4924. var
  4925. lowppn,
  4926. countppn,
  4927. elesizeppn,
  4928. eletypeppn,
  4929. maxcountppn,
  4930. arrayppn,
  4931. rttippn,
  4932. npara,
  4933. paras : tnode;
  4934. ppn : tcallparanode;
  4935. paradef : tdef;
  4936. counter : integer;
  4937. begin
  4938. { determine copy function to use based on the first argument,
  4939. also count the number of arguments in this loop }
  4940. counter:=1;
  4941. paras:=left;
  4942. ppn:=tcallparanode(paras);
  4943. while assigned(ppn.right) do
  4944. begin
  4945. inc(counter);
  4946. ppn:=tcallparanode(ppn.right);
  4947. end;
  4948. paradef:=ppn.left.resultdef;
  4949. { fill up third parameter }
  4950. if counter=2 then
  4951. begin
  4952. paras:=ccallparanode.create(cordconstnode.create(torddef(sinttype).high,sinttype,false),paras);
  4953. counter:=3;
  4954. end;
  4955. if is_ansistring(resultdef) then
  4956. { keep the specific kind of ansistringdef as result }
  4957. result:=ccallnode.createinternres('fpc_ansistr_copy',paras,resultdef)
  4958. else if is_widestring(resultdef) then
  4959. result:=ccallnode.createintern('fpc_widestr_copy',paras)
  4960. else if is_unicodestring(resultdef) then
  4961. result:=ccallnode.createintern('fpc_unicodestr_copy',paras)
  4962. { can't check for resultdef = cansichartype, because resultdef=
  4963. cshortstringtype here }
  4964. else if is_char(paradef) then
  4965. result:=ccallnode.createintern('fpc_char_copy',paras)
  4966. else if is_dynamic_array(resultdef) then
  4967. begin
  4968. { create statements with call }
  4969. elesizeppn:=cordconstnode.create(tarraydef(paradef).elesize,sinttype,false);
  4970. if is_managed_type(tarraydef(paradef).elementdef) then
  4971. eletypeppn:=caddrnode.create_internal(
  4972. crttinode.create(tstoreddef(tarraydef(paradef).elementdef),initrtti,rdt_normal))
  4973. else
  4974. eletypeppn:=cordconstnode.create(0,voidpointertype,false);
  4975. maxcountppn:=geninlinenode(in_length_x,false,ppn.left.getcopy);
  4976. case counter of
  4977. 1:
  4978. begin
  4979. { copy the whole array using [0..high(sizeint)] range }
  4980. countppn:=cordconstnode.create(torddef(sinttype).high,sinttype,false);
  4981. lowppn:=cordconstnode.create(0,sinttype,false);
  4982. end;
  4983. 2:
  4984. begin
  4985. { copy the array using [low..high(sizeint)] range }
  4986. countppn:=cordconstnode.create(torddef(sinttype).high,sinttype,false);
  4987. lowppn:=tcallparanode(paras).left.getcopy;
  4988. end;
  4989. 3:
  4990. begin
  4991. countppn:=tcallparanode(paras).left.getcopy;
  4992. lowppn:=tcallparanode(tcallparanode(paras).right).left.getcopy;
  4993. end;
  4994. else
  4995. internalerror(2012100703);
  4996. end;
  4997. if is_open_array(paradef) then
  4998. begin
  4999. arrayppn:=caddrnode.create_internal(ppn.left);
  5000. end
  5001. else if is_dynamic_array(paradef) then
  5002. begin
  5003. arrayppn:=ctypeconvnode.create_internal(ppn.left,voidpointertype);
  5004. end
  5005. else
  5006. internalerror(2012100704);
  5007. rttippn:=caddrnode.create_internal(crttinode.create(tstoreddef(resultdef),initrtti,rdt_normal));
  5008. { create call to fpc_array_to_dynarray_copy }
  5009. npara:=ccallparanode.create(eletypeppn,
  5010. ccallparanode.create(elesizeppn,
  5011. ccallparanode.create(maxcountppn,
  5012. ccallparanode.create(countppn,
  5013. ccallparanode.create(lowppn,
  5014. ccallparanode.create(rttippn,
  5015. ccallparanode.create(arrayppn,nil)))))));
  5016. result:=ccallnode.createinternres('fpc_array_to_dynarray_copy',npara,resultdef);
  5017. ppn.left:=nil;
  5018. paras.free;
  5019. end
  5020. else
  5021. result:=ccallnode.createintern('fpc_shortstr_copy',paras);
  5022. { parameters are reused }
  5023. left:=nil;
  5024. end;
  5025. function tinlinenode.first_new: tnode;
  5026. var
  5027. newstatement : tstatementnode;
  5028. newblock : tblocknode;
  5029. temp : ttempcreatenode;
  5030. para : tcallparanode;
  5031. begin
  5032. { create statements with call to getmem+initialize }
  5033. newblock:=internalstatements(newstatement);
  5034. { create temp for result }
  5035. temp := ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,true);
  5036. addstatement(newstatement,temp);
  5037. { create call to fpc_getmem }
  5038. para := ccallparanode.create(cordconstnode.create
  5039. (tpointerdef(left.resultdef).pointeddef.size,s32inttype,true),nil);
  5040. addstatement(newstatement,cassignmentnode.create(
  5041. ctemprefnode.create(temp),
  5042. ccallnode.createintern('fpc_getmem',para)));
  5043. { create call to fpc_initialize }
  5044. if is_managed_type(tpointerdef(left.resultdef).pointeddef) then
  5045. begin
  5046. para := ccallparanode.create(caddrnode.create_internal(crttinode.create
  5047. (tstoreddef(tpointerdef(left.resultdef).pointeddef),initrtti,rdt_normal)),
  5048. ccallparanode.create(ctemprefnode.create
  5049. (temp),nil));
  5050. addstatement(newstatement,ccallnode.createintern('fpc_initialize',para));
  5051. end;
  5052. { the last statement should return the value as
  5053. location and type, this is done be referencing the
  5054. temp and converting it first from a persistent temp to
  5055. normal temp }
  5056. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  5057. addstatement(newstatement,ctemprefnode.create(temp));
  5058. result:=newblock;
  5059. end;
  5060. function tinlinenode.first_length: tnode;
  5061. begin
  5062. result:=nil;
  5063. if is_shortstring(left.resultdef) then
  5064. expectloc:=left.expectloc
  5065. else
  5066. begin
  5067. { ansi/wide string }
  5068. expectloc:=LOC_REGISTER;
  5069. end;
  5070. end;
  5071. function tinlinenode.first_high: tnode;
  5072. begin
  5073. result:=nil;
  5074. if not(is_dynamic_array(left.resultdef)) then
  5075. Internalerror(2019122802);
  5076. expectloc:=LOC_REGISTER;
  5077. end;
  5078. function tinlinenode.first_assigned: tnode;
  5079. begin
  5080. { Comparison must not call procvars, indicate that with nf_load_procvar flag }
  5081. result:=caddnode.create(unequaln,tcallparanode(left).left,cnilnode.create);
  5082. include(result.flags,nf_load_procvar);
  5083. tcallparanode(left).left:=nil;
  5084. end;
  5085. function tinlinenode.first_assert: tnode;
  5086. var
  5087. paras: tcallparanode;
  5088. begin
  5089. paras:=tcallparanode(tcallparanode(left).right);
  5090. paras:=ccallparanode.create(cstringconstnode.createstr(current_module.sourcefiles.get_file_name(current_filepos.fileindex)),paras);
  5091. paras:=ccallparanode.create(genintconstnode(fileinfo.line),paras);
  5092. {$ifdef SUPPORT_GET_FRAME}
  5093. paras:=ccallparanode.create(geninlinenode(in_get_frame,false,nil),paras);
  5094. {$else}
  5095. paras:=ccallparanode.create(ccallnode.createinternfromunit('SYSTEM','GET_FRAME',nil),paras);
  5096. {$endif}
  5097. result:=cifnode.create(cnotnode.create(tcallparanode(left).left),
  5098. ccallnode.createintern('fpc_assert',paras),nil);
  5099. include(result.flags,nf_internal);
  5100. tcallparanode(left).left:=nil;
  5101. tcallparanode(left).right:=nil;
  5102. end;
  5103. function tinlinenode.first_popcnt: tnode;
  5104. var
  5105. suffix : string;
  5106. begin
  5107. case torddef(left.resultdef).ordtype of
  5108. u8bit: suffix:='byte';
  5109. u16bit: suffix:='word';
  5110. u32bit: suffix:='dword';
  5111. u64bit: suffix:='qword';
  5112. else
  5113. internalerror(2012082601);
  5114. end;
  5115. result:=ccallnode.createintern('fpc_popcnt_'+suffix,ccallparanode.create(left,nil));
  5116. left:=nil;
  5117. end;
  5118. function tinlinenode.first_bitscan: tnode;
  5119. begin
  5120. result:=nil;
  5121. expectloc:=LOC_REGISTER;
  5122. end;
  5123. function tinlinenode.typecheck_seg: tnode;
  5124. begin
  5125. if target_info.system in systems_managed_vm then
  5126. message(parser_e_feature_unsupported_for_vm);
  5127. set_varstate(left,vs_read,[]);
  5128. result:=cordconstnode.create(0,s32inttype,false);
  5129. end;
  5130. function tinlinenode.first_seg: tnode;
  5131. begin
  5132. internalerror(200104046);
  5133. result:=nil;
  5134. end;
  5135. function tinlinenode.first_sar: tnode;
  5136. begin
  5137. result:=nil;
  5138. expectloc:=LOC_REGISTER;
  5139. {$if not defined(cpu64bitalu) and not defined(cpucg64shiftsupport)}
  5140. if is_64bitint(resultdef) then
  5141. begin
  5142. if (inlinenumber=in_sar_x) then
  5143. left:=ccallparanode.create(cordconstnode.create(1,u8inttype,false),
  5144. ccallparanode.create(left,nil));
  5145. result:=ccallnode.createintern('fpc_sarint64',left);
  5146. left:=nil;
  5147. end;
  5148. {$endif not defined(cpu64bitalu) and not defined(cpucg64shiftsupport)}
  5149. end;
  5150. function tinlinenode.handle_box: tnode;
  5151. begin
  5152. result:=nil;
  5153. if not assigned(left) or
  5154. assigned(tcallparanode(left).right) then
  5155. CGMessage1(parser_e_wrong_parameter_size,'FpcInternalBox');
  5156. resultdef:=class_tobject;
  5157. end;
  5158. function tinlinenode.handle_unbox: tnode;
  5159. begin
  5160. result:=nil;
  5161. if not assigned(left) or
  5162. not assigned(tcallparanode(left).right) or
  5163. assigned(tcallparanode(tcallparanode(left).right).right) then
  5164. CGMessage1(parser_e_wrong_parameter_size,'FpcInternalUnBox');
  5165. if tcallparanode(left).left.nodetype<>typen then
  5166. internalerror(2011071701);
  5167. ttypenode(tcallparanode(left).left).allowed:=true;
  5168. resultdef:=tcallparanode(left).left.resultdef;
  5169. end;
  5170. function tinlinenode.handle_insert: tnode;
  5171. procedure do_error;
  5172. begin
  5173. CGMessagePos1(fileinfo,parser_e_wrong_parameter_size,'Insert');
  5174. write_system_parameter_lists('fpc_shortstr_insert');
  5175. write_system_parameter_lists('fpc_shortstr_insert_char');
  5176. write_system_parameter_lists('fpc_unicodestr_insert');
  5177. if tf_winlikewidestring in target_info.flags then
  5178. write_system_parameter_lists('fpc_widestr_insert');
  5179. write_system_parameter_lists('fpc_ansistr_insert');
  5180. MessagePos1(fileinfo,sym_e_param_list,'Insert(Dynamic Array;var Dynamic Array;'+sinttype.typename+');');
  5181. MessagePos1(fileinfo,sym_e_param_list,'Insert(Element;var Dynamic Array;'+sinttype.typename+');');
  5182. end;
  5183. var
  5184. procname : String;
  5185. newn,
  5186. datan,
  5187. datacountn,
  5188. firstn,
  5189. secondn : tnode;
  5190. first,
  5191. second : tdef;
  5192. isconstr,
  5193. iscomparray,
  5194. iscompelem : boolean;
  5195. datatemp : ttempcreatenode;
  5196. insertblock : tblocknode;
  5197. insertstatement : tstatementnode;
  5198. begin
  5199. if not assigned(left) or
  5200. not assigned(tcallparanode(left).right) or
  5201. not assigned(tcallparanode(tcallparanode(left).right).right) or
  5202. assigned(tcallparanode(tcallparanode(tcallparanode(left).right).right).right) then
  5203. begin
  5204. do_error;
  5205. exit(cerrornode.create);
  5206. end;
  5207. { determine the correct function based on the second parameter }
  5208. firstn:=tcallparanode(tcallparanode(tcallparanode(left).right).right).left;
  5209. first:=firstn.resultdef;
  5210. secondn:=tcallparanode(tcallparanode(left).right).left;
  5211. second:=secondn.resultdef;
  5212. if is_shortstring(second) then
  5213. begin
  5214. if is_char(first) then
  5215. procname:='fpc_shortstr_insert_char'
  5216. else
  5217. procname:='fpc_shortstr_insert';
  5218. end
  5219. else if is_unicodestring(second) then
  5220. procname:='fpc_unicodestr_insert'
  5221. else if is_widestring(second) then
  5222. procname:='fpc_widestr_insert'
  5223. else if is_ansistring(second) then
  5224. procname:='fpc_ansistr_insert'
  5225. else if is_dynamic_array(second) then
  5226. begin
  5227. { The first parameter needs to be
  5228. a) a dynamic array of the same type
  5229. b) a single element of the same type
  5230. c) a static array of the same type (not Delphi compatible)
  5231. }
  5232. isconstr:=is_array_constructor(first);
  5233. iscomparray:=(first.typ=arraydef) and equal_defs(tarraydef(first).elementdef,tarraydef(second).elementdef);
  5234. iscompelem:=compare_defs(first,tarraydef(second).elementdef,niln)<>te_incompatible;
  5235. if not iscomparray
  5236. and not iscompelem
  5237. and not isconstr then
  5238. begin
  5239. CGMessagePos(fileinfo,type_e_array_required);
  5240. exit(cerrornode.create);
  5241. end;
  5242. insertblock:=internalstatements(insertstatement);
  5243. datatemp:=nil;
  5244. if iscomparray then
  5245. begin
  5246. datatemp:=ctempcreatenode.create_value(first,first.size,tt_normal,false,firstn);
  5247. addstatement(insertstatement,datatemp);
  5248. if is_dynamic_array(first) then
  5249. datan:=ctypeconvnode.create_internal(ctemprefnode.create(datatemp),voidpointertype)
  5250. else
  5251. datan:=caddrnode.create_internal(cvecnode.create(ctemprefnode.create(datatemp),cordconstnode.create(0,sizesinttype,false)));
  5252. datacountn:=cinlinenode.create(in_length_x,false,ctemprefnode.create(datatemp));
  5253. end
  5254. else if isconstr then
  5255. begin
  5256. inserttypeconv(firstn,second);
  5257. datatemp:=ctempcreatenode.create_value(second,second.size,tt_normal,false,firstn);
  5258. addstatement(insertstatement,datatemp);
  5259. datan:=ctypeconvnode.create_internal(ctemprefnode.create(datatemp),voidpointertype);
  5260. datacountn:=cinlinenode.create(in_length_x,false,ctemprefnode.create(datatemp));
  5261. end
  5262. else
  5263. begin
  5264. if is_const(firstn) then
  5265. begin
  5266. datatemp:=ctempcreatenode.create_value(tarraydef(second).elementdef,tarraydef(second).elementdef.size,tt_normal,false,firstn);
  5267. addstatement(insertstatement,datatemp);
  5268. datan:=caddrnode.create_internal(ctemprefnode.create(datatemp));
  5269. end
  5270. else
  5271. begin
  5272. datan:=firstn;
  5273. inserttypeconv(datan,tarraydef(second).elementdef);
  5274. datan:=caddrnode.create_internal(datan);
  5275. end;
  5276. datacountn:=cordconstnode.create(1,sizesinttype,false);
  5277. end;
  5278. procname:='fpc_dynarray_insert';
  5279. { recreate the parameters as array pointer, source, data, count, typeinfo }
  5280. newn:=ccallparanode.create(caddrnode.create_internal(crttinode.create(tstoreddef(second),initrtti,rdt_normal)),
  5281. ccallparanode.create(datacountn,
  5282. ccallparanode.create(datan,
  5283. ccallparanode.create(tcallparanode(left).left,
  5284. ccallparanode.create(ctypeconvnode.create_internal(secondn,voidpointertype),nil)))));
  5285. addstatement(insertstatement,ccallnode.createintern(procname,newn));
  5286. if assigned(datatemp) then
  5287. addstatement(insertstatement,ctempdeletenode.create(datatemp));
  5288. tcallparanode(tcallparanode(tcallparanode(left).right).right).left:=nil; // insert idx
  5289. tcallparanode(tcallparanode(left).right).left:=nil; // dyn array
  5290. tcallparanode(left).left:=nil; // insert element/array
  5291. left.free;
  5292. left:=nil;
  5293. result:=insertblock;
  5294. exit; { ! }
  5295. end
  5296. else if second.typ=undefineddef then
  5297. { just pick one }
  5298. procname:='fpc_ansistr_insert'
  5299. else
  5300. begin
  5301. do_error;
  5302. exit(cerrornode.create);
  5303. end;
  5304. result:=ccallnode.createintern(procname,left);
  5305. left:=nil;
  5306. end;
  5307. function tinlinenode.handle_delete: tnode;
  5308. procedure do_error;
  5309. begin
  5310. CGMessagePos1(fileinfo,parser_e_wrong_parameter_size,'Delete');
  5311. write_system_parameter_lists('fpc_shortstr_delete');
  5312. write_system_parameter_lists('fpc_unicodestr_delete');
  5313. if tf_winlikewidestring in target_info.flags then
  5314. write_system_parameter_lists('fpc_widestr_delete');
  5315. write_system_parameter_lists('fpc_ansistr_delete');
  5316. MessagePos1(fileinfo,sym_e_param_list,'Delete(var Dynamic Array;'+sinttype.typename+';'+sinttype.typename+');');
  5317. end;
  5318. var
  5319. procname : String;
  5320. first : tdef;
  5321. firstn,
  5322. newn : tnode;
  5323. begin
  5324. if not assigned(left) or
  5325. not assigned(tcallparanode(left).right) or
  5326. not assigned(tcallparanode(tcallparanode(left).right).right) or
  5327. assigned(tcallparanode(tcallparanode(tcallparanode(left).right).right).right) then
  5328. begin
  5329. do_error;
  5330. exit(cerrornode.create);
  5331. end;
  5332. { determine the correct function based on the first parameter }
  5333. firstn:=tcallparanode(tcallparanode(tcallparanode(left).right).right).left;
  5334. first:=firstn.resultdef;
  5335. if is_shortstring(first) then
  5336. procname:='fpc_shortstr_delete'
  5337. else if is_unicodestring(first) then
  5338. procname:='fpc_unicodestr_delete'
  5339. else if is_widestring(first) then
  5340. procname:='fpc_widestr_delete'
  5341. else if is_ansistring(first) then
  5342. procname:='fpc_ansistr_delete'
  5343. else if is_dynamic_array(first) then
  5344. begin
  5345. procname:='fpc_dynarray_delete';
  5346. { recreate the parameters as array pointer, src, count, typeinfo }
  5347. newn:=ccallparanode.create(caddrnode.create_internal
  5348. (crttinode.create(tstoreddef(first),initrtti,rdt_normal)),
  5349. ccallparanode.create(tcallparanode(left).left,
  5350. ccallparanode.create(tcallparanode(tcallparanode(left).right).left,
  5351. ccallparanode.create(ctypeconvnode.create_internal(firstn,voidpointertype),nil))));
  5352. tcallparanode(tcallparanode(tcallparanode(left).right).right).left:=nil;
  5353. tcallparanode(tcallparanode(left).right).left:=nil;
  5354. tcallparanode(left).left:=nil;
  5355. left.free;
  5356. left:=newn;
  5357. end
  5358. else if first.typ=undefineddef then
  5359. { just pick one }
  5360. procname:='fpc_ansistr_delete'
  5361. else
  5362. begin
  5363. do_error;
  5364. exit(cerrornode.create);
  5365. end;
  5366. result:=ccallnode.createintern(procname,left);
  5367. left:=nil;
  5368. end;
  5369. function tinlinenode.handle_concat:tnode;
  5370. procedure do_error;
  5371. begin
  5372. CGMessagePos1(fileinfo,parser_e_wrong_parameter_size,'Concat');
  5373. MessagePos1(fileinfo,sym_e_param_list,'Concat(String[;String;...])');
  5374. MessagePos1(fileinfo,sym_e_param_list,'Concat(Dynamic Array[;Dynamic Array;...])');
  5375. end;
  5376. var
  5377. cpn : tcallparanode;
  5378. list : tfpobjectlist;
  5379. n,
  5380. arrn,
  5381. firstn : tnode;
  5382. i : longint;
  5383. arrconstr : tarrayconstructornode;
  5384. newstatement : tstatementnode;
  5385. tempnode : ttempcreatenode;
  5386. lastchanged : boolean;
  5387. begin
  5388. if not assigned(left) then
  5389. begin
  5390. do_error;
  5391. exit(cerrornode.create);
  5392. end;
  5393. result:=nil;
  5394. { the arguments are right to left, but we need to work on them from
  5395. left to right, so insert them in a list and process that from back
  5396. to front }
  5397. list:=tfpobjectlist.create(false);
  5398. { remember the last (aka first) dynamic array parameter (important
  5399. in case of array constructors) }
  5400. arrn:=nil;
  5401. cpn:=tcallparanode(left);
  5402. while assigned(cpn) do
  5403. begin
  5404. list.add(cpn.left);
  5405. if is_dynamic_array(cpn.left.resultdef) then
  5406. arrn:=cpn.left;
  5407. cpn.left:=nil;
  5408. cpn:=tcallparanode(cpn.right);
  5409. end;
  5410. if list.count=0 then
  5411. internalerror(2017100901);
  5412. firstn:=tnode(list.last);
  5413. if not assigned(firstn) then
  5414. internalerror(2017100902);
  5415. { are we dealing with strings or dynamic arrays? }
  5416. if is_dynamic_array(firstn.resultdef) or is_array_constructor(firstn.resultdef) then
  5417. begin
  5418. { try to combine all consecutive array constructors }
  5419. lastchanged:=false;
  5420. i:=0;
  5421. repeat
  5422. if lastchanged or is_array_constructor(tnode(list[i]).resultdef) then
  5423. begin
  5424. if (i<list.count-1) and is_array_constructor(tnode(list[i+1]).resultdef) then
  5425. begin
  5426. arrconstr:=tarrayconstructornode(list[i+1]);
  5427. while assigned(arrconstr.right) do
  5428. arrconstr:=tarrayconstructornode(arrconstr.right);
  5429. arrconstr.right:=tnode(list[i]);
  5430. list[i]:=list[i+1];
  5431. list.delete(i+1);
  5432. lastchanged:=true;
  5433. tnode(list[i]).resultdef:=nil;
  5434. { don't increase index! }
  5435. continue;
  5436. end;
  5437. if lastchanged then
  5438. begin
  5439. { we concatted all consecutive ones, so typecheck the new one again }
  5440. n:=tnode(list[i]);
  5441. typecheckpass(n);
  5442. list[i]:=n;
  5443. end;
  5444. lastchanged:=false;
  5445. end;
  5446. inc(i);
  5447. until i=list.count;
  5448. if list.count=1 then
  5449. begin
  5450. { no need to call the concat helper }
  5451. result:=firstn;
  5452. end
  5453. else
  5454. begin
  5455. { if we reach this point then the concat list didn't consist
  5456. solely of array constructors }
  5457. if not assigned(arrn) then
  5458. internalerror(2017101001);
  5459. result:=internalstatements(newstatement);
  5460. { generate the open array constructor for the source arrays
  5461. note: the order needs to be swapped again here! }
  5462. arrconstr:=nil;
  5463. for i:=0 to list.count-1 do
  5464. begin
  5465. n:=tnode(list[i]);
  5466. { first convert to the target type }
  5467. if not is_array_constructor(n.resultdef) then
  5468. inserttypeconv(n,arrn.resultdef);
  5469. { we need to ensure that we get a reference counted
  5470. assignement for the temp array }
  5471. tempnode:=ctempcreatenode.create(arrn.resultdef,arrn.resultdef.size,tt_persistent,true);
  5472. addstatement(newstatement,tempnode);
  5473. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode),n));
  5474. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  5475. n:=ctemprefnode.create(tempnode);
  5476. { then to a plain pointer for the helper }
  5477. inserttypeconv_internal(n,voidpointertype);
  5478. arrconstr:=carrayconstructornode.create(n,arrconstr);
  5479. end;
  5480. Include(arrconstr.arrayconstructornodeflags,acnf_allow_array_constructor);
  5481. { based on the code from nopt.genmultistringadd() }
  5482. tempnode:=ctempcreatenode.create(arrn.resultdef,arrn.resultdef.size,tt_persistent,true);
  5483. addstatement(newstatement,tempnode);
  5484. { initialize the temp, since it will be passed to a
  5485. var-parameter (and finalization, which is performed by the
  5486. ttempcreate node and which takes care of the initialization
  5487. on native targets, is a noop on managed VM targets) }
  5488. if (target_info.system in systems_managed_vm) and
  5489. is_managed_type(arrn.resultdef) then
  5490. addstatement(newstatement,cinlinenode.create(in_setlength_x,
  5491. false,
  5492. ccallparanode.create(genintconstnode(0),
  5493. ccallparanode.create(ctemprefnode.create(tempnode),nil))));
  5494. cpn:=ccallparanode.create(
  5495. arrconstr,
  5496. ccallparanode.create(
  5497. caddrnode.create_internal(crttinode.create(tstoreddef(arrn.resultdef),initrtti,rdt_normal)),
  5498. ccallparanode.create(ctypeconvnode.create_internal(ctemprefnode.create(tempnode),voidpointertype),nil))
  5499. );
  5500. addstatement(
  5501. newstatement,
  5502. ccallnode.createintern(
  5503. 'fpc_dynarray_concat_multi',
  5504. cpn
  5505. )
  5506. );
  5507. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  5508. addstatement(newstatement,ctemprefnode.create(tempnode));
  5509. end;
  5510. end
  5511. else
  5512. begin
  5513. { enforce strings }
  5514. for i:=list.count-1 downto 0 do
  5515. begin
  5516. if assigned(result) then
  5517. result:=caddnode.create(addn,result,tnode(list[i]))
  5518. else
  5519. begin
  5520. result:=tnode(list[i]);
  5521. { Force string type if it isn't yet }
  5522. if not(
  5523. (result.resultdef.typ=stringdef) or
  5524. is_chararray(result.resultdef) or
  5525. is_char(result.resultdef)
  5526. ) then
  5527. inserttypeconv(result,cshortstringtype);
  5528. end;
  5529. end;
  5530. end;
  5531. list.free;
  5532. end;
  5533. function tinlinenode.first_pack_unpack: tnode;
  5534. var
  5535. loopstatement : tstatementnode;
  5536. loop : tblocknode;
  5537. loopvar : ttempcreatenode;
  5538. tempnode,
  5539. source,
  5540. target,
  5541. index,
  5542. unpackednode,
  5543. packednode,
  5544. sourcevecindex,
  5545. targetvecindex,
  5546. loopbody : tnode;
  5547. temprangedef : tdef;
  5548. ulorange,
  5549. uhirange,
  5550. plorange,
  5551. phirange : TConstExprInt;
  5552. begin
  5553. { transform into a for loop which assigns the data of the (un)packed }
  5554. { array to the other one }
  5555. source := left;
  5556. if (inlinenumber = in_unpack_x_y_z) then
  5557. begin
  5558. target := tcallparanode(source).right;
  5559. index := tcallparanode(target).right;
  5560. packednode := tcallparanode(source).left;
  5561. unpackednode := tcallparanode(target).left;
  5562. end
  5563. else
  5564. begin
  5565. index := tcallparanode(source).right;
  5566. target := tcallparanode(index).right;
  5567. packednode := tcallparanode(target).left;
  5568. unpackednode := tcallparanode(source).left;
  5569. end;
  5570. source := tcallparanode(source).left;
  5571. target := tcallparanode(target).left;
  5572. index := tcallparanode(index).left;
  5573. loop := internalstatements(loopstatement);
  5574. loopvar := ctempcreatenode.create(
  5575. tarraydef(packednode.resultdef).rangedef,
  5576. tarraydef(packednode.resultdef).rangedef.size,
  5577. tt_persistent,true);
  5578. addstatement(loopstatement,loopvar);
  5579. { For range checking: we have to convert to an integer type (in case the index type }
  5580. { is an enum), add the index and loop variable together, convert the result }
  5581. { implicitly to an orddef with range equal to the rangedef to get range checking }
  5582. { and finally convert it explicitly back to the actual rangedef to avoid type }
  5583. { errors }
  5584. temprangedef:=nil;
  5585. getrange(unpackednode.resultdef,ulorange,uhirange);
  5586. getrange(packednode.resultdef,plorange,phirange);
  5587. { does not really need to be registered, but then we would have to
  5588. record it elsewhere so it still can be freed }
  5589. temprangedef:=corddef.create(torddef(sinttype).ordtype,ulorange,uhirange,true);
  5590. sourcevecindex := ctemprefnode.create(loopvar);
  5591. targetvecindex := ctypeconvnode.create_internal(index.getcopy,sinttype);
  5592. targetvecindex := caddnode.create(subn,targetvecindex,cordconstnode.create(plorange,sinttype,true));
  5593. targetvecindex := caddnode.create(addn,targetvecindex,ctemprefnode.create(loopvar));
  5594. targetvecindex := ctypeconvnode.create(targetvecindex,temprangedef);
  5595. targetvecindex := ctypeconvnode.create_explicit(targetvecindex,tarraydef(unpackednode.resultdef).rangedef);
  5596. if (inlinenumber = in_pack_x_y_z) then
  5597. begin
  5598. { swap source and target vec indices }
  5599. tempnode := sourcevecindex;
  5600. sourcevecindex := targetvecindex;
  5601. targetvecindex := tempnode;
  5602. end;
  5603. { create the assignment in the loop body }
  5604. loopbody :=
  5605. cassignmentnode.create(
  5606. cvecnode.create(target.getcopy,targetvecindex),
  5607. cvecnode.create(source.getcopy,sourcevecindex)
  5608. );
  5609. { create the actual for loop }
  5610. tempnode := cfornode.create(
  5611. ctemprefnode.create(loopvar),
  5612. cinlinenode.create(in_low_x,false,packednode.getcopy),
  5613. cinlinenode.create(in_high_x,false,packednode.getcopy),
  5614. loopbody,
  5615. false);
  5616. addstatement(loopstatement,tempnode);
  5617. { free the loop counter }
  5618. addstatement(loopstatement,ctempdeletenode.create(loopvar));
  5619. result := loop;
  5620. end;
  5621. function tinlinenode.may_have_sideeffect_norecurse: boolean;
  5622. begin
  5623. result:=
  5624. (inlinenumber in [in_write_x,in_writeln_x,in_read_x,in_readln_x,in_str_x_string,
  5625. in_val_x,in_reset_x,in_rewrite_x,in_reset_typedfile,in_rewrite_typedfile,
  5626. in_reset_typedfile_name,in_rewrite_typedfile_name,in_settextbuf_file_x,
  5627. in_inc_x,in_dec_x,in_include_x_y,in_exclude_x_y,in_break,in_continue,in_setlength_x,
  5628. in_finalize_x,in_new_x,in_dispose_x,in_exit,in_copy_x,in_initialize_x,in_leave,in_cycle,
  5629. in_and_assign_x_y,in_or_assign_x_y,in_xor_assign_x_y,in_sar_assign_x_y,in_shl_assign_x_y,
  5630. in_shr_assign_x_y,in_rol_assign_x_y,in_ror_assign_x_y,in_neg_assign_x,in_not_assign_x]) or
  5631. ((inlinenumber = in_assert_x_y) and
  5632. (cs_do_assertion in localswitches));
  5633. end;
  5634. function tinlinenode.may_ignore_result:boolean;
  5635. begin
  5636. case inlinenumber of
  5637. in_atomic_inc,
  5638. in_atomic_dec,
  5639. in_atomic_xchg,
  5640. in_atomic_cmp_xchg:
  5641. result:=true;
  5642. else
  5643. result:=is_void(resultdef);
  5644. end;
  5645. end;
  5646. function tinlinenode.first_fma: tnode;
  5647. begin
  5648. CGMessage1(cg_e_function_not_support_by_selected_instruction_set,'FMA');
  5649. result:=nil;
  5650. end;
  5651. function tinlinenode.first_minmax: tnode;
  5652. begin
  5653. CGMessage1(cg_e_function_not_support_by_selected_instruction_set,'MIN/MAX');
  5654. result:=nil;
  5655. end;
  5656. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  5657. function tinlinenode.first_ShiftRot_assign_64bitint: tnode;
  5658. var
  5659. procname: string[31];
  5660. begin
  5661. {$ifdef cpucg64shiftsupport}
  5662. if inlinenumber in [in_sar_assign_x_y,in_shl_assign_x_y,in_shr_assign_x_y] then
  5663. begin
  5664. result:=nil;
  5665. expectloc:=tcallparanode(tcallparanode(left).right).left.expectloc;
  5666. exit;
  5667. end;
  5668. {$endif cpucg64shiftsupport}
  5669. result := nil;
  5670. if is_signed(tcallparanode(left).right.resultdef) then
  5671. procname:='int64'
  5672. else
  5673. procname:='qword';
  5674. case inlinenumber of
  5675. in_sar_assign_x_y:
  5676. procname := 'fpc_sar_assign_'+procname;
  5677. in_shl_assign_x_y:
  5678. procname := 'fpc_shl_assign_'+procname;
  5679. in_shr_assign_x_y:
  5680. procname := 'fpc_shr_assign_'+procname;
  5681. in_rol_assign_x_y:
  5682. procname := 'fpc_rol_assign_'+procname;
  5683. in_ror_assign_x_y:
  5684. procname := 'fpc_ror_assign_'+procname;
  5685. else
  5686. internalerror(2017041301);
  5687. end;
  5688. result := ccallnode.createintern(procname,ccallparanode.create(tcallparanode(left).left,
  5689. ccallparanode.create(tcallparanode(tcallparanode(left).right).left,nil)));
  5690. tcallparanode(tcallparanode(left).right).left := nil;
  5691. tcallparanode(left).left := nil;
  5692. firstpass(result);
  5693. end;
  5694. {$endif not cpu64bitalu and nto cpuhighleveltarget}
  5695. function tinlinenode.first_AndOrXorShiftRot_assign: tnode;
  5696. begin
  5697. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  5698. { 64 bit ints have their own shift handling }
  5699. if is_64bit(tcallparanode(left).right.resultdef) and
  5700. (inlinenumber in [in_sar_assign_x_y,in_shl_assign_x_y,in_shr_assign_x_y,in_rol_assign_x_y,in_ror_assign_x_y]) then
  5701. result := first_ShiftRot_assign_64bitint
  5702. else
  5703. {$endif not cpu64bitalu and not cpuhighleveltarget}
  5704. begin
  5705. result:=nil;
  5706. expectloc:=tcallparanode(tcallparanode(left).right).left.expectloc;
  5707. end;
  5708. end;
  5709. function tinlinenode.first_NegNot_assign: tnode;
  5710. begin
  5711. result:=nil;
  5712. expectloc:=left.expectloc;
  5713. end;
  5714. function tinlinenode.first_atomic: tnode;
  5715. var
  5716. name : string;
  5717. n,n2,cmpn,succn,valn : tnode;
  5718. c : sizeint;
  5719. stmt : tstatementnode;
  5720. tmp,tmp2: ttempcreatenode;
  5721. begin
  5722. { by default we redirect to the corresponding compilerprocs }
  5723. name:='fpc_atomic_';
  5724. case inlinenumber of
  5725. in_atomic_inc:
  5726. if assigned(tcallparanode(left).right) then
  5727. name:=name+'add'
  5728. else
  5729. name:=name+'inc';
  5730. in_atomic_dec:
  5731. if assigned(tcallparanode(left).right) then
  5732. name:=name+'sub'
  5733. else
  5734. name:=name+'dec';
  5735. in_atomic_xchg:
  5736. name:=name+'xchg';
  5737. in_atomic_cmp_xchg:
  5738. name:=name+'cmp_xchg';
  5739. else
  5740. internalerror(2022093008);
  5741. end;
  5742. name:=name+'_';
  5743. if is_pointer(resultdef) then
  5744. name:=name+tostr(voidpointertype.size*8)
  5745. else if is_integer(resultdef) then
  5746. case torddef(resultdef).ordtype of
  5747. s8bit:
  5748. name:=name+'8';
  5749. s16bit:
  5750. name:=name+'16';
  5751. s32bit:
  5752. name:=name+'32';
  5753. s64bit:
  5754. name:=name+'64';
  5755. else
  5756. internalerror(2022100101);
  5757. end
  5758. else
  5759. internalerror(2022093009);
  5760. { for the call node we need to reverse the parameters }
  5761. c:=reverseparameters(tcallparanode(left));
  5762. succn:=nil;
  5763. cmpn:=nil;
  5764. valn:=nil;
  5765. if (inlinenumber=in_atomic_cmp_xchg) and (c=4) then
  5766. begin
  5767. { don't pass along the Succeeded parameter }
  5768. succn:=tcallparanode(left).left;
  5769. n:=tcallparanode(left).right;
  5770. tcallparanode(left).left:=nil;
  5771. tcallparanode(left).right:=nil;
  5772. left.free;
  5773. left:=tcallparanode(n);
  5774. { get a copy of the Comparand parameter }
  5775. cmpn:=tcallparanode(left).left.getcopy;
  5776. end
  5777. else if ((inlinenumber=in_atomic_inc) or (inlinenumber=in_atomic_dec)) and (c=2) then
  5778. begin
  5779. valn:=tcallparanode(left).left.getcopy;
  5780. end;
  5781. result:=ctypeconvnode.create_internal(ccallnode.createintern(name,left),resultdef);
  5782. left:=nil;
  5783. if assigned(succn) then
  5784. begin
  5785. { we need to execute the intrinsic and then we check whether the
  5786. returned result, namely the original value, is equal to the
  5787. comparand which means that the Succeeded parameter needs to be
  5788. True (otherwise it needs to be False). }
  5789. n:=internalstatements(stmt);
  5790. tmp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  5791. addstatement(stmt,tmp);
  5792. addstatement(stmt,cassignmentnode.create(ctemprefnode.create(tmp),result));
  5793. cmpn:=cmpn.getcopy;
  5794. inserttypeconv_internal(cmpn,resultdef);
  5795. addstatement(stmt,
  5796. cassignmentnode.create(succn,
  5797. caddnode.create(equaln,cmpn,
  5798. ctemprefnode.create(tmp))));
  5799. addstatement(stmt,ctempdeletenode.create_normal_temp(tmp));
  5800. addstatement(stmt,ctemprefnode.create(tmp));
  5801. result:=n;
  5802. end
  5803. else if ((inlinenumber=in_atomic_dec) or (inlinenumber=in_atomic_inc)) and (c=2) then
  5804. begin
  5805. { the helpers return the original value, due to ease of implementation with the
  5806. existing Interlocked* implementations, but the intrinsics need to return the
  5807. resulting value so we add/sub the Value to/from the result }
  5808. n:=internalstatements(stmt);
  5809. tmp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  5810. addstatement(stmt,tmp);
  5811. if inlinenumber=in_atomic_inc then
  5812. n2:=caddnode.create(addn,result,valn)
  5813. else
  5814. n2:=caddnode.create(subn,result,valn);
  5815. addstatement(stmt,cassignmentnode.create(ctemprefnode.create(tmp),n2));
  5816. addstatement(stmt,ctempdeletenode.create_normal_temp(tmp));
  5817. addstatement(stmt,ctemprefnode.create(tmp));
  5818. result:=n;
  5819. end;
  5820. end;
  5821. function tinlinenode.first_cpu : tnode;
  5822. begin
  5823. Result:=nil;
  5824. internalerror(2017110101);
  5825. end;
  5826. procedure tinlinenode.CheckParameters(count: integer);
  5827. var
  5828. p: tnode;
  5829. begin
  5830. if count=1 then
  5831. begin
  5832. // Sometimes there are more callparanodes
  5833. if left is tcallparanode then
  5834. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid])
  5835. else
  5836. set_varstate(left,vs_read,[vsf_must_be_valid])
  5837. end
  5838. else
  5839. begin
  5840. p:=left;
  5841. while count>0 do
  5842. begin
  5843. set_varstate(tcallparanode(p).left,vs_read,[vsf_must_be_valid]);
  5844. p:=tcallparanode(p).right;
  5845. dec(count);
  5846. end;
  5847. end;
  5848. end;
  5849. end.