ninl.pas 252 KB

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