ninl.pas 251 KB

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