ninl.pas 252 KB

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