trans-intrinsic.c 282 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543
  1. /* Intrinsic translation
  2. Copyright (C) 2002-2015 Free Software Foundation, Inc.
  3. Contributed by Paul Brook <paul@nowt.org>
  4. and Steven Bosscher <s.bosscher@student.tudelft.nl>
  5. This file is part of GCC.
  6. GCC is free software; you can redistribute it and/or modify it under
  7. the terms of the GNU General Public License as published by the Free
  8. Software Foundation; either version 3, or (at your option) any later
  9. version.
  10. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with GCC; see the file COPYING3. If not see
  16. <http://www.gnu.org/licenses/>. */
  17. /* trans-intrinsic.c-- generate GENERIC trees for calls to intrinsics. */
  18. #include "config.h"
  19. #include "system.h"
  20. #include "coretypes.h"
  21. #include "tm.h" /* For UNITS_PER_WORD. */
  22. #include "hash-set.h"
  23. #include "machmode.h"
  24. #include "vec.h"
  25. #include "double-int.h"
  26. #include "input.h"
  27. #include "alias.h"
  28. #include "symtab.h"
  29. #include "wide-int.h"
  30. #include "inchash.h"
  31. #include "real.h"
  32. #include "tree.h"
  33. #include "fold-const.h"
  34. #include "stringpool.h"
  35. #include "tree-nested.h"
  36. #include "stor-layout.h"
  37. #include "ggc.h"
  38. #include "gfortran.h"
  39. #include "diagnostic-core.h" /* For internal_error. */
  40. #include "toplev.h" /* For rest_of_decl_compilation. */
  41. #include "flags.h"
  42. #include "arith.h"
  43. #include "intrinsic.h"
  44. #include "trans.h"
  45. #include "trans-const.h"
  46. #include "trans-types.h"
  47. #include "trans-array.h"
  48. #include "dependency.h" /* For CAF array alias analysis. */
  49. /* Only for gfc_trans_assign and gfc_trans_pointer_assign. */
  50. #include "trans-stmt.h"
  51. #include "tree-nested.h"
  52. /* This maps Fortran intrinsic math functions to external library or GCC
  53. builtin functions. */
  54. typedef struct GTY(()) gfc_intrinsic_map_t {
  55. /* The explicit enum is required to work around inadequacies in the
  56. garbage collection/gengtype parsing mechanism. */
  57. enum gfc_isym_id id;
  58. /* Enum value from the "language-independent", aka C-centric, part
  59. of gcc, or END_BUILTINS of no such value set. */
  60. enum built_in_function float_built_in;
  61. enum built_in_function double_built_in;
  62. enum built_in_function long_double_built_in;
  63. enum built_in_function complex_float_built_in;
  64. enum built_in_function complex_double_built_in;
  65. enum built_in_function complex_long_double_built_in;
  66. /* True if the naming pattern is to prepend "c" for complex and
  67. append "f" for kind=4. False if the naming pattern is to
  68. prepend "_gfortran_" and append "[rc](4|8|10|16)". */
  69. bool libm_name;
  70. /* True if a complex version of the function exists. */
  71. bool complex_available;
  72. /* True if the function should be marked const. */
  73. bool is_constant;
  74. /* The base library name of this function. */
  75. const char *name;
  76. /* Cache decls created for the various operand types. */
  77. tree real4_decl;
  78. tree real8_decl;
  79. tree real10_decl;
  80. tree real16_decl;
  81. tree complex4_decl;
  82. tree complex8_decl;
  83. tree complex10_decl;
  84. tree complex16_decl;
  85. }
  86. gfc_intrinsic_map_t;
  87. /* ??? The NARGS==1 hack here is based on the fact that (c99 at least)
  88. defines complex variants of all of the entries in mathbuiltins.def
  89. except for atan2. */
  90. #define DEFINE_MATH_BUILTIN(ID, NAME, ARGTYPE) \
  91. { GFC_ISYM_ ## ID, BUILT_IN_ ## ID ## F, BUILT_IN_ ## ID, \
  92. BUILT_IN_ ## ID ## L, END_BUILTINS, END_BUILTINS, END_BUILTINS, \
  93. true, false, true, NAME, NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE, \
  94. NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE},
  95. #define DEFINE_MATH_BUILTIN_C(ID, NAME, ARGTYPE) \
  96. { GFC_ISYM_ ## ID, BUILT_IN_ ## ID ## F, BUILT_IN_ ## ID, \
  97. BUILT_IN_ ## ID ## L, BUILT_IN_C ## ID ## F, BUILT_IN_C ## ID, \
  98. BUILT_IN_C ## ID ## L, true, true, true, NAME, NULL_TREE, NULL_TREE, \
  99. NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE},
  100. #define LIB_FUNCTION(ID, NAME, HAVE_COMPLEX) \
  101. { GFC_ISYM_ ## ID, END_BUILTINS, END_BUILTINS, END_BUILTINS, \
  102. END_BUILTINS, END_BUILTINS, END_BUILTINS, \
  103. false, HAVE_COMPLEX, true, NAME, NULL_TREE, NULL_TREE, NULL_TREE, \
  104. NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE }
  105. #define OTHER_BUILTIN(ID, NAME, TYPE, CONST) \
  106. { GFC_ISYM_NONE, BUILT_IN_ ## ID ## F, BUILT_IN_ ## ID, \
  107. BUILT_IN_ ## ID ## L, END_BUILTINS, END_BUILTINS, END_BUILTINS, \
  108. true, false, CONST, NAME, NULL_TREE, NULL_TREE, \
  109. NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE},
  110. static GTY(()) gfc_intrinsic_map_t gfc_intrinsic_map[] =
  111. {
  112. /* Functions built into gcc itself (DEFINE_MATH_BUILTIN and
  113. DEFINE_MATH_BUILTIN_C), then the built-ins that don't correspond
  114. to any GFC_ISYM id directly, which use the OTHER_BUILTIN macro. */
  115. #include "mathbuiltins.def"
  116. /* Functions in libgfortran. */
  117. LIB_FUNCTION (ERFC_SCALED, "erfc_scaled", false),
  118. /* End the list. */
  119. LIB_FUNCTION (NONE, NULL, false)
  120. };
  121. #undef OTHER_BUILTIN
  122. #undef LIB_FUNCTION
  123. #undef DEFINE_MATH_BUILTIN
  124. #undef DEFINE_MATH_BUILTIN_C
  125. enum rounding_mode { RND_ROUND, RND_TRUNC, RND_CEIL, RND_FLOOR };
  126. /* Find the correct variant of a given builtin from its argument. */
  127. static tree
  128. builtin_decl_for_precision (enum built_in_function base_built_in,
  129. int precision)
  130. {
  131. enum built_in_function i = END_BUILTINS;
  132. gfc_intrinsic_map_t *m;
  133. for (m = gfc_intrinsic_map; m->double_built_in != base_built_in ; m++)
  134. ;
  135. if (precision == TYPE_PRECISION (float_type_node))
  136. i = m->float_built_in;
  137. else if (precision == TYPE_PRECISION (double_type_node))
  138. i = m->double_built_in;
  139. else if (precision == TYPE_PRECISION (long_double_type_node))
  140. i = m->long_double_built_in;
  141. else if (precision == TYPE_PRECISION (float128_type_node))
  142. {
  143. /* Special treatment, because it is not exactly a built-in, but
  144. a library function. */
  145. return m->real16_decl;
  146. }
  147. return (i == END_BUILTINS ? NULL_TREE : builtin_decl_explicit (i));
  148. }
  149. tree
  150. gfc_builtin_decl_for_float_kind (enum built_in_function double_built_in,
  151. int kind)
  152. {
  153. int i = gfc_validate_kind (BT_REAL, kind, false);
  154. if (gfc_real_kinds[i].c_float128)
  155. {
  156. /* For __float128, the story is a bit different, because we return
  157. a decl to a library function rather than a built-in. */
  158. gfc_intrinsic_map_t *m;
  159. for (m = gfc_intrinsic_map; m->double_built_in != double_built_in ; m++)
  160. ;
  161. return m->real16_decl;
  162. }
  163. return builtin_decl_for_precision (double_built_in,
  164. gfc_real_kinds[i].mode_precision);
  165. }
  166. /* Evaluate the arguments to an intrinsic function. The value
  167. of NARGS may be less than the actual number of arguments in EXPR
  168. to allow optional "KIND" arguments that are not included in the
  169. generated code to be ignored. */
  170. static void
  171. gfc_conv_intrinsic_function_args (gfc_se *se, gfc_expr *expr,
  172. tree *argarray, int nargs)
  173. {
  174. gfc_actual_arglist *actual;
  175. gfc_expr *e;
  176. gfc_intrinsic_arg *formal;
  177. gfc_se argse;
  178. int curr_arg;
  179. formal = expr->value.function.isym->formal;
  180. actual = expr->value.function.actual;
  181. for (curr_arg = 0; curr_arg < nargs; curr_arg++,
  182. actual = actual->next,
  183. formal = formal ? formal->next : NULL)
  184. {
  185. gcc_assert (actual);
  186. e = actual->expr;
  187. /* Skip omitted optional arguments. */
  188. if (!e)
  189. {
  190. --curr_arg;
  191. continue;
  192. }
  193. /* Evaluate the parameter. This will substitute scalarized
  194. references automatically. */
  195. gfc_init_se (&argse, se);
  196. if (e->ts.type == BT_CHARACTER)
  197. {
  198. gfc_conv_expr (&argse, e);
  199. gfc_conv_string_parameter (&argse);
  200. argarray[curr_arg++] = argse.string_length;
  201. gcc_assert (curr_arg < nargs);
  202. }
  203. else
  204. gfc_conv_expr_val (&argse, e);
  205. /* If an optional argument is itself an optional dummy argument,
  206. check its presence and substitute a null if absent. */
  207. if (e->expr_type == EXPR_VARIABLE
  208. && e->symtree->n.sym->attr.optional
  209. && formal
  210. && formal->optional)
  211. gfc_conv_missing_dummy (&argse, e, formal->ts, 0);
  212. gfc_add_block_to_block (&se->pre, &argse.pre);
  213. gfc_add_block_to_block (&se->post, &argse.post);
  214. argarray[curr_arg] = argse.expr;
  215. }
  216. }
  217. /* Count the number of actual arguments to the intrinsic function EXPR
  218. including any "hidden" string length arguments. */
  219. static unsigned int
  220. gfc_intrinsic_argument_list_length (gfc_expr *expr)
  221. {
  222. int n = 0;
  223. gfc_actual_arglist *actual;
  224. for (actual = expr->value.function.actual; actual; actual = actual->next)
  225. {
  226. if (!actual->expr)
  227. continue;
  228. if (actual->expr->ts.type == BT_CHARACTER)
  229. n += 2;
  230. else
  231. n++;
  232. }
  233. return n;
  234. }
  235. /* Conversions between different types are output by the frontend as
  236. intrinsic functions. We implement these directly with inline code. */
  237. static void
  238. gfc_conv_intrinsic_conversion (gfc_se * se, gfc_expr * expr)
  239. {
  240. tree type;
  241. tree *args;
  242. int nargs;
  243. nargs = gfc_intrinsic_argument_list_length (expr);
  244. args = XALLOCAVEC (tree, nargs);
  245. /* Evaluate all the arguments passed. Whilst we're only interested in the
  246. first one here, there are other parts of the front-end that assume this
  247. and will trigger an ICE if it's not the case. */
  248. type = gfc_typenode_for_spec (&expr->ts);
  249. gcc_assert (expr->value.function.actual->expr);
  250. gfc_conv_intrinsic_function_args (se, expr, args, nargs);
  251. /* Conversion between character kinds involves a call to a library
  252. function. */
  253. if (expr->ts.type == BT_CHARACTER)
  254. {
  255. tree fndecl, var, addr, tmp;
  256. if (expr->ts.kind == 1
  257. && expr->value.function.actual->expr->ts.kind == 4)
  258. fndecl = gfor_fndecl_convert_char4_to_char1;
  259. else if (expr->ts.kind == 4
  260. && expr->value.function.actual->expr->ts.kind == 1)
  261. fndecl = gfor_fndecl_convert_char1_to_char4;
  262. else
  263. gcc_unreachable ();
  264. /* Create the variable storing the converted value. */
  265. type = gfc_get_pchar_type (expr->ts.kind);
  266. var = gfc_create_var (type, "str");
  267. addr = gfc_build_addr_expr (build_pointer_type (type), var);
  268. /* Call the library function that will perform the conversion. */
  269. gcc_assert (nargs >= 2);
  270. tmp = build_call_expr_loc (input_location,
  271. fndecl, 3, addr, args[0], args[1]);
  272. gfc_add_expr_to_block (&se->pre, tmp);
  273. /* Free the temporary afterwards. */
  274. tmp = gfc_call_free (var);
  275. gfc_add_expr_to_block (&se->post, tmp);
  276. se->expr = var;
  277. se->string_length = args[0];
  278. return;
  279. }
  280. /* Conversion from complex to non-complex involves taking the real
  281. component of the value. */
  282. if (TREE_CODE (TREE_TYPE (args[0])) == COMPLEX_TYPE
  283. && expr->ts.type != BT_COMPLEX)
  284. {
  285. tree artype;
  286. artype = TREE_TYPE (TREE_TYPE (args[0]));
  287. args[0] = fold_build1_loc (input_location, REALPART_EXPR, artype,
  288. args[0]);
  289. }
  290. se->expr = convert (type, args[0]);
  291. }
  292. /* This is needed because the gcc backend only implements
  293. FIX_TRUNC_EXPR, which is the same as INT() in Fortran.
  294. FLOOR(x) = INT(x) <= x ? INT(x) : INT(x) - 1
  295. Similarly for CEILING. */
  296. static tree
  297. build_fixbound_expr (stmtblock_t * pblock, tree arg, tree type, int up)
  298. {
  299. tree tmp;
  300. tree cond;
  301. tree argtype;
  302. tree intval;
  303. argtype = TREE_TYPE (arg);
  304. arg = gfc_evaluate_now (arg, pblock);
  305. intval = convert (type, arg);
  306. intval = gfc_evaluate_now (intval, pblock);
  307. tmp = convert (argtype, intval);
  308. cond = fold_build2_loc (input_location, up ? GE_EXPR : LE_EXPR,
  309. boolean_type_node, tmp, arg);
  310. tmp = fold_build2_loc (input_location, up ? PLUS_EXPR : MINUS_EXPR, type,
  311. intval, build_int_cst (type, 1));
  312. tmp = fold_build3_loc (input_location, COND_EXPR, type, cond, intval, tmp);
  313. return tmp;
  314. }
  315. /* Round to nearest integer, away from zero. */
  316. static tree
  317. build_round_expr (tree arg, tree restype)
  318. {
  319. tree argtype;
  320. tree fn;
  321. int argprec, resprec;
  322. argtype = TREE_TYPE (arg);
  323. argprec = TYPE_PRECISION (argtype);
  324. resprec = TYPE_PRECISION (restype);
  325. /* Depending on the type of the result, choose the int intrinsic
  326. (iround, available only as a builtin, therefore cannot use it for
  327. __float128), long int intrinsic (lround family) or long long
  328. intrinsic (llround). We might also need to convert the result
  329. afterwards. */
  330. if (resprec <= INT_TYPE_SIZE && argprec <= LONG_DOUBLE_TYPE_SIZE)
  331. fn = builtin_decl_for_precision (BUILT_IN_IROUND, argprec);
  332. else if (resprec <= LONG_TYPE_SIZE)
  333. fn = builtin_decl_for_precision (BUILT_IN_LROUND, argprec);
  334. else if (resprec <= LONG_LONG_TYPE_SIZE)
  335. fn = builtin_decl_for_precision (BUILT_IN_LLROUND, argprec);
  336. else
  337. gcc_unreachable ();
  338. return fold_convert (restype, build_call_expr_loc (input_location,
  339. fn, 1, arg));
  340. }
  341. /* Convert a real to an integer using a specific rounding mode.
  342. Ideally we would just build the corresponding GENERIC node,
  343. however the RTL expander only actually supports FIX_TRUNC_EXPR. */
  344. static tree
  345. build_fix_expr (stmtblock_t * pblock, tree arg, tree type,
  346. enum rounding_mode op)
  347. {
  348. switch (op)
  349. {
  350. case RND_FLOOR:
  351. return build_fixbound_expr (pblock, arg, type, 0);
  352. break;
  353. case RND_CEIL:
  354. return build_fixbound_expr (pblock, arg, type, 1);
  355. break;
  356. case RND_ROUND:
  357. return build_round_expr (arg, type);
  358. break;
  359. case RND_TRUNC:
  360. return fold_build1_loc (input_location, FIX_TRUNC_EXPR, type, arg);
  361. break;
  362. default:
  363. gcc_unreachable ();
  364. }
  365. }
  366. /* Round a real value using the specified rounding mode.
  367. We use a temporary integer of that same kind size as the result.
  368. Values larger than those that can be represented by this kind are
  369. unchanged, as they will not be accurate enough to represent the
  370. rounding.
  371. huge = HUGE (KIND (a))
  372. aint (a) = ((a > huge) || (a < -huge)) ? a : (real)(int)a
  373. */
  374. static void
  375. gfc_conv_intrinsic_aint (gfc_se * se, gfc_expr * expr, enum rounding_mode op)
  376. {
  377. tree type;
  378. tree itype;
  379. tree arg[2];
  380. tree tmp;
  381. tree cond;
  382. tree decl;
  383. mpfr_t huge;
  384. int n, nargs;
  385. int kind;
  386. kind = expr->ts.kind;
  387. nargs = gfc_intrinsic_argument_list_length (expr);
  388. decl = NULL_TREE;
  389. /* We have builtin functions for some cases. */
  390. switch (op)
  391. {
  392. case RND_ROUND:
  393. decl = gfc_builtin_decl_for_float_kind (BUILT_IN_ROUND, kind);
  394. break;
  395. case RND_TRUNC:
  396. decl = gfc_builtin_decl_for_float_kind (BUILT_IN_TRUNC, kind);
  397. break;
  398. default:
  399. gcc_unreachable ();
  400. }
  401. /* Evaluate the argument. */
  402. gcc_assert (expr->value.function.actual->expr);
  403. gfc_conv_intrinsic_function_args (se, expr, arg, nargs);
  404. /* Use a builtin function if one exists. */
  405. if (decl != NULL_TREE)
  406. {
  407. se->expr = build_call_expr_loc (input_location, decl, 1, arg[0]);
  408. return;
  409. }
  410. /* This code is probably redundant, but we'll keep it lying around just
  411. in case. */
  412. type = gfc_typenode_for_spec (&expr->ts);
  413. arg[0] = gfc_evaluate_now (arg[0], &se->pre);
  414. /* Test if the value is too large to handle sensibly. */
  415. gfc_set_model_kind (kind);
  416. mpfr_init (huge);
  417. n = gfc_validate_kind (BT_INTEGER, kind, false);
  418. mpfr_set_z (huge, gfc_integer_kinds[n].huge, GFC_RND_MODE);
  419. tmp = gfc_conv_mpfr_to_tree (huge, kind, 0);
  420. cond = fold_build2_loc (input_location, LT_EXPR, boolean_type_node, arg[0],
  421. tmp);
  422. mpfr_neg (huge, huge, GFC_RND_MODE);
  423. tmp = gfc_conv_mpfr_to_tree (huge, kind, 0);
  424. tmp = fold_build2_loc (input_location, GT_EXPR, boolean_type_node, arg[0],
  425. tmp);
  426. cond = fold_build2_loc (input_location, TRUTH_AND_EXPR, boolean_type_node,
  427. cond, tmp);
  428. itype = gfc_get_int_type (kind);
  429. tmp = build_fix_expr (&se->pre, arg[0], itype, op);
  430. tmp = convert (type, tmp);
  431. se->expr = fold_build3_loc (input_location, COND_EXPR, type, cond, tmp,
  432. arg[0]);
  433. mpfr_clear (huge);
  434. }
  435. /* Convert to an integer using the specified rounding mode. */
  436. static void
  437. gfc_conv_intrinsic_int (gfc_se * se, gfc_expr * expr, enum rounding_mode op)
  438. {
  439. tree type;
  440. tree *args;
  441. int nargs;
  442. nargs = gfc_intrinsic_argument_list_length (expr);
  443. args = XALLOCAVEC (tree, nargs);
  444. /* Evaluate the argument, we process all arguments even though we only
  445. use the first one for code generation purposes. */
  446. type = gfc_typenode_for_spec (&expr->ts);
  447. gcc_assert (expr->value.function.actual->expr);
  448. gfc_conv_intrinsic_function_args (se, expr, args, nargs);
  449. if (TREE_CODE (TREE_TYPE (args[0])) == INTEGER_TYPE)
  450. {
  451. /* Conversion to a different integer kind. */
  452. se->expr = convert (type, args[0]);
  453. }
  454. else
  455. {
  456. /* Conversion from complex to non-complex involves taking the real
  457. component of the value. */
  458. if (TREE_CODE (TREE_TYPE (args[0])) == COMPLEX_TYPE
  459. && expr->ts.type != BT_COMPLEX)
  460. {
  461. tree artype;
  462. artype = TREE_TYPE (TREE_TYPE (args[0]));
  463. args[0] = fold_build1_loc (input_location, REALPART_EXPR, artype,
  464. args[0]);
  465. }
  466. se->expr = build_fix_expr (&se->pre, args[0], type, op);
  467. }
  468. }
  469. /* Get the imaginary component of a value. */
  470. static void
  471. gfc_conv_intrinsic_imagpart (gfc_se * se, gfc_expr * expr)
  472. {
  473. tree arg;
  474. gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
  475. se->expr = fold_build1_loc (input_location, IMAGPART_EXPR,
  476. TREE_TYPE (TREE_TYPE (arg)), arg);
  477. }
  478. /* Get the complex conjugate of a value. */
  479. static void
  480. gfc_conv_intrinsic_conjg (gfc_se * se, gfc_expr * expr)
  481. {
  482. tree arg;
  483. gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
  484. se->expr = fold_build1_loc (input_location, CONJ_EXPR, TREE_TYPE (arg), arg);
  485. }
  486. static tree
  487. define_quad_builtin (const char *name, tree type, bool is_const)
  488. {
  489. tree fndecl;
  490. fndecl = build_decl (input_location, FUNCTION_DECL, get_identifier (name),
  491. type);
  492. /* Mark the decl as external. */
  493. DECL_EXTERNAL (fndecl) = 1;
  494. TREE_PUBLIC (fndecl) = 1;
  495. /* Mark it __attribute__((const)). */
  496. TREE_READONLY (fndecl) = is_const;
  497. rest_of_decl_compilation (fndecl, 1, 0);
  498. return fndecl;
  499. }
  500. /* Initialize function decls for library functions. The external functions
  501. are created as required. Builtin functions are added here. */
  502. void
  503. gfc_build_intrinsic_lib_fndecls (void)
  504. {
  505. gfc_intrinsic_map_t *m;
  506. tree quad_decls[END_BUILTINS + 1];
  507. if (gfc_real16_is_float128)
  508. {
  509. /* If we have soft-float types, we create the decls for their
  510. C99-like library functions. For now, we only handle __float128
  511. q-suffixed functions. */
  512. tree type, complex_type, func_1, func_2, func_cabs, func_frexp;
  513. tree func_iround, func_lround, func_llround, func_scalbn, func_cpow;
  514. memset (quad_decls, 0, sizeof(tree) * (END_BUILTINS + 1));
  515. type = float128_type_node;
  516. complex_type = complex_float128_type_node;
  517. /* type (*) (type) */
  518. func_1 = build_function_type_list (type, type, NULL_TREE);
  519. /* int (*) (type) */
  520. func_iround = build_function_type_list (integer_type_node,
  521. type, NULL_TREE);
  522. /* long (*) (type) */
  523. func_lround = build_function_type_list (long_integer_type_node,
  524. type, NULL_TREE);
  525. /* long long (*) (type) */
  526. func_llround = build_function_type_list (long_long_integer_type_node,
  527. type, NULL_TREE);
  528. /* type (*) (type, type) */
  529. func_2 = build_function_type_list (type, type, type, NULL_TREE);
  530. /* type (*) (type, &int) */
  531. func_frexp
  532. = build_function_type_list (type,
  533. type,
  534. build_pointer_type (integer_type_node),
  535. NULL_TREE);
  536. /* type (*) (type, int) */
  537. func_scalbn = build_function_type_list (type,
  538. type, integer_type_node, NULL_TREE);
  539. /* type (*) (complex type) */
  540. func_cabs = build_function_type_list (type, complex_type, NULL_TREE);
  541. /* complex type (*) (complex type, complex type) */
  542. func_cpow
  543. = build_function_type_list (complex_type,
  544. complex_type, complex_type, NULL_TREE);
  545. #define DEFINE_MATH_BUILTIN(ID, NAME, ARGTYPE)
  546. #define DEFINE_MATH_BUILTIN_C(ID, NAME, ARGTYPE)
  547. #define LIB_FUNCTION(ID, NAME, HAVE_COMPLEX)
  548. /* Only these built-ins are actually needed here. These are used directly
  549. from the code, when calling builtin_decl_for_precision() or
  550. builtin_decl_for_float_type(). The others are all constructed by
  551. gfc_get_intrinsic_lib_fndecl(). */
  552. #define OTHER_BUILTIN(ID, NAME, TYPE, CONST) \
  553. quad_decls[BUILT_IN_ ## ID] = define_quad_builtin (NAME "q", func_ ## TYPE, CONST);
  554. #include "mathbuiltins.def"
  555. #undef OTHER_BUILTIN
  556. #undef LIB_FUNCTION
  557. #undef DEFINE_MATH_BUILTIN
  558. #undef DEFINE_MATH_BUILTIN_C
  559. }
  560. /* Add GCC builtin functions. */
  561. for (m = gfc_intrinsic_map;
  562. m->id != GFC_ISYM_NONE || m->double_built_in != END_BUILTINS; m++)
  563. {
  564. if (m->float_built_in != END_BUILTINS)
  565. m->real4_decl = builtin_decl_explicit (m->float_built_in);
  566. if (m->complex_float_built_in != END_BUILTINS)
  567. m->complex4_decl = builtin_decl_explicit (m->complex_float_built_in);
  568. if (m->double_built_in != END_BUILTINS)
  569. m->real8_decl = builtin_decl_explicit (m->double_built_in);
  570. if (m->complex_double_built_in != END_BUILTINS)
  571. m->complex8_decl = builtin_decl_explicit (m->complex_double_built_in);
  572. /* If real(kind=10) exists, it is always long double. */
  573. if (m->long_double_built_in != END_BUILTINS)
  574. m->real10_decl = builtin_decl_explicit (m->long_double_built_in);
  575. if (m->complex_long_double_built_in != END_BUILTINS)
  576. m->complex10_decl
  577. = builtin_decl_explicit (m->complex_long_double_built_in);
  578. if (!gfc_real16_is_float128)
  579. {
  580. if (m->long_double_built_in != END_BUILTINS)
  581. m->real16_decl = builtin_decl_explicit (m->long_double_built_in);
  582. if (m->complex_long_double_built_in != END_BUILTINS)
  583. m->complex16_decl
  584. = builtin_decl_explicit (m->complex_long_double_built_in);
  585. }
  586. else if (quad_decls[m->double_built_in] != NULL_TREE)
  587. {
  588. /* Quad-precision function calls are constructed when first
  589. needed by builtin_decl_for_precision(), except for those
  590. that will be used directly (define by OTHER_BUILTIN). */
  591. m->real16_decl = quad_decls[m->double_built_in];
  592. }
  593. else if (quad_decls[m->complex_double_built_in] != NULL_TREE)
  594. {
  595. /* Same thing for the complex ones. */
  596. m->complex16_decl = quad_decls[m->double_built_in];
  597. }
  598. }
  599. }
  600. /* Create a fndecl for a simple intrinsic library function. */
  601. static tree
  602. gfc_get_intrinsic_lib_fndecl (gfc_intrinsic_map_t * m, gfc_expr * expr)
  603. {
  604. tree type;
  605. vec<tree, va_gc> *argtypes;
  606. tree fndecl;
  607. gfc_actual_arglist *actual;
  608. tree *pdecl;
  609. gfc_typespec *ts;
  610. char name[GFC_MAX_SYMBOL_LEN + 3];
  611. ts = &expr->ts;
  612. if (ts->type == BT_REAL)
  613. {
  614. switch (ts->kind)
  615. {
  616. case 4:
  617. pdecl = &m->real4_decl;
  618. break;
  619. case 8:
  620. pdecl = &m->real8_decl;
  621. break;
  622. case 10:
  623. pdecl = &m->real10_decl;
  624. break;
  625. case 16:
  626. pdecl = &m->real16_decl;
  627. break;
  628. default:
  629. gcc_unreachable ();
  630. }
  631. }
  632. else if (ts->type == BT_COMPLEX)
  633. {
  634. gcc_assert (m->complex_available);
  635. switch (ts->kind)
  636. {
  637. case 4:
  638. pdecl = &m->complex4_decl;
  639. break;
  640. case 8:
  641. pdecl = &m->complex8_decl;
  642. break;
  643. case 10:
  644. pdecl = &m->complex10_decl;
  645. break;
  646. case 16:
  647. pdecl = &m->complex16_decl;
  648. break;
  649. default:
  650. gcc_unreachable ();
  651. }
  652. }
  653. else
  654. gcc_unreachable ();
  655. if (*pdecl)
  656. return *pdecl;
  657. if (m->libm_name)
  658. {
  659. int n = gfc_validate_kind (BT_REAL, ts->kind, false);
  660. if (gfc_real_kinds[n].c_float)
  661. snprintf (name, sizeof (name), "%s%s%s",
  662. ts->type == BT_COMPLEX ? "c" : "", m->name, "f");
  663. else if (gfc_real_kinds[n].c_double)
  664. snprintf (name, sizeof (name), "%s%s",
  665. ts->type == BT_COMPLEX ? "c" : "", m->name);
  666. else if (gfc_real_kinds[n].c_long_double)
  667. snprintf (name, sizeof (name), "%s%s%s",
  668. ts->type == BT_COMPLEX ? "c" : "", m->name, "l");
  669. else if (gfc_real_kinds[n].c_float128)
  670. snprintf (name, sizeof (name), "%s%s%s",
  671. ts->type == BT_COMPLEX ? "c" : "", m->name, "q");
  672. else
  673. gcc_unreachable ();
  674. }
  675. else
  676. {
  677. snprintf (name, sizeof (name), PREFIX ("%s_%c%d"), m->name,
  678. ts->type == BT_COMPLEX ? 'c' : 'r',
  679. ts->kind);
  680. }
  681. argtypes = NULL;
  682. for (actual = expr->value.function.actual; actual; actual = actual->next)
  683. {
  684. type = gfc_typenode_for_spec (&actual->expr->ts);
  685. vec_safe_push (argtypes, type);
  686. }
  687. type = build_function_type_vec (gfc_typenode_for_spec (ts), argtypes);
  688. fndecl = build_decl (input_location,
  689. FUNCTION_DECL, get_identifier (name), type);
  690. /* Mark the decl as external. */
  691. DECL_EXTERNAL (fndecl) = 1;
  692. TREE_PUBLIC (fndecl) = 1;
  693. /* Mark it __attribute__((const)), if possible. */
  694. TREE_READONLY (fndecl) = m->is_constant;
  695. rest_of_decl_compilation (fndecl, 1, 0);
  696. (*pdecl) = fndecl;
  697. return fndecl;
  698. }
  699. /* Convert an intrinsic function into an external or builtin call. */
  700. static void
  701. gfc_conv_intrinsic_lib_function (gfc_se * se, gfc_expr * expr)
  702. {
  703. gfc_intrinsic_map_t *m;
  704. tree fndecl;
  705. tree rettype;
  706. tree *args;
  707. unsigned int num_args;
  708. gfc_isym_id id;
  709. id = expr->value.function.isym->id;
  710. /* Find the entry for this function. */
  711. for (m = gfc_intrinsic_map;
  712. m->id != GFC_ISYM_NONE || m->double_built_in != END_BUILTINS; m++)
  713. {
  714. if (id == m->id)
  715. break;
  716. }
  717. if (m->id == GFC_ISYM_NONE)
  718. {
  719. gfc_internal_error ("Intrinsic function %qs (%d) not recognized",
  720. expr->value.function.name, id);
  721. }
  722. /* Get the decl and generate the call. */
  723. num_args = gfc_intrinsic_argument_list_length (expr);
  724. args = XALLOCAVEC (tree, num_args);
  725. gfc_conv_intrinsic_function_args (se, expr, args, num_args);
  726. fndecl = gfc_get_intrinsic_lib_fndecl (m, expr);
  727. rettype = TREE_TYPE (TREE_TYPE (fndecl));
  728. fndecl = build_addr (fndecl, current_function_decl);
  729. se->expr = build_call_array_loc (input_location, rettype, fndecl, num_args, args);
  730. }
  731. /* If bounds-checking is enabled, create code to verify at runtime that the
  732. string lengths for both expressions are the same (needed for e.g. MERGE).
  733. If bounds-checking is not enabled, does nothing. */
  734. void
  735. gfc_trans_same_strlen_check (const char* intr_name, locus* where,
  736. tree a, tree b, stmtblock_t* target)
  737. {
  738. tree cond;
  739. tree name;
  740. /* If bounds-checking is disabled, do nothing. */
  741. if (!(gfc_option.rtcheck & GFC_RTCHECK_BOUNDS))
  742. return;
  743. /* Compare the two string lengths. */
  744. cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, a, b);
  745. /* Output the runtime-check. */
  746. name = gfc_build_cstring_const (intr_name);
  747. name = gfc_build_addr_expr (pchar_type_node, name);
  748. gfc_trans_runtime_check (true, false, cond, target, where,
  749. "Unequal character lengths (%ld/%ld) in %s",
  750. fold_convert (long_integer_type_node, a),
  751. fold_convert (long_integer_type_node, b), name);
  752. }
  753. /* The EXPONENT(X) intrinsic function is translated into
  754. int ret;
  755. return isfinite(X) ? (frexp (X, &ret) , ret) : huge
  756. so that if X is a NaN or infinity, the result is HUGE(0).
  757. */
  758. static void
  759. gfc_conv_intrinsic_exponent (gfc_se *se, gfc_expr *expr)
  760. {
  761. tree arg, type, res, tmp, frexp, cond, huge;
  762. int i;
  763. frexp = gfc_builtin_decl_for_float_kind (BUILT_IN_FREXP,
  764. expr->value.function.actual->expr->ts.kind);
  765. gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
  766. arg = gfc_evaluate_now (arg, &se->pre);
  767. i = gfc_validate_kind (BT_INTEGER, gfc_c_int_kind, false);
  768. huge = gfc_conv_mpz_to_tree (gfc_integer_kinds[i].huge, gfc_c_int_kind);
  769. cond = build_call_expr_loc (input_location,
  770. builtin_decl_explicit (BUILT_IN_ISFINITE),
  771. 1, arg);
  772. res = gfc_create_var (integer_type_node, NULL);
  773. tmp = build_call_expr_loc (input_location, frexp, 2, arg,
  774. gfc_build_addr_expr (NULL_TREE, res));
  775. tmp = fold_build2_loc (input_location, COMPOUND_EXPR, integer_type_node,
  776. tmp, res);
  777. se->expr = fold_build3_loc (input_location, COND_EXPR, integer_type_node,
  778. cond, tmp, huge);
  779. type = gfc_typenode_for_spec (&expr->ts);
  780. se->expr = fold_convert (type, se->expr);
  781. }
  782. /* Fill in the following structure
  783. struct caf_vector_t {
  784. size_t nvec; // size of the vector
  785. union {
  786. struct {
  787. void *vector;
  788. int kind;
  789. } v;
  790. struct {
  791. ptrdiff_t lower_bound;
  792. ptrdiff_t upper_bound;
  793. ptrdiff_t stride;
  794. } triplet;
  795. } u;
  796. } */
  797. static void
  798. conv_caf_vector_subscript_elem (stmtblock_t *block, int i, tree desc,
  799. tree lower, tree upper, tree stride,
  800. tree vector, int kind, tree nvec)
  801. {
  802. tree field, type, tmp;
  803. desc = gfc_build_array_ref (desc, gfc_rank_cst[i], NULL_TREE);
  804. type = TREE_TYPE (desc);
  805. field = gfc_advance_chain (TYPE_FIELDS (type), 0);
  806. tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
  807. desc, field, NULL_TREE);
  808. gfc_add_modify (block, tmp, fold_convert (TREE_TYPE (field), nvec));
  809. /* Access union. */
  810. field = gfc_advance_chain (TYPE_FIELDS (type), 1);
  811. desc = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
  812. desc, field, NULL_TREE);
  813. type = TREE_TYPE (desc);
  814. /* Access the inner struct. */
  815. field = gfc_advance_chain (TYPE_FIELDS (type), vector != NULL_TREE ? 0 : 1);
  816. desc = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
  817. desc, field, NULL_TREE);
  818. type = TREE_TYPE (desc);
  819. if (vector != NULL_TREE)
  820. {
  821. /* Set dim.lower/upper/stride. */
  822. field = gfc_advance_chain (TYPE_FIELDS (type), 0);
  823. tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
  824. desc, field, NULL_TREE);
  825. gfc_add_modify (block, tmp, fold_convert (TREE_TYPE (field), vector));
  826. field = gfc_advance_chain (TYPE_FIELDS (type), 1);
  827. tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
  828. desc, field, NULL_TREE);
  829. gfc_add_modify (block, tmp, build_int_cst (integer_type_node, kind));
  830. }
  831. else
  832. {
  833. /* Set vector and kind. */
  834. field = gfc_advance_chain (TYPE_FIELDS (type), 0);
  835. tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
  836. desc, field, NULL_TREE);
  837. gfc_add_modify (block, tmp, fold_convert (TREE_TYPE (field), lower));
  838. field = gfc_advance_chain (TYPE_FIELDS (type), 1);
  839. tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
  840. desc, field, NULL_TREE);
  841. gfc_add_modify (block, tmp, fold_convert (TREE_TYPE (field), upper));
  842. field = gfc_advance_chain (TYPE_FIELDS (type), 2);
  843. tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
  844. desc, field, NULL_TREE);
  845. gfc_add_modify (block, tmp, fold_convert (TREE_TYPE (field), stride));
  846. }
  847. }
  848. static tree
  849. conv_caf_vector_subscript (stmtblock_t *block, tree desc, gfc_array_ref *ar)
  850. {
  851. gfc_se argse;
  852. tree var, lower, upper = NULL_TREE, stride = NULL_TREE, vector, nvec;
  853. tree lbound, ubound, tmp;
  854. int i;
  855. var = gfc_create_var (gfc_get_caf_vector_type (ar->dimen), "vector");
  856. for (i = 0; i < ar->dimen; i++)
  857. switch (ar->dimen_type[i])
  858. {
  859. case DIMEN_RANGE:
  860. if (ar->end[i])
  861. {
  862. gfc_init_se (&argse, NULL);
  863. gfc_conv_expr (&argse, ar->end[i]);
  864. gfc_add_block_to_block (block, &argse.pre);
  865. upper = gfc_evaluate_now (argse.expr, block);
  866. }
  867. else
  868. upper = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[i]);
  869. if (ar->stride[i])
  870. {
  871. gfc_init_se (&argse, NULL);
  872. gfc_conv_expr (&argse, ar->stride[i]);
  873. gfc_add_block_to_block (block, &argse.pre);
  874. stride = gfc_evaluate_now (argse.expr, block);
  875. }
  876. else
  877. stride = gfc_index_one_node;
  878. /* Fall through. */
  879. case DIMEN_ELEMENT:
  880. if (ar->start[i])
  881. {
  882. gfc_init_se (&argse, NULL);
  883. gfc_conv_expr (&argse, ar->start[i]);
  884. gfc_add_block_to_block (block, &argse.pre);
  885. lower = gfc_evaluate_now (argse.expr, block);
  886. }
  887. else
  888. lower = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[i]);
  889. if (ar->dimen_type[i] == DIMEN_ELEMENT)
  890. {
  891. upper = lower;
  892. stride = gfc_index_one_node;
  893. }
  894. vector = NULL_TREE;
  895. nvec = size_zero_node;
  896. conv_caf_vector_subscript_elem (block, i, var, lower, upper, stride,
  897. vector, 0, nvec);
  898. break;
  899. case DIMEN_VECTOR:
  900. gfc_init_se (&argse, NULL);
  901. argse.descriptor_only = 1;
  902. gfc_conv_expr_descriptor (&argse, ar->start[i]);
  903. gfc_add_block_to_block (block, &argse.pre);
  904. vector = argse.expr;
  905. lbound = gfc_conv_descriptor_lbound_get (vector, gfc_rank_cst[0]);
  906. ubound = gfc_conv_descriptor_ubound_get (vector, gfc_rank_cst[0]);
  907. nvec = gfc_conv_array_extent_dim (lbound, ubound, NULL);
  908. tmp = gfc_conv_descriptor_stride_get (vector, gfc_rank_cst[0]);
  909. nvec = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
  910. TREE_TYPE (nvec), nvec, tmp);
  911. lower = gfc_index_zero_node;
  912. upper = gfc_index_zero_node;
  913. stride = gfc_index_zero_node;
  914. vector = gfc_conv_descriptor_data_get (vector);
  915. conv_caf_vector_subscript_elem (block, i, var, lower, upper, stride,
  916. vector, ar->start[i]->ts.kind, nvec);
  917. break;
  918. default:
  919. gcc_unreachable();
  920. }
  921. return gfc_build_addr_expr (NULL_TREE, var);
  922. }
  923. /* Get data from a remote coarray. */
  924. static void
  925. gfc_conv_intrinsic_caf_get (gfc_se *se, gfc_expr *expr, tree lhs, tree lhs_kind,
  926. tree may_require_tmp)
  927. {
  928. gfc_expr *array_expr;
  929. gfc_se argse;
  930. tree caf_decl, token, offset, image_index, tmp;
  931. tree res_var, dst_var, type, kind, vec;
  932. gcc_assert (flag_coarray == GFC_FCOARRAY_LIB);
  933. if (se->ss && se->ss->info->useflags)
  934. {
  935. /* Access the previously obtained result. */
  936. gfc_conv_tmp_array_ref (se);
  937. return;
  938. }
  939. /* If lhs is set, the CAF_GET intrinsic has already been stripped. */
  940. array_expr = (lhs == NULL_TREE) ? expr->value.function.actual->expr : expr;
  941. type = gfc_typenode_for_spec (&array_expr->ts);
  942. res_var = lhs;
  943. dst_var = lhs;
  944. vec = null_pointer_node;
  945. gfc_init_se (&argse, NULL);
  946. if (array_expr->rank == 0)
  947. {
  948. symbol_attribute attr;
  949. gfc_clear_attr (&attr);
  950. gfc_conv_expr (&argse, array_expr);
  951. if (lhs == NULL_TREE)
  952. {
  953. gfc_clear_attr (&attr);
  954. if (array_expr->ts.type == BT_CHARACTER)
  955. res_var = gfc_conv_string_tmp (se, build_pointer_type (type),
  956. argse.string_length);
  957. else
  958. res_var = gfc_create_var (type, "caf_res");
  959. dst_var = gfc_conv_scalar_to_descriptor (&argse, res_var, attr);
  960. dst_var = gfc_build_addr_expr (NULL_TREE, dst_var);
  961. }
  962. argse.expr = gfc_conv_scalar_to_descriptor (&argse, argse.expr, attr);
  963. argse.expr = gfc_build_addr_expr (NULL_TREE, argse.expr);
  964. }
  965. else
  966. {
  967. /* If has_vector, pass descriptor for whole array and the
  968. vector bounds separately. */
  969. gfc_array_ref *ar, ar2;
  970. bool has_vector = false;
  971. if (gfc_is_coindexed (expr) && gfc_has_vector_subscript (expr))
  972. {
  973. has_vector = true;
  974. ar = gfc_find_array_ref (expr);
  975. ar2 = *ar;
  976. memset (ar, '\0', sizeof (*ar));
  977. ar->as = ar2.as;
  978. ar->type = AR_FULL;
  979. }
  980. gfc_conv_expr_descriptor (&argse, array_expr);
  981. /* Using gfc_conv_expr_descriptor, we only get the descriptor, but that
  982. has the wrong type if component references are done. */
  983. gfc_add_modify (&argse.pre, gfc_conv_descriptor_dtype (argse.expr),
  984. gfc_get_dtype_rank_type (has_vector ? ar2.dimen
  985. : array_expr->rank,
  986. type));
  987. if (has_vector)
  988. {
  989. vec = conv_caf_vector_subscript (&argse.pre, argse.expr, &ar2);
  990. *ar = ar2;
  991. }
  992. if (lhs == NULL_TREE)
  993. {
  994. /* Create temporary. */
  995. for (int n = 0; n < se->ss->loop->dimen; n++)
  996. if (se->loop->to[n] == NULL_TREE)
  997. {
  998. se->loop->from[n] =
  999. gfc_conv_descriptor_lbound_get (argse.expr, gfc_rank_cst[n]);
  1000. se->loop->to[n] =
  1001. gfc_conv_descriptor_ubound_get (argse.expr, gfc_rank_cst[n]);
  1002. }
  1003. gfc_trans_create_temp_array (&argse.pre, &argse.post, se->ss, type,
  1004. NULL_TREE, false, true, false,
  1005. &array_expr->where);
  1006. res_var = se->ss->info->data.array.descriptor;
  1007. dst_var = gfc_build_addr_expr (NULL_TREE, res_var);
  1008. }
  1009. argse.expr = gfc_build_addr_expr (NULL_TREE, argse.expr);
  1010. }
  1011. kind = build_int_cst (integer_type_node, expr->ts.kind);
  1012. if (lhs_kind == NULL_TREE)
  1013. lhs_kind = kind;
  1014. gfc_add_block_to_block (&se->pre, &argse.pre);
  1015. gfc_add_block_to_block (&se->post, &argse.post);
  1016. caf_decl = gfc_get_tree_for_caf_expr (array_expr);
  1017. if (TREE_CODE (TREE_TYPE (caf_decl)) == REFERENCE_TYPE)
  1018. caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
  1019. image_index = gfc_caf_get_image_index (&se->pre, array_expr, caf_decl);
  1020. gfc_get_caf_token_offset (&token, &offset, caf_decl, argse.expr, array_expr);
  1021. /* No overlap possible as we have generated a temporary. */
  1022. if (lhs == NULL_TREE)
  1023. may_require_tmp = boolean_false_node;
  1024. tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_get, 9,
  1025. token, offset, image_index, argse.expr, vec,
  1026. dst_var, kind, lhs_kind, may_require_tmp);
  1027. gfc_add_expr_to_block (&se->pre, tmp);
  1028. if (se->ss)
  1029. gfc_advance_se_ss_chain (se);
  1030. se->expr = res_var;
  1031. if (array_expr->ts.type == BT_CHARACTER)
  1032. se->string_length = argse.string_length;
  1033. }
  1034. /* Send data to a remove coarray. */
  1035. static tree
  1036. conv_caf_send (gfc_code *code) {
  1037. gfc_expr *lhs_expr, *rhs_expr;
  1038. gfc_se lhs_se, rhs_se;
  1039. stmtblock_t block;
  1040. tree caf_decl, token, offset, image_index, tmp, lhs_kind, rhs_kind;
  1041. tree may_require_tmp;
  1042. tree lhs_type = NULL_TREE;
  1043. tree vec = null_pointer_node, rhs_vec = null_pointer_node;
  1044. gcc_assert (flag_coarray == GFC_FCOARRAY_LIB);
  1045. lhs_expr = code->ext.actual->expr;
  1046. rhs_expr = code->ext.actual->next->expr;
  1047. may_require_tmp = gfc_check_dependency (lhs_expr, rhs_expr, false) == 0
  1048. ? boolean_false_node : boolean_true_node;
  1049. gfc_init_block (&block);
  1050. /* LHS. */
  1051. gfc_init_se (&lhs_se, NULL);
  1052. if (lhs_expr->rank == 0)
  1053. {
  1054. symbol_attribute attr;
  1055. gfc_clear_attr (&attr);
  1056. gfc_conv_expr (&lhs_se, lhs_expr);
  1057. lhs_type = TREE_TYPE (lhs_se.expr);
  1058. lhs_se.expr = gfc_conv_scalar_to_descriptor (&lhs_se, lhs_se.expr, attr);
  1059. lhs_se.expr = gfc_build_addr_expr (NULL_TREE, lhs_se.expr);
  1060. }
  1061. else
  1062. {
  1063. /* If has_vector, pass descriptor for whole array and the
  1064. vector bounds separately. */
  1065. gfc_array_ref *ar, ar2;
  1066. bool has_vector = false;
  1067. if (gfc_is_coindexed (lhs_expr) && gfc_has_vector_subscript (lhs_expr))
  1068. {
  1069. has_vector = true;
  1070. ar = gfc_find_array_ref (lhs_expr);
  1071. ar2 = *ar;
  1072. memset (ar, '\0', sizeof (*ar));
  1073. ar->as = ar2.as;
  1074. ar->type = AR_FULL;
  1075. }
  1076. lhs_se.want_pointer = 1;
  1077. gfc_conv_expr_descriptor (&lhs_se, lhs_expr);
  1078. /* Using gfc_conv_expr_descriptor, we only get the descriptor, but that
  1079. has the wrong type if component references are done. */
  1080. lhs_type = gfc_typenode_for_spec (&lhs_expr->ts);
  1081. tmp = build_fold_indirect_ref_loc (input_location, lhs_se.expr);
  1082. gfc_add_modify (&lhs_se.pre, gfc_conv_descriptor_dtype (tmp),
  1083. gfc_get_dtype_rank_type (has_vector ? ar2.dimen
  1084. : lhs_expr->rank,
  1085. lhs_type));
  1086. if (has_vector)
  1087. {
  1088. vec = conv_caf_vector_subscript (&block, lhs_se.expr, &ar2);
  1089. *ar = ar2;
  1090. }
  1091. }
  1092. lhs_kind = build_int_cst (integer_type_node, lhs_expr->ts.kind);
  1093. gfc_add_block_to_block (&block, &lhs_se.pre);
  1094. /* Special case: RHS is a coarray but LHS is not; this code path avoids a
  1095. temporary and a loop. */
  1096. if (!gfc_is_coindexed (lhs_expr))
  1097. {
  1098. gcc_assert (gfc_is_coindexed (rhs_expr));
  1099. gfc_init_se (&rhs_se, NULL);
  1100. gfc_conv_intrinsic_caf_get (&rhs_se, rhs_expr, lhs_se.expr, lhs_kind,
  1101. may_require_tmp);
  1102. gfc_add_block_to_block (&block, &rhs_se.pre);
  1103. gfc_add_block_to_block (&block, &rhs_se.post);
  1104. gfc_add_block_to_block (&block, &lhs_se.post);
  1105. return gfc_finish_block (&block);
  1106. }
  1107. /* Obtain token, offset and image index for the LHS. */
  1108. caf_decl = gfc_get_tree_for_caf_expr (lhs_expr);
  1109. if (TREE_CODE (TREE_TYPE (caf_decl)) == REFERENCE_TYPE)
  1110. caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
  1111. image_index = gfc_caf_get_image_index (&block, lhs_expr, caf_decl);
  1112. gfc_get_caf_token_offset (&token, &offset, caf_decl, lhs_se.expr, lhs_expr);
  1113. /* RHS. */
  1114. gfc_init_se (&rhs_se, NULL);
  1115. if (rhs_expr->expr_type == EXPR_FUNCTION && rhs_expr->value.function.isym
  1116. && rhs_expr->value.function.isym->id == GFC_ISYM_CONVERSION)
  1117. rhs_expr = rhs_expr->value.function.actual->expr;
  1118. if (rhs_expr->rank == 0)
  1119. {
  1120. symbol_attribute attr;
  1121. gfc_clear_attr (&attr);
  1122. gfc_conv_expr (&rhs_se, rhs_expr);
  1123. if (!gfc_is_coindexed (rhs_expr) && rhs_expr->ts.type != BT_CHARACTER)
  1124. rhs_se.expr = fold_convert (lhs_type , rhs_se.expr);
  1125. rhs_se.expr = gfc_conv_scalar_to_descriptor (&rhs_se, rhs_se.expr, attr);
  1126. rhs_se.expr = gfc_build_addr_expr (NULL_TREE, rhs_se.expr);
  1127. }
  1128. else
  1129. {
  1130. /* If has_vector, pass descriptor for whole array and the
  1131. vector bounds separately. */
  1132. gfc_array_ref *ar, ar2;
  1133. bool has_vector = false;
  1134. tree tmp2;
  1135. if (gfc_is_coindexed (rhs_expr) && gfc_has_vector_subscript (rhs_expr))
  1136. {
  1137. has_vector = true;
  1138. ar = gfc_find_array_ref (rhs_expr);
  1139. ar2 = *ar;
  1140. memset (ar, '\0', sizeof (*ar));
  1141. ar->as = ar2.as;
  1142. ar->type = AR_FULL;
  1143. }
  1144. rhs_se.want_pointer = 1;
  1145. gfc_conv_expr_descriptor (&rhs_se, rhs_expr);
  1146. /* Using gfc_conv_expr_descriptor, we only get the descriptor, but that
  1147. has the wrong type if component references are done. */
  1148. tmp = build_fold_indirect_ref_loc (input_location, rhs_se.expr);
  1149. tmp2 = gfc_typenode_for_spec (&rhs_expr->ts);
  1150. gfc_add_modify (&rhs_se.pre, gfc_conv_descriptor_dtype (tmp),
  1151. gfc_get_dtype_rank_type (has_vector ? ar2.dimen
  1152. : rhs_expr->rank,
  1153. tmp2));
  1154. if (has_vector)
  1155. {
  1156. rhs_vec = conv_caf_vector_subscript (&block, rhs_se.expr, &ar2);
  1157. *ar = ar2;
  1158. }
  1159. }
  1160. gfc_add_block_to_block (&block, &rhs_se.pre);
  1161. rhs_kind = build_int_cst (integer_type_node, rhs_expr->ts.kind);
  1162. if (!gfc_is_coindexed (rhs_expr))
  1163. tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_send, 9, token,
  1164. offset, image_index, lhs_se.expr, vec,
  1165. rhs_se.expr, lhs_kind, rhs_kind, may_require_tmp);
  1166. else
  1167. {
  1168. tree rhs_token, rhs_offset, rhs_image_index;
  1169. caf_decl = gfc_get_tree_for_caf_expr (rhs_expr);
  1170. if (TREE_CODE (TREE_TYPE (caf_decl)) == REFERENCE_TYPE)
  1171. caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
  1172. rhs_image_index = gfc_caf_get_image_index (&block, rhs_expr, caf_decl);
  1173. gfc_get_caf_token_offset (&rhs_token, &rhs_offset, caf_decl, rhs_se.expr,
  1174. rhs_expr);
  1175. tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_sendget, 13,
  1176. token, offset, image_index, lhs_se.expr, vec,
  1177. rhs_token, rhs_offset, rhs_image_index,
  1178. rhs_se.expr, rhs_vec, lhs_kind, rhs_kind,
  1179. may_require_tmp);
  1180. }
  1181. gfc_add_expr_to_block (&block, tmp);
  1182. gfc_add_block_to_block (&block, &lhs_se.post);
  1183. gfc_add_block_to_block (&block, &rhs_se.post);
  1184. return gfc_finish_block (&block);
  1185. }
  1186. static void
  1187. trans_this_image (gfc_se * se, gfc_expr *expr)
  1188. {
  1189. stmtblock_t loop;
  1190. tree type, desc, dim_arg, cond, tmp, m, loop_var, exit_label, min_var,
  1191. lbound, ubound, extent, ml;
  1192. gfc_se argse;
  1193. int rank, corank;
  1194. gfc_expr *distance = expr->value.function.actual->next->next->expr;
  1195. if (expr->value.function.actual->expr
  1196. && !gfc_is_coarray (expr->value.function.actual->expr))
  1197. distance = expr->value.function.actual->expr;
  1198. /* The case -fcoarray=single is handled elsewhere. */
  1199. gcc_assert (flag_coarray != GFC_FCOARRAY_SINGLE);
  1200. /* Argument-free version: THIS_IMAGE(). */
  1201. if (distance || expr->value.function.actual->expr == NULL)
  1202. {
  1203. if (distance)
  1204. {
  1205. gfc_init_se (&argse, NULL);
  1206. gfc_conv_expr_val (&argse, distance);
  1207. gfc_add_block_to_block (&se->pre, &argse.pre);
  1208. gfc_add_block_to_block (&se->post, &argse.post);
  1209. tmp = fold_convert (integer_type_node, argse.expr);
  1210. }
  1211. else
  1212. tmp = integer_zero_node;
  1213. tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_this_image, 1,
  1214. tmp);
  1215. se->expr = fold_convert (gfc_get_int_type (gfc_default_integer_kind),
  1216. tmp);
  1217. return;
  1218. }
  1219. /* Coarray-argument version: THIS_IMAGE(coarray [, dim]). */
  1220. type = gfc_get_int_type (gfc_default_integer_kind);
  1221. corank = gfc_get_corank (expr->value.function.actual->expr);
  1222. rank = expr->value.function.actual->expr->rank;
  1223. /* Obtain the descriptor of the COARRAY. */
  1224. gfc_init_se (&argse, NULL);
  1225. argse.want_coarray = 1;
  1226. gfc_conv_expr_descriptor (&argse, expr->value.function.actual->expr);
  1227. gfc_add_block_to_block (&se->pre, &argse.pre);
  1228. gfc_add_block_to_block (&se->post, &argse.post);
  1229. desc = argse.expr;
  1230. if (se->ss)
  1231. {
  1232. /* Create an implicit second parameter from the loop variable. */
  1233. gcc_assert (!expr->value.function.actual->next->expr);
  1234. gcc_assert (corank > 0);
  1235. gcc_assert (se->loop->dimen == 1);
  1236. gcc_assert (se->ss->info->expr == expr);
  1237. dim_arg = se->loop->loopvar[0];
  1238. dim_arg = fold_build2_loc (input_location, PLUS_EXPR,
  1239. gfc_array_index_type, dim_arg,
  1240. build_int_cst (TREE_TYPE (dim_arg), 1));
  1241. gfc_advance_se_ss_chain (se);
  1242. }
  1243. else
  1244. {
  1245. /* Use the passed DIM= argument. */
  1246. gcc_assert (expr->value.function.actual->next->expr);
  1247. gfc_init_se (&argse, NULL);
  1248. gfc_conv_expr_type (&argse, expr->value.function.actual->next->expr,
  1249. gfc_array_index_type);
  1250. gfc_add_block_to_block (&se->pre, &argse.pre);
  1251. dim_arg = argse.expr;
  1252. if (INTEGER_CST_P (dim_arg))
  1253. {
  1254. if (wi::ltu_p (dim_arg, 1)
  1255. || wi::gtu_p (dim_arg, GFC_TYPE_ARRAY_CORANK (TREE_TYPE (desc))))
  1256. gfc_error ("%<dim%> argument of %s intrinsic at %L is not a valid "
  1257. "dimension index", expr->value.function.isym->name,
  1258. &expr->where);
  1259. }
  1260. else if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
  1261. {
  1262. dim_arg = gfc_evaluate_now (dim_arg, &se->pre);
  1263. cond = fold_build2_loc (input_location, LT_EXPR, boolean_type_node,
  1264. dim_arg,
  1265. build_int_cst (TREE_TYPE (dim_arg), 1));
  1266. tmp = gfc_rank_cst[GFC_TYPE_ARRAY_CORANK (TREE_TYPE (desc))];
  1267. tmp = fold_build2_loc (input_location, GT_EXPR, boolean_type_node,
  1268. dim_arg, tmp);
  1269. cond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
  1270. boolean_type_node, cond, tmp);
  1271. gfc_trans_runtime_check (true, false, cond, &se->pre, &expr->where,
  1272. gfc_msg_fault);
  1273. }
  1274. }
  1275. /* Used algorithm; cf. Fortran 2008, C.10. Note, due to the scalarizer,
  1276. one always has a dim_arg argument.
  1277. m = this_image() - 1
  1278. if (corank == 1)
  1279. {
  1280. sub(1) = m + lcobound(corank)
  1281. return;
  1282. }
  1283. i = rank
  1284. min_var = min (rank + corank - 2, rank + dim_arg - 1)
  1285. for (;;)
  1286. {
  1287. extent = gfc_extent(i)
  1288. ml = m
  1289. m = m/extent
  1290. if (i >= min_var)
  1291. goto exit_label
  1292. i++
  1293. }
  1294. exit_label:
  1295. sub(dim_arg) = (dim_arg < corank) ? ml - m*extent + lcobound(dim_arg)
  1296. : m + lcobound(corank)
  1297. */
  1298. /* this_image () - 1. */
  1299. tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_this_image, 1,
  1300. integer_zero_node);
  1301. tmp = fold_build2_loc (input_location, MINUS_EXPR, type,
  1302. fold_convert (type, tmp), build_int_cst (type, 1));
  1303. if (corank == 1)
  1304. {
  1305. /* sub(1) = m + lcobound(corank). */
  1306. lbound = gfc_conv_descriptor_lbound_get (desc,
  1307. build_int_cst (TREE_TYPE (gfc_array_index_type),
  1308. corank+rank-1));
  1309. lbound = fold_convert (type, lbound);
  1310. tmp = fold_build2_loc (input_location, PLUS_EXPR, type, tmp, lbound);
  1311. se->expr = tmp;
  1312. return;
  1313. }
  1314. m = gfc_create_var (type, NULL);
  1315. ml = gfc_create_var (type, NULL);
  1316. loop_var = gfc_create_var (integer_type_node, NULL);
  1317. min_var = gfc_create_var (integer_type_node, NULL);
  1318. /* m = this_image () - 1. */
  1319. gfc_add_modify (&se->pre, m, tmp);
  1320. /* min_var = min (rank + corank-2, rank + dim_arg - 1). */
  1321. tmp = fold_build2_loc (input_location, PLUS_EXPR, integer_type_node,
  1322. fold_convert (integer_type_node, dim_arg),
  1323. build_int_cst (integer_type_node, rank - 1));
  1324. tmp = fold_build2_loc (input_location, MIN_EXPR, integer_type_node,
  1325. build_int_cst (integer_type_node, rank + corank - 2),
  1326. tmp);
  1327. gfc_add_modify (&se->pre, min_var, tmp);
  1328. /* i = rank. */
  1329. tmp = build_int_cst (integer_type_node, rank);
  1330. gfc_add_modify (&se->pre, loop_var, tmp);
  1331. exit_label = gfc_build_label_decl (NULL_TREE);
  1332. TREE_USED (exit_label) = 1;
  1333. /* Loop body. */
  1334. gfc_init_block (&loop);
  1335. /* ml = m. */
  1336. gfc_add_modify (&loop, ml, m);
  1337. /* extent = ... */
  1338. lbound = gfc_conv_descriptor_lbound_get (desc, loop_var);
  1339. ubound = gfc_conv_descriptor_ubound_get (desc, loop_var);
  1340. extent = gfc_conv_array_extent_dim (lbound, ubound, NULL);
  1341. extent = fold_convert (type, extent);
  1342. /* m = m/extent. */
  1343. gfc_add_modify (&loop, m,
  1344. fold_build2_loc (input_location, TRUNC_DIV_EXPR, type,
  1345. m, extent));
  1346. /* Exit condition: if (i >= min_var) goto exit_label. */
  1347. cond = fold_build2_loc (input_location, GE_EXPR, boolean_type_node, loop_var,
  1348. min_var);
  1349. tmp = build1_v (GOTO_EXPR, exit_label);
  1350. tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
  1351. build_empty_stmt (input_location));
  1352. gfc_add_expr_to_block (&loop, tmp);
  1353. /* Increment loop variable: i++. */
  1354. gfc_add_modify (&loop, loop_var,
  1355. fold_build2_loc (input_location, PLUS_EXPR, integer_type_node,
  1356. loop_var,
  1357. build_int_cst (integer_type_node, 1)));
  1358. /* Making the loop... actually loop! */
  1359. tmp = gfc_finish_block (&loop);
  1360. tmp = build1_v (LOOP_EXPR, tmp);
  1361. gfc_add_expr_to_block (&se->pre, tmp);
  1362. /* The exit label. */
  1363. tmp = build1_v (LABEL_EXPR, exit_label);
  1364. gfc_add_expr_to_block (&se->pre, tmp);
  1365. /* sub(co_dim) = (co_dim < corank) ? ml - m*extent + lcobound(dim_arg)
  1366. : m + lcobound(corank) */
  1367. cond = fold_build2_loc (input_location, LT_EXPR, boolean_type_node, dim_arg,
  1368. build_int_cst (TREE_TYPE (dim_arg), corank));
  1369. lbound = gfc_conv_descriptor_lbound_get (desc,
  1370. fold_build2_loc (input_location, PLUS_EXPR,
  1371. gfc_array_index_type, dim_arg,
  1372. build_int_cst (TREE_TYPE (dim_arg), rank-1)));
  1373. lbound = fold_convert (type, lbound);
  1374. tmp = fold_build2_loc (input_location, MINUS_EXPR, type, ml,
  1375. fold_build2_loc (input_location, MULT_EXPR, type,
  1376. m, extent));
  1377. tmp = fold_build2_loc (input_location, PLUS_EXPR, type, tmp, lbound);
  1378. se->expr = fold_build3_loc (input_location, COND_EXPR, type, cond, tmp,
  1379. fold_build2_loc (input_location, PLUS_EXPR, type,
  1380. m, lbound));
  1381. }
  1382. static void
  1383. trans_image_index (gfc_se * se, gfc_expr *expr)
  1384. {
  1385. tree num_images, cond, coindex, type, lbound, ubound, desc, subdesc,
  1386. tmp, invalid_bound;
  1387. gfc_se argse, subse;
  1388. int rank, corank, codim;
  1389. type = gfc_get_int_type (gfc_default_integer_kind);
  1390. corank = gfc_get_corank (expr->value.function.actual->expr);
  1391. rank = expr->value.function.actual->expr->rank;
  1392. /* Obtain the descriptor of the COARRAY. */
  1393. gfc_init_se (&argse, NULL);
  1394. argse.want_coarray = 1;
  1395. gfc_conv_expr_descriptor (&argse, expr->value.function.actual->expr);
  1396. gfc_add_block_to_block (&se->pre, &argse.pre);
  1397. gfc_add_block_to_block (&se->post, &argse.post);
  1398. desc = argse.expr;
  1399. /* Obtain a handle to the SUB argument. */
  1400. gfc_init_se (&subse, NULL);
  1401. gfc_conv_expr_descriptor (&subse, expr->value.function.actual->next->expr);
  1402. gfc_add_block_to_block (&se->pre, &subse.pre);
  1403. gfc_add_block_to_block (&se->post, &subse.post);
  1404. subdesc = build_fold_indirect_ref_loc (input_location,
  1405. gfc_conv_descriptor_data_get (subse.expr));
  1406. /* Fortran 2008 does not require that the values remain in the cobounds,
  1407. thus we need explicitly check this - and return 0 if they are exceeded. */
  1408. lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[rank+corank-1]);
  1409. tmp = gfc_build_array_ref (subdesc, gfc_rank_cst[corank-1], NULL);
  1410. invalid_bound = fold_build2_loc (input_location, LT_EXPR, boolean_type_node,
  1411. fold_convert (gfc_array_index_type, tmp),
  1412. lbound);
  1413. for (codim = corank + rank - 2; codim >= rank; codim--)
  1414. {
  1415. lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[codim]);
  1416. ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[codim]);
  1417. tmp = gfc_build_array_ref (subdesc, gfc_rank_cst[codim-rank], NULL);
  1418. cond = fold_build2_loc (input_location, LT_EXPR, boolean_type_node,
  1419. fold_convert (gfc_array_index_type, tmp),
  1420. lbound);
  1421. invalid_bound = fold_build2_loc (input_location, TRUTH_OR_EXPR,
  1422. boolean_type_node, invalid_bound, cond);
  1423. cond = fold_build2_loc (input_location, GT_EXPR, boolean_type_node,
  1424. fold_convert (gfc_array_index_type, tmp),
  1425. ubound);
  1426. invalid_bound = fold_build2_loc (input_location, TRUTH_OR_EXPR,
  1427. boolean_type_node, invalid_bound, cond);
  1428. }
  1429. invalid_bound = gfc_unlikely (invalid_bound, PRED_FORTRAN_INVALID_BOUND);
  1430. /* See Fortran 2008, C.10 for the following algorithm. */
  1431. /* coindex = sub(corank) - lcobound(n). */
  1432. coindex = fold_convert (gfc_array_index_type,
  1433. gfc_build_array_ref (subdesc, gfc_rank_cst[corank-1],
  1434. NULL));
  1435. lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[rank+corank-1]);
  1436. coindex = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
  1437. fold_convert (gfc_array_index_type, coindex),
  1438. lbound);
  1439. for (codim = corank + rank - 2; codim >= rank; codim--)
  1440. {
  1441. tree extent, ubound;
  1442. /* coindex = coindex*extent(codim) + sub(codim) - lcobound(codim). */
  1443. lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[codim]);
  1444. ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[codim]);
  1445. extent = gfc_conv_array_extent_dim (lbound, ubound, NULL);
  1446. /* coindex *= extent. */
  1447. coindex = fold_build2_loc (input_location, MULT_EXPR,
  1448. gfc_array_index_type, coindex, extent);
  1449. /* coindex += sub(codim). */
  1450. tmp = gfc_build_array_ref (subdesc, gfc_rank_cst[codim-rank], NULL);
  1451. coindex = fold_build2_loc (input_location, PLUS_EXPR,
  1452. gfc_array_index_type, coindex,
  1453. fold_convert (gfc_array_index_type, tmp));
  1454. /* coindex -= lbound(codim). */
  1455. lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[codim]);
  1456. coindex = fold_build2_loc (input_location, MINUS_EXPR,
  1457. gfc_array_index_type, coindex, lbound);
  1458. }
  1459. coindex = fold_build2_loc (input_location, PLUS_EXPR, type,
  1460. fold_convert(type, coindex),
  1461. build_int_cst (type, 1));
  1462. /* Return 0 if "coindex" exceeds num_images(). */
  1463. if (flag_coarray == GFC_FCOARRAY_SINGLE)
  1464. num_images = build_int_cst (type, 1);
  1465. else
  1466. {
  1467. tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_num_images, 2,
  1468. integer_zero_node,
  1469. build_int_cst (integer_type_node, -1));
  1470. num_images = fold_convert (type, tmp);
  1471. }
  1472. tmp = gfc_create_var (type, NULL);
  1473. gfc_add_modify (&se->pre, tmp, coindex);
  1474. cond = fold_build2_loc (input_location, GT_EXPR, boolean_type_node, tmp,
  1475. num_images);
  1476. cond = fold_build2_loc (input_location, TRUTH_OR_EXPR, boolean_type_node,
  1477. cond,
  1478. fold_convert (boolean_type_node, invalid_bound));
  1479. se->expr = fold_build3_loc (input_location, COND_EXPR, type, cond,
  1480. build_int_cst (type, 0), tmp);
  1481. }
  1482. static void
  1483. trans_num_images (gfc_se * se, gfc_expr *expr)
  1484. {
  1485. tree tmp, distance, failed;
  1486. gfc_se argse;
  1487. if (expr->value.function.actual->expr)
  1488. {
  1489. gfc_init_se (&argse, NULL);
  1490. gfc_conv_expr_val (&argse, expr->value.function.actual->expr);
  1491. gfc_add_block_to_block (&se->pre, &argse.pre);
  1492. gfc_add_block_to_block (&se->post, &argse.post);
  1493. distance = fold_convert (integer_type_node, argse.expr);
  1494. }
  1495. else
  1496. distance = integer_zero_node;
  1497. if (expr->value.function.actual->next->expr)
  1498. {
  1499. gfc_init_se (&argse, NULL);
  1500. gfc_conv_expr_val (&argse, expr->value.function.actual->next->expr);
  1501. gfc_add_block_to_block (&se->pre, &argse.pre);
  1502. gfc_add_block_to_block (&se->post, &argse.post);
  1503. failed = fold_convert (integer_type_node, argse.expr);
  1504. }
  1505. else
  1506. failed = build_int_cst (integer_type_node, -1);
  1507. tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_num_images, 2,
  1508. distance, failed);
  1509. se->expr = fold_convert (gfc_get_int_type (gfc_default_integer_kind), tmp);
  1510. }
  1511. static void
  1512. gfc_conv_intrinsic_rank (gfc_se *se, gfc_expr *expr)
  1513. {
  1514. gfc_se argse;
  1515. gfc_init_se (&argse, NULL);
  1516. argse.data_not_needed = 1;
  1517. argse.descriptor_only = 1;
  1518. gfc_conv_expr_descriptor (&argse, expr->value.function.actual->expr);
  1519. gfc_add_block_to_block (&se->pre, &argse.pre);
  1520. gfc_add_block_to_block (&se->post, &argse.post);
  1521. se->expr = gfc_conv_descriptor_rank (argse.expr);
  1522. }
  1523. /* Evaluate a single upper or lower bound. */
  1524. /* TODO: bound intrinsic generates way too much unnecessary code. */
  1525. static void
  1526. gfc_conv_intrinsic_bound (gfc_se * se, gfc_expr * expr, int upper)
  1527. {
  1528. gfc_actual_arglist *arg;
  1529. gfc_actual_arglist *arg2;
  1530. tree desc;
  1531. tree type;
  1532. tree bound;
  1533. tree tmp;
  1534. tree cond, cond1, cond3, cond4, size;
  1535. tree ubound;
  1536. tree lbound;
  1537. gfc_se argse;
  1538. gfc_array_spec * as;
  1539. bool assumed_rank_lb_one;
  1540. arg = expr->value.function.actual;
  1541. arg2 = arg->next;
  1542. if (se->ss)
  1543. {
  1544. /* Create an implicit second parameter from the loop variable. */
  1545. gcc_assert (!arg2->expr);
  1546. gcc_assert (se->loop->dimen == 1);
  1547. gcc_assert (se->ss->info->expr == expr);
  1548. gfc_advance_se_ss_chain (se);
  1549. bound = se->loop->loopvar[0];
  1550. bound = fold_build2_loc (input_location, MINUS_EXPR,
  1551. gfc_array_index_type, bound,
  1552. se->loop->from[0]);
  1553. }
  1554. else
  1555. {
  1556. /* use the passed argument. */
  1557. gcc_assert (arg2->expr);
  1558. gfc_init_se (&argse, NULL);
  1559. gfc_conv_expr_type (&argse, arg2->expr, gfc_array_index_type);
  1560. gfc_add_block_to_block (&se->pre, &argse.pre);
  1561. bound = argse.expr;
  1562. /* Convert from one based to zero based. */
  1563. bound = fold_build2_loc (input_location, MINUS_EXPR,
  1564. gfc_array_index_type, bound,
  1565. gfc_index_one_node);
  1566. }
  1567. /* TODO: don't re-evaluate the descriptor on each iteration. */
  1568. /* Get a descriptor for the first parameter. */
  1569. gfc_init_se (&argse, NULL);
  1570. gfc_conv_expr_descriptor (&argse, arg->expr);
  1571. gfc_add_block_to_block (&se->pre, &argse.pre);
  1572. gfc_add_block_to_block (&se->post, &argse.post);
  1573. desc = argse.expr;
  1574. as = gfc_get_full_arrayspec_from_expr (arg->expr);
  1575. if (INTEGER_CST_P (bound))
  1576. {
  1577. if (((!as || as->type != AS_ASSUMED_RANK)
  1578. && wi::geu_p (bound, GFC_TYPE_ARRAY_RANK (TREE_TYPE (desc))))
  1579. || wi::gtu_p (bound, GFC_MAX_DIMENSIONS))
  1580. gfc_error ("%<dim%> argument of %s intrinsic at %L is not a valid "
  1581. "dimension index", upper ? "UBOUND" : "LBOUND",
  1582. &expr->where);
  1583. }
  1584. if (!INTEGER_CST_P (bound) || (as && as->type == AS_ASSUMED_RANK))
  1585. {
  1586. if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
  1587. {
  1588. bound = gfc_evaluate_now (bound, &se->pre);
  1589. cond = fold_build2_loc (input_location, LT_EXPR, boolean_type_node,
  1590. bound, build_int_cst (TREE_TYPE (bound), 0));
  1591. if (as && as->type == AS_ASSUMED_RANK)
  1592. tmp = gfc_conv_descriptor_rank (desc);
  1593. else
  1594. tmp = gfc_rank_cst[GFC_TYPE_ARRAY_RANK (TREE_TYPE (desc))];
  1595. tmp = fold_build2_loc (input_location, GE_EXPR, boolean_type_node,
  1596. bound, fold_convert(TREE_TYPE (bound), tmp));
  1597. cond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
  1598. boolean_type_node, cond, tmp);
  1599. gfc_trans_runtime_check (true, false, cond, &se->pre, &expr->where,
  1600. gfc_msg_fault);
  1601. }
  1602. }
  1603. /* Take care of the lbound shift for assumed-rank arrays, which are
  1604. nonallocatable and nonpointers. Those has a lbound of 1. */
  1605. assumed_rank_lb_one = as && as->type == AS_ASSUMED_RANK
  1606. && ((arg->expr->ts.type != BT_CLASS
  1607. && !arg->expr->symtree->n.sym->attr.allocatable
  1608. && !arg->expr->symtree->n.sym->attr.pointer)
  1609. || (arg->expr->ts.type == BT_CLASS
  1610. && !CLASS_DATA (arg->expr)->attr.allocatable
  1611. && !CLASS_DATA (arg->expr)->attr.class_pointer));
  1612. ubound = gfc_conv_descriptor_ubound_get (desc, bound);
  1613. lbound = gfc_conv_descriptor_lbound_get (desc, bound);
  1614. /* 13.14.53: Result value for LBOUND
  1615. Case (i): For an array section or for an array expression other than a
  1616. whole array or array structure component, LBOUND(ARRAY, DIM)
  1617. has the value 1. For a whole array or array structure
  1618. component, LBOUND(ARRAY, DIM) has the value:
  1619. (a) equal to the lower bound for subscript DIM of ARRAY if
  1620. dimension DIM of ARRAY does not have extent zero
  1621. or if ARRAY is an assumed-size array of rank DIM,
  1622. or (b) 1 otherwise.
  1623. 13.14.113: Result value for UBOUND
  1624. Case (i): For an array section or for an array expression other than a
  1625. whole array or array structure component, UBOUND(ARRAY, DIM)
  1626. has the value equal to the number of elements in the given
  1627. dimension; otherwise, it has a value equal to the upper bound
  1628. for subscript DIM of ARRAY if dimension DIM of ARRAY does
  1629. not have size zero and has value zero if dimension DIM has
  1630. size zero. */
  1631. if (!upper && assumed_rank_lb_one)
  1632. se->expr = gfc_index_one_node;
  1633. else if (as)
  1634. {
  1635. tree stride = gfc_conv_descriptor_stride_get (desc, bound);
  1636. cond1 = fold_build2_loc (input_location, GE_EXPR, boolean_type_node,
  1637. ubound, lbound);
  1638. cond3 = fold_build2_loc (input_location, GE_EXPR, boolean_type_node,
  1639. stride, gfc_index_zero_node);
  1640. cond3 = fold_build2_loc (input_location, TRUTH_AND_EXPR,
  1641. boolean_type_node, cond3, cond1);
  1642. cond4 = fold_build2_loc (input_location, LT_EXPR, boolean_type_node,
  1643. stride, gfc_index_zero_node);
  1644. if (upper)
  1645. {
  1646. tree cond5;
  1647. cond = fold_build2_loc (input_location, TRUTH_OR_EXPR,
  1648. boolean_type_node, cond3, cond4);
  1649. cond5 = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
  1650. gfc_index_one_node, lbound);
  1651. cond5 = fold_build2_loc (input_location, TRUTH_AND_EXPR,
  1652. boolean_type_node, cond4, cond5);
  1653. cond = fold_build2_loc (input_location, TRUTH_OR_EXPR,
  1654. boolean_type_node, cond, cond5);
  1655. if (assumed_rank_lb_one)
  1656. {
  1657. tmp = fold_build2_loc (input_location, MINUS_EXPR,
  1658. gfc_array_index_type, ubound, lbound);
  1659. tmp = fold_build2_loc (input_location, PLUS_EXPR,
  1660. gfc_array_index_type, tmp, gfc_index_one_node);
  1661. }
  1662. else
  1663. tmp = ubound;
  1664. se->expr = fold_build3_loc (input_location, COND_EXPR,
  1665. gfc_array_index_type, cond,
  1666. tmp, gfc_index_zero_node);
  1667. }
  1668. else
  1669. {
  1670. if (as->type == AS_ASSUMED_SIZE)
  1671. cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
  1672. bound, build_int_cst (TREE_TYPE (bound),
  1673. arg->expr->rank - 1));
  1674. else
  1675. cond = boolean_false_node;
  1676. cond1 = fold_build2_loc (input_location, TRUTH_OR_EXPR,
  1677. boolean_type_node, cond3, cond4);
  1678. cond = fold_build2_loc (input_location, TRUTH_OR_EXPR,
  1679. boolean_type_node, cond, cond1);
  1680. se->expr = fold_build3_loc (input_location, COND_EXPR,
  1681. gfc_array_index_type, cond,
  1682. lbound, gfc_index_one_node);
  1683. }
  1684. }
  1685. else
  1686. {
  1687. if (upper)
  1688. {
  1689. size = fold_build2_loc (input_location, MINUS_EXPR,
  1690. gfc_array_index_type, ubound, lbound);
  1691. se->expr = fold_build2_loc (input_location, PLUS_EXPR,
  1692. gfc_array_index_type, size,
  1693. gfc_index_one_node);
  1694. se->expr = fold_build2_loc (input_location, MAX_EXPR,
  1695. gfc_array_index_type, se->expr,
  1696. gfc_index_zero_node);
  1697. }
  1698. else
  1699. se->expr = gfc_index_one_node;
  1700. }
  1701. type = gfc_typenode_for_spec (&expr->ts);
  1702. se->expr = convert (type, se->expr);
  1703. }
  1704. static void
  1705. conv_intrinsic_cobound (gfc_se * se, gfc_expr * expr)
  1706. {
  1707. gfc_actual_arglist *arg;
  1708. gfc_actual_arglist *arg2;
  1709. gfc_se argse;
  1710. tree bound, resbound, resbound2, desc, cond, tmp;
  1711. tree type;
  1712. int corank;
  1713. gcc_assert (expr->value.function.isym->id == GFC_ISYM_LCOBOUND
  1714. || expr->value.function.isym->id == GFC_ISYM_UCOBOUND
  1715. || expr->value.function.isym->id == GFC_ISYM_THIS_IMAGE);
  1716. arg = expr->value.function.actual;
  1717. arg2 = arg->next;
  1718. gcc_assert (arg->expr->expr_type == EXPR_VARIABLE);
  1719. corank = gfc_get_corank (arg->expr);
  1720. gfc_init_se (&argse, NULL);
  1721. argse.want_coarray = 1;
  1722. gfc_conv_expr_descriptor (&argse, arg->expr);
  1723. gfc_add_block_to_block (&se->pre, &argse.pre);
  1724. gfc_add_block_to_block (&se->post, &argse.post);
  1725. desc = argse.expr;
  1726. if (se->ss)
  1727. {
  1728. /* Create an implicit second parameter from the loop variable. */
  1729. gcc_assert (!arg2->expr);
  1730. gcc_assert (corank > 0);
  1731. gcc_assert (se->loop->dimen == 1);
  1732. gcc_assert (se->ss->info->expr == expr);
  1733. bound = se->loop->loopvar[0];
  1734. bound = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
  1735. bound, gfc_rank_cst[arg->expr->rank]);
  1736. gfc_advance_se_ss_chain (se);
  1737. }
  1738. else
  1739. {
  1740. /* use the passed argument. */
  1741. gcc_assert (arg2->expr);
  1742. gfc_init_se (&argse, NULL);
  1743. gfc_conv_expr_type (&argse, arg2->expr, gfc_array_index_type);
  1744. gfc_add_block_to_block (&se->pre, &argse.pre);
  1745. bound = argse.expr;
  1746. if (INTEGER_CST_P (bound))
  1747. {
  1748. if (wi::ltu_p (bound, 1)
  1749. || wi::gtu_p (bound, GFC_TYPE_ARRAY_CORANK (TREE_TYPE (desc))))
  1750. gfc_error ("%<dim%> argument of %s intrinsic at %L is not a valid "
  1751. "dimension index", expr->value.function.isym->name,
  1752. &expr->where);
  1753. }
  1754. else if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
  1755. {
  1756. bound = gfc_evaluate_now (bound, &se->pre);
  1757. cond = fold_build2_loc (input_location, LT_EXPR, boolean_type_node,
  1758. bound, build_int_cst (TREE_TYPE (bound), 1));
  1759. tmp = gfc_rank_cst[GFC_TYPE_ARRAY_CORANK (TREE_TYPE (desc))];
  1760. tmp = fold_build2_loc (input_location, GT_EXPR, boolean_type_node,
  1761. bound, tmp);
  1762. cond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
  1763. boolean_type_node, cond, tmp);
  1764. gfc_trans_runtime_check (true, false, cond, &se->pre, &expr->where,
  1765. gfc_msg_fault);
  1766. }
  1767. /* Subtract 1 to get to zero based and add dimensions. */
  1768. switch (arg->expr->rank)
  1769. {
  1770. case 0:
  1771. bound = fold_build2_loc (input_location, MINUS_EXPR,
  1772. gfc_array_index_type, bound,
  1773. gfc_index_one_node);
  1774. case 1:
  1775. break;
  1776. default:
  1777. bound = fold_build2_loc (input_location, PLUS_EXPR,
  1778. gfc_array_index_type, bound,
  1779. gfc_rank_cst[arg->expr->rank - 1]);
  1780. }
  1781. }
  1782. resbound = gfc_conv_descriptor_lbound_get (desc, bound);
  1783. /* Handle UCOBOUND with special handling of the last codimension. */
  1784. if (expr->value.function.isym->id == GFC_ISYM_UCOBOUND)
  1785. {
  1786. /* Last codimension: For -fcoarray=single just return
  1787. the lcobound - otherwise add
  1788. ceiling (real (num_images ()) / real (size)) - 1
  1789. = (num_images () + size - 1) / size - 1
  1790. = (num_images - 1) / size(),
  1791. where size is the product of the extent of all but the last
  1792. codimension. */
  1793. if (flag_coarray != GFC_FCOARRAY_SINGLE && corank > 1)
  1794. {
  1795. tree cosize;
  1796. cosize = gfc_conv_descriptor_cosize (desc, arg->expr->rank, corank);
  1797. tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_num_images,
  1798. 2, integer_zero_node,
  1799. build_int_cst (integer_type_node, -1));
  1800. tmp = fold_build2_loc (input_location, MINUS_EXPR,
  1801. gfc_array_index_type,
  1802. fold_convert (gfc_array_index_type, tmp),
  1803. build_int_cst (gfc_array_index_type, 1));
  1804. tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
  1805. gfc_array_index_type, tmp,
  1806. fold_convert (gfc_array_index_type, cosize));
  1807. resbound = fold_build2_loc (input_location, PLUS_EXPR,
  1808. gfc_array_index_type, resbound, tmp);
  1809. }
  1810. else if (flag_coarray != GFC_FCOARRAY_SINGLE)
  1811. {
  1812. /* ubound = lbound + num_images() - 1. */
  1813. tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_num_images,
  1814. 2, integer_zero_node,
  1815. build_int_cst (integer_type_node, -1));
  1816. tmp = fold_build2_loc (input_location, MINUS_EXPR,
  1817. gfc_array_index_type,
  1818. fold_convert (gfc_array_index_type, tmp),
  1819. build_int_cst (gfc_array_index_type, 1));
  1820. resbound = fold_build2_loc (input_location, PLUS_EXPR,
  1821. gfc_array_index_type, resbound, tmp);
  1822. }
  1823. if (corank > 1)
  1824. {
  1825. cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
  1826. bound,
  1827. build_int_cst (TREE_TYPE (bound),
  1828. arg->expr->rank + corank - 1));
  1829. resbound2 = gfc_conv_descriptor_ubound_get (desc, bound);
  1830. se->expr = fold_build3_loc (input_location, COND_EXPR,
  1831. gfc_array_index_type, cond,
  1832. resbound, resbound2);
  1833. }
  1834. else
  1835. se->expr = resbound;
  1836. }
  1837. else
  1838. se->expr = resbound;
  1839. type = gfc_typenode_for_spec (&expr->ts);
  1840. se->expr = convert (type, se->expr);
  1841. }
  1842. static void
  1843. conv_intrinsic_stride (gfc_se * se, gfc_expr * expr)
  1844. {
  1845. gfc_actual_arglist *array_arg;
  1846. gfc_actual_arglist *dim_arg;
  1847. gfc_se argse;
  1848. tree desc, tmp;
  1849. array_arg = expr->value.function.actual;
  1850. dim_arg = array_arg->next;
  1851. gcc_assert (array_arg->expr->expr_type == EXPR_VARIABLE);
  1852. gfc_init_se (&argse, NULL);
  1853. gfc_conv_expr_descriptor (&argse, array_arg->expr);
  1854. gfc_add_block_to_block (&se->pre, &argse.pre);
  1855. gfc_add_block_to_block (&se->post, &argse.post);
  1856. desc = argse.expr;
  1857. gcc_assert (dim_arg->expr);
  1858. gfc_init_se (&argse, NULL);
  1859. gfc_conv_expr_type (&argse, dim_arg->expr, gfc_array_index_type);
  1860. gfc_add_block_to_block (&se->pre, &argse.pre);
  1861. tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
  1862. argse.expr, gfc_index_one_node);
  1863. se->expr = gfc_conv_descriptor_stride_get (desc, tmp);
  1864. }
  1865. static void
  1866. gfc_conv_intrinsic_abs (gfc_se * se, gfc_expr * expr)
  1867. {
  1868. tree arg, cabs;
  1869. gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
  1870. switch (expr->value.function.actual->expr->ts.type)
  1871. {
  1872. case BT_INTEGER:
  1873. case BT_REAL:
  1874. se->expr = fold_build1_loc (input_location, ABS_EXPR, TREE_TYPE (arg),
  1875. arg);
  1876. break;
  1877. case BT_COMPLEX:
  1878. cabs = gfc_builtin_decl_for_float_kind (BUILT_IN_CABS, expr->ts.kind);
  1879. se->expr = build_call_expr_loc (input_location, cabs, 1, arg);
  1880. break;
  1881. default:
  1882. gcc_unreachable ();
  1883. }
  1884. }
  1885. /* Create a complex value from one or two real components. */
  1886. static void
  1887. gfc_conv_intrinsic_cmplx (gfc_se * se, gfc_expr * expr, int both)
  1888. {
  1889. tree real;
  1890. tree imag;
  1891. tree type;
  1892. tree *args;
  1893. unsigned int num_args;
  1894. num_args = gfc_intrinsic_argument_list_length (expr);
  1895. args = XALLOCAVEC (tree, num_args);
  1896. type = gfc_typenode_for_spec (&expr->ts);
  1897. gfc_conv_intrinsic_function_args (se, expr, args, num_args);
  1898. real = convert (TREE_TYPE (type), args[0]);
  1899. if (both)
  1900. imag = convert (TREE_TYPE (type), args[1]);
  1901. else if (TREE_CODE (TREE_TYPE (args[0])) == COMPLEX_TYPE)
  1902. {
  1903. imag = fold_build1_loc (input_location, IMAGPART_EXPR,
  1904. TREE_TYPE (TREE_TYPE (args[0])), args[0]);
  1905. imag = convert (TREE_TYPE (type), imag);
  1906. }
  1907. else
  1908. imag = build_real_from_int_cst (TREE_TYPE (type), integer_zero_node);
  1909. se->expr = fold_build2_loc (input_location, COMPLEX_EXPR, type, real, imag);
  1910. }
  1911. /* Remainder function MOD(A, P) = A - INT(A / P) * P
  1912. MODULO(A, P) = A - FLOOR (A / P) * P
  1913. The obvious algorithms above are numerically instable for large
  1914. arguments, hence these intrinsics are instead implemented via calls
  1915. to the fmod family of functions. It is the responsibility of the
  1916. user to ensure that the second argument is non-zero. */
  1917. static void
  1918. gfc_conv_intrinsic_mod (gfc_se * se, gfc_expr * expr, int modulo)
  1919. {
  1920. tree type;
  1921. tree tmp;
  1922. tree test;
  1923. tree test2;
  1924. tree fmod;
  1925. tree zero;
  1926. tree args[2];
  1927. gfc_conv_intrinsic_function_args (se, expr, args, 2);
  1928. switch (expr->ts.type)
  1929. {
  1930. case BT_INTEGER:
  1931. /* Integer case is easy, we've got a builtin op. */
  1932. type = TREE_TYPE (args[0]);
  1933. if (modulo)
  1934. se->expr = fold_build2_loc (input_location, FLOOR_MOD_EXPR, type,
  1935. args[0], args[1]);
  1936. else
  1937. se->expr = fold_build2_loc (input_location, TRUNC_MOD_EXPR, type,
  1938. args[0], args[1]);
  1939. break;
  1940. case BT_REAL:
  1941. fmod = NULL_TREE;
  1942. /* Check if we have a builtin fmod. */
  1943. fmod = gfc_builtin_decl_for_float_kind (BUILT_IN_FMOD, expr->ts.kind);
  1944. /* The builtin should always be available. */
  1945. gcc_assert (fmod != NULL_TREE);
  1946. tmp = build_addr (fmod, current_function_decl);
  1947. se->expr = build_call_array_loc (input_location,
  1948. TREE_TYPE (TREE_TYPE (fmod)),
  1949. tmp, 2, args);
  1950. if (modulo == 0)
  1951. return;
  1952. type = TREE_TYPE (args[0]);
  1953. args[0] = gfc_evaluate_now (args[0], &se->pre);
  1954. args[1] = gfc_evaluate_now (args[1], &se->pre);
  1955. /* Definition:
  1956. modulo = arg - floor (arg/arg2) * arg2
  1957. In order to calculate the result accurately, we use the fmod
  1958. function as follows.
  1959. res = fmod (arg, arg2);
  1960. if (res)
  1961. {
  1962. if ((arg < 0) xor (arg2 < 0))
  1963. res += arg2;
  1964. }
  1965. else
  1966. res = copysign (0., arg2);
  1967. => As two nested ternary exprs:
  1968. res = res ? (((arg < 0) xor (arg2 < 0)) ? res + arg2 : res)
  1969. : copysign (0., arg2);
  1970. */
  1971. zero = gfc_build_const (type, integer_zero_node);
  1972. tmp = gfc_evaluate_now (se->expr, &se->pre);
  1973. if (!flag_signed_zeros)
  1974. {
  1975. test = fold_build2_loc (input_location, LT_EXPR, boolean_type_node,
  1976. args[0], zero);
  1977. test2 = fold_build2_loc (input_location, LT_EXPR, boolean_type_node,
  1978. args[1], zero);
  1979. test2 = fold_build2_loc (input_location, TRUTH_XOR_EXPR,
  1980. boolean_type_node, test, test2);
  1981. test = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
  1982. tmp, zero);
  1983. test = fold_build2_loc (input_location, TRUTH_AND_EXPR,
  1984. boolean_type_node, test, test2);
  1985. test = gfc_evaluate_now (test, &se->pre);
  1986. se->expr = fold_build3_loc (input_location, COND_EXPR, type, test,
  1987. fold_build2_loc (input_location,
  1988. PLUS_EXPR,
  1989. type, tmp, args[1]),
  1990. tmp);
  1991. }
  1992. else
  1993. {
  1994. tree expr1, copysign, cscall;
  1995. copysign = gfc_builtin_decl_for_float_kind (BUILT_IN_COPYSIGN,
  1996. expr->ts.kind);
  1997. test = fold_build2_loc (input_location, LT_EXPR, boolean_type_node,
  1998. args[0], zero);
  1999. test2 = fold_build2_loc (input_location, LT_EXPR, boolean_type_node,
  2000. args[1], zero);
  2001. test2 = fold_build2_loc (input_location, TRUTH_XOR_EXPR,
  2002. boolean_type_node, test, test2);
  2003. expr1 = fold_build3_loc (input_location, COND_EXPR, type, test2,
  2004. fold_build2_loc (input_location,
  2005. PLUS_EXPR,
  2006. type, tmp, args[1]),
  2007. tmp);
  2008. test = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
  2009. tmp, zero);
  2010. cscall = build_call_expr_loc (input_location, copysign, 2, zero,
  2011. args[1]);
  2012. se->expr = fold_build3_loc (input_location, COND_EXPR, type, test,
  2013. expr1, cscall);
  2014. }
  2015. return;
  2016. default:
  2017. gcc_unreachable ();
  2018. }
  2019. }
  2020. /* DSHIFTL(I,J,S) = (I << S) | (J >> (BITSIZE(J) - S))
  2021. DSHIFTR(I,J,S) = (I << (BITSIZE(I) - S)) | (J >> S)
  2022. where the right shifts are logical (i.e. 0's are shifted in).
  2023. Because SHIFT_EXPR's want shifts strictly smaller than the integral
  2024. type width, we have to special-case both S == 0 and S == BITSIZE(J):
  2025. DSHIFTL(I,J,0) = I
  2026. DSHIFTL(I,J,BITSIZE) = J
  2027. DSHIFTR(I,J,0) = J
  2028. DSHIFTR(I,J,BITSIZE) = I. */
  2029. static void
  2030. gfc_conv_intrinsic_dshift (gfc_se * se, gfc_expr * expr, bool dshiftl)
  2031. {
  2032. tree type, utype, stype, arg1, arg2, shift, res, left, right;
  2033. tree args[3], cond, tmp;
  2034. int bitsize;
  2035. gfc_conv_intrinsic_function_args (se, expr, args, 3);
  2036. gcc_assert (TREE_TYPE (args[0]) == TREE_TYPE (args[1]));
  2037. type = TREE_TYPE (args[0]);
  2038. bitsize = TYPE_PRECISION (type);
  2039. utype = unsigned_type_for (type);
  2040. stype = TREE_TYPE (args[2]);
  2041. arg1 = gfc_evaluate_now (args[0], &se->pre);
  2042. arg2 = gfc_evaluate_now (args[1], &se->pre);
  2043. shift = gfc_evaluate_now (args[2], &se->pre);
  2044. /* The generic case. */
  2045. tmp = fold_build2_loc (input_location, MINUS_EXPR, stype,
  2046. build_int_cst (stype, bitsize), shift);
  2047. left = fold_build2_loc (input_location, LSHIFT_EXPR, type,
  2048. arg1, dshiftl ? shift : tmp);
  2049. right = fold_build2_loc (input_location, RSHIFT_EXPR, utype,
  2050. fold_convert (utype, arg2), dshiftl ? tmp : shift);
  2051. right = fold_convert (type, right);
  2052. res = fold_build2_loc (input_location, BIT_IOR_EXPR, type, left, right);
  2053. /* Special cases. */
  2054. cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, shift,
  2055. build_int_cst (stype, 0));
  2056. res = fold_build3_loc (input_location, COND_EXPR, type, cond,
  2057. dshiftl ? arg1 : arg2, res);
  2058. cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, shift,
  2059. build_int_cst (stype, bitsize));
  2060. res = fold_build3_loc (input_location, COND_EXPR, type, cond,
  2061. dshiftl ? arg2 : arg1, res);
  2062. se->expr = res;
  2063. }
  2064. /* Positive difference DIM (x, y) = ((x - y) < 0) ? 0 : x - y. */
  2065. static void
  2066. gfc_conv_intrinsic_dim (gfc_se * se, gfc_expr * expr)
  2067. {
  2068. tree val;
  2069. tree tmp;
  2070. tree type;
  2071. tree zero;
  2072. tree args[2];
  2073. gfc_conv_intrinsic_function_args (se, expr, args, 2);
  2074. type = TREE_TYPE (args[0]);
  2075. val = fold_build2_loc (input_location, MINUS_EXPR, type, args[0], args[1]);
  2076. val = gfc_evaluate_now (val, &se->pre);
  2077. zero = gfc_build_const (type, integer_zero_node);
  2078. tmp = fold_build2_loc (input_location, LE_EXPR, boolean_type_node, val, zero);
  2079. se->expr = fold_build3_loc (input_location, COND_EXPR, type, tmp, zero, val);
  2080. }
  2081. /* SIGN(A, B) is absolute value of A times sign of B.
  2082. The real value versions use library functions to ensure the correct
  2083. handling of negative zero. Integer case implemented as:
  2084. SIGN(A, B) = { tmp = (A ^ B) >> C; (A + tmp) ^ tmp }
  2085. */
  2086. static void
  2087. gfc_conv_intrinsic_sign (gfc_se * se, gfc_expr * expr)
  2088. {
  2089. tree tmp;
  2090. tree type;
  2091. tree args[2];
  2092. gfc_conv_intrinsic_function_args (se, expr, args, 2);
  2093. if (expr->ts.type == BT_REAL)
  2094. {
  2095. tree abs;
  2096. tmp = gfc_builtin_decl_for_float_kind (BUILT_IN_COPYSIGN, expr->ts.kind);
  2097. abs = gfc_builtin_decl_for_float_kind (BUILT_IN_FABS, expr->ts.kind);
  2098. /* We explicitly have to ignore the minus sign. We do so by using
  2099. result = (arg1 == 0) ? abs(arg0) : copysign(arg0, arg1). */
  2100. if (!flag_sign_zero
  2101. && MODE_HAS_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (args[1]))))
  2102. {
  2103. tree cond, zero;
  2104. zero = build_real_from_int_cst (TREE_TYPE (args[1]), integer_zero_node);
  2105. cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
  2106. args[1], zero);
  2107. se->expr = fold_build3_loc (input_location, COND_EXPR,
  2108. TREE_TYPE (args[0]), cond,
  2109. build_call_expr_loc (input_location, abs, 1,
  2110. args[0]),
  2111. build_call_expr_loc (input_location, tmp, 2,
  2112. args[0], args[1]));
  2113. }
  2114. else
  2115. se->expr = build_call_expr_loc (input_location, tmp, 2,
  2116. args[0], args[1]);
  2117. return;
  2118. }
  2119. /* Having excluded floating point types, we know we are now dealing
  2120. with signed integer types. */
  2121. type = TREE_TYPE (args[0]);
  2122. /* Args[0] is used multiple times below. */
  2123. args[0] = gfc_evaluate_now (args[0], &se->pre);
  2124. /* Construct (A ^ B) >> 31, which generates a bit mask of all zeros if
  2125. the signs of A and B are the same, and of all ones if they differ. */
  2126. tmp = fold_build2_loc (input_location, BIT_XOR_EXPR, type, args[0], args[1]);
  2127. tmp = fold_build2_loc (input_location, RSHIFT_EXPR, type, tmp,
  2128. build_int_cst (type, TYPE_PRECISION (type) - 1));
  2129. tmp = gfc_evaluate_now (tmp, &se->pre);
  2130. /* Construct (A + tmp) ^ tmp, which is A if tmp is zero, and -A if tmp]
  2131. is all ones (i.e. -1). */
  2132. se->expr = fold_build2_loc (input_location, BIT_XOR_EXPR, type,
  2133. fold_build2_loc (input_location, PLUS_EXPR,
  2134. type, args[0], tmp), tmp);
  2135. }
  2136. /* Test for the presence of an optional argument. */
  2137. static void
  2138. gfc_conv_intrinsic_present (gfc_se * se, gfc_expr * expr)
  2139. {
  2140. gfc_expr *arg;
  2141. arg = expr->value.function.actual->expr;
  2142. gcc_assert (arg->expr_type == EXPR_VARIABLE);
  2143. se->expr = gfc_conv_expr_present (arg->symtree->n.sym);
  2144. se->expr = convert (gfc_typenode_for_spec (&expr->ts), se->expr);
  2145. }
  2146. /* Calculate the double precision product of two single precision values. */
  2147. static void
  2148. gfc_conv_intrinsic_dprod (gfc_se * se, gfc_expr * expr)
  2149. {
  2150. tree type;
  2151. tree args[2];
  2152. gfc_conv_intrinsic_function_args (se, expr, args, 2);
  2153. /* Convert the args to double precision before multiplying. */
  2154. type = gfc_typenode_for_spec (&expr->ts);
  2155. args[0] = convert (type, args[0]);
  2156. args[1] = convert (type, args[1]);
  2157. se->expr = fold_build2_loc (input_location, MULT_EXPR, type, args[0],
  2158. args[1]);
  2159. }
  2160. /* Return a length one character string containing an ascii character. */
  2161. static void
  2162. gfc_conv_intrinsic_char (gfc_se * se, gfc_expr * expr)
  2163. {
  2164. tree arg[2];
  2165. tree var;
  2166. tree type;
  2167. unsigned int num_args;
  2168. num_args = gfc_intrinsic_argument_list_length (expr);
  2169. gfc_conv_intrinsic_function_args (se, expr, arg, num_args);
  2170. type = gfc_get_char_type (expr->ts.kind);
  2171. var = gfc_create_var (type, "char");
  2172. arg[0] = fold_build1_loc (input_location, NOP_EXPR, type, arg[0]);
  2173. gfc_add_modify (&se->pre, var, arg[0]);
  2174. se->expr = gfc_build_addr_expr (build_pointer_type (type), var);
  2175. se->string_length = build_int_cst (gfc_charlen_type_node, 1);
  2176. }
  2177. static void
  2178. gfc_conv_intrinsic_ctime (gfc_se * se, gfc_expr * expr)
  2179. {
  2180. tree var;
  2181. tree len;
  2182. tree tmp;
  2183. tree cond;
  2184. tree fndecl;
  2185. tree *args;
  2186. unsigned int num_args;
  2187. num_args = gfc_intrinsic_argument_list_length (expr) + 2;
  2188. args = XALLOCAVEC (tree, num_args);
  2189. var = gfc_create_var (pchar_type_node, "pstr");
  2190. len = gfc_create_var (gfc_charlen_type_node, "len");
  2191. gfc_conv_intrinsic_function_args (se, expr, &args[2], num_args - 2);
  2192. args[0] = gfc_build_addr_expr (NULL_TREE, var);
  2193. args[1] = gfc_build_addr_expr (NULL_TREE, len);
  2194. fndecl = build_addr (gfor_fndecl_ctime, current_function_decl);
  2195. tmp = build_call_array_loc (input_location,
  2196. TREE_TYPE (TREE_TYPE (gfor_fndecl_ctime)),
  2197. fndecl, num_args, args);
  2198. gfc_add_expr_to_block (&se->pre, tmp);
  2199. /* Free the temporary afterwards, if necessary. */
  2200. cond = fold_build2_loc (input_location, GT_EXPR, boolean_type_node,
  2201. len, build_int_cst (TREE_TYPE (len), 0));
  2202. tmp = gfc_call_free (var);
  2203. tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
  2204. gfc_add_expr_to_block (&se->post, tmp);
  2205. se->expr = var;
  2206. se->string_length = len;
  2207. }
  2208. static void
  2209. gfc_conv_intrinsic_fdate (gfc_se * se, gfc_expr * expr)
  2210. {
  2211. tree var;
  2212. tree len;
  2213. tree tmp;
  2214. tree cond;
  2215. tree fndecl;
  2216. tree *args;
  2217. unsigned int num_args;
  2218. num_args = gfc_intrinsic_argument_list_length (expr) + 2;
  2219. args = XALLOCAVEC (tree, num_args);
  2220. var = gfc_create_var (pchar_type_node, "pstr");
  2221. len = gfc_create_var (gfc_charlen_type_node, "len");
  2222. gfc_conv_intrinsic_function_args (se, expr, &args[2], num_args - 2);
  2223. args[0] = gfc_build_addr_expr (NULL_TREE, var);
  2224. args[1] = gfc_build_addr_expr (NULL_TREE, len);
  2225. fndecl = build_addr (gfor_fndecl_fdate, current_function_decl);
  2226. tmp = build_call_array_loc (input_location,
  2227. TREE_TYPE (TREE_TYPE (gfor_fndecl_fdate)),
  2228. fndecl, num_args, args);
  2229. gfc_add_expr_to_block (&se->pre, tmp);
  2230. /* Free the temporary afterwards, if necessary. */
  2231. cond = fold_build2_loc (input_location, GT_EXPR, boolean_type_node,
  2232. len, build_int_cst (TREE_TYPE (len), 0));
  2233. tmp = gfc_call_free (var);
  2234. tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
  2235. gfc_add_expr_to_block (&se->post, tmp);
  2236. se->expr = var;
  2237. se->string_length = len;
  2238. }
  2239. /* Call the SYSTEM_CLOCK library functions, handling the type and kind
  2240. conversions. */
  2241. static tree
  2242. conv_intrinsic_system_clock (gfc_code *code)
  2243. {
  2244. stmtblock_t block;
  2245. gfc_se count_se, count_rate_se, count_max_se;
  2246. tree arg1 = NULL_TREE, arg2 = NULL_TREE, arg3 = NULL_TREE;
  2247. tree tmp;
  2248. int least;
  2249. gfc_expr *count = code->ext.actual->expr;
  2250. gfc_expr *count_rate = code->ext.actual->next->expr;
  2251. gfc_expr *count_max = code->ext.actual->next->next->expr;
  2252. /* Evaluate our arguments. */
  2253. if (count)
  2254. {
  2255. gfc_init_se (&count_se, NULL);
  2256. gfc_conv_expr (&count_se, count);
  2257. }
  2258. if (count_rate)
  2259. {
  2260. gfc_init_se (&count_rate_se, NULL);
  2261. gfc_conv_expr (&count_rate_se, count_rate);
  2262. }
  2263. if (count_max)
  2264. {
  2265. gfc_init_se (&count_max_se, NULL);
  2266. gfc_conv_expr (&count_max_se, count_max);
  2267. }
  2268. /* Find the smallest kind found of the arguments. */
  2269. least = 16;
  2270. least = (count && count->ts.kind < least) ? count->ts.kind : least;
  2271. least = (count_rate && count_rate->ts.kind < least) ? count_rate->ts.kind
  2272. : least;
  2273. least = (count_max && count_max->ts.kind < least) ? count_max->ts.kind
  2274. : least;
  2275. /* Prepare temporary variables. */
  2276. if (count)
  2277. {
  2278. if (least >= 8)
  2279. arg1 = gfc_create_var (gfc_get_int_type (8), "count");
  2280. else if (least == 4)
  2281. arg1 = gfc_create_var (gfc_get_int_type (4), "count");
  2282. else if (count->ts.kind == 1)
  2283. arg1 = gfc_conv_mpz_to_tree (gfc_integer_kinds[0].pedantic_min_int,
  2284. count->ts.kind);
  2285. else
  2286. arg1 = gfc_conv_mpz_to_tree (gfc_integer_kinds[1].pedantic_min_int,
  2287. count->ts.kind);
  2288. }
  2289. if (count_rate)
  2290. {
  2291. if (least >= 8)
  2292. arg2 = gfc_create_var (gfc_get_int_type (8), "count_rate");
  2293. else if (least == 4)
  2294. arg2 = gfc_create_var (gfc_get_int_type (4), "count_rate");
  2295. else
  2296. arg2 = integer_zero_node;
  2297. }
  2298. if (count_max)
  2299. {
  2300. if (least >= 8)
  2301. arg3 = gfc_create_var (gfc_get_int_type (8), "count_max");
  2302. else if (least == 4)
  2303. arg3 = gfc_create_var (gfc_get_int_type (4), "count_max");
  2304. else
  2305. arg3 = integer_zero_node;
  2306. }
  2307. /* Make the function call. */
  2308. gfc_init_block (&block);
  2309. if (least <= 2)
  2310. {
  2311. if (least == 1)
  2312. {
  2313. arg1 ? gfc_build_addr_expr (NULL_TREE, arg1)
  2314. : null_pointer_node;
  2315. arg2 ? gfc_build_addr_expr (NULL_TREE, arg2)
  2316. : null_pointer_node;
  2317. arg3 ? gfc_build_addr_expr (NULL_TREE, arg3)
  2318. : null_pointer_node;
  2319. }
  2320. if (least == 2)
  2321. {
  2322. arg1 ? gfc_build_addr_expr (NULL_TREE, arg1)
  2323. : null_pointer_node;
  2324. arg2 ? gfc_build_addr_expr (NULL_TREE, arg2)
  2325. : null_pointer_node;
  2326. arg3 ? gfc_build_addr_expr (NULL_TREE, arg3)
  2327. : null_pointer_node;
  2328. }
  2329. }
  2330. else
  2331. {
  2332. if (least == 4)
  2333. {
  2334. tmp = build_call_expr_loc (input_location,
  2335. gfor_fndecl_system_clock4, 3,
  2336. arg1 ? gfc_build_addr_expr (NULL_TREE, arg1)
  2337. : null_pointer_node,
  2338. arg2 ? gfc_build_addr_expr (NULL_TREE, arg2)
  2339. : null_pointer_node,
  2340. arg3 ? gfc_build_addr_expr (NULL_TREE, arg3)
  2341. : null_pointer_node);
  2342. gfc_add_expr_to_block (&block, tmp);
  2343. }
  2344. /* Handle kind>=8, 10, or 16 arguments */
  2345. if (least >= 8)
  2346. {
  2347. tmp = build_call_expr_loc (input_location,
  2348. gfor_fndecl_system_clock8, 3,
  2349. arg1 ? gfc_build_addr_expr (NULL_TREE, arg1)
  2350. : null_pointer_node,
  2351. arg2 ? gfc_build_addr_expr (NULL_TREE, arg2)
  2352. : null_pointer_node,
  2353. arg3 ? gfc_build_addr_expr (NULL_TREE, arg3)
  2354. : null_pointer_node);
  2355. gfc_add_expr_to_block (&block, tmp);
  2356. }
  2357. }
  2358. /* And store values back if needed. */
  2359. if (arg1 && arg1 != count_se.expr)
  2360. gfc_add_modify (&block, count_se.expr,
  2361. fold_convert (TREE_TYPE (count_se.expr), arg1));
  2362. if (arg2 && arg2 != count_rate_se.expr)
  2363. gfc_add_modify (&block, count_rate_se.expr,
  2364. fold_convert (TREE_TYPE (count_rate_se.expr), arg2));
  2365. if (arg3 && arg3 != count_max_se.expr)
  2366. gfc_add_modify (&block, count_max_se.expr,
  2367. fold_convert (TREE_TYPE (count_max_se.expr), arg3));
  2368. return gfc_finish_block (&block);
  2369. }
  2370. /* Return a character string containing the tty name. */
  2371. static void
  2372. gfc_conv_intrinsic_ttynam (gfc_se * se, gfc_expr * expr)
  2373. {
  2374. tree var;
  2375. tree len;
  2376. tree tmp;
  2377. tree cond;
  2378. tree fndecl;
  2379. tree *args;
  2380. unsigned int num_args;
  2381. num_args = gfc_intrinsic_argument_list_length (expr) + 2;
  2382. args = XALLOCAVEC (tree, num_args);
  2383. var = gfc_create_var (pchar_type_node, "pstr");
  2384. len = gfc_create_var (gfc_charlen_type_node, "len");
  2385. gfc_conv_intrinsic_function_args (se, expr, &args[2], num_args - 2);
  2386. args[0] = gfc_build_addr_expr (NULL_TREE, var);
  2387. args[1] = gfc_build_addr_expr (NULL_TREE, len);
  2388. fndecl = build_addr (gfor_fndecl_ttynam, current_function_decl);
  2389. tmp = build_call_array_loc (input_location,
  2390. TREE_TYPE (TREE_TYPE (gfor_fndecl_ttynam)),
  2391. fndecl, num_args, args);
  2392. gfc_add_expr_to_block (&se->pre, tmp);
  2393. /* Free the temporary afterwards, if necessary. */
  2394. cond = fold_build2_loc (input_location, GT_EXPR, boolean_type_node,
  2395. len, build_int_cst (TREE_TYPE (len), 0));
  2396. tmp = gfc_call_free (var);
  2397. tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
  2398. gfc_add_expr_to_block (&se->post, tmp);
  2399. se->expr = var;
  2400. se->string_length = len;
  2401. }
  2402. /* Get the minimum/maximum value of all the parameters.
  2403. minmax (a1, a2, a3, ...)
  2404. {
  2405. mvar = a1;
  2406. if (a2 .op. mvar || isnan (mvar))
  2407. mvar = a2;
  2408. if (a3 .op. mvar || isnan (mvar))
  2409. mvar = a3;
  2410. ...
  2411. return mvar
  2412. }
  2413. */
  2414. /* TODO: Mismatching types can occur when specific names are used.
  2415. These should be handled during resolution. */
  2416. static void
  2417. gfc_conv_intrinsic_minmax (gfc_se * se, gfc_expr * expr, enum tree_code op)
  2418. {
  2419. tree tmp;
  2420. tree mvar;
  2421. tree val;
  2422. tree thencase;
  2423. tree *args;
  2424. tree type;
  2425. gfc_actual_arglist *argexpr;
  2426. unsigned int i, nargs;
  2427. nargs = gfc_intrinsic_argument_list_length (expr);
  2428. args = XALLOCAVEC (tree, nargs);
  2429. gfc_conv_intrinsic_function_args (se, expr, args, nargs);
  2430. type = gfc_typenode_for_spec (&expr->ts);
  2431. argexpr = expr->value.function.actual;
  2432. if (TREE_TYPE (args[0]) != type)
  2433. args[0] = convert (type, args[0]);
  2434. /* Only evaluate the argument once. */
  2435. if (TREE_CODE (args[0]) != VAR_DECL && !TREE_CONSTANT (args[0]))
  2436. args[0] = gfc_evaluate_now (args[0], &se->pre);
  2437. mvar = gfc_create_var (type, "M");
  2438. gfc_add_modify (&se->pre, mvar, args[0]);
  2439. for (i = 1, argexpr = argexpr->next; i < nargs; i++)
  2440. {
  2441. tree cond, isnan;
  2442. val = args[i];
  2443. /* Handle absent optional arguments by ignoring the comparison. */
  2444. if (argexpr->expr->expr_type == EXPR_VARIABLE
  2445. && argexpr->expr->symtree->n.sym->attr.optional
  2446. && TREE_CODE (val) == INDIRECT_REF)
  2447. cond = fold_build2_loc (input_location,
  2448. NE_EXPR, boolean_type_node,
  2449. TREE_OPERAND (val, 0),
  2450. build_int_cst (TREE_TYPE (TREE_OPERAND (val, 0)), 0));
  2451. else
  2452. {
  2453. cond = NULL_TREE;
  2454. /* Only evaluate the argument once. */
  2455. if (TREE_CODE (val) != VAR_DECL && !TREE_CONSTANT (val))
  2456. val = gfc_evaluate_now (val, &se->pre);
  2457. }
  2458. thencase = build2_v (MODIFY_EXPR, mvar, convert (type, val));
  2459. tmp = fold_build2_loc (input_location, op, boolean_type_node,
  2460. convert (type, val), mvar);
  2461. /* FIXME: When the IEEE_ARITHMETIC module is implemented, the call to
  2462. __builtin_isnan might be made dependent on that module being loaded,
  2463. to help performance of programs that don't rely on IEEE semantics. */
  2464. if (FLOAT_TYPE_P (TREE_TYPE (mvar)))
  2465. {
  2466. isnan = build_call_expr_loc (input_location,
  2467. builtin_decl_explicit (BUILT_IN_ISNAN),
  2468. 1, mvar);
  2469. tmp = fold_build2_loc (input_location, TRUTH_OR_EXPR,
  2470. boolean_type_node, tmp,
  2471. fold_convert (boolean_type_node, isnan));
  2472. }
  2473. tmp = build3_v (COND_EXPR, tmp, thencase,
  2474. build_empty_stmt (input_location));
  2475. if (cond != NULL_TREE)
  2476. tmp = build3_v (COND_EXPR, cond, tmp,
  2477. build_empty_stmt (input_location));
  2478. gfc_add_expr_to_block (&se->pre, tmp);
  2479. argexpr = argexpr->next;
  2480. }
  2481. se->expr = mvar;
  2482. }
  2483. /* Generate library calls for MIN and MAX intrinsics for character
  2484. variables. */
  2485. static void
  2486. gfc_conv_intrinsic_minmax_char (gfc_se * se, gfc_expr * expr, int op)
  2487. {
  2488. tree *args;
  2489. tree var, len, fndecl, tmp, cond, function;
  2490. unsigned int nargs;
  2491. nargs = gfc_intrinsic_argument_list_length (expr);
  2492. args = XALLOCAVEC (tree, nargs + 4);
  2493. gfc_conv_intrinsic_function_args (se, expr, &args[4], nargs);
  2494. /* Create the result variables. */
  2495. len = gfc_create_var (gfc_charlen_type_node, "len");
  2496. args[0] = gfc_build_addr_expr (NULL_TREE, len);
  2497. var = gfc_create_var (gfc_get_pchar_type (expr->ts.kind), "pstr");
  2498. args[1] = gfc_build_addr_expr (ppvoid_type_node, var);
  2499. args[2] = build_int_cst (integer_type_node, op);
  2500. args[3] = build_int_cst (integer_type_node, nargs / 2);
  2501. if (expr->ts.kind == 1)
  2502. function = gfor_fndecl_string_minmax;
  2503. else if (expr->ts.kind == 4)
  2504. function = gfor_fndecl_string_minmax_char4;
  2505. else
  2506. gcc_unreachable ();
  2507. /* Make the function call. */
  2508. fndecl = build_addr (function, current_function_decl);
  2509. tmp = build_call_array_loc (input_location,
  2510. TREE_TYPE (TREE_TYPE (function)), fndecl,
  2511. nargs + 4, args);
  2512. gfc_add_expr_to_block (&se->pre, tmp);
  2513. /* Free the temporary afterwards, if necessary. */
  2514. cond = fold_build2_loc (input_location, GT_EXPR, boolean_type_node,
  2515. len, build_int_cst (TREE_TYPE (len), 0));
  2516. tmp = gfc_call_free (var);
  2517. tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
  2518. gfc_add_expr_to_block (&se->post, tmp);
  2519. se->expr = var;
  2520. se->string_length = len;
  2521. }
  2522. /* Create a symbol node for this intrinsic. The symbol from the frontend
  2523. has the generic name. */
  2524. static gfc_symbol *
  2525. gfc_get_symbol_for_expr (gfc_expr * expr, bool ignore_optional)
  2526. {
  2527. gfc_symbol *sym;
  2528. /* TODO: Add symbols for intrinsic function to the global namespace. */
  2529. gcc_assert (strlen (expr->value.function.name) <= GFC_MAX_SYMBOL_LEN - 5);
  2530. sym = gfc_new_symbol (expr->value.function.name, NULL);
  2531. sym->ts = expr->ts;
  2532. sym->attr.external = 1;
  2533. sym->attr.function = 1;
  2534. sym->attr.always_explicit = 1;
  2535. sym->attr.proc = PROC_INTRINSIC;
  2536. sym->attr.flavor = FL_PROCEDURE;
  2537. sym->result = sym;
  2538. if (expr->rank > 0)
  2539. {
  2540. sym->attr.dimension = 1;
  2541. sym->as = gfc_get_array_spec ();
  2542. sym->as->type = AS_ASSUMED_SHAPE;
  2543. sym->as->rank = expr->rank;
  2544. }
  2545. gfc_copy_formal_args_intr (sym, expr->value.function.isym,
  2546. ignore_optional ? expr->value.function.actual
  2547. : NULL);
  2548. return sym;
  2549. }
  2550. /* Generate a call to an external intrinsic function. */
  2551. static void
  2552. gfc_conv_intrinsic_funcall (gfc_se * se, gfc_expr * expr)
  2553. {
  2554. gfc_symbol *sym;
  2555. vec<tree, va_gc> *append_args;
  2556. gcc_assert (!se->ss || se->ss->info->expr == expr);
  2557. if (se->ss)
  2558. gcc_assert (expr->rank > 0);
  2559. else
  2560. gcc_assert (expr->rank == 0);
  2561. sym = gfc_get_symbol_for_expr (expr, se->ignore_optional);
  2562. /* Calls to libgfortran_matmul need to be appended special arguments,
  2563. to be able to call the BLAS ?gemm functions if required and possible. */
  2564. append_args = NULL;
  2565. if (expr->value.function.isym->id == GFC_ISYM_MATMUL
  2566. && sym->ts.type != BT_LOGICAL)
  2567. {
  2568. tree cint = gfc_get_int_type (gfc_c_int_kind);
  2569. if (flag_external_blas
  2570. && (sym->ts.type == BT_REAL || sym->ts.type == BT_COMPLEX)
  2571. && (sym->ts.kind == 4 || sym->ts.kind == 8))
  2572. {
  2573. tree gemm_fndecl;
  2574. if (sym->ts.type == BT_REAL)
  2575. {
  2576. if (sym->ts.kind == 4)
  2577. gemm_fndecl = gfor_fndecl_sgemm;
  2578. else
  2579. gemm_fndecl = gfor_fndecl_dgemm;
  2580. }
  2581. else
  2582. {
  2583. if (sym->ts.kind == 4)
  2584. gemm_fndecl = gfor_fndecl_cgemm;
  2585. else
  2586. gemm_fndecl = gfor_fndecl_zgemm;
  2587. }
  2588. vec_alloc (append_args, 3);
  2589. append_args->quick_push (build_int_cst (cint, 1));
  2590. append_args->quick_push (build_int_cst (cint,
  2591. flag_blas_matmul_limit));
  2592. append_args->quick_push (gfc_build_addr_expr (NULL_TREE,
  2593. gemm_fndecl));
  2594. }
  2595. else
  2596. {
  2597. vec_alloc (append_args, 3);
  2598. append_args->quick_push (build_int_cst (cint, 0));
  2599. append_args->quick_push (build_int_cst (cint, 0));
  2600. append_args->quick_push (null_pointer_node);
  2601. }
  2602. }
  2603. gfc_conv_procedure_call (se, sym, expr->value.function.actual, expr,
  2604. append_args);
  2605. gfc_free_symbol (sym);
  2606. }
  2607. /* ANY and ALL intrinsics. ANY->op == NE_EXPR, ALL->op == EQ_EXPR.
  2608. Implemented as
  2609. any(a)
  2610. {
  2611. forall (i=...)
  2612. if (a[i] != 0)
  2613. return 1
  2614. end forall
  2615. return 0
  2616. }
  2617. all(a)
  2618. {
  2619. forall (i=...)
  2620. if (a[i] == 0)
  2621. return 0
  2622. end forall
  2623. return 1
  2624. }
  2625. */
  2626. static void
  2627. gfc_conv_intrinsic_anyall (gfc_se * se, gfc_expr * expr, enum tree_code op)
  2628. {
  2629. tree resvar;
  2630. stmtblock_t block;
  2631. stmtblock_t body;
  2632. tree type;
  2633. tree tmp;
  2634. tree found;
  2635. gfc_loopinfo loop;
  2636. gfc_actual_arglist *actual;
  2637. gfc_ss *arrayss;
  2638. gfc_se arrayse;
  2639. tree exit_label;
  2640. if (se->ss)
  2641. {
  2642. gfc_conv_intrinsic_funcall (se, expr);
  2643. return;
  2644. }
  2645. actual = expr->value.function.actual;
  2646. type = gfc_typenode_for_spec (&expr->ts);
  2647. /* Initialize the result. */
  2648. resvar = gfc_create_var (type, "test");
  2649. if (op == EQ_EXPR)
  2650. tmp = convert (type, boolean_true_node);
  2651. else
  2652. tmp = convert (type, boolean_false_node);
  2653. gfc_add_modify (&se->pre, resvar, tmp);
  2654. /* Walk the arguments. */
  2655. arrayss = gfc_walk_expr (actual->expr);
  2656. gcc_assert (arrayss != gfc_ss_terminator);
  2657. /* Initialize the scalarizer. */
  2658. gfc_init_loopinfo (&loop);
  2659. exit_label = gfc_build_label_decl (NULL_TREE);
  2660. TREE_USED (exit_label) = 1;
  2661. gfc_add_ss_to_loop (&loop, arrayss);
  2662. /* Initialize the loop. */
  2663. gfc_conv_ss_startstride (&loop);
  2664. gfc_conv_loop_setup (&loop, &expr->where);
  2665. gfc_mark_ss_chain_used (arrayss, 1);
  2666. /* Generate the loop body. */
  2667. gfc_start_scalarized_body (&loop, &body);
  2668. /* If the condition matches then set the return value. */
  2669. gfc_start_block (&block);
  2670. if (op == EQ_EXPR)
  2671. tmp = convert (type, boolean_false_node);
  2672. else
  2673. tmp = convert (type, boolean_true_node);
  2674. gfc_add_modify (&block, resvar, tmp);
  2675. /* And break out of the loop. */
  2676. tmp = build1_v (GOTO_EXPR, exit_label);
  2677. gfc_add_expr_to_block (&block, tmp);
  2678. found = gfc_finish_block (&block);
  2679. /* Check this element. */
  2680. gfc_init_se (&arrayse, NULL);
  2681. gfc_copy_loopinfo_to_se (&arrayse, &loop);
  2682. arrayse.ss = arrayss;
  2683. gfc_conv_expr_val (&arrayse, actual->expr);
  2684. gfc_add_block_to_block (&body, &arrayse.pre);
  2685. tmp = fold_build2_loc (input_location, op, boolean_type_node, arrayse.expr,
  2686. build_int_cst (TREE_TYPE (arrayse.expr), 0));
  2687. tmp = build3_v (COND_EXPR, tmp, found, build_empty_stmt (input_location));
  2688. gfc_add_expr_to_block (&body, tmp);
  2689. gfc_add_block_to_block (&body, &arrayse.post);
  2690. gfc_trans_scalarizing_loops (&loop, &body);
  2691. /* Add the exit label. */
  2692. tmp = build1_v (LABEL_EXPR, exit_label);
  2693. gfc_add_expr_to_block (&loop.pre, tmp);
  2694. gfc_add_block_to_block (&se->pre, &loop.pre);
  2695. gfc_add_block_to_block (&se->pre, &loop.post);
  2696. gfc_cleanup_loop (&loop);
  2697. se->expr = resvar;
  2698. }
  2699. /* COUNT(A) = Number of true elements in A. */
  2700. static void
  2701. gfc_conv_intrinsic_count (gfc_se * se, gfc_expr * expr)
  2702. {
  2703. tree resvar;
  2704. tree type;
  2705. stmtblock_t body;
  2706. tree tmp;
  2707. gfc_loopinfo loop;
  2708. gfc_actual_arglist *actual;
  2709. gfc_ss *arrayss;
  2710. gfc_se arrayse;
  2711. if (se->ss)
  2712. {
  2713. gfc_conv_intrinsic_funcall (se, expr);
  2714. return;
  2715. }
  2716. actual = expr->value.function.actual;
  2717. type = gfc_typenode_for_spec (&expr->ts);
  2718. /* Initialize the result. */
  2719. resvar = gfc_create_var (type, "count");
  2720. gfc_add_modify (&se->pre, resvar, build_int_cst (type, 0));
  2721. /* Walk the arguments. */
  2722. arrayss = gfc_walk_expr (actual->expr);
  2723. gcc_assert (arrayss != gfc_ss_terminator);
  2724. /* Initialize the scalarizer. */
  2725. gfc_init_loopinfo (&loop);
  2726. gfc_add_ss_to_loop (&loop, arrayss);
  2727. /* Initialize the loop. */
  2728. gfc_conv_ss_startstride (&loop);
  2729. gfc_conv_loop_setup (&loop, &expr->where);
  2730. gfc_mark_ss_chain_used (arrayss, 1);
  2731. /* Generate the loop body. */
  2732. gfc_start_scalarized_body (&loop, &body);
  2733. tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (resvar),
  2734. resvar, build_int_cst (TREE_TYPE (resvar), 1));
  2735. tmp = build2_v (MODIFY_EXPR, resvar, tmp);
  2736. gfc_init_se (&arrayse, NULL);
  2737. gfc_copy_loopinfo_to_se (&arrayse, &loop);
  2738. arrayse.ss = arrayss;
  2739. gfc_conv_expr_val (&arrayse, actual->expr);
  2740. tmp = build3_v (COND_EXPR, arrayse.expr, tmp,
  2741. build_empty_stmt (input_location));
  2742. gfc_add_block_to_block (&body, &arrayse.pre);
  2743. gfc_add_expr_to_block (&body, tmp);
  2744. gfc_add_block_to_block (&body, &arrayse.post);
  2745. gfc_trans_scalarizing_loops (&loop, &body);
  2746. gfc_add_block_to_block (&se->pre, &loop.pre);
  2747. gfc_add_block_to_block (&se->pre, &loop.post);
  2748. gfc_cleanup_loop (&loop);
  2749. se->expr = resvar;
  2750. }
  2751. /* Update given gfc_se to have ss component pointing to the nested gfc_ss
  2752. struct and return the corresponding loopinfo. */
  2753. static gfc_loopinfo *
  2754. enter_nested_loop (gfc_se *se)
  2755. {
  2756. se->ss = se->ss->nested_ss;
  2757. gcc_assert (se->ss == se->ss->loop->ss);
  2758. return se->ss->loop;
  2759. }
  2760. /* Inline implementation of the sum and product intrinsics. */
  2761. static void
  2762. gfc_conv_intrinsic_arith (gfc_se * se, gfc_expr * expr, enum tree_code op,
  2763. bool norm2)
  2764. {
  2765. tree resvar;
  2766. tree scale = NULL_TREE;
  2767. tree type;
  2768. stmtblock_t body;
  2769. stmtblock_t block;
  2770. tree tmp;
  2771. gfc_loopinfo loop, *ploop;
  2772. gfc_actual_arglist *arg_array, *arg_mask;
  2773. gfc_ss *arrayss = NULL;
  2774. gfc_ss *maskss = NULL;
  2775. gfc_se arrayse;
  2776. gfc_se maskse;
  2777. gfc_se *parent_se;
  2778. gfc_expr *arrayexpr;
  2779. gfc_expr *maskexpr;
  2780. if (expr->rank > 0)
  2781. {
  2782. gcc_assert (gfc_inline_intrinsic_function_p (expr));
  2783. parent_se = se;
  2784. }
  2785. else
  2786. parent_se = NULL;
  2787. type = gfc_typenode_for_spec (&expr->ts);
  2788. /* Initialize the result. */
  2789. resvar = gfc_create_var (type, "val");
  2790. if (norm2)
  2791. {
  2792. /* result = 0.0;
  2793. scale = 1.0. */
  2794. scale = gfc_create_var (type, "scale");
  2795. gfc_add_modify (&se->pre, scale,
  2796. gfc_build_const (type, integer_one_node));
  2797. tmp = gfc_build_const (type, integer_zero_node);
  2798. }
  2799. else if (op == PLUS_EXPR || op == BIT_IOR_EXPR || op == BIT_XOR_EXPR)
  2800. tmp = gfc_build_const (type, integer_zero_node);
  2801. else if (op == NE_EXPR)
  2802. /* PARITY. */
  2803. tmp = convert (type, boolean_false_node);
  2804. else if (op == BIT_AND_EXPR)
  2805. tmp = gfc_build_const (type, fold_build1_loc (input_location, NEGATE_EXPR,
  2806. type, integer_one_node));
  2807. else
  2808. tmp = gfc_build_const (type, integer_one_node);
  2809. gfc_add_modify (&se->pre, resvar, tmp);
  2810. arg_array = expr->value.function.actual;
  2811. arrayexpr = arg_array->expr;
  2812. if (op == NE_EXPR || norm2)
  2813. /* PARITY and NORM2. */
  2814. maskexpr = NULL;
  2815. else
  2816. {
  2817. arg_mask = arg_array->next->next;
  2818. gcc_assert (arg_mask != NULL);
  2819. maskexpr = arg_mask->expr;
  2820. }
  2821. if (expr->rank == 0)
  2822. {
  2823. /* Walk the arguments. */
  2824. arrayss = gfc_walk_expr (arrayexpr);
  2825. gcc_assert (arrayss != gfc_ss_terminator);
  2826. if (maskexpr && maskexpr->rank > 0)
  2827. {
  2828. maskss = gfc_walk_expr (maskexpr);
  2829. gcc_assert (maskss != gfc_ss_terminator);
  2830. }
  2831. else
  2832. maskss = NULL;
  2833. /* Initialize the scalarizer. */
  2834. gfc_init_loopinfo (&loop);
  2835. gfc_add_ss_to_loop (&loop, arrayss);
  2836. if (maskexpr && maskexpr->rank > 0)
  2837. gfc_add_ss_to_loop (&loop, maskss);
  2838. /* Initialize the loop. */
  2839. gfc_conv_ss_startstride (&loop);
  2840. gfc_conv_loop_setup (&loop, &expr->where);
  2841. gfc_mark_ss_chain_used (arrayss, 1);
  2842. if (maskexpr && maskexpr->rank > 0)
  2843. gfc_mark_ss_chain_used (maskss, 1);
  2844. ploop = &loop;
  2845. }
  2846. else
  2847. /* All the work has been done in the parent loops. */
  2848. ploop = enter_nested_loop (se);
  2849. gcc_assert (ploop);
  2850. /* Generate the loop body. */
  2851. gfc_start_scalarized_body (ploop, &body);
  2852. /* If we have a mask, only add this element if the mask is set. */
  2853. if (maskexpr && maskexpr->rank > 0)
  2854. {
  2855. gfc_init_se (&maskse, parent_se);
  2856. gfc_copy_loopinfo_to_se (&maskse, ploop);
  2857. if (expr->rank == 0)
  2858. maskse.ss = maskss;
  2859. gfc_conv_expr_val (&maskse, maskexpr);
  2860. gfc_add_block_to_block (&body, &maskse.pre);
  2861. gfc_start_block (&block);
  2862. }
  2863. else
  2864. gfc_init_block (&block);
  2865. /* Do the actual summation/product. */
  2866. gfc_init_se (&arrayse, parent_se);
  2867. gfc_copy_loopinfo_to_se (&arrayse, ploop);
  2868. if (expr->rank == 0)
  2869. arrayse.ss = arrayss;
  2870. gfc_conv_expr_val (&arrayse, arrayexpr);
  2871. gfc_add_block_to_block (&block, &arrayse.pre);
  2872. if (norm2)
  2873. {
  2874. /* if (x (i) != 0.0)
  2875. {
  2876. absX = abs(x(i))
  2877. if (absX > scale)
  2878. {
  2879. val = scale/absX;
  2880. result = 1.0 + result * val * val;
  2881. scale = absX;
  2882. }
  2883. else
  2884. {
  2885. val = absX/scale;
  2886. result += val * val;
  2887. }
  2888. } */
  2889. tree res1, res2, cond, absX, val;
  2890. stmtblock_t ifblock1, ifblock2, ifblock3;
  2891. gfc_init_block (&ifblock1);
  2892. absX = gfc_create_var (type, "absX");
  2893. gfc_add_modify (&ifblock1, absX,
  2894. fold_build1_loc (input_location, ABS_EXPR, type,
  2895. arrayse.expr));
  2896. val = gfc_create_var (type, "val");
  2897. gfc_add_expr_to_block (&ifblock1, val);
  2898. gfc_init_block (&ifblock2);
  2899. gfc_add_modify (&ifblock2, val,
  2900. fold_build2_loc (input_location, RDIV_EXPR, type, scale,
  2901. absX));
  2902. res1 = fold_build2_loc (input_location, MULT_EXPR, type, val, val);
  2903. res1 = fold_build2_loc (input_location, MULT_EXPR, type, resvar, res1);
  2904. res1 = fold_build2_loc (input_location, PLUS_EXPR, type, res1,
  2905. gfc_build_const (type, integer_one_node));
  2906. gfc_add_modify (&ifblock2, resvar, res1);
  2907. gfc_add_modify (&ifblock2, scale, absX);
  2908. res1 = gfc_finish_block (&ifblock2);
  2909. gfc_init_block (&ifblock3);
  2910. gfc_add_modify (&ifblock3, val,
  2911. fold_build2_loc (input_location, RDIV_EXPR, type, absX,
  2912. scale));
  2913. res2 = fold_build2_loc (input_location, MULT_EXPR, type, val, val);
  2914. res2 = fold_build2_loc (input_location, PLUS_EXPR, type, resvar, res2);
  2915. gfc_add_modify (&ifblock3, resvar, res2);
  2916. res2 = gfc_finish_block (&ifblock3);
  2917. cond = fold_build2_loc (input_location, GT_EXPR, boolean_type_node,
  2918. absX, scale);
  2919. tmp = build3_v (COND_EXPR, cond, res1, res2);
  2920. gfc_add_expr_to_block (&ifblock1, tmp);
  2921. tmp = gfc_finish_block (&ifblock1);
  2922. cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
  2923. arrayse.expr,
  2924. gfc_build_const (type, integer_zero_node));
  2925. tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
  2926. gfc_add_expr_to_block (&block, tmp);
  2927. }
  2928. else
  2929. {
  2930. tmp = fold_build2_loc (input_location, op, type, resvar, arrayse.expr);
  2931. gfc_add_modify (&block, resvar, tmp);
  2932. }
  2933. gfc_add_block_to_block (&block, &arrayse.post);
  2934. if (maskexpr && maskexpr->rank > 0)
  2935. {
  2936. /* We enclose the above in if (mask) {...} . */
  2937. tmp = gfc_finish_block (&block);
  2938. tmp = build3_v (COND_EXPR, maskse.expr, tmp,
  2939. build_empty_stmt (input_location));
  2940. }
  2941. else
  2942. tmp = gfc_finish_block (&block);
  2943. gfc_add_expr_to_block (&body, tmp);
  2944. gfc_trans_scalarizing_loops (ploop, &body);
  2945. /* For a scalar mask, enclose the loop in an if statement. */
  2946. if (maskexpr && maskexpr->rank == 0)
  2947. {
  2948. gfc_init_block (&block);
  2949. gfc_add_block_to_block (&block, &ploop->pre);
  2950. gfc_add_block_to_block (&block, &ploop->post);
  2951. tmp = gfc_finish_block (&block);
  2952. if (expr->rank > 0)
  2953. {
  2954. tmp = build3_v (COND_EXPR, se->ss->info->data.scalar.value, tmp,
  2955. build_empty_stmt (input_location));
  2956. gfc_advance_se_ss_chain (se);
  2957. }
  2958. else
  2959. {
  2960. gcc_assert (expr->rank == 0);
  2961. gfc_init_se (&maskse, NULL);
  2962. gfc_conv_expr_val (&maskse, maskexpr);
  2963. tmp = build3_v (COND_EXPR, maskse.expr, tmp,
  2964. build_empty_stmt (input_location));
  2965. }
  2966. gfc_add_expr_to_block (&block, tmp);
  2967. gfc_add_block_to_block (&se->pre, &block);
  2968. gcc_assert (se->post.head == NULL);
  2969. }
  2970. else
  2971. {
  2972. gfc_add_block_to_block (&se->pre, &ploop->pre);
  2973. gfc_add_block_to_block (&se->pre, &ploop->post);
  2974. }
  2975. if (expr->rank == 0)
  2976. gfc_cleanup_loop (ploop);
  2977. if (norm2)
  2978. {
  2979. /* result = scale * sqrt(result). */
  2980. tree sqrt;
  2981. sqrt = gfc_builtin_decl_for_float_kind (BUILT_IN_SQRT, expr->ts.kind);
  2982. resvar = build_call_expr_loc (input_location,
  2983. sqrt, 1, resvar);
  2984. resvar = fold_build2_loc (input_location, MULT_EXPR, type, scale, resvar);
  2985. }
  2986. se->expr = resvar;
  2987. }
  2988. /* Inline implementation of the dot_product intrinsic. This function
  2989. is based on gfc_conv_intrinsic_arith (the previous function). */
  2990. static void
  2991. gfc_conv_intrinsic_dot_product (gfc_se * se, gfc_expr * expr)
  2992. {
  2993. tree resvar;
  2994. tree type;
  2995. stmtblock_t body;
  2996. stmtblock_t block;
  2997. tree tmp;
  2998. gfc_loopinfo loop;
  2999. gfc_actual_arglist *actual;
  3000. gfc_ss *arrayss1, *arrayss2;
  3001. gfc_se arrayse1, arrayse2;
  3002. gfc_expr *arrayexpr1, *arrayexpr2;
  3003. type = gfc_typenode_for_spec (&expr->ts);
  3004. /* Initialize the result. */
  3005. resvar = gfc_create_var (type, "val");
  3006. if (expr->ts.type == BT_LOGICAL)
  3007. tmp = build_int_cst (type, 0);
  3008. else
  3009. tmp = gfc_build_const (type, integer_zero_node);
  3010. gfc_add_modify (&se->pre, resvar, tmp);
  3011. /* Walk argument #1. */
  3012. actual = expr->value.function.actual;
  3013. arrayexpr1 = actual->expr;
  3014. arrayss1 = gfc_walk_expr (arrayexpr1);
  3015. gcc_assert (arrayss1 != gfc_ss_terminator);
  3016. /* Walk argument #2. */
  3017. actual = actual->next;
  3018. arrayexpr2 = actual->expr;
  3019. arrayss2 = gfc_walk_expr (arrayexpr2);
  3020. gcc_assert (arrayss2 != gfc_ss_terminator);
  3021. /* Initialize the scalarizer. */
  3022. gfc_init_loopinfo (&loop);
  3023. gfc_add_ss_to_loop (&loop, arrayss1);
  3024. gfc_add_ss_to_loop (&loop, arrayss2);
  3025. /* Initialize the loop. */
  3026. gfc_conv_ss_startstride (&loop);
  3027. gfc_conv_loop_setup (&loop, &expr->where);
  3028. gfc_mark_ss_chain_used (arrayss1, 1);
  3029. gfc_mark_ss_chain_used (arrayss2, 1);
  3030. /* Generate the loop body. */
  3031. gfc_start_scalarized_body (&loop, &body);
  3032. gfc_init_block (&block);
  3033. /* Make the tree expression for [conjg(]array1[)]. */
  3034. gfc_init_se (&arrayse1, NULL);
  3035. gfc_copy_loopinfo_to_se (&arrayse1, &loop);
  3036. arrayse1.ss = arrayss1;
  3037. gfc_conv_expr_val (&arrayse1, arrayexpr1);
  3038. if (expr->ts.type == BT_COMPLEX)
  3039. arrayse1.expr = fold_build1_loc (input_location, CONJ_EXPR, type,
  3040. arrayse1.expr);
  3041. gfc_add_block_to_block (&block, &arrayse1.pre);
  3042. /* Make the tree expression for array2. */
  3043. gfc_init_se (&arrayse2, NULL);
  3044. gfc_copy_loopinfo_to_se (&arrayse2, &loop);
  3045. arrayse2.ss = arrayss2;
  3046. gfc_conv_expr_val (&arrayse2, arrayexpr2);
  3047. gfc_add_block_to_block (&block, &arrayse2.pre);
  3048. /* Do the actual product and sum. */
  3049. if (expr->ts.type == BT_LOGICAL)
  3050. {
  3051. tmp = fold_build2_loc (input_location, TRUTH_AND_EXPR, type,
  3052. arrayse1.expr, arrayse2.expr);
  3053. tmp = fold_build2_loc (input_location, TRUTH_OR_EXPR, type, resvar, tmp);
  3054. }
  3055. else
  3056. {
  3057. tmp = fold_build2_loc (input_location, MULT_EXPR, type, arrayse1.expr,
  3058. arrayse2.expr);
  3059. tmp = fold_build2_loc (input_location, PLUS_EXPR, type, resvar, tmp);
  3060. }
  3061. gfc_add_modify (&block, resvar, tmp);
  3062. /* Finish up the loop block and the loop. */
  3063. tmp = gfc_finish_block (&block);
  3064. gfc_add_expr_to_block (&body, tmp);
  3065. gfc_trans_scalarizing_loops (&loop, &body);
  3066. gfc_add_block_to_block (&se->pre, &loop.pre);
  3067. gfc_add_block_to_block (&se->pre, &loop.post);
  3068. gfc_cleanup_loop (&loop);
  3069. se->expr = resvar;
  3070. }
  3071. /* Emit code for minloc or maxloc intrinsic. There are many different cases
  3072. we need to handle. For performance reasons we sometimes create two
  3073. loops instead of one, where the second one is much simpler.
  3074. Examples for minloc intrinsic:
  3075. 1) Result is an array, a call is generated
  3076. 2) Array mask is used and NaNs need to be supported:
  3077. limit = Infinity;
  3078. pos = 0;
  3079. S = from;
  3080. while (S <= to) {
  3081. if (mask[S]) {
  3082. if (pos == 0) pos = S + (1 - from);
  3083. if (a[S] <= limit) { limit = a[S]; pos = S + (1 - from); goto lab1; }
  3084. }
  3085. S++;
  3086. }
  3087. goto lab2;
  3088. lab1:;
  3089. while (S <= to) {
  3090. if (mask[S]) if (a[S] < limit) { limit = a[S]; pos = S + (1 - from); }
  3091. S++;
  3092. }
  3093. lab2:;
  3094. 3) NaNs need to be supported, but it is known at compile time or cheaply
  3095. at runtime whether array is nonempty or not:
  3096. limit = Infinity;
  3097. pos = 0;
  3098. S = from;
  3099. while (S <= to) {
  3100. if (a[S] <= limit) { limit = a[S]; pos = S + (1 - from); goto lab1; }
  3101. S++;
  3102. }
  3103. if (from <= to) pos = 1;
  3104. goto lab2;
  3105. lab1:;
  3106. while (S <= to) {
  3107. if (a[S] < limit) { limit = a[S]; pos = S + (1 - from); }
  3108. S++;
  3109. }
  3110. lab2:;
  3111. 4) NaNs aren't supported, array mask is used:
  3112. limit = infinities_supported ? Infinity : huge (limit);
  3113. pos = 0;
  3114. S = from;
  3115. while (S <= to) {
  3116. if (mask[S]) { limit = a[S]; pos = S + (1 - from); goto lab1; }
  3117. S++;
  3118. }
  3119. goto lab2;
  3120. lab1:;
  3121. while (S <= to) {
  3122. if (mask[S]) if (a[S] < limit) { limit = a[S]; pos = S + (1 - from); }
  3123. S++;
  3124. }
  3125. lab2:;
  3126. 5) Same without array mask:
  3127. limit = infinities_supported ? Infinity : huge (limit);
  3128. pos = (from <= to) ? 1 : 0;
  3129. S = from;
  3130. while (S <= to) {
  3131. if (a[S] < limit) { limit = a[S]; pos = S + (1 - from); }
  3132. S++;
  3133. }
  3134. For 3) and 5), if mask is scalar, this all goes into a conditional,
  3135. setting pos = 0; in the else branch. */
  3136. static void
  3137. gfc_conv_intrinsic_minmaxloc (gfc_se * se, gfc_expr * expr, enum tree_code op)
  3138. {
  3139. stmtblock_t body;
  3140. stmtblock_t block;
  3141. stmtblock_t ifblock;
  3142. stmtblock_t elseblock;
  3143. tree limit;
  3144. tree type;
  3145. tree tmp;
  3146. tree cond;
  3147. tree elsetmp;
  3148. tree ifbody;
  3149. tree offset;
  3150. tree nonempty;
  3151. tree lab1, lab2;
  3152. gfc_loopinfo loop;
  3153. gfc_actual_arglist *actual;
  3154. gfc_ss *arrayss;
  3155. gfc_ss *maskss;
  3156. gfc_se arrayse;
  3157. gfc_se maskse;
  3158. gfc_expr *arrayexpr;
  3159. gfc_expr *maskexpr;
  3160. tree pos;
  3161. int n;
  3162. if (se->ss)
  3163. {
  3164. gfc_conv_intrinsic_funcall (se, expr);
  3165. return;
  3166. }
  3167. /* Initialize the result. */
  3168. pos = gfc_create_var (gfc_array_index_type, "pos");
  3169. offset = gfc_create_var (gfc_array_index_type, "offset");
  3170. type = gfc_typenode_for_spec (&expr->ts);
  3171. /* Walk the arguments. */
  3172. actual = expr->value.function.actual;
  3173. arrayexpr = actual->expr;
  3174. arrayss = gfc_walk_expr (arrayexpr);
  3175. gcc_assert (arrayss != gfc_ss_terminator);
  3176. actual = actual->next->next;
  3177. gcc_assert (actual);
  3178. maskexpr = actual->expr;
  3179. nonempty = NULL;
  3180. if (maskexpr && maskexpr->rank != 0)
  3181. {
  3182. maskss = gfc_walk_expr (maskexpr);
  3183. gcc_assert (maskss != gfc_ss_terminator);
  3184. }
  3185. else
  3186. {
  3187. mpz_t asize;
  3188. if (gfc_array_size (arrayexpr, &asize))
  3189. {
  3190. nonempty = gfc_conv_mpz_to_tree (asize, gfc_index_integer_kind);
  3191. mpz_clear (asize);
  3192. nonempty = fold_build2_loc (input_location, GT_EXPR,
  3193. boolean_type_node, nonempty,
  3194. gfc_index_zero_node);
  3195. }
  3196. maskss = NULL;
  3197. }
  3198. limit = gfc_create_var (gfc_typenode_for_spec (&arrayexpr->ts), "limit");
  3199. switch (arrayexpr->ts.type)
  3200. {
  3201. case BT_REAL:
  3202. tmp = gfc_build_inf_or_huge (TREE_TYPE (limit), arrayexpr->ts.kind);
  3203. break;
  3204. case BT_INTEGER:
  3205. n = gfc_validate_kind (arrayexpr->ts.type, arrayexpr->ts.kind, false);
  3206. tmp = gfc_conv_mpz_to_tree (gfc_integer_kinds[n].huge,
  3207. arrayexpr->ts.kind);
  3208. break;
  3209. default:
  3210. gcc_unreachable ();
  3211. }
  3212. /* We start with the most negative possible value for MAXLOC, and the most
  3213. positive possible value for MINLOC. The most negative possible value is
  3214. -HUGE for BT_REAL and (-HUGE - 1) for BT_INTEGER; the most positive
  3215. possible value is HUGE in both cases. */
  3216. if (op == GT_EXPR)
  3217. tmp = fold_build1_loc (input_location, NEGATE_EXPR, TREE_TYPE (tmp), tmp);
  3218. if (op == GT_EXPR && arrayexpr->ts.type == BT_INTEGER)
  3219. tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (tmp), tmp,
  3220. build_int_cst (TREE_TYPE (tmp), 1));
  3221. gfc_add_modify (&se->pre, limit, tmp);
  3222. /* Initialize the scalarizer. */
  3223. gfc_init_loopinfo (&loop);
  3224. gfc_add_ss_to_loop (&loop, arrayss);
  3225. if (maskss)
  3226. gfc_add_ss_to_loop (&loop, maskss);
  3227. /* Initialize the loop. */
  3228. gfc_conv_ss_startstride (&loop);
  3229. /* The code generated can have more than one loop in sequence (see the
  3230. comment at the function header). This doesn't work well with the
  3231. scalarizer, which changes arrays' offset when the scalarization loops
  3232. are generated (see gfc_trans_preloop_setup). Fortunately, {min,max}loc
  3233. are currently inlined in the scalar case only (for which loop is of rank
  3234. one). As there is no dependency to care about in that case, there is no
  3235. temporary, so that we can use the scalarizer temporary code to handle
  3236. multiple loops. Thus, we set temp_dim here, we call gfc_mark_ss_chain_used
  3237. with flag=3 later, and we use gfc_trans_scalarized_loop_boundary even later
  3238. to restore offset.
  3239. TODO: this prevents inlining of rank > 0 minmaxloc calls, so this
  3240. should eventually go away. We could either create two loops properly,
  3241. or find another way to save/restore the array offsets between the two
  3242. loops (without conflicting with temporary management), or use a single
  3243. loop minmaxloc implementation. See PR 31067. */
  3244. loop.temp_dim = loop.dimen;
  3245. gfc_conv_loop_setup (&loop, &expr->where);
  3246. gcc_assert (loop.dimen == 1);
  3247. if (nonempty == NULL && maskss == NULL && loop.from[0] && loop.to[0])
  3248. nonempty = fold_build2_loc (input_location, LE_EXPR, boolean_type_node,
  3249. loop.from[0], loop.to[0]);
  3250. lab1 = NULL;
  3251. lab2 = NULL;
  3252. /* Initialize the position to zero, following Fortran 2003. We are free
  3253. to do this because Fortran 95 allows the result of an entirely false
  3254. mask to be processor dependent. If we know at compile time the array
  3255. is non-empty and no MASK is used, we can initialize to 1 to simplify
  3256. the inner loop. */
  3257. if (nonempty != NULL && !HONOR_NANS (DECL_MODE (limit)))
  3258. gfc_add_modify (&loop.pre, pos,
  3259. fold_build3_loc (input_location, COND_EXPR,
  3260. gfc_array_index_type,
  3261. nonempty, gfc_index_one_node,
  3262. gfc_index_zero_node));
  3263. else
  3264. {
  3265. gfc_add_modify (&loop.pre, pos, gfc_index_zero_node);
  3266. lab1 = gfc_build_label_decl (NULL_TREE);
  3267. TREE_USED (lab1) = 1;
  3268. lab2 = gfc_build_label_decl (NULL_TREE);
  3269. TREE_USED (lab2) = 1;
  3270. }
  3271. /* An offset must be added to the loop
  3272. counter to obtain the required position. */
  3273. gcc_assert (loop.from[0]);
  3274. tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
  3275. gfc_index_one_node, loop.from[0]);
  3276. gfc_add_modify (&loop.pre, offset, tmp);
  3277. gfc_mark_ss_chain_used (arrayss, lab1 ? 3 : 1);
  3278. if (maskss)
  3279. gfc_mark_ss_chain_used (maskss, lab1 ? 3 : 1);
  3280. /* Generate the loop body. */
  3281. gfc_start_scalarized_body (&loop, &body);
  3282. /* If we have a mask, only check this element if the mask is set. */
  3283. if (maskss)
  3284. {
  3285. gfc_init_se (&maskse, NULL);
  3286. gfc_copy_loopinfo_to_se (&maskse, &loop);
  3287. maskse.ss = maskss;
  3288. gfc_conv_expr_val (&maskse, maskexpr);
  3289. gfc_add_block_to_block (&body, &maskse.pre);
  3290. gfc_start_block (&block);
  3291. }
  3292. else
  3293. gfc_init_block (&block);
  3294. /* Compare with the current limit. */
  3295. gfc_init_se (&arrayse, NULL);
  3296. gfc_copy_loopinfo_to_se (&arrayse, &loop);
  3297. arrayse.ss = arrayss;
  3298. gfc_conv_expr_val (&arrayse, arrayexpr);
  3299. gfc_add_block_to_block (&block, &arrayse.pre);
  3300. /* We do the following if this is a more extreme value. */
  3301. gfc_start_block (&ifblock);
  3302. /* Assign the value to the limit... */
  3303. gfc_add_modify (&ifblock, limit, arrayse.expr);
  3304. if (nonempty == NULL && HONOR_NANS (DECL_MODE (limit)))
  3305. {
  3306. stmtblock_t ifblock2;
  3307. tree ifbody2;
  3308. gfc_start_block (&ifblock2);
  3309. tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (pos),
  3310. loop.loopvar[0], offset);
  3311. gfc_add_modify (&ifblock2, pos, tmp);
  3312. ifbody2 = gfc_finish_block (&ifblock2);
  3313. cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, pos,
  3314. gfc_index_zero_node);
  3315. tmp = build3_v (COND_EXPR, cond, ifbody2,
  3316. build_empty_stmt (input_location));
  3317. gfc_add_expr_to_block (&block, tmp);
  3318. }
  3319. tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (pos),
  3320. loop.loopvar[0], offset);
  3321. gfc_add_modify (&ifblock, pos, tmp);
  3322. if (lab1)
  3323. gfc_add_expr_to_block (&ifblock, build1_v (GOTO_EXPR, lab1));
  3324. ifbody = gfc_finish_block (&ifblock);
  3325. if (!lab1 || HONOR_NANS (DECL_MODE (limit)))
  3326. {
  3327. if (lab1)
  3328. cond = fold_build2_loc (input_location,
  3329. op == GT_EXPR ? GE_EXPR : LE_EXPR,
  3330. boolean_type_node, arrayse.expr, limit);
  3331. else
  3332. cond = fold_build2_loc (input_location, op, boolean_type_node,
  3333. arrayse.expr, limit);
  3334. ifbody = build3_v (COND_EXPR, cond, ifbody,
  3335. build_empty_stmt (input_location));
  3336. }
  3337. gfc_add_expr_to_block (&block, ifbody);
  3338. if (maskss)
  3339. {
  3340. /* We enclose the above in if (mask) {...}. */
  3341. tmp = gfc_finish_block (&block);
  3342. tmp = build3_v (COND_EXPR, maskse.expr, tmp,
  3343. build_empty_stmt (input_location));
  3344. }
  3345. else
  3346. tmp = gfc_finish_block (&block);
  3347. gfc_add_expr_to_block (&body, tmp);
  3348. if (lab1)
  3349. {
  3350. gfc_trans_scalarized_loop_boundary (&loop, &body);
  3351. if (HONOR_NANS (DECL_MODE (limit)))
  3352. {
  3353. if (nonempty != NULL)
  3354. {
  3355. ifbody = build2_v (MODIFY_EXPR, pos, gfc_index_one_node);
  3356. tmp = build3_v (COND_EXPR, nonempty, ifbody,
  3357. build_empty_stmt (input_location));
  3358. gfc_add_expr_to_block (&loop.code[0], tmp);
  3359. }
  3360. }
  3361. gfc_add_expr_to_block (&loop.code[0], build1_v (GOTO_EXPR, lab2));
  3362. gfc_add_expr_to_block (&loop.code[0], build1_v (LABEL_EXPR, lab1));
  3363. /* If we have a mask, only check this element if the mask is set. */
  3364. if (maskss)
  3365. {
  3366. gfc_init_se (&maskse, NULL);
  3367. gfc_copy_loopinfo_to_se (&maskse, &loop);
  3368. maskse.ss = maskss;
  3369. gfc_conv_expr_val (&maskse, maskexpr);
  3370. gfc_add_block_to_block (&body, &maskse.pre);
  3371. gfc_start_block (&block);
  3372. }
  3373. else
  3374. gfc_init_block (&block);
  3375. /* Compare with the current limit. */
  3376. gfc_init_se (&arrayse, NULL);
  3377. gfc_copy_loopinfo_to_se (&arrayse, &loop);
  3378. arrayse.ss = arrayss;
  3379. gfc_conv_expr_val (&arrayse, arrayexpr);
  3380. gfc_add_block_to_block (&block, &arrayse.pre);
  3381. /* We do the following if this is a more extreme value. */
  3382. gfc_start_block (&ifblock);
  3383. /* Assign the value to the limit... */
  3384. gfc_add_modify (&ifblock, limit, arrayse.expr);
  3385. tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (pos),
  3386. loop.loopvar[0], offset);
  3387. gfc_add_modify (&ifblock, pos, tmp);
  3388. ifbody = gfc_finish_block (&ifblock);
  3389. cond = fold_build2_loc (input_location, op, boolean_type_node,
  3390. arrayse.expr, limit);
  3391. tmp = build3_v (COND_EXPR, cond, ifbody,
  3392. build_empty_stmt (input_location));
  3393. gfc_add_expr_to_block (&block, tmp);
  3394. if (maskss)
  3395. {
  3396. /* We enclose the above in if (mask) {...}. */
  3397. tmp = gfc_finish_block (&block);
  3398. tmp = build3_v (COND_EXPR, maskse.expr, tmp,
  3399. build_empty_stmt (input_location));
  3400. }
  3401. else
  3402. tmp = gfc_finish_block (&block);
  3403. gfc_add_expr_to_block (&body, tmp);
  3404. /* Avoid initializing loopvar[0] again, it should be left where
  3405. it finished by the first loop. */
  3406. loop.from[0] = loop.loopvar[0];
  3407. }
  3408. gfc_trans_scalarizing_loops (&loop, &body);
  3409. if (lab2)
  3410. gfc_add_expr_to_block (&loop.pre, build1_v (LABEL_EXPR, lab2));
  3411. /* For a scalar mask, enclose the loop in an if statement. */
  3412. if (maskexpr && maskss == NULL)
  3413. {
  3414. gfc_init_se (&maskse, NULL);
  3415. gfc_conv_expr_val (&maskse, maskexpr);
  3416. gfc_init_block (&block);
  3417. gfc_add_block_to_block (&block, &loop.pre);
  3418. gfc_add_block_to_block (&block, &loop.post);
  3419. tmp = gfc_finish_block (&block);
  3420. /* For the else part of the scalar mask, just initialize
  3421. the pos variable the same way as above. */
  3422. gfc_init_block (&elseblock);
  3423. gfc_add_modify (&elseblock, pos, gfc_index_zero_node);
  3424. elsetmp = gfc_finish_block (&elseblock);
  3425. tmp = build3_v (COND_EXPR, maskse.expr, tmp, elsetmp);
  3426. gfc_add_expr_to_block (&block, tmp);
  3427. gfc_add_block_to_block (&se->pre, &block);
  3428. }
  3429. else
  3430. {
  3431. gfc_add_block_to_block (&se->pre, &loop.pre);
  3432. gfc_add_block_to_block (&se->pre, &loop.post);
  3433. }
  3434. gfc_cleanup_loop (&loop);
  3435. se->expr = convert (type, pos);
  3436. }
  3437. /* Emit code for minval or maxval intrinsic. There are many different cases
  3438. we need to handle. For performance reasons we sometimes create two
  3439. loops instead of one, where the second one is much simpler.
  3440. Examples for minval intrinsic:
  3441. 1) Result is an array, a call is generated
  3442. 2) Array mask is used and NaNs need to be supported, rank 1:
  3443. limit = Infinity;
  3444. nonempty = false;
  3445. S = from;
  3446. while (S <= to) {
  3447. if (mask[S]) { nonempty = true; if (a[S] <= limit) goto lab; }
  3448. S++;
  3449. }
  3450. limit = nonempty ? NaN : huge (limit);
  3451. lab:
  3452. while (S <= to) { if(mask[S]) limit = min (a[S], limit); S++; }
  3453. 3) NaNs need to be supported, but it is known at compile time or cheaply
  3454. at runtime whether array is nonempty or not, rank 1:
  3455. limit = Infinity;
  3456. S = from;
  3457. while (S <= to) { if (a[S] <= limit) goto lab; S++; }
  3458. limit = (from <= to) ? NaN : huge (limit);
  3459. lab:
  3460. while (S <= to) { limit = min (a[S], limit); S++; }
  3461. 4) Array mask is used and NaNs need to be supported, rank > 1:
  3462. limit = Infinity;
  3463. nonempty = false;
  3464. fast = false;
  3465. S1 = from1;
  3466. while (S1 <= to1) {
  3467. S2 = from2;
  3468. while (S2 <= to2) {
  3469. if (mask[S1][S2]) {
  3470. if (fast) limit = min (a[S1][S2], limit);
  3471. else {
  3472. nonempty = true;
  3473. if (a[S1][S2] <= limit) {
  3474. limit = a[S1][S2];
  3475. fast = true;
  3476. }
  3477. }
  3478. }
  3479. S2++;
  3480. }
  3481. S1++;
  3482. }
  3483. if (!fast)
  3484. limit = nonempty ? NaN : huge (limit);
  3485. 5) NaNs need to be supported, but it is known at compile time or cheaply
  3486. at runtime whether array is nonempty or not, rank > 1:
  3487. limit = Infinity;
  3488. fast = false;
  3489. S1 = from1;
  3490. while (S1 <= to1) {
  3491. S2 = from2;
  3492. while (S2 <= to2) {
  3493. if (fast) limit = min (a[S1][S2], limit);
  3494. else {
  3495. if (a[S1][S2] <= limit) {
  3496. limit = a[S1][S2];
  3497. fast = true;
  3498. }
  3499. }
  3500. S2++;
  3501. }
  3502. S1++;
  3503. }
  3504. if (!fast)
  3505. limit = (nonempty_array) ? NaN : huge (limit);
  3506. 6) NaNs aren't supported, but infinities are. Array mask is used:
  3507. limit = Infinity;
  3508. nonempty = false;
  3509. S = from;
  3510. while (S <= to) {
  3511. if (mask[S]) { nonempty = true; limit = min (a[S], limit); }
  3512. S++;
  3513. }
  3514. limit = nonempty ? limit : huge (limit);
  3515. 7) Same without array mask:
  3516. limit = Infinity;
  3517. S = from;
  3518. while (S <= to) { limit = min (a[S], limit); S++; }
  3519. limit = (from <= to) ? limit : huge (limit);
  3520. 8) Neither NaNs nor infinities are supported (-ffast-math or BT_INTEGER):
  3521. limit = huge (limit);
  3522. S = from;
  3523. while (S <= to) { limit = min (a[S], limit); S++); }
  3524. (or
  3525. while (S <= to) { if (mask[S]) limit = min (a[S], limit); S++; }
  3526. with array mask instead).
  3527. For 3), 5), 7) and 8), if mask is scalar, this all goes into a conditional,
  3528. setting limit = huge (limit); in the else branch. */
  3529. static void
  3530. gfc_conv_intrinsic_minmaxval (gfc_se * se, gfc_expr * expr, enum tree_code op)
  3531. {
  3532. tree limit;
  3533. tree type;
  3534. tree tmp;
  3535. tree ifbody;
  3536. tree nonempty;
  3537. tree nonempty_var;
  3538. tree lab;
  3539. tree fast;
  3540. tree huge_cst = NULL, nan_cst = NULL;
  3541. stmtblock_t body;
  3542. stmtblock_t block, block2;
  3543. gfc_loopinfo loop;
  3544. gfc_actual_arglist *actual;
  3545. gfc_ss *arrayss;
  3546. gfc_ss *maskss;
  3547. gfc_se arrayse;
  3548. gfc_se maskse;
  3549. gfc_expr *arrayexpr;
  3550. gfc_expr *maskexpr;
  3551. int n;
  3552. if (se->ss)
  3553. {
  3554. gfc_conv_intrinsic_funcall (se, expr);
  3555. return;
  3556. }
  3557. type = gfc_typenode_for_spec (&expr->ts);
  3558. /* Initialize the result. */
  3559. limit = gfc_create_var (type, "limit");
  3560. n = gfc_validate_kind (expr->ts.type, expr->ts.kind, false);
  3561. switch (expr->ts.type)
  3562. {
  3563. case BT_REAL:
  3564. huge_cst = gfc_conv_mpfr_to_tree (gfc_real_kinds[n].huge,
  3565. expr->ts.kind, 0);
  3566. if (HONOR_INFINITIES (DECL_MODE (limit)))
  3567. {
  3568. REAL_VALUE_TYPE real;
  3569. real_inf (&real);
  3570. tmp = build_real (type, real);
  3571. }
  3572. else
  3573. tmp = huge_cst;
  3574. if (HONOR_NANS (DECL_MODE (limit)))
  3575. nan_cst = gfc_build_nan (type, "");
  3576. break;
  3577. case BT_INTEGER:
  3578. tmp = gfc_conv_mpz_to_tree (gfc_integer_kinds[n].huge, expr->ts.kind);
  3579. break;
  3580. default:
  3581. gcc_unreachable ();
  3582. }
  3583. /* We start with the most negative possible value for MAXVAL, and the most
  3584. positive possible value for MINVAL. The most negative possible value is
  3585. -HUGE for BT_REAL and (-HUGE - 1) for BT_INTEGER; the most positive
  3586. possible value is HUGE in both cases. */
  3587. if (op == GT_EXPR)
  3588. {
  3589. tmp = fold_build1_loc (input_location, NEGATE_EXPR, TREE_TYPE (tmp), tmp);
  3590. if (huge_cst)
  3591. huge_cst = fold_build1_loc (input_location, NEGATE_EXPR,
  3592. TREE_TYPE (huge_cst), huge_cst);
  3593. }
  3594. if (op == GT_EXPR && expr->ts.type == BT_INTEGER)
  3595. tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (tmp),
  3596. tmp, build_int_cst (type, 1));
  3597. gfc_add_modify (&se->pre, limit, tmp);
  3598. /* Walk the arguments. */
  3599. actual = expr->value.function.actual;
  3600. arrayexpr = actual->expr;
  3601. arrayss = gfc_walk_expr (arrayexpr);
  3602. gcc_assert (arrayss != gfc_ss_terminator);
  3603. actual = actual->next->next;
  3604. gcc_assert (actual);
  3605. maskexpr = actual->expr;
  3606. nonempty = NULL;
  3607. if (maskexpr && maskexpr->rank != 0)
  3608. {
  3609. maskss = gfc_walk_expr (maskexpr);
  3610. gcc_assert (maskss != gfc_ss_terminator);
  3611. }
  3612. else
  3613. {
  3614. mpz_t asize;
  3615. if (gfc_array_size (arrayexpr, &asize))
  3616. {
  3617. nonempty = gfc_conv_mpz_to_tree (asize, gfc_index_integer_kind);
  3618. mpz_clear (asize);
  3619. nonempty = fold_build2_loc (input_location, GT_EXPR,
  3620. boolean_type_node, nonempty,
  3621. gfc_index_zero_node);
  3622. }
  3623. maskss = NULL;
  3624. }
  3625. /* Initialize the scalarizer. */
  3626. gfc_init_loopinfo (&loop);
  3627. gfc_add_ss_to_loop (&loop, arrayss);
  3628. if (maskss)
  3629. gfc_add_ss_to_loop (&loop, maskss);
  3630. /* Initialize the loop. */
  3631. gfc_conv_ss_startstride (&loop);
  3632. /* The code generated can have more than one loop in sequence (see the
  3633. comment at the function header). This doesn't work well with the
  3634. scalarizer, which changes arrays' offset when the scalarization loops
  3635. are generated (see gfc_trans_preloop_setup). Fortunately, {min,max}val
  3636. are currently inlined in the scalar case only. As there is no dependency
  3637. to care about in that case, there is no temporary, so that we can use the
  3638. scalarizer temporary code to handle multiple loops. Thus, we set temp_dim
  3639. here, we call gfc_mark_ss_chain_used with flag=3 later, and we use
  3640. gfc_trans_scalarized_loop_boundary even later to restore offset.
  3641. TODO: this prevents inlining of rank > 0 minmaxval calls, so this
  3642. should eventually go away. We could either create two loops properly,
  3643. or find another way to save/restore the array offsets between the two
  3644. loops (without conflicting with temporary management), or use a single
  3645. loop minmaxval implementation. See PR 31067. */
  3646. loop.temp_dim = loop.dimen;
  3647. gfc_conv_loop_setup (&loop, &expr->where);
  3648. if (nonempty == NULL && maskss == NULL
  3649. && loop.dimen == 1 && loop.from[0] && loop.to[0])
  3650. nonempty = fold_build2_loc (input_location, LE_EXPR, boolean_type_node,
  3651. loop.from[0], loop.to[0]);
  3652. nonempty_var = NULL;
  3653. if (nonempty == NULL
  3654. && (HONOR_INFINITIES (DECL_MODE (limit))
  3655. || HONOR_NANS (DECL_MODE (limit))))
  3656. {
  3657. nonempty_var = gfc_create_var (boolean_type_node, "nonempty");
  3658. gfc_add_modify (&se->pre, nonempty_var, boolean_false_node);
  3659. nonempty = nonempty_var;
  3660. }
  3661. lab = NULL;
  3662. fast = NULL;
  3663. if (HONOR_NANS (DECL_MODE (limit)))
  3664. {
  3665. if (loop.dimen == 1)
  3666. {
  3667. lab = gfc_build_label_decl (NULL_TREE);
  3668. TREE_USED (lab) = 1;
  3669. }
  3670. else
  3671. {
  3672. fast = gfc_create_var (boolean_type_node, "fast");
  3673. gfc_add_modify (&se->pre, fast, boolean_false_node);
  3674. }
  3675. }
  3676. gfc_mark_ss_chain_used (arrayss, lab ? 3 : 1);
  3677. if (maskss)
  3678. gfc_mark_ss_chain_used (maskss, lab ? 3 : 1);
  3679. /* Generate the loop body. */
  3680. gfc_start_scalarized_body (&loop, &body);
  3681. /* If we have a mask, only add this element if the mask is set. */
  3682. if (maskss)
  3683. {
  3684. gfc_init_se (&maskse, NULL);
  3685. gfc_copy_loopinfo_to_se (&maskse, &loop);
  3686. maskse.ss = maskss;
  3687. gfc_conv_expr_val (&maskse, maskexpr);
  3688. gfc_add_block_to_block (&body, &maskse.pre);
  3689. gfc_start_block (&block);
  3690. }
  3691. else
  3692. gfc_init_block (&block);
  3693. /* Compare with the current limit. */
  3694. gfc_init_se (&arrayse, NULL);
  3695. gfc_copy_loopinfo_to_se (&arrayse, &loop);
  3696. arrayse.ss = arrayss;
  3697. gfc_conv_expr_val (&arrayse, arrayexpr);
  3698. gfc_add_block_to_block (&block, &arrayse.pre);
  3699. gfc_init_block (&block2);
  3700. if (nonempty_var)
  3701. gfc_add_modify (&block2, nonempty_var, boolean_true_node);
  3702. if (HONOR_NANS (DECL_MODE (limit)))
  3703. {
  3704. tmp = fold_build2_loc (input_location, op == GT_EXPR ? GE_EXPR : LE_EXPR,
  3705. boolean_type_node, arrayse.expr, limit);
  3706. if (lab)
  3707. ifbody = build1_v (GOTO_EXPR, lab);
  3708. else
  3709. {
  3710. stmtblock_t ifblock;
  3711. gfc_init_block (&ifblock);
  3712. gfc_add_modify (&ifblock, limit, arrayse.expr);
  3713. gfc_add_modify (&ifblock, fast, boolean_true_node);
  3714. ifbody = gfc_finish_block (&ifblock);
  3715. }
  3716. tmp = build3_v (COND_EXPR, tmp, ifbody,
  3717. build_empty_stmt (input_location));
  3718. gfc_add_expr_to_block (&block2, tmp);
  3719. }
  3720. else
  3721. {
  3722. /* MIN_EXPR/MAX_EXPR has unspecified behavior with NaNs or
  3723. signed zeros. */
  3724. if (HONOR_SIGNED_ZEROS (DECL_MODE (limit)))
  3725. {
  3726. tmp = fold_build2_loc (input_location, op, boolean_type_node,
  3727. arrayse.expr, limit);
  3728. ifbody = build2_v (MODIFY_EXPR, limit, arrayse.expr);
  3729. tmp = build3_v (COND_EXPR, tmp, ifbody,
  3730. build_empty_stmt (input_location));
  3731. gfc_add_expr_to_block (&block2, tmp);
  3732. }
  3733. else
  3734. {
  3735. tmp = fold_build2_loc (input_location,
  3736. op == GT_EXPR ? MAX_EXPR : MIN_EXPR,
  3737. type, arrayse.expr, limit);
  3738. gfc_add_modify (&block2, limit, tmp);
  3739. }
  3740. }
  3741. if (fast)
  3742. {
  3743. tree elsebody = gfc_finish_block (&block2);
  3744. /* MIN_EXPR/MAX_EXPR has unspecified behavior with NaNs or
  3745. signed zeros. */
  3746. if (HONOR_NANS (DECL_MODE (limit))
  3747. || HONOR_SIGNED_ZEROS (DECL_MODE (limit)))
  3748. {
  3749. tmp = fold_build2_loc (input_location, op, boolean_type_node,
  3750. arrayse.expr, limit);
  3751. ifbody = build2_v (MODIFY_EXPR, limit, arrayse.expr);
  3752. ifbody = build3_v (COND_EXPR, tmp, ifbody,
  3753. build_empty_stmt (input_location));
  3754. }
  3755. else
  3756. {
  3757. tmp = fold_build2_loc (input_location,
  3758. op == GT_EXPR ? MAX_EXPR : MIN_EXPR,
  3759. type, arrayse.expr, limit);
  3760. ifbody = build2_v (MODIFY_EXPR, limit, tmp);
  3761. }
  3762. tmp = build3_v (COND_EXPR, fast, ifbody, elsebody);
  3763. gfc_add_expr_to_block (&block, tmp);
  3764. }
  3765. else
  3766. gfc_add_block_to_block (&block, &block2);
  3767. gfc_add_block_to_block (&block, &arrayse.post);
  3768. tmp = gfc_finish_block (&block);
  3769. if (maskss)
  3770. /* We enclose the above in if (mask) {...}. */
  3771. tmp = build3_v (COND_EXPR, maskse.expr, tmp,
  3772. build_empty_stmt (input_location));
  3773. gfc_add_expr_to_block (&body, tmp);
  3774. if (lab)
  3775. {
  3776. gfc_trans_scalarized_loop_boundary (&loop, &body);
  3777. tmp = fold_build3_loc (input_location, COND_EXPR, type, nonempty,
  3778. nan_cst, huge_cst);
  3779. gfc_add_modify (&loop.code[0], limit, tmp);
  3780. gfc_add_expr_to_block (&loop.code[0], build1_v (LABEL_EXPR, lab));
  3781. /* If we have a mask, only add this element if the mask is set. */
  3782. if (maskss)
  3783. {
  3784. gfc_init_se (&maskse, NULL);
  3785. gfc_copy_loopinfo_to_se (&maskse, &loop);
  3786. maskse.ss = maskss;
  3787. gfc_conv_expr_val (&maskse, maskexpr);
  3788. gfc_add_block_to_block (&body, &maskse.pre);
  3789. gfc_start_block (&block);
  3790. }
  3791. else
  3792. gfc_init_block (&block);
  3793. /* Compare with the current limit. */
  3794. gfc_init_se (&arrayse, NULL);
  3795. gfc_copy_loopinfo_to_se (&arrayse, &loop);
  3796. arrayse.ss = arrayss;
  3797. gfc_conv_expr_val (&arrayse, arrayexpr);
  3798. gfc_add_block_to_block (&block, &arrayse.pre);
  3799. /* MIN_EXPR/MAX_EXPR has unspecified behavior with NaNs or
  3800. signed zeros. */
  3801. if (HONOR_NANS (DECL_MODE (limit))
  3802. || HONOR_SIGNED_ZEROS (DECL_MODE (limit)))
  3803. {
  3804. tmp = fold_build2_loc (input_location, op, boolean_type_node,
  3805. arrayse.expr, limit);
  3806. ifbody = build2_v (MODIFY_EXPR, limit, arrayse.expr);
  3807. tmp = build3_v (COND_EXPR, tmp, ifbody,
  3808. build_empty_stmt (input_location));
  3809. gfc_add_expr_to_block (&block, tmp);
  3810. }
  3811. else
  3812. {
  3813. tmp = fold_build2_loc (input_location,
  3814. op == GT_EXPR ? MAX_EXPR : MIN_EXPR,
  3815. type, arrayse.expr, limit);
  3816. gfc_add_modify (&block, limit, tmp);
  3817. }
  3818. gfc_add_block_to_block (&block, &arrayse.post);
  3819. tmp = gfc_finish_block (&block);
  3820. if (maskss)
  3821. /* We enclose the above in if (mask) {...}. */
  3822. tmp = build3_v (COND_EXPR, maskse.expr, tmp,
  3823. build_empty_stmt (input_location));
  3824. gfc_add_expr_to_block (&body, tmp);
  3825. /* Avoid initializing loopvar[0] again, it should be left where
  3826. it finished by the first loop. */
  3827. loop.from[0] = loop.loopvar[0];
  3828. }
  3829. gfc_trans_scalarizing_loops (&loop, &body);
  3830. if (fast)
  3831. {
  3832. tmp = fold_build3_loc (input_location, COND_EXPR, type, nonempty,
  3833. nan_cst, huge_cst);
  3834. ifbody = build2_v (MODIFY_EXPR, limit, tmp);
  3835. tmp = build3_v (COND_EXPR, fast, build_empty_stmt (input_location),
  3836. ifbody);
  3837. gfc_add_expr_to_block (&loop.pre, tmp);
  3838. }
  3839. else if (HONOR_INFINITIES (DECL_MODE (limit)) && !lab)
  3840. {
  3841. tmp = fold_build3_loc (input_location, COND_EXPR, type, nonempty, limit,
  3842. huge_cst);
  3843. gfc_add_modify (&loop.pre, limit, tmp);
  3844. }
  3845. /* For a scalar mask, enclose the loop in an if statement. */
  3846. if (maskexpr && maskss == NULL)
  3847. {
  3848. tree else_stmt;
  3849. gfc_init_se (&maskse, NULL);
  3850. gfc_conv_expr_val (&maskse, maskexpr);
  3851. gfc_init_block (&block);
  3852. gfc_add_block_to_block (&block, &loop.pre);
  3853. gfc_add_block_to_block (&block, &loop.post);
  3854. tmp = gfc_finish_block (&block);
  3855. if (HONOR_INFINITIES (DECL_MODE (limit)))
  3856. else_stmt = build2_v (MODIFY_EXPR, limit, huge_cst);
  3857. else
  3858. else_stmt = build_empty_stmt (input_location);
  3859. tmp = build3_v (COND_EXPR, maskse.expr, tmp, else_stmt);
  3860. gfc_add_expr_to_block (&block, tmp);
  3861. gfc_add_block_to_block (&se->pre, &block);
  3862. }
  3863. else
  3864. {
  3865. gfc_add_block_to_block (&se->pre, &loop.pre);
  3866. gfc_add_block_to_block (&se->pre, &loop.post);
  3867. }
  3868. gfc_cleanup_loop (&loop);
  3869. se->expr = limit;
  3870. }
  3871. /* BTEST (i, pos) = (i & (1 << pos)) != 0. */
  3872. static void
  3873. gfc_conv_intrinsic_btest (gfc_se * se, gfc_expr * expr)
  3874. {
  3875. tree args[2];
  3876. tree type;
  3877. tree tmp;
  3878. gfc_conv_intrinsic_function_args (se, expr, args, 2);
  3879. type = TREE_TYPE (args[0]);
  3880. tmp = fold_build2_loc (input_location, LSHIFT_EXPR, type,
  3881. build_int_cst (type, 1), args[1]);
  3882. tmp = fold_build2_loc (input_location, BIT_AND_EXPR, type, args[0], tmp);
  3883. tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, tmp,
  3884. build_int_cst (type, 0));
  3885. type = gfc_typenode_for_spec (&expr->ts);
  3886. se->expr = convert (type, tmp);
  3887. }
  3888. /* Generate code for BGE, BGT, BLE and BLT intrinsics. */
  3889. static void
  3890. gfc_conv_intrinsic_bitcomp (gfc_se * se, gfc_expr * expr, enum tree_code op)
  3891. {
  3892. tree args[2];
  3893. gfc_conv_intrinsic_function_args (se, expr, args, 2);
  3894. /* Convert both arguments to the unsigned type of the same size. */
  3895. args[0] = fold_convert (unsigned_type_for (TREE_TYPE (args[0])), args[0]);
  3896. args[1] = fold_convert (unsigned_type_for (TREE_TYPE (args[1])), args[1]);
  3897. /* If they have unequal type size, convert to the larger one. */
  3898. if (TYPE_PRECISION (TREE_TYPE (args[0]))
  3899. > TYPE_PRECISION (TREE_TYPE (args[1])))
  3900. args[1] = fold_convert (TREE_TYPE (args[0]), args[1]);
  3901. else if (TYPE_PRECISION (TREE_TYPE (args[1]))
  3902. > TYPE_PRECISION (TREE_TYPE (args[0])))
  3903. args[0] = fold_convert (TREE_TYPE (args[1]), args[0]);
  3904. /* Now, we compare them. */
  3905. se->expr = fold_build2_loc (input_location, op, boolean_type_node,
  3906. args[0], args[1]);
  3907. }
  3908. /* Generate code to perform the specified operation. */
  3909. static void
  3910. gfc_conv_intrinsic_bitop (gfc_se * se, gfc_expr * expr, enum tree_code op)
  3911. {
  3912. tree args[2];
  3913. gfc_conv_intrinsic_function_args (se, expr, args, 2);
  3914. se->expr = fold_build2_loc (input_location, op, TREE_TYPE (args[0]),
  3915. args[0], args[1]);
  3916. }
  3917. /* Bitwise not. */
  3918. static void
  3919. gfc_conv_intrinsic_not (gfc_se * se, gfc_expr * expr)
  3920. {
  3921. tree arg;
  3922. gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
  3923. se->expr = fold_build1_loc (input_location, BIT_NOT_EXPR,
  3924. TREE_TYPE (arg), arg);
  3925. }
  3926. /* Set or clear a single bit. */
  3927. static void
  3928. gfc_conv_intrinsic_singlebitop (gfc_se * se, gfc_expr * expr, int set)
  3929. {
  3930. tree args[2];
  3931. tree type;
  3932. tree tmp;
  3933. enum tree_code op;
  3934. gfc_conv_intrinsic_function_args (se, expr, args, 2);
  3935. type = TREE_TYPE (args[0]);
  3936. tmp = fold_build2_loc (input_location, LSHIFT_EXPR, type,
  3937. build_int_cst (type, 1), args[1]);
  3938. if (set)
  3939. op = BIT_IOR_EXPR;
  3940. else
  3941. {
  3942. op = BIT_AND_EXPR;
  3943. tmp = fold_build1_loc (input_location, BIT_NOT_EXPR, type, tmp);
  3944. }
  3945. se->expr = fold_build2_loc (input_location, op, type, args[0], tmp);
  3946. }
  3947. /* Extract a sequence of bits.
  3948. IBITS(I, POS, LEN) = (I >> POS) & ~((~0) << LEN). */
  3949. static void
  3950. gfc_conv_intrinsic_ibits (gfc_se * se, gfc_expr * expr)
  3951. {
  3952. tree args[3];
  3953. tree type;
  3954. tree tmp;
  3955. tree mask;
  3956. gfc_conv_intrinsic_function_args (se, expr, args, 3);
  3957. type = TREE_TYPE (args[0]);
  3958. mask = build_int_cst (type, -1);
  3959. mask = fold_build2_loc (input_location, LSHIFT_EXPR, type, mask, args[2]);
  3960. mask = fold_build1_loc (input_location, BIT_NOT_EXPR, type, mask);
  3961. tmp = fold_build2_loc (input_location, RSHIFT_EXPR, type, args[0], args[1]);
  3962. se->expr = fold_build2_loc (input_location, BIT_AND_EXPR, type, tmp, mask);
  3963. }
  3964. static void
  3965. gfc_conv_intrinsic_shift (gfc_se * se, gfc_expr * expr, bool right_shift,
  3966. bool arithmetic)
  3967. {
  3968. tree args[2], type, num_bits, cond;
  3969. gfc_conv_intrinsic_function_args (se, expr, args, 2);
  3970. args[0] = gfc_evaluate_now (args[0], &se->pre);
  3971. args[1] = gfc_evaluate_now (args[1], &se->pre);
  3972. type = TREE_TYPE (args[0]);
  3973. if (!arithmetic)
  3974. args[0] = fold_convert (unsigned_type_for (type), args[0]);
  3975. else
  3976. gcc_assert (right_shift);
  3977. se->expr = fold_build2_loc (input_location,
  3978. right_shift ? RSHIFT_EXPR : LSHIFT_EXPR,
  3979. TREE_TYPE (args[0]), args[0], args[1]);
  3980. if (!arithmetic)
  3981. se->expr = fold_convert (type, se->expr);
  3982. /* The Fortran standard allows shift widths <= BIT_SIZE(I), whereas
  3983. gcc requires a shift width < BIT_SIZE(I), so we have to catch this
  3984. special case. */
  3985. num_bits = build_int_cst (TREE_TYPE (args[1]), TYPE_PRECISION (type));
  3986. cond = fold_build2_loc (input_location, GE_EXPR, boolean_type_node,
  3987. args[1], num_bits);
  3988. se->expr = fold_build3_loc (input_location, COND_EXPR, type, cond,
  3989. build_int_cst (type, 0), se->expr);
  3990. }
  3991. /* ISHFT (I, SHIFT) = (abs (shift) >= BIT_SIZE (i))
  3992. ? 0
  3993. : ((shift >= 0) ? i << shift : i >> -shift)
  3994. where all shifts are logical shifts. */
  3995. static void
  3996. gfc_conv_intrinsic_ishft (gfc_se * se, gfc_expr * expr)
  3997. {
  3998. tree args[2];
  3999. tree type;
  4000. tree utype;
  4001. tree tmp;
  4002. tree width;
  4003. tree num_bits;
  4004. tree cond;
  4005. tree lshift;
  4006. tree rshift;
  4007. gfc_conv_intrinsic_function_args (se, expr, args, 2);
  4008. args[0] = gfc_evaluate_now (args[0], &se->pre);
  4009. args[1] = gfc_evaluate_now (args[1], &se->pre);
  4010. type = TREE_TYPE (args[0]);
  4011. utype = unsigned_type_for (type);
  4012. width = fold_build1_loc (input_location, ABS_EXPR, TREE_TYPE (args[1]),
  4013. args[1]);
  4014. /* Left shift if positive. */
  4015. lshift = fold_build2_loc (input_location, LSHIFT_EXPR, type, args[0], width);
  4016. /* Right shift if negative.
  4017. We convert to an unsigned type because we want a logical shift.
  4018. The standard doesn't define the case of shifting negative
  4019. numbers, and we try to be compatible with other compilers, most
  4020. notably g77, here. */
  4021. rshift = fold_convert (type, fold_build2_loc (input_location, RSHIFT_EXPR,
  4022. utype, convert (utype, args[0]), width));
  4023. tmp = fold_build2_loc (input_location, GE_EXPR, boolean_type_node, args[1],
  4024. build_int_cst (TREE_TYPE (args[1]), 0));
  4025. tmp = fold_build3_loc (input_location, COND_EXPR, type, tmp, lshift, rshift);
  4026. /* The Fortran standard allows shift widths <= BIT_SIZE(I), whereas
  4027. gcc requires a shift width < BIT_SIZE(I), so we have to catch this
  4028. special case. */
  4029. num_bits = build_int_cst (TREE_TYPE (args[1]), TYPE_PRECISION (type));
  4030. cond = fold_build2_loc (input_location, GE_EXPR, boolean_type_node, width,
  4031. num_bits);
  4032. se->expr = fold_build3_loc (input_location, COND_EXPR, type, cond,
  4033. build_int_cst (type, 0), tmp);
  4034. }
  4035. /* Circular shift. AKA rotate or barrel shift. */
  4036. static void
  4037. gfc_conv_intrinsic_ishftc (gfc_se * se, gfc_expr * expr)
  4038. {
  4039. tree *args;
  4040. tree type;
  4041. tree tmp;
  4042. tree lrot;
  4043. tree rrot;
  4044. tree zero;
  4045. unsigned int num_args;
  4046. num_args = gfc_intrinsic_argument_list_length (expr);
  4047. args = XALLOCAVEC (tree, num_args);
  4048. gfc_conv_intrinsic_function_args (se, expr, args, num_args);
  4049. if (num_args == 3)
  4050. {
  4051. /* Use a library function for the 3 parameter version. */
  4052. tree int4type = gfc_get_int_type (4);
  4053. type = TREE_TYPE (args[0]);
  4054. /* We convert the first argument to at least 4 bytes, and
  4055. convert back afterwards. This removes the need for library
  4056. functions for all argument sizes, and function will be
  4057. aligned to at least 32 bits, so there's no loss. */
  4058. if (expr->ts.kind < 4)
  4059. args[0] = convert (int4type, args[0]);
  4060. /* Convert the SHIFT and SIZE args to INTEGER*4 otherwise we would
  4061. need loads of library functions. They cannot have values >
  4062. BIT_SIZE (I) so the conversion is safe. */
  4063. args[1] = convert (int4type, args[1]);
  4064. args[2] = convert (int4type, args[2]);
  4065. switch (expr->ts.kind)
  4066. {
  4067. case 1:
  4068. case 2:
  4069. case 4:
  4070. tmp = gfor_fndecl_math_ishftc4;
  4071. break;
  4072. case 8:
  4073. tmp = gfor_fndecl_math_ishftc8;
  4074. break;
  4075. case 16:
  4076. tmp = gfor_fndecl_math_ishftc16;
  4077. break;
  4078. default:
  4079. gcc_unreachable ();
  4080. }
  4081. se->expr = build_call_expr_loc (input_location,
  4082. tmp, 3, args[0], args[1], args[2]);
  4083. /* Convert the result back to the original type, if we extended
  4084. the first argument's width above. */
  4085. if (expr->ts.kind < 4)
  4086. se->expr = convert (type, se->expr);
  4087. return;
  4088. }
  4089. type = TREE_TYPE (args[0]);
  4090. /* Evaluate arguments only once. */
  4091. args[0] = gfc_evaluate_now (args[0], &se->pre);
  4092. args[1] = gfc_evaluate_now (args[1], &se->pre);
  4093. /* Rotate left if positive. */
  4094. lrot = fold_build2_loc (input_location, LROTATE_EXPR, type, args[0], args[1]);
  4095. /* Rotate right if negative. */
  4096. tmp = fold_build1_loc (input_location, NEGATE_EXPR, TREE_TYPE (args[1]),
  4097. args[1]);
  4098. rrot = fold_build2_loc (input_location,RROTATE_EXPR, type, args[0], tmp);
  4099. zero = build_int_cst (TREE_TYPE (args[1]), 0);
  4100. tmp = fold_build2_loc (input_location, GT_EXPR, boolean_type_node, args[1],
  4101. zero);
  4102. rrot = fold_build3_loc (input_location, COND_EXPR, type, tmp, lrot, rrot);
  4103. /* Do nothing if shift == 0. */
  4104. tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, args[1],
  4105. zero);
  4106. se->expr = fold_build3_loc (input_location, COND_EXPR, type, tmp, args[0],
  4107. rrot);
  4108. }
  4109. /* LEADZ (i) = (i == 0) ? BIT_SIZE (i)
  4110. : __builtin_clz(i) - (BIT_SIZE('int') - BIT_SIZE(i))
  4111. The conditional expression is necessary because the result of LEADZ(0)
  4112. is defined, but the result of __builtin_clz(0) is undefined for most
  4113. targets.
  4114. For INTEGER kinds smaller than the C 'int' type, we have to subtract the
  4115. difference in bit size between the argument of LEADZ and the C int. */
  4116. static void
  4117. gfc_conv_intrinsic_leadz (gfc_se * se, gfc_expr * expr)
  4118. {
  4119. tree arg;
  4120. tree arg_type;
  4121. tree cond;
  4122. tree result_type;
  4123. tree leadz;
  4124. tree bit_size;
  4125. tree tmp;
  4126. tree func;
  4127. int s, argsize;
  4128. gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
  4129. argsize = TYPE_PRECISION (TREE_TYPE (arg));
  4130. /* Which variant of __builtin_clz* should we call? */
  4131. if (argsize <= INT_TYPE_SIZE)
  4132. {
  4133. arg_type = unsigned_type_node;
  4134. func = builtin_decl_explicit (BUILT_IN_CLZ);
  4135. }
  4136. else if (argsize <= LONG_TYPE_SIZE)
  4137. {
  4138. arg_type = long_unsigned_type_node;
  4139. func = builtin_decl_explicit (BUILT_IN_CLZL);
  4140. }
  4141. else if (argsize <= LONG_LONG_TYPE_SIZE)
  4142. {
  4143. arg_type = long_long_unsigned_type_node;
  4144. func = builtin_decl_explicit (BUILT_IN_CLZLL);
  4145. }
  4146. else
  4147. {
  4148. gcc_assert (argsize == 2 * LONG_LONG_TYPE_SIZE);
  4149. arg_type = gfc_build_uint_type (argsize);
  4150. func = NULL_TREE;
  4151. }
  4152. /* Convert the actual argument twice: first, to the unsigned type of the
  4153. same size; then, to the proper argument type for the built-in
  4154. function. But the return type is of the default INTEGER kind. */
  4155. arg = fold_convert (gfc_build_uint_type (argsize), arg);
  4156. arg = fold_convert (arg_type, arg);
  4157. arg = gfc_evaluate_now (arg, &se->pre);
  4158. result_type = gfc_get_int_type (gfc_default_integer_kind);
  4159. /* Compute LEADZ for the case i .ne. 0. */
  4160. if (func)
  4161. {
  4162. s = TYPE_PRECISION (arg_type) - argsize;
  4163. tmp = fold_convert (result_type,
  4164. build_call_expr_loc (input_location, func,
  4165. 1, arg));
  4166. leadz = fold_build2_loc (input_location, MINUS_EXPR, result_type,
  4167. tmp, build_int_cst (result_type, s));
  4168. }
  4169. else
  4170. {
  4171. /* We end up here if the argument type is larger than 'long long'.
  4172. We generate this code:
  4173. if (x & (ULL_MAX << ULL_SIZE) != 0)
  4174. return clzll ((unsigned long long) (x >> ULLSIZE));
  4175. else
  4176. return ULL_SIZE + clzll ((unsigned long long) x);
  4177. where ULL_MAX is the largest value that a ULL_MAX can hold
  4178. (0xFFFFFFFFFFFFFFFF for a 64-bit long long type), and ULLSIZE
  4179. is the bit-size of the long long type (64 in this example). */
  4180. tree ullsize, ullmax, tmp1, tmp2, btmp;
  4181. ullsize = build_int_cst (result_type, LONG_LONG_TYPE_SIZE);
  4182. ullmax = fold_build1_loc (input_location, BIT_NOT_EXPR,
  4183. long_long_unsigned_type_node,
  4184. build_int_cst (long_long_unsigned_type_node,
  4185. 0));
  4186. cond = fold_build2_loc (input_location, LSHIFT_EXPR, arg_type,
  4187. fold_convert (arg_type, ullmax), ullsize);
  4188. cond = fold_build2_loc (input_location, BIT_AND_EXPR, arg_type,
  4189. arg, cond);
  4190. cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
  4191. cond, build_int_cst (arg_type, 0));
  4192. tmp1 = fold_build2_loc (input_location, RSHIFT_EXPR, arg_type,
  4193. arg, ullsize);
  4194. tmp1 = fold_convert (long_long_unsigned_type_node, tmp1);
  4195. btmp = builtin_decl_explicit (BUILT_IN_CLZLL);
  4196. tmp1 = fold_convert (result_type,
  4197. build_call_expr_loc (input_location, btmp, 1, tmp1));
  4198. tmp2 = fold_convert (long_long_unsigned_type_node, arg);
  4199. btmp = builtin_decl_explicit (BUILT_IN_CLZLL);
  4200. tmp2 = fold_convert (result_type,
  4201. build_call_expr_loc (input_location, btmp, 1, tmp2));
  4202. tmp2 = fold_build2_loc (input_location, PLUS_EXPR, result_type,
  4203. tmp2, ullsize);
  4204. leadz = fold_build3_loc (input_location, COND_EXPR, result_type,
  4205. cond, tmp1, tmp2);
  4206. }
  4207. /* Build BIT_SIZE. */
  4208. bit_size = build_int_cst (result_type, argsize);
  4209. cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
  4210. arg, build_int_cst (arg_type, 0));
  4211. se->expr = fold_build3_loc (input_location, COND_EXPR, result_type, cond,
  4212. bit_size, leadz);
  4213. }
  4214. /* TRAILZ(i) = (i == 0) ? BIT_SIZE (i) : __builtin_ctz(i)
  4215. The conditional expression is necessary because the result of TRAILZ(0)
  4216. is defined, but the result of __builtin_ctz(0) is undefined for most
  4217. targets. */
  4218. static void
  4219. gfc_conv_intrinsic_trailz (gfc_se * se, gfc_expr *expr)
  4220. {
  4221. tree arg;
  4222. tree arg_type;
  4223. tree cond;
  4224. tree result_type;
  4225. tree trailz;
  4226. tree bit_size;
  4227. tree func;
  4228. int argsize;
  4229. gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
  4230. argsize = TYPE_PRECISION (TREE_TYPE (arg));
  4231. /* Which variant of __builtin_ctz* should we call? */
  4232. if (argsize <= INT_TYPE_SIZE)
  4233. {
  4234. arg_type = unsigned_type_node;
  4235. func = builtin_decl_explicit (BUILT_IN_CTZ);
  4236. }
  4237. else if (argsize <= LONG_TYPE_SIZE)
  4238. {
  4239. arg_type = long_unsigned_type_node;
  4240. func = builtin_decl_explicit (BUILT_IN_CTZL);
  4241. }
  4242. else if (argsize <= LONG_LONG_TYPE_SIZE)
  4243. {
  4244. arg_type = long_long_unsigned_type_node;
  4245. func = builtin_decl_explicit (BUILT_IN_CTZLL);
  4246. }
  4247. else
  4248. {
  4249. gcc_assert (argsize == 2 * LONG_LONG_TYPE_SIZE);
  4250. arg_type = gfc_build_uint_type (argsize);
  4251. func = NULL_TREE;
  4252. }
  4253. /* Convert the actual argument twice: first, to the unsigned type of the
  4254. same size; then, to the proper argument type for the built-in
  4255. function. But the return type is of the default INTEGER kind. */
  4256. arg = fold_convert (gfc_build_uint_type (argsize), arg);
  4257. arg = fold_convert (arg_type, arg);
  4258. arg = gfc_evaluate_now (arg, &se->pre);
  4259. result_type = gfc_get_int_type (gfc_default_integer_kind);
  4260. /* Compute TRAILZ for the case i .ne. 0. */
  4261. if (func)
  4262. trailz = fold_convert (result_type, build_call_expr_loc (input_location,
  4263. func, 1, arg));
  4264. else
  4265. {
  4266. /* We end up here if the argument type is larger than 'long long'.
  4267. We generate this code:
  4268. if ((x & ULL_MAX) == 0)
  4269. return ULL_SIZE + ctzll ((unsigned long long) (x >> ULLSIZE));
  4270. else
  4271. return ctzll ((unsigned long long) x);
  4272. where ULL_MAX is the largest value that a ULL_MAX can hold
  4273. (0xFFFFFFFFFFFFFFFF for a 64-bit long long type), and ULLSIZE
  4274. is the bit-size of the long long type (64 in this example). */
  4275. tree ullsize, ullmax, tmp1, tmp2, btmp;
  4276. ullsize = build_int_cst (result_type, LONG_LONG_TYPE_SIZE);
  4277. ullmax = fold_build1_loc (input_location, BIT_NOT_EXPR,
  4278. long_long_unsigned_type_node,
  4279. build_int_cst (long_long_unsigned_type_node, 0));
  4280. cond = fold_build2_loc (input_location, BIT_AND_EXPR, arg_type, arg,
  4281. fold_convert (arg_type, ullmax));
  4282. cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, cond,
  4283. build_int_cst (arg_type, 0));
  4284. tmp1 = fold_build2_loc (input_location, RSHIFT_EXPR, arg_type,
  4285. arg, ullsize);
  4286. tmp1 = fold_convert (long_long_unsigned_type_node, tmp1);
  4287. btmp = builtin_decl_explicit (BUILT_IN_CTZLL);
  4288. tmp1 = fold_convert (result_type,
  4289. build_call_expr_loc (input_location, btmp, 1, tmp1));
  4290. tmp1 = fold_build2_loc (input_location, PLUS_EXPR, result_type,
  4291. tmp1, ullsize);
  4292. tmp2 = fold_convert (long_long_unsigned_type_node, arg);
  4293. btmp = builtin_decl_explicit (BUILT_IN_CTZLL);
  4294. tmp2 = fold_convert (result_type,
  4295. build_call_expr_loc (input_location, btmp, 1, tmp2));
  4296. trailz = fold_build3_loc (input_location, COND_EXPR, result_type,
  4297. cond, tmp1, tmp2);
  4298. }
  4299. /* Build BIT_SIZE. */
  4300. bit_size = build_int_cst (result_type, argsize);
  4301. cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
  4302. arg, build_int_cst (arg_type, 0));
  4303. se->expr = fold_build3_loc (input_location, COND_EXPR, result_type, cond,
  4304. bit_size, trailz);
  4305. }
  4306. /* Using __builtin_popcount for POPCNT and __builtin_parity for POPPAR;
  4307. for types larger than "long long", we call the long long built-in for
  4308. the lower and higher bits and combine the result. */
  4309. static void
  4310. gfc_conv_intrinsic_popcnt_poppar (gfc_se * se, gfc_expr *expr, int parity)
  4311. {
  4312. tree arg;
  4313. tree arg_type;
  4314. tree result_type;
  4315. tree func;
  4316. int argsize;
  4317. gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
  4318. argsize = TYPE_PRECISION (TREE_TYPE (arg));
  4319. result_type = gfc_get_int_type (gfc_default_integer_kind);
  4320. /* Which variant of the builtin should we call? */
  4321. if (argsize <= INT_TYPE_SIZE)
  4322. {
  4323. arg_type = unsigned_type_node;
  4324. func = builtin_decl_explicit (parity
  4325. ? BUILT_IN_PARITY
  4326. : BUILT_IN_POPCOUNT);
  4327. }
  4328. else if (argsize <= LONG_TYPE_SIZE)
  4329. {
  4330. arg_type = long_unsigned_type_node;
  4331. func = builtin_decl_explicit (parity
  4332. ? BUILT_IN_PARITYL
  4333. : BUILT_IN_POPCOUNTL);
  4334. }
  4335. else if (argsize <= LONG_LONG_TYPE_SIZE)
  4336. {
  4337. arg_type = long_long_unsigned_type_node;
  4338. func = builtin_decl_explicit (parity
  4339. ? BUILT_IN_PARITYLL
  4340. : BUILT_IN_POPCOUNTLL);
  4341. }
  4342. else
  4343. {
  4344. /* Our argument type is larger than 'long long', which mean none
  4345. of the POPCOUNT builtins covers it. We thus call the 'long long'
  4346. variant multiple times, and add the results. */
  4347. tree utype, arg2, call1, call2;
  4348. /* For now, we only cover the case where argsize is twice as large
  4349. as 'long long'. */
  4350. gcc_assert (argsize == 2 * LONG_LONG_TYPE_SIZE);
  4351. func = builtin_decl_explicit (parity
  4352. ? BUILT_IN_PARITYLL
  4353. : BUILT_IN_POPCOUNTLL);
  4354. /* Convert it to an integer, and store into a variable. */
  4355. utype = gfc_build_uint_type (argsize);
  4356. arg = fold_convert (utype, arg);
  4357. arg = gfc_evaluate_now (arg, &se->pre);
  4358. /* Call the builtin twice. */
  4359. call1 = build_call_expr_loc (input_location, func, 1,
  4360. fold_convert (long_long_unsigned_type_node,
  4361. arg));
  4362. arg2 = fold_build2_loc (input_location, RSHIFT_EXPR, utype, arg,
  4363. build_int_cst (utype, LONG_LONG_TYPE_SIZE));
  4364. call2 = build_call_expr_loc (input_location, func, 1,
  4365. fold_convert (long_long_unsigned_type_node,
  4366. arg2));
  4367. /* Combine the results. */
  4368. if (parity)
  4369. se->expr = fold_build2_loc (input_location, BIT_XOR_EXPR, result_type,
  4370. call1, call2);
  4371. else
  4372. se->expr = fold_build2_loc (input_location, PLUS_EXPR, result_type,
  4373. call1, call2);
  4374. return;
  4375. }
  4376. /* Convert the actual argument twice: first, to the unsigned type of the
  4377. same size; then, to the proper argument type for the built-in
  4378. function. */
  4379. arg = fold_convert (gfc_build_uint_type (argsize), arg);
  4380. arg = fold_convert (arg_type, arg);
  4381. se->expr = fold_convert (result_type,
  4382. build_call_expr_loc (input_location, func, 1, arg));
  4383. }
  4384. /* Process an intrinsic with unspecified argument-types that has an optional
  4385. argument (which could be of type character), e.g. EOSHIFT. For those, we
  4386. need to append the string length of the optional argument if it is not
  4387. present and the type is really character.
  4388. primary specifies the position (starting at 1) of the non-optional argument
  4389. specifying the type and optional gives the position of the optional
  4390. argument in the arglist. */
  4391. static void
  4392. conv_generic_with_optional_char_arg (gfc_se* se, gfc_expr* expr,
  4393. unsigned primary, unsigned optional)
  4394. {
  4395. gfc_actual_arglist* prim_arg;
  4396. gfc_actual_arglist* opt_arg;
  4397. unsigned cur_pos;
  4398. gfc_actual_arglist* arg;
  4399. gfc_symbol* sym;
  4400. vec<tree, va_gc> *append_args;
  4401. /* Find the two arguments given as position. */
  4402. cur_pos = 0;
  4403. prim_arg = NULL;
  4404. opt_arg = NULL;
  4405. for (arg = expr->value.function.actual; arg; arg = arg->next)
  4406. {
  4407. ++cur_pos;
  4408. if (cur_pos == primary)
  4409. prim_arg = arg;
  4410. if (cur_pos == optional)
  4411. opt_arg = arg;
  4412. if (cur_pos >= primary && cur_pos >= optional)
  4413. break;
  4414. }
  4415. gcc_assert (prim_arg);
  4416. gcc_assert (prim_arg->expr);
  4417. gcc_assert (opt_arg);
  4418. /* If we do have type CHARACTER and the optional argument is really absent,
  4419. append a dummy 0 as string length. */
  4420. append_args = NULL;
  4421. if (prim_arg->expr->ts.type == BT_CHARACTER && !opt_arg->expr)
  4422. {
  4423. tree dummy;
  4424. dummy = build_int_cst (gfc_charlen_type_node, 0);
  4425. vec_alloc (append_args, 1);
  4426. append_args->quick_push (dummy);
  4427. }
  4428. /* Build the call itself. */
  4429. gcc_assert (!se->ignore_optional);
  4430. sym = gfc_get_symbol_for_expr (expr, false);
  4431. gfc_conv_procedure_call (se, sym, expr->value.function.actual, expr,
  4432. append_args);
  4433. gfc_free_symbol (sym);
  4434. }
  4435. /* The length of a character string. */
  4436. static void
  4437. gfc_conv_intrinsic_len (gfc_se * se, gfc_expr * expr)
  4438. {
  4439. tree len;
  4440. tree type;
  4441. tree decl;
  4442. gfc_symbol *sym;
  4443. gfc_se argse;
  4444. gfc_expr *arg;
  4445. gcc_assert (!se->ss);
  4446. arg = expr->value.function.actual->expr;
  4447. type = gfc_typenode_for_spec (&expr->ts);
  4448. switch (arg->expr_type)
  4449. {
  4450. case EXPR_CONSTANT:
  4451. len = build_int_cst (gfc_charlen_type_node, arg->value.character.length);
  4452. break;
  4453. case EXPR_ARRAY:
  4454. /* Obtain the string length from the function used by
  4455. trans-array.c(gfc_trans_array_constructor). */
  4456. len = NULL_TREE;
  4457. get_array_ctor_strlen (&se->pre, arg->value.constructor, &len);
  4458. break;
  4459. case EXPR_VARIABLE:
  4460. if (arg->ref == NULL
  4461. || (arg->ref->next == NULL && arg->ref->type == REF_ARRAY))
  4462. {
  4463. /* This doesn't catch all cases.
  4464. See http://gcc.gnu.org/ml/fortran/2004-06/msg00165.html
  4465. and the surrounding thread. */
  4466. sym = arg->symtree->n.sym;
  4467. decl = gfc_get_symbol_decl (sym);
  4468. if (decl == current_function_decl && sym->attr.function
  4469. && (sym->result == sym))
  4470. decl = gfc_get_fake_result_decl (sym, 0);
  4471. len = sym->ts.u.cl->backend_decl;
  4472. gcc_assert (len);
  4473. break;
  4474. }
  4475. /* Otherwise fall through. */
  4476. default:
  4477. /* Anybody stupid enough to do this deserves inefficient code. */
  4478. gfc_init_se (&argse, se);
  4479. if (arg->rank == 0)
  4480. gfc_conv_expr (&argse, arg);
  4481. else
  4482. gfc_conv_expr_descriptor (&argse, arg);
  4483. gfc_add_block_to_block (&se->pre, &argse.pre);
  4484. gfc_add_block_to_block (&se->post, &argse.post);
  4485. len = argse.string_length;
  4486. break;
  4487. }
  4488. se->expr = convert (type, len);
  4489. }
  4490. /* The length of a character string not including trailing blanks. */
  4491. static void
  4492. gfc_conv_intrinsic_len_trim (gfc_se * se, gfc_expr * expr)
  4493. {
  4494. int kind = expr->value.function.actual->expr->ts.kind;
  4495. tree args[2], type, fndecl;
  4496. gfc_conv_intrinsic_function_args (se, expr, args, 2);
  4497. type = gfc_typenode_for_spec (&expr->ts);
  4498. if (kind == 1)
  4499. fndecl = gfor_fndecl_string_len_trim;
  4500. else if (kind == 4)
  4501. fndecl = gfor_fndecl_string_len_trim_char4;
  4502. else
  4503. gcc_unreachable ();
  4504. se->expr = build_call_expr_loc (input_location,
  4505. fndecl, 2, args[0], args[1]);
  4506. se->expr = convert (type, se->expr);
  4507. }
  4508. /* Returns the starting position of a substring within a string. */
  4509. static void
  4510. gfc_conv_intrinsic_index_scan_verify (gfc_se * se, gfc_expr * expr,
  4511. tree function)
  4512. {
  4513. tree logical4_type_node = gfc_get_logical_type (4);
  4514. tree type;
  4515. tree fndecl;
  4516. tree *args;
  4517. unsigned int num_args;
  4518. args = XALLOCAVEC (tree, 5);
  4519. /* Get number of arguments; characters count double due to the
  4520. string length argument. Kind= is not passed to the library
  4521. and thus ignored. */
  4522. if (expr->value.function.actual->next->next->expr == NULL)
  4523. num_args = 4;
  4524. else
  4525. num_args = 5;
  4526. gfc_conv_intrinsic_function_args (se, expr, args, num_args);
  4527. type = gfc_typenode_for_spec (&expr->ts);
  4528. if (num_args == 4)
  4529. args[4] = build_int_cst (logical4_type_node, 0);
  4530. else
  4531. args[4] = convert (logical4_type_node, args[4]);
  4532. fndecl = build_addr (function, current_function_decl);
  4533. se->expr = build_call_array_loc (input_location,
  4534. TREE_TYPE (TREE_TYPE (function)), fndecl,
  4535. 5, args);
  4536. se->expr = convert (type, se->expr);
  4537. }
  4538. /* The ascii value for a single character. */
  4539. static void
  4540. gfc_conv_intrinsic_ichar (gfc_se * se, gfc_expr * expr)
  4541. {
  4542. tree args[3], type, pchartype;
  4543. int nargs;
  4544. nargs = gfc_intrinsic_argument_list_length (expr);
  4545. gfc_conv_intrinsic_function_args (se, expr, args, nargs);
  4546. gcc_assert (POINTER_TYPE_P (TREE_TYPE (args[1])));
  4547. pchartype = gfc_get_pchar_type (expr->value.function.actual->expr->ts.kind);
  4548. args[1] = fold_build1_loc (input_location, NOP_EXPR, pchartype, args[1]);
  4549. type = gfc_typenode_for_spec (&expr->ts);
  4550. se->expr = build_fold_indirect_ref_loc (input_location,
  4551. args[1]);
  4552. se->expr = convert (type, se->expr);
  4553. }
  4554. /* Intrinsic ISNAN calls __builtin_isnan. */
  4555. static void
  4556. gfc_conv_intrinsic_isnan (gfc_se * se, gfc_expr * expr)
  4557. {
  4558. tree arg;
  4559. gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
  4560. se->expr = build_call_expr_loc (input_location,
  4561. builtin_decl_explicit (BUILT_IN_ISNAN),
  4562. 1, arg);
  4563. STRIP_TYPE_NOPS (se->expr);
  4564. se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), se->expr);
  4565. }
  4566. /* Intrinsics IS_IOSTAT_END and IS_IOSTAT_EOR just need to compare
  4567. their argument against a constant integer value. */
  4568. static void
  4569. gfc_conv_has_intvalue (gfc_se * se, gfc_expr * expr, const int value)
  4570. {
  4571. tree arg;
  4572. gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
  4573. se->expr = fold_build2_loc (input_location, EQ_EXPR,
  4574. gfc_typenode_for_spec (&expr->ts),
  4575. arg, build_int_cst (TREE_TYPE (arg), value));
  4576. }
  4577. /* MERGE (tsource, fsource, mask) = mask ? tsource : fsource. */
  4578. static void
  4579. gfc_conv_intrinsic_merge (gfc_se * se, gfc_expr * expr)
  4580. {
  4581. tree tsource;
  4582. tree fsource;
  4583. tree mask;
  4584. tree type;
  4585. tree len, len2;
  4586. tree *args;
  4587. unsigned int num_args;
  4588. num_args = gfc_intrinsic_argument_list_length (expr);
  4589. args = XALLOCAVEC (tree, num_args);
  4590. gfc_conv_intrinsic_function_args (se, expr, args, num_args);
  4591. if (expr->ts.type != BT_CHARACTER)
  4592. {
  4593. tsource = args[0];
  4594. fsource = args[1];
  4595. mask = args[2];
  4596. }
  4597. else
  4598. {
  4599. /* We do the same as in the non-character case, but the argument
  4600. list is different because of the string length arguments. We
  4601. also have to set the string length for the result. */
  4602. len = args[0];
  4603. tsource = args[1];
  4604. len2 = args[2];
  4605. fsource = args[3];
  4606. mask = args[4];
  4607. gfc_trans_same_strlen_check ("MERGE intrinsic", &expr->where, len, len2,
  4608. &se->pre);
  4609. se->string_length = len;
  4610. }
  4611. type = TREE_TYPE (tsource);
  4612. se->expr = fold_build3_loc (input_location, COND_EXPR, type, mask, tsource,
  4613. fold_convert (type, fsource));
  4614. }
  4615. /* MERGE_BITS (I, J, MASK) = (I & MASK) | (I & (~MASK)). */
  4616. static void
  4617. gfc_conv_intrinsic_merge_bits (gfc_se * se, gfc_expr * expr)
  4618. {
  4619. tree args[3], mask, type;
  4620. gfc_conv_intrinsic_function_args (se, expr, args, 3);
  4621. mask = gfc_evaluate_now (args[2], &se->pre);
  4622. type = TREE_TYPE (args[0]);
  4623. gcc_assert (TREE_TYPE (args[1]) == type);
  4624. gcc_assert (TREE_TYPE (mask) == type);
  4625. args[0] = fold_build2_loc (input_location, BIT_AND_EXPR, type, args[0], mask);
  4626. args[1] = fold_build2_loc (input_location, BIT_AND_EXPR, type, args[1],
  4627. fold_build1_loc (input_location, BIT_NOT_EXPR,
  4628. type, mask));
  4629. se->expr = fold_build2_loc (input_location, BIT_IOR_EXPR, type,
  4630. args[0], args[1]);
  4631. }
  4632. /* MASKL(n) = n == 0 ? 0 : (~0) << (BIT_SIZE - n)
  4633. MASKR(n) = n == BIT_SIZE ? ~0 : ~((~0) << n) */
  4634. static void
  4635. gfc_conv_intrinsic_mask (gfc_se * se, gfc_expr * expr, int left)
  4636. {
  4637. tree arg, allones, type, utype, res, cond, bitsize;
  4638. int i;
  4639. gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
  4640. arg = gfc_evaluate_now (arg, &se->pre);
  4641. type = gfc_get_int_type (expr->ts.kind);
  4642. utype = unsigned_type_for (type);
  4643. i = gfc_validate_kind (BT_INTEGER, expr->ts.kind, false);
  4644. bitsize = build_int_cst (TREE_TYPE (arg), gfc_integer_kinds[i].bit_size);
  4645. allones = fold_build1_loc (input_location, BIT_NOT_EXPR, utype,
  4646. build_int_cst (utype, 0));
  4647. if (left)
  4648. {
  4649. /* Left-justified mask. */
  4650. res = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (arg),
  4651. bitsize, arg);
  4652. res = fold_build2_loc (input_location, LSHIFT_EXPR, utype, allones,
  4653. fold_convert (utype, res));
  4654. /* Special case arg == 0, because SHIFT_EXPR wants a shift strictly
  4655. smaller than type width. */
  4656. cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, arg,
  4657. build_int_cst (TREE_TYPE (arg), 0));
  4658. res = fold_build3_loc (input_location, COND_EXPR, utype, cond,
  4659. build_int_cst (utype, 0), res);
  4660. }
  4661. else
  4662. {
  4663. /* Right-justified mask. */
  4664. res = fold_build2_loc (input_location, LSHIFT_EXPR, utype, allones,
  4665. fold_convert (utype, arg));
  4666. res = fold_build1_loc (input_location, BIT_NOT_EXPR, utype, res);
  4667. /* Special case agr == bit_size, because SHIFT_EXPR wants a shift
  4668. strictly smaller than type width. */
  4669. cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
  4670. arg, bitsize);
  4671. res = fold_build3_loc (input_location, COND_EXPR, utype,
  4672. cond, allones, res);
  4673. }
  4674. se->expr = fold_convert (type, res);
  4675. }
  4676. /* FRACTION (s) is translated into:
  4677. isfinite (s) ? frexp (s, &dummy_int) : NaN */
  4678. static void
  4679. gfc_conv_intrinsic_fraction (gfc_se * se, gfc_expr * expr)
  4680. {
  4681. tree arg, type, tmp, res, frexp, cond;
  4682. frexp = gfc_builtin_decl_for_float_kind (BUILT_IN_FREXP, expr->ts.kind);
  4683. type = gfc_typenode_for_spec (&expr->ts);
  4684. gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
  4685. arg = gfc_evaluate_now (arg, &se->pre);
  4686. cond = build_call_expr_loc (input_location,
  4687. builtin_decl_explicit (BUILT_IN_ISFINITE),
  4688. 1, arg);
  4689. tmp = gfc_create_var (integer_type_node, NULL);
  4690. res = build_call_expr_loc (input_location, frexp, 2,
  4691. fold_convert (type, arg),
  4692. gfc_build_addr_expr (NULL_TREE, tmp));
  4693. res = fold_convert (type, res);
  4694. se->expr = fold_build3_loc (input_location, COND_EXPR, type,
  4695. cond, res, gfc_build_nan (type, ""));
  4696. }
  4697. /* NEAREST (s, dir) is translated into
  4698. tmp = copysign (HUGE_VAL, dir);
  4699. return nextafter (s, tmp);
  4700. */
  4701. static void
  4702. gfc_conv_intrinsic_nearest (gfc_se * se, gfc_expr * expr)
  4703. {
  4704. tree args[2], type, tmp, nextafter, copysign, huge_val;
  4705. nextafter = gfc_builtin_decl_for_float_kind (BUILT_IN_NEXTAFTER, expr->ts.kind);
  4706. copysign = gfc_builtin_decl_for_float_kind (BUILT_IN_COPYSIGN, expr->ts.kind);
  4707. type = gfc_typenode_for_spec (&expr->ts);
  4708. gfc_conv_intrinsic_function_args (se, expr, args, 2);
  4709. huge_val = gfc_build_inf_or_huge (type, expr->ts.kind);
  4710. tmp = build_call_expr_loc (input_location, copysign, 2, huge_val,
  4711. fold_convert (type, args[1]));
  4712. se->expr = build_call_expr_loc (input_location, nextafter, 2,
  4713. fold_convert (type, args[0]), tmp);
  4714. se->expr = fold_convert (type, se->expr);
  4715. }
  4716. /* SPACING (s) is translated into
  4717. int e;
  4718. if (!isfinite (s))
  4719. res = NaN;
  4720. else if (s == 0)
  4721. res = tiny;
  4722. else
  4723. {
  4724. frexp (s, &e);
  4725. e = e - prec;
  4726. e = MAX_EXPR (e, emin);
  4727. res = scalbn (1., e);
  4728. }
  4729. return res;
  4730. where prec is the precision of s, gfc_real_kinds[k].digits,
  4731. emin is min_exponent - 1, gfc_real_kinds[k].min_exponent - 1,
  4732. and tiny is tiny(s), gfc_real_kinds[k].tiny. */
  4733. static void
  4734. gfc_conv_intrinsic_spacing (gfc_se * se, gfc_expr * expr)
  4735. {
  4736. tree arg, type, prec, emin, tiny, res, e;
  4737. tree cond, nan, tmp, frexp, scalbn;
  4738. int k;
  4739. stmtblock_t block;
  4740. k = gfc_validate_kind (BT_REAL, expr->ts.kind, false);
  4741. prec = build_int_cst (integer_type_node, gfc_real_kinds[k].digits);
  4742. emin = build_int_cst (integer_type_node, gfc_real_kinds[k].min_exponent - 1);
  4743. tiny = gfc_conv_mpfr_to_tree (gfc_real_kinds[k].tiny, expr->ts.kind, 0);
  4744. frexp = gfc_builtin_decl_for_float_kind (BUILT_IN_FREXP, expr->ts.kind);
  4745. scalbn = gfc_builtin_decl_for_float_kind (BUILT_IN_SCALBN, expr->ts.kind);
  4746. gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
  4747. arg = gfc_evaluate_now (arg, &se->pre);
  4748. type = gfc_typenode_for_spec (&expr->ts);
  4749. e = gfc_create_var (integer_type_node, NULL);
  4750. res = gfc_create_var (type, NULL);
  4751. /* Build the block for s /= 0. */
  4752. gfc_start_block (&block);
  4753. tmp = build_call_expr_loc (input_location, frexp, 2, arg,
  4754. gfc_build_addr_expr (NULL_TREE, e));
  4755. gfc_add_expr_to_block (&block, tmp);
  4756. tmp = fold_build2_loc (input_location, MINUS_EXPR, integer_type_node, e,
  4757. prec);
  4758. gfc_add_modify (&block, e, fold_build2_loc (input_location, MAX_EXPR,
  4759. integer_type_node, tmp, emin));
  4760. tmp = build_call_expr_loc (input_location, scalbn, 2,
  4761. build_real_from_int_cst (type, integer_one_node), e);
  4762. gfc_add_modify (&block, res, tmp);
  4763. /* Finish by building the IF statement for value zero. */
  4764. cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, arg,
  4765. build_real_from_int_cst (type, integer_zero_node));
  4766. tmp = build3_v (COND_EXPR, cond, build2_v (MODIFY_EXPR, res, tiny),
  4767. gfc_finish_block (&block));
  4768. /* And deal with infinities and NaNs. */
  4769. cond = build_call_expr_loc (input_location,
  4770. builtin_decl_explicit (BUILT_IN_ISFINITE),
  4771. 1, arg);
  4772. nan = gfc_build_nan (type, "");
  4773. tmp = build3_v (COND_EXPR, cond, tmp, build2_v (MODIFY_EXPR, res, nan));
  4774. gfc_add_expr_to_block (&se->pre, tmp);
  4775. se->expr = res;
  4776. }
  4777. /* RRSPACING (s) is translated into
  4778. int e;
  4779. real x;
  4780. x = fabs (s);
  4781. if (isfinite (x))
  4782. {
  4783. if (x != 0)
  4784. {
  4785. frexp (s, &e);
  4786. x = scalbn (x, precision - e);
  4787. }
  4788. }
  4789. else
  4790. x = NaN;
  4791. return x;
  4792. where precision is gfc_real_kinds[k].digits. */
  4793. static void
  4794. gfc_conv_intrinsic_rrspacing (gfc_se * se, gfc_expr * expr)
  4795. {
  4796. tree arg, type, e, x, cond, nan, stmt, tmp, frexp, scalbn, fabs;
  4797. int prec, k;
  4798. stmtblock_t block;
  4799. k = gfc_validate_kind (BT_REAL, expr->ts.kind, false);
  4800. prec = gfc_real_kinds[k].digits;
  4801. frexp = gfc_builtin_decl_for_float_kind (BUILT_IN_FREXP, expr->ts.kind);
  4802. scalbn = gfc_builtin_decl_for_float_kind (BUILT_IN_SCALBN, expr->ts.kind);
  4803. fabs = gfc_builtin_decl_for_float_kind (BUILT_IN_FABS, expr->ts.kind);
  4804. type = gfc_typenode_for_spec (&expr->ts);
  4805. gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
  4806. arg = gfc_evaluate_now (arg, &se->pre);
  4807. e = gfc_create_var (integer_type_node, NULL);
  4808. x = gfc_create_var (type, NULL);
  4809. gfc_add_modify (&se->pre, x,
  4810. build_call_expr_loc (input_location, fabs, 1, arg));
  4811. gfc_start_block (&block);
  4812. tmp = build_call_expr_loc (input_location, frexp, 2, arg,
  4813. gfc_build_addr_expr (NULL_TREE, e));
  4814. gfc_add_expr_to_block (&block, tmp);
  4815. tmp = fold_build2_loc (input_location, MINUS_EXPR, integer_type_node,
  4816. build_int_cst (integer_type_node, prec), e);
  4817. tmp = build_call_expr_loc (input_location, scalbn, 2, x, tmp);
  4818. gfc_add_modify (&block, x, tmp);
  4819. stmt = gfc_finish_block (&block);
  4820. /* if (x != 0) */
  4821. cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, x,
  4822. build_real_from_int_cst (type, integer_zero_node));
  4823. tmp = build3_v (COND_EXPR, cond, stmt, build_empty_stmt (input_location));
  4824. /* And deal with infinities and NaNs. */
  4825. cond = build_call_expr_loc (input_location,
  4826. builtin_decl_explicit (BUILT_IN_ISFINITE),
  4827. 1, x);
  4828. nan = gfc_build_nan (type, "");
  4829. tmp = build3_v (COND_EXPR, cond, tmp, build2_v (MODIFY_EXPR, x, nan));
  4830. gfc_add_expr_to_block (&se->pre, tmp);
  4831. se->expr = fold_convert (type, x);
  4832. }
  4833. /* SCALE (s, i) is translated into scalbn (s, i). */
  4834. static void
  4835. gfc_conv_intrinsic_scale (gfc_se * se, gfc_expr * expr)
  4836. {
  4837. tree args[2], type, scalbn;
  4838. scalbn = gfc_builtin_decl_for_float_kind (BUILT_IN_SCALBN, expr->ts.kind);
  4839. type = gfc_typenode_for_spec (&expr->ts);
  4840. gfc_conv_intrinsic_function_args (se, expr, args, 2);
  4841. se->expr = build_call_expr_loc (input_location, scalbn, 2,
  4842. fold_convert (type, args[0]),
  4843. fold_convert (integer_type_node, args[1]));
  4844. se->expr = fold_convert (type, se->expr);
  4845. }
  4846. /* SET_EXPONENT (s, i) is translated into
  4847. isfinite(s) ? scalbn (frexp (s, &dummy_int), i) : NaN */
  4848. static void
  4849. gfc_conv_intrinsic_set_exponent (gfc_se * se, gfc_expr * expr)
  4850. {
  4851. tree args[2], type, tmp, frexp, scalbn, cond, nan, res;
  4852. frexp = gfc_builtin_decl_for_float_kind (BUILT_IN_FREXP, expr->ts.kind);
  4853. scalbn = gfc_builtin_decl_for_float_kind (BUILT_IN_SCALBN, expr->ts.kind);
  4854. type = gfc_typenode_for_spec (&expr->ts);
  4855. gfc_conv_intrinsic_function_args (se, expr, args, 2);
  4856. args[0] = gfc_evaluate_now (args[0], &se->pre);
  4857. tmp = gfc_create_var (integer_type_node, NULL);
  4858. tmp = build_call_expr_loc (input_location, frexp, 2,
  4859. fold_convert (type, args[0]),
  4860. gfc_build_addr_expr (NULL_TREE, tmp));
  4861. res = build_call_expr_loc (input_location, scalbn, 2, tmp,
  4862. fold_convert (integer_type_node, args[1]));
  4863. res = fold_convert (type, res);
  4864. /* Call to isfinite */
  4865. cond = build_call_expr_loc (input_location,
  4866. builtin_decl_explicit (BUILT_IN_ISFINITE),
  4867. 1, args[0]);
  4868. nan = gfc_build_nan (type, "");
  4869. se->expr = fold_build3_loc (input_location, COND_EXPR, type, cond,
  4870. res, nan);
  4871. }
  4872. static void
  4873. gfc_conv_intrinsic_size (gfc_se * se, gfc_expr * expr)
  4874. {
  4875. gfc_actual_arglist *actual;
  4876. tree arg1;
  4877. tree type;
  4878. tree fncall0;
  4879. tree fncall1;
  4880. gfc_se argse;
  4881. gfc_init_se (&argse, NULL);
  4882. actual = expr->value.function.actual;
  4883. if (actual->expr->ts.type == BT_CLASS)
  4884. gfc_add_class_array_ref (actual->expr);
  4885. argse.want_pointer = 1;
  4886. argse.data_not_needed = 1;
  4887. gfc_conv_expr_descriptor (&argse, actual->expr);
  4888. gfc_add_block_to_block (&se->pre, &argse.pre);
  4889. gfc_add_block_to_block (&se->post, &argse.post);
  4890. arg1 = gfc_evaluate_now (argse.expr, &se->pre);
  4891. /* Build the call to size0. */
  4892. fncall0 = build_call_expr_loc (input_location,
  4893. gfor_fndecl_size0, 1, arg1);
  4894. actual = actual->next;
  4895. if (actual->expr)
  4896. {
  4897. gfc_init_se (&argse, NULL);
  4898. gfc_conv_expr_type (&argse, actual->expr,
  4899. gfc_array_index_type);
  4900. gfc_add_block_to_block (&se->pre, &argse.pre);
  4901. /* Unusually, for an intrinsic, size does not exclude
  4902. an optional arg2, so we must test for it. */
  4903. if (actual->expr->expr_type == EXPR_VARIABLE
  4904. && actual->expr->symtree->n.sym->attr.dummy
  4905. && actual->expr->symtree->n.sym->attr.optional)
  4906. {
  4907. tree tmp;
  4908. /* Build the call to size1. */
  4909. fncall1 = build_call_expr_loc (input_location,
  4910. gfor_fndecl_size1, 2,
  4911. arg1, argse.expr);
  4912. gfc_init_se (&argse, NULL);
  4913. argse.want_pointer = 1;
  4914. argse.data_not_needed = 1;
  4915. gfc_conv_expr (&argse, actual->expr);
  4916. gfc_add_block_to_block (&se->pre, &argse.pre);
  4917. tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
  4918. argse.expr, null_pointer_node);
  4919. tmp = gfc_evaluate_now (tmp, &se->pre);
  4920. se->expr = fold_build3_loc (input_location, COND_EXPR,
  4921. pvoid_type_node, tmp, fncall1, fncall0);
  4922. }
  4923. else
  4924. {
  4925. se->expr = NULL_TREE;
  4926. argse.expr = fold_build2_loc (input_location, MINUS_EXPR,
  4927. gfc_array_index_type,
  4928. argse.expr, gfc_index_one_node);
  4929. }
  4930. }
  4931. else if (expr->value.function.actual->expr->rank == 1)
  4932. {
  4933. argse.expr = gfc_index_zero_node;
  4934. se->expr = NULL_TREE;
  4935. }
  4936. else
  4937. se->expr = fncall0;
  4938. if (se->expr == NULL_TREE)
  4939. {
  4940. tree ubound, lbound;
  4941. arg1 = build_fold_indirect_ref_loc (input_location,
  4942. arg1);
  4943. ubound = gfc_conv_descriptor_ubound_get (arg1, argse.expr);
  4944. lbound = gfc_conv_descriptor_lbound_get (arg1, argse.expr);
  4945. se->expr = fold_build2_loc (input_location, MINUS_EXPR,
  4946. gfc_array_index_type, ubound, lbound);
  4947. se->expr = fold_build2_loc (input_location, PLUS_EXPR,
  4948. gfc_array_index_type,
  4949. se->expr, gfc_index_one_node);
  4950. se->expr = fold_build2_loc (input_location, MAX_EXPR,
  4951. gfc_array_index_type, se->expr,
  4952. gfc_index_zero_node);
  4953. }
  4954. type = gfc_typenode_for_spec (&expr->ts);
  4955. se->expr = convert (type, se->expr);
  4956. }
  4957. /* Helper function to compute the size of a character variable,
  4958. excluding the terminating null characters. The result has
  4959. gfc_array_index_type type. */
  4960. tree
  4961. size_of_string_in_bytes (int kind, tree string_length)
  4962. {
  4963. tree bytesize;
  4964. int i = gfc_validate_kind (BT_CHARACTER, kind, false);
  4965. bytesize = build_int_cst (gfc_array_index_type,
  4966. gfc_character_kinds[i].bit_size / 8);
  4967. return fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
  4968. bytesize,
  4969. fold_convert (gfc_array_index_type, string_length));
  4970. }
  4971. static void
  4972. gfc_conv_intrinsic_sizeof (gfc_se *se, gfc_expr *expr)
  4973. {
  4974. gfc_expr *arg;
  4975. gfc_se argse;
  4976. tree source_bytes;
  4977. tree tmp;
  4978. tree lower;
  4979. tree upper;
  4980. tree byte_size;
  4981. int n;
  4982. gfc_init_se (&argse, NULL);
  4983. arg = expr->value.function.actual->expr;
  4984. if (arg->rank || arg->ts.type == BT_ASSUMED)
  4985. gfc_conv_expr_descriptor (&argse, arg);
  4986. else
  4987. gfc_conv_expr_reference (&argse, arg);
  4988. if (arg->ts.type == BT_ASSUMED)
  4989. {
  4990. /* This only works if an array descriptor has been passed; thus, extract
  4991. the size from the descriptor. */
  4992. gcc_assert (TYPE_PRECISION (gfc_array_index_type)
  4993. == TYPE_PRECISION (size_type_node));
  4994. tmp = arg->symtree->n.sym->backend_decl;
  4995. tmp = DECL_LANG_SPECIFIC (tmp)
  4996. && GFC_DECL_SAVED_DESCRIPTOR (tmp) != NULL_TREE
  4997. ? GFC_DECL_SAVED_DESCRIPTOR (tmp) : tmp;
  4998. if (POINTER_TYPE_P (TREE_TYPE (tmp)))
  4999. tmp = build_fold_indirect_ref_loc (input_location, tmp);
  5000. tmp = fold_convert (size_type_node, gfc_conv_descriptor_dtype (tmp));
  5001. tmp = fold_build2_loc (input_location, RSHIFT_EXPR, TREE_TYPE (tmp), tmp,
  5002. build_int_cst (TREE_TYPE (tmp),
  5003. GFC_DTYPE_SIZE_SHIFT));
  5004. byte_size = fold_convert (gfc_array_index_type, tmp);
  5005. }
  5006. else if (arg->ts.type == BT_CLASS)
  5007. {
  5008. if (arg->rank)
  5009. byte_size = gfc_class_vtab_size_get (TREE_OPERAND (argse.expr, 0));
  5010. else
  5011. byte_size = gfc_class_vtab_size_get (argse.expr);
  5012. }
  5013. else
  5014. {
  5015. if (arg->ts.type == BT_CHARACTER)
  5016. byte_size = size_of_string_in_bytes (arg->ts.kind, argse.string_length);
  5017. else
  5018. {
  5019. if (arg->rank == 0)
  5020. byte_size = TREE_TYPE (build_fold_indirect_ref_loc (input_location,
  5021. argse.expr));
  5022. else
  5023. byte_size = gfc_get_element_type (TREE_TYPE (argse.expr));
  5024. byte_size = fold_convert (gfc_array_index_type,
  5025. size_in_bytes (byte_size));
  5026. }
  5027. }
  5028. if (arg->rank == 0)
  5029. se->expr = byte_size;
  5030. else
  5031. {
  5032. source_bytes = gfc_create_var (gfc_array_index_type, "bytes");
  5033. gfc_add_modify (&argse.pre, source_bytes, byte_size);
  5034. if (arg->rank == -1)
  5035. {
  5036. tree cond, loop_var, exit_label;
  5037. stmtblock_t body;
  5038. tmp = fold_convert (gfc_array_index_type,
  5039. gfc_conv_descriptor_rank (argse.expr));
  5040. loop_var = gfc_create_var (gfc_array_index_type, "i");
  5041. gfc_add_modify (&argse.pre, loop_var, gfc_index_zero_node);
  5042. exit_label = gfc_build_label_decl (NULL_TREE);
  5043. /* Create loop:
  5044. for (;;)
  5045. {
  5046. if (i >= rank)
  5047. goto exit;
  5048. source_bytes = source_bytes * array.dim[i].extent;
  5049. i = i + 1;
  5050. }
  5051. exit: */
  5052. gfc_start_block (&body);
  5053. cond = fold_build2_loc (input_location, GE_EXPR, boolean_type_node,
  5054. loop_var, tmp);
  5055. tmp = build1_v (GOTO_EXPR, exit_label);
  5056. tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
  5057. cond, tmp, build_empty_stmt (input_location));
  5058. gfc_add_expr_to_block (&body, tmp);
  5059. lower = gfc_conv_descriptor_lbound_get (argse.expr, loop_var);
  5060. upper = gfc_conv_descriptor_ubound_get (argse.expr, loop_var);
  5061. tmp = gfc_conv_array_extent_dim (lower, upper, NULL);
  5062. tmp = fold_build2_loc (input_location, MULT_EXPR,
  5063. gfc_array_index_type, tmp, source_bytes);
  5064. gfc_add_modify (&body, source_bytes, tmp);
  5065. tmp = fold_build2_loc (input_location, PLUS_EXPR,
  5066. gfc_array_index_type, loop_var,
  5067. gfc_index_one_node);
  5068. gfc_add_modify_loc (input_location, &body, loop_var, tmp);
  5069. tmp = gfc_finish_block (&body);
  5070. tmp = fold_build1_loc (input_location, LOOP_EXPR, void_type_node,
  5071. tmp);
  5072. gfc_add_expr_to_block (&argse.pre, tmp);
  5073. tmp = build1_v (LABEL_EXPR, exit_label);
  5074. gfc_add_expr_to_block (&argse.pre, tmp);
  5075. }
  5076. else
  5077. {
  5078. /* Obtain the size of the array in bytes. */
  5079. for (n = 0; n < arg->rank; n++)
  5080. {
  5081. tree idx;
  5082. idx = gfc_rank_cst[n];
  5083. lower = gfc_conv_descriptor_lbound_get (argse.expr, idx);
  5084. upper = gfc_conv_descriptor_ubound_get (argse.expr, idx);
  5085. tmp = gfc_conv_array_extent_dim (lower, upper, NULL);
  5086. tmp = fold_build2_loc (input_location, MULT_EXPR,
  5087. gfc_array_index_type, tmp, source_bytes);
  5088. gfc_add_modify (&argse.pre, source_bytes, tmp);
  5089. }
  5090. }
  5091. se->expr = source_bytes;
  5092. }
  5093. gfc_add_block_to_block (&se->pre, &argse.pre);
  5094. }
  5095. static void
  5096. gfc_conv_intrinsic_storage_size (gfc_se *se, gfc_expr *expr)
  5097. {
  5098. gfc_expr *arg;
  5099. gfc_se argse;
  5100. tree type, result_type, tmp;
  5101. arg = expr->value.function.actual->expr;
  5102. gfc_init_se (&argse, NULL);
  5103. result_type = gfc_get_int_type (expr->ts.kind);
  5104. if (arg->rank == 0)
  5105. {
  5106. if (arg->ts.type == BT_CLASS)
  5107. {
  5108. gfc_add_vptr_component (arg);
  5109. gfc_add_size_component (arg);
  5110. gfc_conv_expr (&argse, arg);
  5111. tmp = fold_convert (result_type, argse.expr);
  5112. goto done;
  5113. }
  5114. gfc_conv_expr_reference (&argse, arg);
  5115. type = TREE_TYPE (build_fold_indirect_ref_loc (input_location,
  5116. argse.expr));
  5117. }
  5118. else
  5119. {
  5120. argse.want_pointer = 0;
  5121. gfc_conv_expr_descriptor (&argse, arg);
  5122. if (arg->ts.type == BT_CLASS)
  5123. {
  5124. tmp = gfc_class_vtab_size_get (TREE_OPERAND (argse.expr, 0));
  5125. tmp = fold_convert (result_type, tmp);
  5126. goto done;
  5127. }
  5128. type = gfc_get_element_type (TREE_TYPE (argse.expr));
  5129. }
  5130. /* Obtain the argument's word length. */
  5131. if (arg->ts.type == BT_CHARACTER)
  5132. tmp = size_of_string_in_bytes (arg->ts.kind, argse.string_length);
  5133. else
  5134. tmp = size_in_bytes (type);
  5135. tmp = fold_convert (result_type, tmp);
  5136. done:
  5137. se->expr = fold_build2_loc (input_location, MULT_EXPR, result_type, tmp,
  5138. build_int_cst (result_type, BITS_PER_UNIT));
  5139. gfc_add_block_to_block (&se->pre, &argse.pre);
  5140. }
  5141. /* Intrinsic string comparison functions. */
  5142. static void
  5143. gfc_conv_intrinsic_strcmp (gfc_se * se, gfc_expr * expr, enum tree_code op)
  5144. {
  5145. tree args[4];
  5146. gfc_conv_intrinsic_function_args (se, expr, args, 4);
  5147. se->expr
  5148. = gfc_build_compare_string (args[0], args[1], args[2], args[3],
  5149. expr->value.function.actual->expr->ts.kind,
  5150. op);
  5151. se->expr = fold_build2_loc (input_location, op,
  5152. gfc_typenode_for_spec (&expr->ts), se->expr,
  5153. build_int_cst (TREE_TYPE (se->expr), 0));
  5154. }
  5155. /* Generate a call to the adjustl/adjustr library function. */
  5156. static void
  5157. gfc_conv_intrinsic_adjust (gfc_se * se, gfc_expr * expr, tree fndecl)
  5158. {
  5159. tree args[3];
  5160. tree len;
  5161. tree type;
  5162. tree var;
  5163. tree tmp;
  5164. gfc_conv_intrinsic_function_args (se, expr, &args[1], 2);
  5165. len = args[1];
  5166. type = TREE_TYPE (args[2]);
  5167. var = gfc_conv_string_tmp (se, type, len);
  5168. args[0] = var;
  5169. tmp = build_call_expr_loc (input_location,
  5170. fndecl, 3, args[0], args[1], args[2]);
  5171. gfc_add_expr_to_block (&se->pre, tmp);
  5172. se->expr = var;
  5173. se->string_length = len;
  5174. }
  5175. /* Generate code for the TRANSFER intrinsic:
  5176. For scalar results:
  5177. DEST = TRANSFER (SOURCE, MOLD)
  5178. where:
  5179. typeof<DEST> = typeof<MOLD>
  5180. and:
  5181. MOLD is scalar.
  5182. For array results:
  5183. DEST(1:N) = TRANSFER (SOURCE, MOLD[, SIZE])
  5184. where:
  5185. typeof<DEST> = typeof<MOLD>
  5186. and:
  5187. N = min (sizeof (SOURCE(:)), sizeof (DEST(:)),
  5188. sizeof (DEST(0) * SIZE). */
  5189. static void
  5190. gfc_conv_intrinsic_transfer (gfc_se * se, gfc_expr * expr)
  5191. {
  5192. tree tmp;
  5193. tree tmpdecl;
  5194. tree ptr;
  5195. tree extent;
  5196. tree source;
  5197. tree source_type;
  5198. tree source_bytes;
  5199. tree mold_type;
  5200. tree dest_word_len;
  5201. tree size_words;
  5202. tree size_bytes;
  5203. tree upper;
  5204. tree lower;
  5205. tree stmt;
  5206. gfc_actual_arglist *arg;
  5207. gfc_se argse;
  5208. gfc_array_info *info;
  5209. stmtblock_t block;
  5210. int n;
  5211. bool scalar_mold;
  5212. gfc_expr *source_expr, *mold_expr;
  5213. info = NULL;
  5214. if (se->loop)
  5215. info = &se->ss->info->data.array;
  5216. /* Convert SOURCE. The output from this stage is:-
  5217. source_bytes = length of the source in bytes
  5218. source = pointer to the source data. */
  5219. arg = expr->value.function.actual;
  5220. source_expr = arg->expr;
  5221. /* Ensure double transfer through LOGICAL preserves all
  5222. the needed bits. */
  5223. if (arg->expr->expr_type == EXPR_FUNCTION
  5224. && arg->expr->value.function.esym == NULL
  5225. && arg->expr->value.function.isym != NULL
  5226. && arg->expr->value.function.isym->id == GFC_ISYM_TRANSFER
  5227. && arg->expr->ts.type == BT_LOGICAL
  5228. && expr->ts.type != arg->expr->ts.type)
  5229. arg->expr->value.function.name = "__transfer_in_transfer";
  5230. gfc_init_se (&argse, NULL);
  5231. source_bytes = gfc_create_var (gfc_array_index_type, NULL);
  5232. /* Obtain the pointer to source and the length of source in bytes. */
  5233. if (arg->expr->rank == 0)
  5234. {
  5235. gfc_conv_expr_reference (&argse, arg->expr);
  5236. if (arg->expr->ts.type == BT_CLASS)
  5237. source = gfc_class_data_get (argse.expr);
  5238. else
  5239. source = argse.expr;
  5240. /* Obtain the source word length. */
  5241. switch (arg->expr->ts.type)
  5242. {
  5243. case BT_CHARACTER:
  5244. tmp = size_of_string_in_bytes (arg->expr->ts.kind,
  5245. argse.string_length);
  5246. break;
  5247. case BT_CLASS:
  5248. tmp = gfc_class_vtab_size_get (argse.expr);
  5249. break;
  5250. default:
  5251. source_type = TREE_TYPE (build_fold_indirect_ref_loc (input_location,
  5252. source));
  5253. tmp = fold_convert (gfc_array_index_type,
  5254. size_in_bytes (source_type));
  5255. break;
  5256. }
  5257. }
  5258. else
  5259. {
  5260. argse.want_pointer = 0;
  5261. gfc_conv_expr_descriptor (&argse, arg->expr);
  5262. source = gfc_conv_descriptor_data_get (argse.expr);
  5263. source_type = gfc_get_element_type (TREE_TYPE (argse.expr));
  5264. /* Repack the source if not simply contiguous. */
  5265. if (!gfc_is_simply_contiguous (arg->expr, false))
  5266. {
  5267. tmp = gfc_build_addr_expr (NULL_TREE, argse.expr);
  5268. if (warn_array_temporaries)
  5269. gfc_warning (OPT_Warray_temporaries,
  5270. "Creating array temporary at %L", &expr->where);
  5271. source = build_call_expr_loc (input_location,
  5272. gfor_fndecl_in_pack, 1, tmp);
  5273. source = gfc_evaluate_now (source, &argse.pre);
  5274. /* Free the temporary. */
  5275. gfc_start_block (&block);
  5276. tmp = gfc_call_free (convert (pvoid_type_node, source));
  5277. gfc_add_expr_to_block (&block, tmp);
  5278. stmt = gfc_finish_block (&block);
  5279. /* Clean up if it was repacked. */
  5280. gfc_init_block (&block);
  5281. tmp = gfc_conv_array_data (argse.expr);
  5282. tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
  5283. source, tmp);
  5284. tmp = build3_v (COND_EXPR, tmp, stmt,
  5285. build_empty_stmt (input_location));
  5286. gfc_add_expr_to_block (&block, tmp);
  5287. gfc_add_block_to_block (&block, &se->post);
  5288. gfc_init_block (&se->post);
  5289. gfc_add_block_to_block (&se->post, &block);
  5290. }
  5291. /* Obtain the source word length. */
  5292. if (arg->expr->ts.type == BT_CHARACTER)
  5293. tmp = size_of_string_in_bytes (arg->expr->ts.kind,
  5294. argse.string_length);
  5295. else
  5296. tmp = fold_convert (gfc_array_index_type,
  5297. size_in_bytes (source_type));
  5298. /* Obtain the size of the array in bytes. */
  5299. extent = gfc_create_var (gfc_array_index_type, NULL);
  5300. for (n = 0; n < arg->expr->rank; n++)
  5301. {
  5302. tree idx;
  5303. idx = gfc_rank_cst[n];
  5304. gfc_add_modify (&argse.pre, source_bytes, tmp);
  5305. lower = gfc_conv_descriptor_lbound_get (argse.expr, idx);
  5306. upper = gfc_conv_descriptor_ubound_get (argse.expr, idx);
  5307. tmp = fold_build2_loc (input_location, MINUS_EXPR,
  5308. gfc_array_index_type, upper, lower);
  5309. gfc_add_modify (&argse.pre, extent, tmp);
  5310. tmp = fold_build2_loc (input_location, PLUS_EXPR,
  5311. gfc_array_index_type, extent,
  5312. gfc_index_one_node);
  5313. tmp = fold_build2_loc (input_location, MULT_EXPR,
  5314. gfc_array_index_type, tmp, source_bytes);
  5315. }
  5316. }
  5317. gfc_add_modify (&argse.pre, source_bytes, tmp);
  5318. gfc_add_block_to_block (&se->pre, &argse.pre);
  5319. gfc_add_block_to_block (&se->post, &argse.post);
  5320. /* Now convert MOLD. The outputs are:
  5321. mold_type = the TREE type of MOLD
  5322. dest_word_len = destination word length in bytes. */
  5323. arg = arg->next;
  5324. mold_expr = arg->expr;
  5325. gfc_init_se (&argse, NULL);
  5326. scalar_mold = arg->expr->rank == 0;
  5327. if (arg->expr->rank == 0)
  5328. {
  5329. gfc_conv_expr_reference (&argse, arg->expr);
  5330. mold_type = TREE_TYPE (build_fold_indirect_ref_loc (input_location,
  5331. argse.expr));
  5332. }
  5333. else
  5334. {
  5335. gfc_init_se (&argse, NULL);
  5336. argse.want_pointer = 0;
  5337. gfc_conv_expr_descriptor (&argse, arg->expr);
  5338. mold_type = gfc_get_element_type (TREE_TYPE (argse.expr));
  5339. }
  5340. gfc_add_block_to_block (&se->pre, &argse.pre);
  5341. gfc_add_block_to_block (&se->post, &argse.post);
  5342. if (strcmp (expr->value.function.name, "__transfer_in_transfer") == 0)
  5343. {
  5344. /* If this TRANSFER is nested in another TRANSFER, use a type
  5345. that preserves all bits. */
  5346. if (arg->expr->ts.type == BT_LOGICAL)
  5347. mold_type = gfc_get_int_type (arg->expr->ts.kind);
  5348. }
  5349. /* Obtain the destination word length. */
  5350. switch (arg->expr->ts.type)
  5351. {
  5352. case BT_CHARACTER:
  5353. tmp = size_of_string_in_bytes (arg->expr->ts.kind, argse.string_length);
  5354. mold_type = gfc_get_character_type_len (arg->expr->ts.kind, tmp);
  5355. break;
  5356. case BT_CLASS:
  5357. tmp = gfc_class_vtab_size_get (argse.expr);
  5358. break;
  5359. default:
  5360. tmp = fold_convert (gfc_array_index_type, size_in_bytes (mold_type));
  5361. break;
  5362. }
  5363. dest_word_len = gfc_create_var (gfc_array_index_type, NULL);
  5364. gfc_add_modify (&se->pre, dest_word_len, tmp);
  5365. /* Finally convert SIZE, if it is present. */
  5366. arg = arg->next;
  5367. size_words = gfc_create_var (gfc_array_index_type, NULL);
  5368. if (arg->expr)
  5369. {
  5370. gfc_init_se (&argse, NULL);
  5371. gfc_conv_expr_reference (&argse, arg->expr);
  5372. tmp = convert (gfc_array_index_type,
  5373. build_fold_indirect_ref_loc (input_location,
  5374. argse.expr));
  5375. gfc_add_block_to_block (&se->pre, &argse.pre);
  5376. gfc_add_block_to_block (&se->post, &argse.post);
  5377. }
  5378. else
  5379. tmp = NULL_TREE;
  5380. /* Separate array and scalar results. */
  5381. if (scalar_mold && tmp == NULL_TREE)
  5382. goto scalar_transfer;
  5383. size_bytes = gfc_create_var (gfc_array_index_type, NULL);
  5384. if (tmp != NULL_TREE)
  5385. tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
  5386. tmp, dest_word_len);
  5387. else
  5388. tmp = source_bytes;
  5389. gfc_add_modify (&se->pre, size_bytes, tmp);
  5390. gfc_add_modify (&se->pre, size_words,
  5391. fold_build2_loc (input_location, CEIL_DIV_EXPR,
  5392. gfc_array_index_type,
  5393. size_bytes, dest_word_len));
  5394. /* Evaluate the bounds of the result. If the loop range exists, we have
  5395. to check if it is too large. If so, we modify loop->to be consistent
  5396. with min(size, size(source)). Otherwise, size is made consistent with
  5397. the loop range, so that the right number of bytes is transferred.*/
  5398. n = se->loop->order[0];
  5399. if (se->loop->to[n] != NULL_TREE)
  5400. {
  5401. tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
  5402. se->loop->to[n], se->loop->from[n]);
  5403. tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
  5404. tmp, gfc_index_one_node);
  5405. tmp = fold_build2_loc (input_location, MIN_EXPR, gfc_array_index_type,
  5406. tmp, size_words);
  5407. gfc_add_modify (&se->pre, size_words, tmp);
  5408. gfc_add_modify (&se->pre, size_bytes,
  5409. fold_build2_loc (input_location, MULT_EXPR,
  5410. gfc_array_index_type,
  5411. size_words, dest_word_len));
  5412. upper = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
  5413. size_words, se->loop->from[n]);
  5414. upper = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
  5415. upper, gfc_index_one_node);
  5416. }
  5417. else
  5418. {
  5419. upper = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
  5420. size_words, gfc_index_one_node);
  5421. se->loop->from[n] = gfc_index_zero_node;
  5422. }
  5423. se->loop->to[n] = upper;
  5424. /* Build a destination descriptor, using the pointer, source, as the
  5425. data field. */
  5426. gfc_trans_create_temp_array (&se->pre, &se->post, se->ss, mold_type,
  5427. NULL_TREE, false, true, false, &expr->where);
  5428. /* Cast the pointer to the result. */
  5429. tmp = gfc_conv_descriptor_data_get (info->descriptor);
  5430. tmp = fold_convert (pvoid_type_node, tmp);
  5431. /* Use memcpy to do the transfer. */
  5432. tmp
  5433. = build_call_expr_loc (input_location,
  5434. builtin_decl_explicit (BUILT_IN_MEMCPY), 3, tmp,
  5435. fold_convert (pvoid_type_node, source),
  5436. fold_convert (size_type_node,
  5437. fold_build2_loc (input_location,
  5438. MIN_EXPR,
  5439. gfc_array_index_type,
  5440. size_bytes,
  5441. source_bytes)));
  5442. gfc_add_expr_to_block (&se->pre, tmp);
  5443. se->expr = info->descriptor;
  5444. if (expr->ts.type == BT_CHARACTER)
  5445. se->string_length = fold_convert (gfc_charlen_type_node, dest_word_len);
  5446. return;
  5447. /* Deal with scalar results. */
  5448. scalar_transfer:
  5449. extent = fold_build2_loc (input_location, MIN_EXPR, gfc_array_index_type,
  5450. dest_word_len, source_bytes);
  5451. extent = fold_build2_loc (input_location, MAX_EXPR, gfc_array_index_type,
  5452. extent, gfc_index_zero_node);
  5453. if (expr->ts.type == BT_CHARACTER)
  5454. {
  5455. tree direct, indirect, free;
  5456. ptr = convert (gfc_get_pchar_type (expr->ts.kind), source);
  5457. tmpdecl = gfc_create_var (gfc_get_pchar_type (expr->ts.kind),
  5458. "transfer");
  5459. /* If source is longer than the destination, use a pointer to
  5460. the source directly. */
  5461. gfc_init_block (&block);
  5462. gfc_add_modify (&block, tmpdecl, ptr);
  5463. direct = gfc_finish_block (&block);
  5464. /* Otherwise, allocate a string with the length of the destination
  5465. and copy the source into it. */
  5466. gfc_init_block (&block);
  5467. tmp = gfc_get_pchar_type (expr->ts.kind);
  5468. tmp = gfc_call_malloc (&block, tmp, dest_word_len);
  5469. gfc_add_modify (&block, tmpdecl,
  5470. fold_convert (TREE_TYPE (ptr), tmp));
  5471. tmp = build_call_expr_loc (input_location,
  5472. builtin_decl_explicit (BUILT_IN_MEMCPY), 3,
  5473. fold_convert (pvoid_type_node, tmpdecl),
  5474. fold_convert (pvoid_type_node, ptr),
  5475. fold_convert (size_type_node, extent));
  5476. gfc_add_expr_to_block (&block, tmp);
  5477. indirect = gfc_finish_block (&block);
  5478. /* Wrap it up with the condition. */
  5479. tmp = fold_build2_loc (input_location, LE_EXPR, boolean_type_node,
  5480. dest_word_len, source_bytes);
  5481. tmp = build3_v (COND_EXPR, tmp, direct, indirect);
  5482. gfc_add_expr_to_block (&se->pre, tmp);
  5483. /* Free the temporary string, if necessary. */
  5484. free = gfc_call_free (tmpdecl);
  5485. tmp = fold_build2_loc (input_location, GT_EXPR, boolean_type_node,
  5486. dest_word_len, source_bytes);
  5487. tmp = build3_v (COND_EXPR, tmp, free, build_empty_stmt (input_location));
  5488. gfc_add_expr_to_block (&se->post, tmp);
  5489. se->expr = tmpdecl;
  5490. se->string_length = fold_convert (gfc_charlen_type_node, dest_word_len);
  5491. }
  5492. else
  5493. {
  5494. tmpdecl = gfc_create_var (mold_type, "transfer");
  5495. ptr = convert (build_pointer_type (mold_type), source);
  5496. /* For CLASS results, allocate the needed memory first. */
  5497. if (mold_expr->ts.type == BT_CLASS)
  5498. {
  5499. tree cdata;
  5500. cdata = gfc_class_data_get (tmpdecl);
  5501. tmp = gfc_call_malloc (&se->pre, TREE_TYPE (cdata), dest_word_len);
  5502. gfc_add_modify (&se->pre, cdata, tmp);
  5503. }
  5504. /* Use memcpy to do the transfer. */
  5505. if (mold_expr->ts.type == BT_CLASS)
  5506. tmp = gfc_class_data_get (tmpdecl);
  5507. else
  5508. tmp = gfc_build_addr_expr (NULL_TREE, tmpdecl);
  5509. tmp = build_call_expr_loc (input_location,
  5510. builtin_decl_explicit (BUILT_IN_MEMCPY), 3,
  5511. fold_convert (pvoid_type_node, tmp),
  5512. fold_convert (pvoid_type_node, ptr),
  5513. fold_convert (size_type_node, extent));
  5514. gfc_add_expr_to_block (&se->pre, tmp);
  5515. /* For CLASS results, set the _vptr. */
  5516. if (mold_expr->ts.type == BT_CLASS)
  5517. {
  5518. tree vptr;
  5519. gfc_symbol *vtab;
  5520. vptr = gfc_class_vptr_get (tmpdecl);
  5521. vtab = gfc_find_derived_vtab (source_expr->ts.u.derived);
  5522. gcc_assert (vtab);
  5523. tmp = gfc_build_addr_expr (NULL_TREE, gfc_get_symbol_decl (vtab));
  5524. gfc_add_modify (&se->pre, vptr, fold_convert (TREE_TYPE (vptr), tmp));
  5525. }
  5526. se->expr = tmpdecl;
  5527. }
  5528. }
  5529. /* Generate code for the ALLOCATED intrinsic.
  5530. Generate inline code that directly check the address of the argument. */
  5531. static void
  5532. gfc_conv_allocated (gfc_se *se, gfc_expr *expr)
  5533. {
  5534. gfc_actual_arglist *arg1;
  5535. gfc_se arg1se;
  5536. tree tmp;
  5537. gfc_init_se (&arg1se, NULL);
  5538. arg1 = expr->value.function.actual;
  5539. if (arg1->expr->ts.type == BT_CLASS)
  5540. {
  5541. /* Make sure that class array expressions have both a _data
  5542. component reference and an array reference.... */
  5543. if (CLASS_DATA (arg1->expr)->attr.dimension)
  5544. gfc_add_class_array_ref (arg1->expr);
  5545. /* .... whilst scalars only need the _data component. */
  5546. else
  5547. gfc_add_data_component (arg1->expr);
  5548. }
  5549. if (arg1->expr->rank == 0)
  5550. {
  5551. /* Allocatable scalar. */
  5552. arg1se.want_pointer = 1;
  5553. gfc_conv_expr (&arg1se, arg1->expr);
  5554. tmp = arg1se.expr;
  5555. }
  5556. else
  5557. {
  5558. /* Allocatable array. */
  5559. arg1se.descriptor_only = 1;
  5560. gfc_conv_expr_descriptor (&arg1se, arg1->expr);
  5561. tmp = gfc_conv_descriptor_data_get (arg1se.expr);
  5562. }
  5563. tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, tmp,
  5564. fold_convert (TREE_TYPE (tmp), null_pointer_node));
  5565. se->expr = convert (gfc_typenode_for_spec (&expr->ts), tmp);
  5566. }
  5567. /* Generate code for the ASSOCIATED intrinsic.
  5568. If both POINTER and TARGET are arrays, generate a call to library function
  5569. _gfor_associated, and pass descriptors of POINTER and TARGET to it.
  5570. In other cases, generate inline code that directly compare the address of
  5571. POINTER with the address of TARGET. */
  5572. static void
  5573. gfc_conv_associated (gfc_se *se, gfc_expr *expr)
  5574. {
  5575. gfc_actual_arglist *arg1;
  5576. gfc_actual_arglist *arg2;
  5577. gfc_se arg1se;
  5578. gfc_se arg2se;
  5579. tree tmp2;
  5580. tree tmp;
  5581. tree nonzero_charlen;
  5582. tree nonzero_arraylen;
  5583. gfc_ss *ss;
  5584. bool scalar;
  5585. gfc_init_se (&arg1se, NULL);
  5586. gfc_init_se (&arg2se, NULL);
  5587. arg1 = expr->value.function.actual;
  5588. arg2 = arg1->next;
  5589. /* Check whether the expression is a scalar or not; we cannot use
  5590. arg1->expr->rank as it can be nonzero for proc pointers. */
  5591. ss = gfc_walk_expr (arg1->expr);
  5592. scalar = ss == gfc_ss_terminator;
  5593. if (!scalar)
  5594. gfc_free_ss_chain (ss);
  5595. if (!arg2->expr)
  5596. {
  5597. /* No optional target. */
  5598. if (scalar)
  5599. {
  5600. /* A pointer to a scalar. */
  5601. arg1se.want_pointer = 1;
  5602. gfc_conv_expr (&arg1se, arg1->expr);
  5603. if (arg1->expr->symtree->n.sym->attr.proc_pointer
  5604. && arg1->expr->symtree->n.sym->attr.dummy)
  5605. arg1se.expr = build_fold_indirect_ref_loc (input_location,
  5606. arg1se.expr);
  5607. if (arg1->expr->ts.type == BT_CLASS)
  5608. {
  5609. tmp2 = gfc_class_data_get (arg1se.expr);
  5610. if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp2)))
  5611. tmp2 = gfc_conv_descriptor_data_get (tmp2);
  5612. }
  5613. else
  5614. tmp2 = arg1se.expr;
  5615. }
  5616. else
  5617. {
  5618. /* A pointer to an array. */
  5619. gfc_conv_expr_descriptor (&arg1se, arg1->expr);
  5620. tmp2 = gfc_conv_descriptor_data_get (arg1se.expr);
  5621. }
  5622. gfc_add_block_to_block (&se->pre, &arg1se.pre);
  5623. gfc_add_block_to_block (&se->post, &arg1se.post);
  5624. tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, tmp2,
  5625. fold_convert (TREE_TYPE (tmp2), null_pointer_node));
  5626. se->expr = tmp;
  5627. }
  5628. else
  5629. {
  5630. /* An optional target. */
  5631. if (arg2->expr->ts.type == BT_CLASS)
  5632. gfc_add_data_component (arg2->expr);
  5633. nonzero_charlen = NULL_TREE;
  5634. if (arg1->expr->ts.type == BT_CHARACTER)
  5635. nonzero_charlen = fold_build2_loc (input_location, NE_EXPR,
  5636. boolean_type_node,
  5637. arg1->expr->ts.u.cl->backend_decl,
  5638. integer_zero_node);
  5639. if (scalar)
  5640. {
  5641. /* A pointer to a scalar. */
  5642. arg1se.want_pointer = 1;
  5643. gfc_conv_expr (&arg1se, arg1->expr);
  5644. if (arg1->expr->symtree->n.sym->attr.proc_pointer
  5645. && arg1->expr->symtree->n.sym->attr.dummy)
  5646. arg1se.expr = build_fold_indirect_ref_loc (input_location,
  5647. arg1se.expr);
  5648. if (arg1->expr->ts.type == BT_CLASS)
  5649. arg1se.expr = gfc_class_data_get (arg1se.expr);
  5650. arg2se.want_pointer = 1;
  5651. gfc_conv_expr (&arg2se, arg2->expr);
  5652. if (arg2->expr->symtree->n.sym->attr.proc_pointer
  5653. && arg2->expr->symtree->n.sym->attr.dummy)
  5654. arg2se.expr = build_fold_indirect_ref_loc (input_location,
  5655. arg2se.expr);
  5656. gfc_add_block_to_block (&se->pre, &arg1se.pre);
  5657. gfc_add_block_to_block (&se->post, &arg1se.post);
  5658. tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
  5659. arg1se.expr, arg2se.expr);
  5660. tmp2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
  5661. arg1se.expr, null_pointer_node);
  5662. se->expr = fold_build2_loc (input_location, TRUTH_AND_EXPR,
  5663. boolean_type_node, tmp, tmp2);
  5664. }
  5665. else
  5666. {
  5667. /* An array pointer of zero length is not associated if target is
  5668. present. */
  5669. arg1se.descriptor_only = 1;
  5670. gfc_conv_expr_lhs (&arg1se, arg1->expr);
  5671. if (arg1->expr->rank == -1)
  5672. {
  5673. tmp = gfc_conv_descriptor_rank (arg1se.expr);
  5674. tmp = fold_build2_loc (input_location, MINUS_EXPR,
  5675. TREE_TYPE (tmp), tmp, gfc_index_one_node);
  5676. }
  5677. else
  5678. tmp = gfc_rank_cst[arg1->expr->rank - 1];
  5679. tmp = gfc_conv_descriptor_stride_get (arg1se.expr, tmp);
  5680. nonzero_arraylen = fold_build2_loc (input_location, NE_EXPR,
  5681. boolean_type_node, tmp,
  5682. build_int_cst (TREE_TYPE (tmp), 0));
  5683. /* A pointer to an array, call library function _gfor_associated. */
  5684. arg1se.want_pointer = 1;
  5685. gfc_conv_expr_descriptor (&arg1se, arg1->expr);
  5686. arg2se.want_pointer = 1;
  5687. gfc_conv_expr_descriptor (&arg2se, arg2->expr);
  5688. gfc_add_block_to_block (&se->pre, &arg2se.pre);
  5689. gfc_add_block_to_block (&se->post, &arg2se.post);
  5690. se->expr = build_call_expr_loc (input_location,
  5691. gfor_fndecl_associated, 2,
  5692. arg1se.expr, arg2se.expr);
  5693. se->expr = convert (boolean_type_node, se->expr);
  5694. se->expr = fold_build2_loc (input_location, TRUTH_AND_EXPR,
  5695. boolean_type_node, se->expr,
  5696. nonzero_arraylen);
  5697. }
  5698. /* If target is present zero character length pointers cannot
  5699. be associated. */
  5700. if (nonzero_charlen != NULL_TREE)
  5701. se->expr = fold_build2_loc (input_location, TRUTH_AND_EXPR,
  5702. boolean_type_node,
  5703. se->expr, nonzero_charlen);
  5704. }
  5705. se->expr = convert (gfc_typenode_for_spec (&expr->ts), se->expr);
  5706. }
  5707. /* Generate code for the SAME_TYPE_AS intrinsic.
  5708. Generate inline code that directly checks the vindices. */
  5709. static void
  5710. gfc_conv_same_type_as (gfc_se *se, gfc_expr *expr)
  5711. {
  5712. gfc_expr *a, *b;
  5713. gfc_se se1, se2;
  5714. tree tmp;
  5715. tree conda = NULL_TREE, condb = NULL_TREE;
  5716. gfc_init_se (&se1, NULL);
  5717. gfc_init_se (&se2, NULL);
  5718. a = expr->value.function.actual->expr;
  5719. b = expr->value.function.actual->next->expr;
  5720. if (UNLIMITED_POLY (a))
  5721. {
  5722. tmp = gfc_class_vptr_get (a->symtree->n.sym->backend_decl);
  5723. conda = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
  5724. tmp, build_int_cst (TREE_TYPE (tmp), 0));
  5725. }
  5726. if (UNLIMITED_POLY (b))
  5727. {
  5728. tmp = gfc_class_vptr_get (b->symtree->n.sym->backend_decl);
  5729. condb = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
  5730. tmp, build_int_cst (TREE_TYPE (tmp), 0));
  5731. }
  5732. if (a->ts.type == BT_CLASS)
  5733. {
  5734. gfc_add_vptr_component (a);
  5735. gfc_add_hash_component (a);
  5736. }
  5737. else if (a->ts.type == BT_DERIVED)
  5738. a = gfc_get_int_expr (gfc_default_integer_kind, NULL,
  5739. a->ts.u.derived->hash_value);
  5740. if (b->ts.type == BT_CLASS)
  5741. {
  5742. gfc_add_vptr_component (b);
  5743. gfc_add_hash_component (b);
  5744. }
  5745. else if (b->ts.type == BT_DERIVED)
  5746. b = gfc_get_int_expr (gfc_default_integer_kind, NULL,
  5747. b->ts.u.derived->hash_value);
  5748. gfc_conv_expr (&se1, a);
  5749. gfc_conv_expr (&se2, b);
  5750. tmp = fold_build2_loc (input_location, EQ_EXPR,
  5751. boolean_type_node, se1.expr,
  5752. fold_convert (TREE_TYPE (se1.expr), se2.expr));
  5753. if (conda)
  5754. tmp = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
  5755. boolean_type_node, conda, tmp);
  5756. if (condb)
  5757. tmp = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
  5758. boolean_type_node, condb, tmp);
  5759. se->expr = convert (gfc_typenode_for_spec (&expr->ts), tmp);
  5760. }
  5761. /* Generate code for SELECTED_CHAR_KIND (NAME) intrinsic function. */
  5762. static void
  5763. gfc_conv_intrinsic_sc_kind (gfc_se *se, gfc_expr *expr)
  5764. {
  5765. tree args[2];
  5766. gfc_conv_intrinsic_function_args (se, expr, args, 2);
  5767. se->expr = build_call_expr_loc (input_location,
  5768. gfor_fndecl_sc_kind, 2, args[0], args[1]);
  5769. se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), se->expr);
  5770. }
  5771. /* Generate code for SELECTED_INT_KIND (R) intrinsic function. */
  5772. static void
  5773. gfc_conv_intrinsic_si_kind (gfc_se *se, gfc_expr *expr)
  5774. {
  5775. tree arg, type;
  5776. gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
  5777. /* The argument to SELECTED_INT_KIND is INTEGER(4). */
  5778. type = gfc_get_int_type (4);
  5779. arg = gfc_build_addr_expr (NULL_TREE, fold_convert (type, arg));
  5780. /* Convert it to the required type. */
  5781. type = gfc_typenode_for_spec (&expr->ts);
  5782. se->expr = build_call_expr_loc (input_location,
  5783. gfor_fndecl_si_kind, 1, arg);
  5784. se->expr = fold_convert (type, se->expr);
  5785. }
  5786. /* Generate code for SELECTED_REAL_KIND (P, R, RADIX) intrinsic function. */
  5787. static void
  5788. gfc_conv_intrinsic_sr_kind (gfc_se *se, gfc_expr *expr)
  5789. {
  5790. gfc_actual_arglist *actual;
  5791. tree type;
  5792. gfc_se argse;
  5793. vec<tree, va_gc> *args = NULL;
  5794. for (actual = expr->value.function.actual; actual; actual = actual->next)
  5795. {
  5796. gfc_init_se (&argse, se);
  5797. /* Pass a NULL pointer for an absent arg. */
  5798. if (actual->expr == NULL)
  5799. argse.expr = null_pointer_node;
  5800. else
  5801. {
  5802. gfc_typespec ts;
  5803. gfc_clear_ts (&ts);
  5804. if (actual->expr->ts.kind != gfc_c_int_kind)
  5805. {
  5806. /* The arguments to SELECTED_REAL_KIND are INTEGER(4). */
  5807. ts.type = BT_INTEGER;
  5808. ts.kind = gfc_c_int_kind;
  5809. gfc_convert_type (actual->expr, &ts, 2);
  5810. }
  5811. gfc_conv_expr_reference (&argse, actual->expr);
  5812. }
  5813. gfc_add_block_to_block (&se->pre, &argse.pre);
  5814. gfc_add_block_to_block (&se->post, &argse.post);
  5815. vec_safe_push (args, argse.expr);
  5816. }
  5817. /* Convert it to the required type. */
  5818. type = gfc_typenode_for_spec (&expr->ts);
  5819. se->expr = build_call_expr_loc_vec (input_location,
  5820. gfor_fndecl_sr_kind, args);
  5821. se->expr = fold_convert (type, se->expr);
  5822. }
  5823. /* Generate code for TRIM (A) intrinsic function. */
  5824. static void
  5825. gfc_conv_intrinsic_trim (gfc_se * se, gfc_expr * expr)
  5826. {
  5827. tree var;
  5828. tree len;
  5829. tree addr;
  5830. tree tmp;
  5831. tree cond;
  5832. tree fndecl;
  5833. tree function;
  5834. tree *args;
  5835. unsigned int num_args;
  5836. num_args = gfc_intrinsic_argument_list_length (expr) + 2;
  5837. args = XALLOCAVEC (tree, num_args);
  5838. var = gfc_create_var (gfc_get_pchar_type (expr->ts.kind), "pstr");
  5839. addr = gfc_build_addr_expr (ppvoid_type_node, var);
  5840. len = gfc_create_var (gfc_charlen_type_node, "len");
  5841. gfc_conv_intrinsic_function_args (se, expr, &args[2], num_args - 2);
  5842. args[0] = gfc_build_addr_expr (NULL_TREE, len);
  5843. args[1] = addr;
  5844. if (expr->ts.kind == 1)
  5845. function = gfor_fndecl_string_trim;
  5846. else if (expr->ts.kind == 4)
  5847. function = gfor_fndecl_string_trim_char4;
  5848. else
  5849. gcc_unreachable ();
  5850. fndecl = build_addr (function, current_function_decl);
  5851. tmp = build_call_array_loc (input_location,
  5852. TREE_TYPE (TREE_TYPE (function)), fndecl,
  5853. num_args, args);
  5854. gfc_add_expr_to_block (&se->pre, tmp);
  5855. /* Free the temporary afterwards, if necessary. */
  5856. cond = fold_build2_loc (input_location, GT_EXPR, boolean_type_node,
  5857. len, build_int_cst (TREE_TYPE (len), 0));
  5858. tmp = gfc_call_free (var);
  5859. tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
  5860. gfc_add_expr_to_block (&se->post, tmp);
  5861. se->expr = var;
  5862. se->string_length = len;
  5863. }
  5864. /* Generate code for REPEAT (STRING, NCOPIES) intrinsic function. */
  5865. static void
  5866. gfc_conv_intrinsic_repeat (gfc_se * se, gfc_expr * expr)
  5867. {
  5868. tree args[3], ncopies, dest, dlen, src, slen, ncopies_type;
  5869. tree type, cond, tmp, count, exit_label, n, max, largest;
  5870. tree size;
  5871. stmtblock_t block, body;
  5872. int i;
  5873. /* We store in charsize the size of a character. */
  5874. i = gfc_validate_kind (BT_CHARACTER, expr->ts.kind, false);
  5875. size = build_int_cst (size_type_node, gfc_character_kinds[i].bit_size / 8);
  5876. /* Get the arguments. */
  5877. gfc_conv_intrinsic_function_args (se, expr, args, 3);
  5878. slen = fold_convert (size_type_node, gfc_evaluate_now (args[0], &se->pre));
  5879. src = args[1];
  5880. ncopies = gfc_evaluate_now (args[2], &se->pre);
  5881. ncopies_type = TREE_TYPE (ncopies);
  5882. /* Check that NCOPIES is not negative. */
  5883. cond = fold_build2_loc (input_location, LT_EXPR, boolean_type_node, ncopies,
  5884. build_int_cst (ncopies_type, 0));
  5885. gfc_trans_runtime_check (true, false, cond, &se->pre, &expr->where,
  5886. "Argument NCOPIES of REPEAT intrinsic is negative "
  5887. "(its value is %ld)",
  5888. fold_convert (long_integer_type_node, ncopies));
  5889. /* If the source length is zero, any non negative value of NCOPIES
  5890. is valid, and nothing happens. */
  5891. n = gfc_create_var (ncopies_type, "ncopies");
  5892. cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, slen,
  5893. build_int_cst (size_type_node, 0));
  5894. tmp = fold_build3_loc (input_location, COND_EXPR, ncopies_type, cond,
  5895. build_int_cst (ncopies_type, 0), ncopies);
  5896. gfc_add_modify (&se->pre, n, tmp);
  5897. ncopies = n;
  5898. /* Check that ncopies is not too large: ncopies should be less than
  5899. (or equal to) MAX / slen, where MAX is the maximal integer of
  5900. the gfc_charlen_type_node type. If slen == 0, we need a special
  5901. case to avoid the division by zero. */
  5902. i = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
  5903. max = gfc_conv_mpz_to_tree (gfc_integer_kinds[i].huge, gfc_charlen_int_kind);
  5904. max = fold_build2_loc (input_location, TRUNC_DIV_EXPR, size_type_node,
  5905. fold_convert (size_type_node, max), slen);
  5906. largest = TYPE_PRECISION (size_type_node) > TYPE_PRECISION (ncopies_type)
  5907. ? size_type_node : ncopies_type;
  5908. cond = fold_build2_loc (input_location, GT_EXPR, boolean_type_node,
  5909. fold_convert (largest, ncopies),
  5910. fold_convert (largest, max));
  5911. tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, slen,
  5912. build_int_cst (size_type_node, 0));
  5913. cond = fold_build3_loc (input_location, COND_EXPR, boolean_type_node, tmp,
  5914. boolean_false_node, cond);
  5915. gfc_trans_runtime_check (true, false, cond, &se->pre, &expr->where,
  5916. "Argument NCOPIES of REPEAT intrinsic is too large");
  5917. /* Compute the destination length. */
  5918. dlen = fold_build2_loc (input_location, MULT_EXPR, gfc_charlen_type_node,
  5919. fold_convert (gfc_charlen_type_node, slen),
  5920. fold_convert (gfc_charlen_type_node, ncopies));
  5921. type = gfc_get_character_type (expr->ts.kind, expr->ts.u.cl);
  5922. dest = gfc_conv_string_tmp (se, build_pointer_type (type), dlen);
  5923. /* Generate the code to do the repeat operation:
  5924. for (i = 0; i < ncopies; i++)
  5925. memmove (dest + (i * slen * size), src, slen*size); */
  5926. gfc_start_block (&block);
  5927. count = gfc_create_var (ncopies_type, "count");
  5928. gfc_add_modify (&block, count, build_int_cst (ncopies_type, 0));
  5929. exit_label = gfc_build_label_decl (NULL_TREE);
  5930. /* Start the loop body. */
  5931. gfc_start_block (&body);
  5932. /* Exit the loop if count >= ncopies. */
  5933. cond = fold_build2_loc (input_location, GE_EXPR, boolean_type_node, count,
  5934. ncopies);
  5935. tmp = build1_v (GOTO_EXPR, exit_label);
  5936. TREE_USED (exit_label) = 1;
  5937. tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
  5938. build_empty_stmt (input_location));
  5939. gfc_add_expr_to_block (&body, tmp);
  5940. /* Call memmove (dest + (i*slen*size), src, slen*size). */
  5941. tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_charlen_type_node,
  5942. fold_convert (gfc_charlen_type_node, slen),
  5943. fold_convert (gfc_charlen_type_node, count));
  5944. tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_charlen_type_node,
  5945. tmp, fold_convert (gfc_charlen_type_node, size));
  5946. tmp = fold_build_pointer_plus_loc (input_location,
  5947. fold_convert (pvoid_type_node, dest), tmp);
  5948. tmp = build_call_expr_loc (input_location,
  5949. builtin_decl_explicit (BUILT_IN_MEMMOVE),
  5950. 3, tmp, src,
  5951. fold_build2_loc (input_location, MULT_EXPR,
  5952. size_type_node, slen,
  5953. fold_convert (size_type_node,
  5954. size)));
  5955. gfc_add_expr_to_block (&body, tmp);
  5956. /* Increment count. */
  5957. tmp = fold_build2_loc (input_location, PLUS_EXPR, ncopies_type,
  5958. count, build_int_cst (TREE_TYPE (count), 1));
  5959. gfc_add_modify (&body, count, tmp);
  5960. /* Build the loop. */
  5961. tmp = build1_v (LOOP_EXPR, gfc_finish_block (&body));
  5962. gfc_add_expr_to_block (&block, tmp);
  5963. /* Add the exit label. */
  5964. tmp = build1_v (LABEL_EXPR, exit_label);
  5965. gfc_add_expr_to_block (&block, tmp);
  5966. /* Finish the block. */
  5967. tmp = gfc_finish_block (&block);
  5968. gfc_add_expr_to_block (&se->pre, tmp);
  5969. /* Set the result value. */
  5970. se->expr = dest;
  5971. se->string_length = dlen;
  5972. }
  5973. /* Generate code for the IARGC intrinsic. */
  5974. static void
  5975. gfc_conv_intrinsic_iargc (gfc_se * se, gfc_expr * expr)
  5976. {
  5977. tree tmp;
  5978. tree fndecl;
  5979. tree type;
  5980. /* Call the library function. This always returns an INTEGER(4). */
  5981. fndecl = gfor_fndecl_iargc;
  5982. tmp = build_call_expr_loc (input_location,
  5983. fndecl, 0);
  5984. /* Convert it to the required type. */
  5985. type = gfc_typenode_for_spec (&expr->ts);
  5986. tmp = fold_convert (type, tmp);
  5987. se->expr = tmp;
  5988. }
  5989. /* The loc intrinsic returns the address of its argument as
  5990. gfc_index_integer_kind integer. */
  5991. static void
  5992. gfc_conv_intrinsic_loc (gfc_se * se, gfc_expr * expr)
  5993. {
  5994. tree temp_var;
  5995. gfc_expr *arg_expr;
  5996. gcc_assert (!se->ss);
  5997. arg_expr = expr->value.function.actual->expr;
  5998. if (arg_expr->rank == 0)
  5999. gfc_conv_expr_reference (se, arg_expr);
  6000. else
  6001. gfc_conv_array_parameter (se, arg_expr, true, NULL, NULL, NULL);
  6002. se->expr = convert (gfc_get_int_type (gfc_index_integer_kind), se->expr);
  6003. /* Create a temporary variable for loc return value. Without this,
  6004. we get an error an ICE in gcc/expr.c(expand_expr_addr_expr_1). */
  6005. temp_var = gfc_create_var (gfc_get_int_type (gfc_index_integer_kind), NULL);
  6006. gfc_add_modify (&se->pre, temp_var, se->expr);
  6007. se->expr = temp_var;
  6008. }
  6009. /* The following routine generates code for the intrinsic
  6010. functions from the ISO_C_BINDING module:
  6011. * C_LOC
  6012. * C_FUNLOC
  6013. * C_ASSOCIATED */
  6014. static void
  6015. conv_isocbinding_function (gfc_se *se, gfc_expr *expr)
  6016. {
  6017. gfc_actual_arglist *arg = expr->value.function.actual;
  6018. if (expr->value.function.isym->id == GFC_ISYM_C_LOC)
  6019. {
  6020. if (arg->expr->rank == 0)
  6021. gfc_conv_expr_reference (se, arg->expr);
  6022. else if (gfc_is_simply_contiguous (arg->expr, false))
  6023. gfc_conv_array_parameter (se, arg->expr, true, NULL, NULL, NULL);
  6024. else
  6025. {
  6026. gfc_conv_expr_descriptor (se, arg->expr);
  6027. se->expr = gfc_conv_descriptor_data_get (se->expr);
  6028. }
  6029. /* TODO -- the following two lines shouldn't be necessary, but if
  6030. they're removed, a bug is exposed later in the code path.
  6031. This workaround was thus introduced, but will have to be
  6032. removed; please see PR 35150 for details about the issue. */
  6033. se->expr = convert (pvoid_type_node, se->expr);
  6034. se->expr = gfc_evaluate_now (se->expr, &se->pre);
  6035. }
  6036. else if (expr->value.function.isym->id == GFC_ISYM_C_FUNLOC)
  6037. gfc_conv_expr_reference (se, arg->expr);
  6038. else if (expr->value.function.isym->id == GFC_ISYM_C_ASSOCIATED)
  6039. {
  6040. gfc_se arg1se;
  6041. gfc_se arg2se;
  6042. /* Build the addr_expr for the first argument. The argument is
  6043. already an *address* so we don't need to set want_pointer in
  6044. the gfc_se. */
  6045. gfc_init_se (&arg1se, NULL);
  6046. gfc_conv_expr (&arg1se, arg->expr);
  6047. gfc_add_block_to_block (&se->pre, &arg1se.pre);
  6048. gfc_add_block_to_block (&se->post, &arg1se.post);
  6049. /* See if we were given two arguments. */
  6050. if (arg->next->expr == NULL)
  6051. /* Only given one arg so generate a null and do a
  6052. not-equal comparison against the first arg. */
  6053. se->expr = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
  6054. arg1se.expr,
  6055. fold_convert (TREE_TYPE (arg1se.expr),
  6056. null_pointer_node));
  6057. else
  6058. {
  6059. tree eq_expr;
  6060. tree not_null_expr;
  6061. /* Given two arguments so build the arg2se from second arg. */
  6062. gfc_init_se (&arg2se, NULL);
  6063. gfc_conv_expr (&arg2se, arg->next->expr);
  6064. gfc_add_block_to_block (&se->pre, &arg2se.pre);
  6065. gfc_add_block_to_block (&se->post, &arg2se.post);
  6066. /* Generate test to compare that the two args are equal. */
  6067. eq_expr = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
  6068. arg1se.expr, arg2se.expr);
  6069. /* Generate test to ensure that the first arg is not null. */
  6070. not_null_expr = fold_build2_loc (input_location, NE_EXPR,
  6071. boolean_type_node,
  6072. arg1se.expr, null_pointer_node);
  6073. /* Finally, the generated test must check that both arg1 is not
  6074. NULL and that it is equal to the second arg. */
  6075. se->expr = fold_build2_loc (input_location, TRUTH_AND_EXPR,
  6076. boolean_type_node,
  6077. not_null_expr, eq_expr);
  6078. }
  6079. }
  6080. else
  6081. gcc_unreachable ();
  6082. }
  6083. /* The following routine generates code for the intrinsic
  6084. subroutines from the ISO_C_BINDING module:
  6085. * C_F_POINTER
  6086. * C_F_PROCPOINTER. */
  6087. static tree
  6088. conv_isocbinding_subroutine (gfc_code *code)
  6089. {
  6090. gfc_se se;
  6091. gfc_se cptrse;
  6092. gfc_se fptrse;
  6093. gfc_se shapese;
  6094. gfc_ss *shape_ss;
  6095. tree desc, dim, tmp, stride, offset;
  6096. stmtblock_t body, block;
  6097. gfc_loopinfo loop;
  6098. gfc_actual_arglist *arg = code->ext.actual;
  6099. gfc_init_se (&se, NULL);
  6100. gfc_init_se (&cptrse, NULL);
  6101. gfc_conv_expr (&cptrse, arg->expr);
  6102. gfc_add_block_to_block (&se.pre, &cptrse.pre);
  6103. gfc_add_block_to_block (&se.post, &cptrse.post);
  6104. gfc_init_se (&fptrse, NULL);
  6105. if (arg->next->expr->rank == 0)
  6106. {
  6107. fptrse.want_pointer = 1;
  6108. gfc_conv_expr (&fptrse, arg->next->expr);
  6109. gfc_add_block_to_block (&se.pre, &fptrse.pre);
  6110. gfc_add_block_to_block (&se.post, &fptrse.post);
  6111. if (arg->next->expr->symtree->n.sym->attr.proc_pointer
  6112. && arg->next->expr->symtree->n.sym->attr.dummy)
  6113. fptrse.expr = build_fold_indirect_ref_loc (input_location,
  6114. fptrse.expr);
  6115. se.expr = fold_build2_loc (input_location, MODIFY_EXPR,
  6116. TREE_TYPE (fptrse.expr),
  6117. fptrse.expr,
  6118. fold_convert (TREE_TYPE (fptrse.expr),
  6119. cptrse.expr));
  6120. gfc_add_expr_to_block (&se.pre, se.expr);
  6121. gfc_add_block_to_block (&se.pre, &se.post);
  6122. return gfc_finish_block (&se.pre);
  6123. }
  6124. gfc_start_block (&block);
  6125. /* Get the descriptor of the Fortran pointer. */
  6126. fptrse.descriptor_only = 1;
  6127. gfc_conv_expr_descriptor (&fptrse, arg->next->expr);
  6128. gfc_add_block_to_block (&block, &fptrse.pre);
  6129. desc = fptrse.expr;
  6130. /* Set data value, dtype, and offset. */
  6131. tmp = GFC_TYPE_ARRAY_DATAPTR_TYPE (TREE_TYPE (desc));
  6132. gfc_conv_descriptor_data_set (&block, desc, fold_convert (tmp, cptrse.expr));
  6133. gfc_add_modify (&block, gfc_conv_descriptor_dtype (desc),
  6134. gfc_get_dtype (TREE_TYPE (desc)));
  6135. /* Start scalarization of the bounds, using the shape argument. */
  6136. shape_ss = gfc_walk_expr (arg->next->next->expr);
  6137. gcc_assert (shape_ss != gfc_ss_terminator);
  6138. gfc_init_se (&shapese, NULL);
  6139. gfc_init_loopinfo (&loop);
  6140. gfc_add_ss_to_loop (&loop, shape_ss);
  6141. gfc_conv_ss_startstride (&loop);
  6142. gfc_conv_loop_setup (&loop, &arg->next->expr->where);
  6143. gfc_mark_ss_chain_used (shape_ss, 1);
  6144. gfc_copy_loopinfo_to_se (&shapese, &loop);
  6145. shapese.ss = shape_ss;
  6146. stride = gfc_create_var (gfc_array_index_type, "stride");
  6147. offset = gfc_create_var (gfc_array_index_type, "offset");
  6148. gfc_add_modify (&block, stride, gfc_index_one_node);
  6149. gfc_add_modify (&block, offset, gfc_index_zero_node);
  6150. /* Loop body. */
  6151. gfc_start_scalarized_body (&loop, &body);
  6152. dim = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
  6153. loop.loopvar[0], loop.from[0]);
  6154. /* Set bounds and stride. */
  6155. gfc_conv_descriptor_lbound_set (&body, desc, dim, gfc_index_one_node);
  6156. gfc_conv_descriptor_stride_set (&body, desc, dim, stride);
  6157. gfc_conv_expr (&shapese, arg->next->next->expr);
  6158. gfc_add_block_to_block (&body, &shapese.pre);
  6159. gfc_conv_descriptor_ubound_set (&body, desc, dim, shapese.expr);
  6160. gfc_add_block_to_block (&body, &shapese.post);
  6161. /* Calculate offset. */
  6162. gfc_add_modify (&body, offset,
  6163. fold_build2_loc (input_location, PLUS_EXPR,
  6164. gfc_array_index_type, offset, stride));
  6165. /* Update stride. */
  6166. gfc_add_modify (&body, stride,
  6167. fold_build2_loc (input_location, MULT_EXPR,
  6168. gfc_array_index_type, stride,
  6169. fold_convert (gfc_array_index_type,
  6170. shapese.expr)));
  6171. /* Finish scalarization loop. */
  6172. gfc_trans_scalarizing_loops (&loop, &body);
  6173. gfc_add_block_to_block (&block, &loop.pre);
  6174. gfc_add_block_to_block (&block, &loop.post);
  6175. gfc_add_block_to_block (&block, &fptrse.post);
  6176. gfc_cleanup_loop (&loop);
  6177. gfc_add_modify (&block, offset,
  6178. fold_build1_loc (input_location, NEGATE_EXPR,
  6179. gfc_array_index_type, offset));
  6180. gfc_conv_descriptor_offset_set (&block, desc, offset);
  6181. gfc_add_expr_to_block (&se.pre, gfc_finish_block (&block));
  6182. gfc_add_block_to_block (&se.pre, &se.post);
  6183. return gfc_finish_block (&se.pre);
  6184. }
  6185. /* Save and restore floating-point state. */
  6186. tree
  6187. gfc_save_fp_state (stmtblock_t *block)
  6188. {
  6189. tree type, fpstate, tmp;
  6190. type = build_array_type (char_type_node,
  6191. build_range_type (size_type_node, size_zero_node,
  6192. size_int (GFC_FPE_STATE_BUFFER_SIZE)));
  6193. fpstate = gfc_create_var (type, "fpstate");
  6194. fpstate = gfc_build_addr_expr (pvoid_type_node, fpstate);
  6195. tmp = build_call_expr_loc (input_location, gfor_fndecl_ieee_procedure_entry,
  6196. 1, fpstate);
  6197. gfc_add_expr_to_block (block, tmp);
  6198. return fpstate;
  6199. }
  6200. void
  6201. gfc_restore_fp_state (stmtblock_t *block, tree fpstate)
  6202. {
  6203. tree tmp;
  6204. tmp = build_call_expr_loc (input_location, gfor_fndecl_ieee_procedure_exit,
  6205. 1, fpstate);
  6206. gfc_add_expr_to_block (block, tmp);
  6207. }
  6208. /* Generate code for arguments of IEEE functions. */
  6209. static void
  6210. conv_ieee_function_args (gfc_se *se, gfc_expr *expr, tree *argarray,
  6211. int nargs)
  6212. {
  6213. gfc_actual_arglist *actual;
  6214. gfc_expr *e;
  6215. gfc_se argse;
  6216. int arg;
  6217. actual = expr->value.function.actual;
  6218. for (arg = 0; arg < nargs; arg++, actual = actual->next)
  6219. {
  6220. gcc_assert (actual);
  6221. e = actual->expr;
  6222. gfc_init_se (&argse, se);
  6223. gfc_conv_expr_val (&argse, e);
  6224. gfc_add_block_to_block (&se->pre, &argse.pre);
  6225. gfc_add_block_to_block (&se->post, &argse.post);
  6226. argarray[arg] = argse.expr;
  6227. }
  6228. }
  6229. /* Generate code for intrinsics IEEE_IS_NAN, IEEE_IS_FINITE,
  6230. and IEEE_UNORDERED, which translate directly to GCC type-generic
  6231. built-ins. */
  6232. static void
  6233. conv_intrinsic_ieee_builtin (gfc_se * se, gfc_expr * expr,
  6234. enum built_in_function code, int nargs)
  6235. {
  6236. tree args[2];
  6237. gcc_assert ((unsigned) nargs <= sizeof(args)/sizeof(args[0]));
  6238. conv_ieee_function_args (se, expr, args, nargs);
  6239. se->expr = build_call_expr_loc_array (input_location,
  6240. builtin_decl_explicit (code),
  6241. nargs, args);
  6242. STRIP_TYPE_NOPS (se->expr);
  6243. se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), se->expr);
  6244. }
  6245. /* Generate code for IEEE_IS_NORMAL intrinsic:
  6246. IEEE_IS_NORMAL(x) --> (__builtin_isnormal(x) || x == 0) */
  6247. static void
  6248. conv_intrinsic_ieee_is_normal (gfc_se * se, gfc_expr * expr)
  6249. {
  6250. tree arg, isnormal, iszero;
  6251. /* Convert arg, evaluate it only once. */
  6252. conv_ieee_function_args (se, expr, &arg, 1);
  6253. arg = gfc_evaluate_now (arg, &se->pre);
  6254. isnormal = build_call_expr_loc (input_location,
  6255. builtin_decl_explicit (BUILT_IN_ISNORMAL),
  6256. 1, arg);
  6257. iszero = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, arg,
  6258. build_real_from_int_cst (TREE_TYPE (arg),
  6259. integer_zero_node));
  6260. se->expr = fold_build2_loc (input_location, TRUTH_OR_EXPR,
  6261. boolean_type_node, isnormal, iszero);
  6262. se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), se->expr);
  6263. }
  6264. /* Generate code for IEEE_IS_NEGATIVE intrinsic:
  6265. IEEE_IS_NEGATIVE(x) --> (__builtin_signbit(x) && !__builtin_isnan(x)) */
  6266. static void
  6267. conv_intrinsic_ieee_is_negative (gfc_se * se, gfc_expr * expr)
  6268. {
  6269. tree arg, signbit, isnan, decl;
  6270. int argprec;
  6271. /* Convert arg, evaluate it only once. */
  6272. conv_ieee_function_args (se, expr, &arg, 1);
  6273. arg = gfc_evaluate_now (arg, &se->pre);
  6274. isnan = build_call_expr_loc (input_location,
  6275. builtin_decl_explicit (BUILT_IN_ISNAN),
  6276. 1, arg);
  6277. STRIP_TYPE_NOPS (isnan);
  6278. argprec = TYPE_PRECISION (TREE_TYPE (arg));
  6279. decl = builtin_decl_for_precision (BUILT_IN_SIGNBIT, argprec);
  6280. signbit = build_call_expr_loc (input_location, decl, 1, arg);
  6281. signbit = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
  6282. signbit, integer_zero_node);
  6283. se->expr = fold_build2_loc (input_location, TRUTH_AND_EXPR,
  6284. boolean_type_node, signbit,
  6285. fold_build1_loc (input_location, TRUTH_NOT_EXPR,
  6286. TREE_TYPE(isnan), isnan));
  6287. se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), se->expr);
  6288. }
  6289. /* Generate code for IEEE_LOGB and IEEE_RINT. */
  6290. static void
  6291. conv_intrinsic_ieee_logb_rint (gfc_se * se, gfc_expr * expr,
  6292. enum built_in_function code)
  6293. {
  6294. tree arg, decl, call, fpstate;
  6295. int argprec;
  6296. conv_ieee_function_args (se, expr, &arg, 1);
  6297. argprec = TYPE_PRECISION (TREE_TYPE (arg));
  6298. decl = builtin_decl_for_precision (code, argprec);
  6299. /* Save floating-point state. */
  6300. fpstate = gfc_save_fp_state (&se->pre);
  6301. /* Make the function call. */
  6302. call = build_call_expr_loc (input_location, decl, 1, arg);
  6303. se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), call);
  6304. /* Restore floating-point state. */
  6305. gfc_restore_fp_state (&se->post, fpstate);
  6306. }
  6307. /* Generate code for IEEE_REM. */
  6308. static void
  6309. conv_intrinsic_ieee_rem (gfc_se * se, gfc_expr * expr)
  6310. {
  6311. tree args[2], decl, call, fpstate;
  6312. int argprec;
  6313. conv_ieee_function_args (se, expr, args, 2);
  6314. /* If arguments have unequal size, convert them to the larger. */
  6315. if (TYPE_PRECISION (TREE_TYPE (args[0]))
  6316. > TYPE_PRECISION (TREE_TYPE (args[1])))
  6317. args[1] = fold_convert (TREE_TYPE (args[0]), args[1]);
  6318. else if (TYPE_PRECISION (TREE_TYPE (args[1]))
  6319. > TYPE_PRECISION (TREE_TYPE (args[0])))
  6320. args[0] = fold_convert (TREE_TYPE (args[1]), args[0]);
  6321. argprec = TYPE_PRECISION (TREE_TYPE (args[0]));
  6322. decl = builtin_decl_for_precision (BUILT_IN_REMAINDER, argprec);
  6323. /* Save floating-point state. */
  6324. fpstate = gfc_save_fp_state (&se->pre);
  6325. /* Make the function call. */
  6326. call = build_call_expr_loc_array (input_location, decl, 2, args);
  6327. se->expr = fold_convert (TREE_TYPE (args[0]), call);
  6328. /* Restore floating-point state. */
  6329. gfc_restore_fp_state (&se->post, fpstate);
  6330. }
  6331. /* Generate code for IEEE_NEXT_AFTER. */
  6332. static void
  6333. conv_intrinsic_ieee_next_after (gfc_se * se, gfc_expr * expr)
  6334. {
  6335. tree args[2], decl, call, fpstate;
  6336. int argprec;
  6337. conv_ieee_function_args (se, expr, args, 2);
  6338. /* Result has the characteristics of first argument. */
  6339. args[1] = fold_convert (TREE_TYPE (args[0]), args[1]);
  6340. argprec = TYPE_PRECISION (TREE_TYPE (args[0]));
  6341. decl = builtin_decl_for_precision (BUILT_IN_NEXTAFTER, argprec);
  6342. /* Save floating-point state. */
  6343. fpstate = gfc_save_fp_state (&se->pre);
  6344. /* Make the function call. */
  6345. call = build_call_expr_loc_array (input_location, decl, 2, args);
  6346. se->expr = fold_convert (TREE_TYPE (args[0]), call);
  6347. /* Restore floating-point state. */
  6348. gfc_restore_fp_state (&se->post, fpstate);
  6349. }
  6350. /* Generate code for IEEE_SCALB. */
  6351. static void
  6352. conv_intrinsic_ieee_scalb (gfc_se * se, gfc_expr * expr)
  6353. {
  6354. tree args[2], decl, call, huge, type;
  6355. int argprec, n;
  6356. conv_ieee_function_args (se, expr, args, 2);
  6357. /* Result has the characteristics of first argument. */
  6358. argprec = TYPE_PRECISION (TREE_TYPE (args[0]));
  6359. decl = builtin_decl_for_precision (BUILT_IN_SCALBN, argprec);
  6360. if (TYPE_PRECISION (TREE_TYPE (args[1])) > TYPE_PRECISION (integer_type_node))
  6361. {
  6362. /* We need to fold the integer into the range of a C int. */
  6363. args[1] = gfc_evaluate_now (args[1], &se->pre);
  6364. type = TREE_TYPE (args[1]);
  6365. n = gfc_validate_kind (BT_INTEGER, gfc_c_int_kind, false);
  6366. huge = gfc_conv_mpz_to_tree (gfc_integer_kinds[n].huge,
  6367. gfc_c_int_kind);
  6368. huge = fold_convert (type, huge);
  6369. args[1] = fold_build2_loc (input_location, MIN_EXPR, type, args[1],
  6370. huge);
  6371. args[1] = fold_build2_loc (input_location, MAX_EXPR, type, args[1],
  6372. fold_build1_loc (input_location, NEGATE_EXPR,
  6373. type, huge));
  6374. }
  6375. args[1] = fold_convert (integer_type_node, args[1]);
  6376. /* Make the function call. */
  6377. call = build_call_expr_loc_array (input_location, decl, 2, args);
  6378. se->expr = fold_convert (TREE_TYPE (args[0]), call);
  6379. }
  6380. /* Generate code for IEEE_COPY_SIGN. */
  6381. static void
  6382. conv_intrinsic_ieee_copy_sign (gfc_se * se, gfc_expr * expr)
  6383. {
  6384. tree args[2], decl, sign;
  6385. int argprec;
  6386. conv_ieee_function_args (se, expr, args, 2);
  6387. /* Get the sign of the second argument. */
  6388. argprec = TYPE_PRECISION (TREE_TYPE (args[1]));
  6389. decl = builtin_decl_for_precision (BUILT_IN_SIGNBIT, argprec);
  6390. sign = build_call_expr_loc (input_location, decl, 1, args[1]);
  6391. sign = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
  6392. sign, integer_zero_node);
  6393. /* Create a value of one, with the right sign. */
  6394. sign = fold_build3_loc (input_location, COND_EXPR, integer_type_node,
  6395. sign,
  6396. fold_build1_loc (input_location, NEGATE_EXPR,
  6397. integer_type_node,
  6398. integer_one_node),
  6399. integer_one_node);
  6400. args[1] = fold_convert (TREE_TYPE (args[0]), sign);
  6401. argprec = TYPE_PRECISION (TREE_TYPE (args[0]));
  6402. decl = builtin_decl_for_precision (BUILT_IN_COPYSIGN, argprec);
  6403. se->expr = build_call_expr_loc_array (input_location, decl, 2, args);
  6404. }
  6405. /* Generate code for an intrinsic function from the IEEE_ARITHMETIC
  6406. module. */
  6407. bool
  6408. gfc_conv_ieee_arithmetic_function (gfc_se * se, gfc_expr * expr)
  6409. {
  6410. const char *name = expr->value.function.name;
  6411. #define STARTS_WITH(A,B) (strncmp((A), (B), strlen(B)) == 0)
  6412. if (STARTS_WITH (name, "_gfortran_ieee_is_nan"))
  6413. conv_intrinsic_ieee_builtin (se, expr, BUILT_IN_ISNAN, 1);
  6414. else if (STARTS_WITH (name, "_gfortran_ieee_is_finite"))
  6415. conv_intrinsic_ieee_builtin (se, expr, BUILT_IN_ISFINITE, 1);
  6416. else if (STARTS_WITH (name, "_gfortran_ieee_unordered"))
  6417. conv_intrinsic_ieee_builtin (se, expr, BUILT_IN_ISUNORDERED, 2);
  6418. else if (STARTS_WITH (name, "_gfortran_ieee_is_normal"))
  6419. conv_intrinsic_ieee_is_normal (se, expr);
  6420. else if (STARTS_WITH (name, "_gfortran_ieee_is_negative"))
  6421. conv_intrinsic_ieee_is_negative (se, expr);
  6422. else if (STARTS_WITH (name, "_gfortran_ieee_copy_sign"))
  6423. conv_intrinsic_ieee_copy_sign (se, expr);
  6424. else if (STARTS_WITH (name, "_gfortran_ieee_scalb"))
  6425. conv_intrinsic_ieee_scalb (se, expr);
  6426. else if (STARTS_WITH (name, "_gfortran_ieee_next_after"))
  6427. conv_intrinsic_ieee_next_after (se, expr);
  6428. else if (STARTS_WITH (name, "_gfortran_ieee_rem"))
  6429. conv_intrinsic_ieee_rem (se, expr);
  6430. else if (STARTS_WITH (name, "_gfortran_ieee_logb"))
  6431. conv_intrinsic_ieee_logb_rint (se, expr, BUILT_IN_LOGB);
  6432. else if (STARTS_WITH (name, "_gfortran_ieee_rint"))
  6433. conv_intrinsic_ieee_logb_rint (se, expr, BUILT_IN_RINT);
  6434. else
  6435. /* It is not among the functions we translate directly. We return
  6436. false, so a library function call is emitted. */
  6437. return false;
  6438. #undef STARTS_WITH
  6439. return true;
  6440. }
  6441. /* Generate code for an intrinsic function. Some map directly to library
  6442. calls, others get special handling. In some cases the name of the function
  6443. used depends on the type specifiers. */
  6444. void
  6445. gfc_conv_intrinsic_function (gfc_se * se, gfc_expr * expr)
  6446. {
  6447. const char *name;
  6448. int lib, kind;
  6449. tree fndecl;
  6450. name = &expr->value.function.name[2];
  6451. if (expr->rank > 0)
  6452. {
  6453. lib = gfc_is_intrinsic_libcall (expr);
  6454. if (lib != 0)
  6455. {
  6456. if (lib == 1)
  6457. se->ignore_optional = 1;
  6458. switch (expr->value.function.isym->id)
  6459. {
  6460. case GFC_ISYM_EOSHIFT:
  6461. case GFC_ISYM_PACK:
  6462. case GFC_ISYM_RESHAPE:
  6463. /* For all of those the first argument specifies the type and the
  6464. third is optional. */
  6465. conv_generic_with_optional_char_arg (se, expr, 1, 3);
  6466. break;
  6467. default:
  6468. gfc_conv_intrinsic_funcall (se, expr);
  6469. break;
  6470. }
  6471. return;
  6472. }
  6473. }
  6474. switch (expr->value.function.isym->id)
  6475. {
  6476. case GFC_ISYM_NONE:
  6477. gcc_unreachable ();
  6478. case GFC_ISYM_REPEAT:
  6479. gfc_conv_intrinsic_repeat (se, expr);
  6480. break;
  6481. case GFC_ISYM_TRIM:
  6482. gfc_conv_intrinsic_trim (se, expr);
  6483. break;
  6484. case GFC_ISYM_SC_KIND:
  6485. gfc_conv_intrinsic_sc_kind (se, expr);
  6486. break;
  6487. case GFC_ISYM_SI_KIND:
  6488. gfc_conv_intrinsic_si_kind (se, expr);
  6489. break;
  6490. case GFC_ISYM_SR_KIND:
  6491. gfc_conv_intrinsic_sr_kind (se, expr);
  6492. break;
  6493. case GFC_ISYM_EXPONENT:
  6494. gfc_conv_intrinsic_exponent (se, expr);
  6495. break;
  6496. case GFC_ISYM_SCAN:
  6497. kind = expr->value.function.actual->expr->ts.kind;
  6498. if (kind == 1)
  6499. fndecl = gfor_fndecl_string_scan;
  6500. else if (kind == 4)
  6501. fndecl = gfor_fndecl_string_scan_char4;
  6502. else
  6503. gcc_unreachable ();
  6504. gfc_conv_intrinsic_index_scan_verify (se, expr, fndecl);
  6505. break;
  6506. case GFC_ISYM_VERIFY:
  6507. kind = expr->value.function.actual->expr->ts.kind;
  6508. if (kind == 1)
  6509. fndecl = gfor_fndecl_string_verify;
  6510. else if (kind == 4)
  6511. fndecl = gfor_fndecl_string_verify_char4;
  6512. else
  6513. gcc_unreachable ();
  6514. gfc_conv_intrinsic_index_scan_verify (se, expr, fndecl);
  6515. break;
  6516. case GFC_ISYM_ALLOCATED:
  6517. gfc_conv_allocated (se, expr);
  6518. break;
  6519. case GFC_ISYM_ASSOCIATED:
  6520. gfc_conv_associated(se, expr);
  6521. break;
  6522. case GFC_ISYM_SAME_TYPE_AS:
  6523. gfc_conv_same_type_as (se, expr);
  6524. break;
  6525. case GFC_ISYM_ABS:
  6526. gfc_conv_intrinsic_abs (se, expr);
  6527. break;
  6528. case GFC_ISYM_ADJUSTL:
  6529. if (expr->ts.kind == 1)
  6530. fndecl = gfor_fndecl_adjustl;
  6531. else if (expr->ts.kind == 4)
  6532. fndecl = gfor_fndecl_adjustl_char4;
  6533. else
  6534. gcc_unreachable ();
  6535. gfc_conv_intrinsic_adjust (se, expr, fndecl);
  6536. break;
  6537. case GFC_ISYM_ADJUSTR:
  6538. if (expr->ts.kind == 1)
  6539. fndecl = gfor_fndecl_adjustr;
  6540. else if (expr->ts.kind == 4)
  6541. fndecl = gfor_fndecl_adjustr_char4;
  6542. else
  6543. gcc_unreachable ();
  6544. gfc_conv_intrinsic_adjust (se, expr, fndecl);
  6545. break;
  6546. case GFC_ISYM_AIMAG:
  6547. gfc_conv_intrinsic_imagpart (se, expr);
  6548. break;
  6549. case GFC_ISYM_AINT:
  6550. gfc_conv_intrinsic_aint (se, expr, RND_TRUNC);
  6551. break;
  6552. case GFC_ISYM_ALL:
  6553. gfc_conv_intrinsic_anyall (se, expr, EQ_EXPR);
  6554. break;
  6555. case GFC_ISYM_ANINT:
  6556. gfc_conv_intrinsic_aint (se, expr, RND_ROUND);
  6557. break;
  6558. case GFC_ISYM_AND:
  6559. gfc_conv_intrinsic_bitop (se, expr, BIT_AND_EXPR);
  6560. break;
  6561. case GFC_ISYM_ANY:
  6562. gfc_conv_intrinsic_anyall (se, expr, NE_EXPR);
  6563. break;
  6564. case GFC_ISYM_BTEST:
  6565. gfc_conv_intrinsic_btest (se, expr);
  6566. break;
  6567. case GFC_ISYM_BGE:
  6568. gfc_conv_intrinsic_bitcomp (se, expr, GE_EXPR);
  6569. break;
  6570. case GFC_ISYM_BGT:
  6571. gfc_conv_intrinsic_bitcomp (se, expr, GT_EXPR);
  6572. break;
  6573. case GFC_ISYM_BLE:
  6574. gfc_conv_intrinsic_bitcomp (se, expr, LE_EXPR);
  6575. break;
  6576. case GFC_ISYM_BLT:
  6577. gfc_conv_intrinsic_bitcomp (se, expr, LT_EXPR);
  6578. break;
  6579. case GFC_ISYM_C_ASSOCIATED:
  6580. case GFC_ISYM_C_FUNLOC:
  6581. case GFC_ISYM_C_LOC:
  6582. conv_isocbinding_function (se, expr);
  6583. break;
  6584. case GFC_ISYM_ACHAR:
  6585. case GFC_ISYM_CHAR:
  6586. gfc_conv_intrinsic_char (se, expr);
  6587. break;
  6588. case GFC_ISYM_CONVERSION:
  6589. case GFC_ISYM_REAL:
  6590. case GFC_ISYM_LOGICAL:
  6591. case GFC_ISYM_DBLE:
  6592. gfc_conv_intrinsic_conversion (se, expr);
  6593. break;
  6594. /* Integer conversions are handled separately to make sure we get the
  6595. correct rounding mode. */
  6596. case GFC_ISYM_INT:
  6597. case GFC_ISYM_INT2:
  6598. case GFC_ISYM_INT8:
  6599. case GFC_ISYM_LONG:
  6600. gfc_conv_intrinsic_int (se, expr, RND_TRUNC);
  6601. break;
  6602. case GFC_ISYM_NINT:
  6603. gfc_conv_intrinsic_int (se, expr, RND_ROUND);
  6604. break;
  6605. case GFC_ISYM_CEILING:
  6606. gfc_conv_intrinsic_int (se, expr, RND_CEIL);
  6607. break;
  6608. case GFC_ISYM_FLOOR:
  6609. gfc_conv_intrinsic_int (se, expr, RND_FLOOR);
  6610. break;
  6611. case GFC_ISYM_MOD:
  6612. gfc_conv_intrinsic_mod (se, expr, 0);
  6613. break;
  6614. case GFC_ISYM_MODULO:
  6615. gfc_conv_intrinsic_mod (se, expr, 1);
  6616. break;
  6617. case GFC_ISYM_CAF_GET:
  6618. gfc_conv_intrinsic_caf_get (se, expr, NULL_TREE, NULL_TREE, NULL_TREE);
  6619. break;
  6620. case GFC_ISYM_CMPLX:
  6621. gfc_conv_intrinsic_cmplx (se, expr, name[5] == '1');
  6622. break;
  6623. case GFC_ISYM_COMMAND_ARGUMENT_COUNT:
  6624. gfc_conv_intrinsic_iargc (se, expr);
  6625. break;
  6626. case GFC_ISYM_COMPLEX:
  6627. gfc_conv_intrinsic_cmplx (se, expr, 1);
  6628. break;
  6629. case GFC_ISYM_CONJG:
  6630. gfc_conv_intrinsic_conjg (se, expr);
  6631. break;
  6632. case GFC_ISYM_COUNT:
  6633. gfc_conv_intrinsic_count (se, expr);
  6634. break;
  6635. case GFC_ISYM_CTIME:
  6636. gfc_conv_intrinsic_ctime (se, expr);
  6637. break;
  6638. case GFC_ISYM_DIM:
  6639. gfc_conv_intrinsic_dim (se, expr);
  6640. break;
  6641. case GFC_ISYM_DOT_PRODUCT:
  6642. gfc_conv_intrinsic_dot_product (se, expr);
  6643. break;
  6644. case GFC_ISYM_DPROD:
  6645. gfc_conv_intrinsic_dprod (se, expr);
  6646. break;
  6647. case GFC_ISYM_DSHIFTL:
  6648. gfc_conv_intrinsic_dshift (se, expr, true);
  6649. break;
  6650. case GFC_ISYM_DSHIFTR:
  6651. gfc_conv_intrinsic_dshift (se, expr, false);
  6652. break;
  6653. case GFC_ISYM_FDATE:
  6654. gfc_conv_intrinsic_fdate (se, expr);
  6655. break;
  6656. case GFC_ISYM_FRACTION:
  6657. gfc_conv_intrinsic_fraction (se, expr);
  6658. break;
  6659. case GFC_ISYM_IALL:
  6660. gfc_conv_intrinsic_arith (se, expr, BIT_AND_EXPR, false);
  6661. break;
  6662. case GFC_ISYM_IAND:
  6663. gfc_conv_intrinsic_bitop (se, expr, BIT_AND_EXPR);
  6664. break;
  6665. case GFC_ISYM_IANY:
  6666. gfc_conv_intrinsic_arith (se, expr, BIT_IOR_EXPR, false);
  6667. break;
  6668. case GFC_ISYM_IBCLR:
  6669. gfc_conv_intrinsic_singlebitop (se, expr, 0);
  6670. break;
  6671. case GFC_ISYM_IBITS:
  6672. gfc_conv_intrinsic_ibits (se, expr);
  6673. break;
  6674. case GFC_ISYM_IBSET:
  6675. gfc_conv_intrinsic_singlebitop (se, expr, 1);
  6676. break;
  6677. case GFC_ISYM_IACHAR:
  6678. case GFC_ISYM_ICHAR:
  6679. /* We assume ASCII character sequence. */
  6680. gfc_conv_intrinsic_ichar (se, expr);
  6681. break;
  6682. case GFC_ISYM_IARGC:
  6683. gfc_conv_intrinsic_iargc (se, expr);
  6684. break;
  6685. case GFC_ISYM_IEOR:
  6686. gfc_conv_intrinsic_bitop (se, expr, BIT_XOR_EXPR);
  6687. break;
  6688. case GFC_ISYM_INDEX:
  6689. kind = expr->value.function.actual->expr->ts.kind;
  6690. if (kind == 1)
  6691. fndecl = gfor_fndecl_string_index;
  6692. else if (kind == 4)
  6693. fndecl = gfor_fndecl_string_index_char4;
  6694. else
  6695. gcc_unreachable ();
  6696. gfc_conv_intrinsic_index_scan_verify (se, expr, fndecl);
  6697. break;
  6698. case GFC_ISYM_IOR:
  6699. gfc_conv_intrinsic_bitop (se, expr, BIT_IOR_EXPR);
  6700. break;
  6701. case GFC_ISYM_IPARITY:
  6702. gfc_conv_intrinsic_arith (se, expr, BIT_XOR_EXPR, false);
  6703. break;
  6704. case GFC_ISYM_IS_IOSTAT_END:
  6705. gfc_conv_has_intvalue (se, expr, LIBERROR_END);
  6706. break;
  6707. case GFC_ISYM_IS_IOSTAT_EOR:
  6708. gfc_conv_has_intvalue (se, expr, LIBERROR_EOR);
  6709. break;
  6710. case GFC_ISYM_ISNAN:
  6711. gfc_conv_intrinsic_isnan (se, expr);
  6712. break;
  6713. case GFC_ISYM_LSHIFT:
  6714. gfc_conv_intrinsic_shift (se, expr, false, false);
  6715. break;
  6716. case GFC_ISYM_RSHIFT:
  6717. gfc_conv_intrinsic_shift (se, expr, true, true);
  6718. break;
  6719. case GFC_ISYM_SHIFTA:
  6720. gfc_conv_intrinsic_shift (se, expr, true, true);
  6721. break;
  6722. case GFC_ISYM_SHIFTL:
  6723. gfc_conv_intrinsic_shift (se, expr, false, false);
  6724. break;
  6725. case GFC_ISYM_SHIFTR:
  6726. gfc_conv_intrinsic_shift (se, expr, true, false);
  6727. break;
  6728. case GFC_ISYM_ISHFT:
  6729. gfc_conv_intrinsic_ishft (se, expr);
  6730. break;
  6731. case GFC_ISYM_ISHFTC:
  6732. gfc_conv_intrinsic_ishftc (se, expr);
  6733. break;
  6734. case GFC_ISYM_LEADZ:
  6735. gfc_conv_intrinsic_leadz (se, expr);
  6736. break;
  6737. case GFC_ISYM_TRAILZ:
  6738. gfc_conv_intrinsic_trailz (se, expr);
  6739. break;
  6740. case GFC_ISYM_POPCNT:
  6741. gfc_conv_intrinsic_popcnt_poppar (se, expr, 0);
  6742. break;
  6743. case GFC_ISYM_POPPAR:
  6744. gfc_conv_intrinsic_popcnt_poppar (se, expr, 1);
  6745. break;
  6746. case GFC_ISYM_LBOUND:
  6747. gfc_conv_intrinsic_bound (se, expr, 0);
  6748. break;
  6749. case GFC_ISYM_LCOBOUND:
  6750. conv_intrinsic_cobound (se, expr);
  6751. break;
  6752. case GFC_ISYM_TRANSPOSE:
  6753. /* The scalarizer has already been set up for reversed dimension access
  6754. order ; now we just get the argument value normally. */
  6755. gfc_conv_expr (se, expr->value.function.actual->expr);
  6756. break;
  6757. case GFC_ISYM_LEN:
  6758. gfc_conv_intrinsic_len (se, expr);
  6759. break;
  6760. case GFC_ISYM_LEN_TRIM:
  6761. gfc_conv_intrinsic_len_trim (se, expr);
  6762. break;
  6763. case GFC_ISYM_LGE:
  6764. gfc_conv_intrinsic_strcmp (se, expr, GE_EXPR);
  6765. break;
  6766. case GFC_ISYM_LGT:
  6767. gfc_conv_intrinsic_strcmp (se, expr, GT_EXPR);
  6768. break;
  6769. case GFC_ISYM_LLE:
  6770. gfc_conv_intrinsic_strcmp (se, expr, LE_EXPR);
  6771. break;
  6772. case GFC_ISYM_LLT:
  6773. gfc_conv_intrinsic_strcmp (se, expr, LT_EXPR);
  6774. break;
  6775. case GFC_ISYM_MASKL:
  6776. gfc_conv_intrinsic_mask (se, expr, 1);
  6777. break;
  6778. case GFC_ISYM_MASKR:
  6779. gfc_conv_intrinsic_mask (se, expr, 0);
  6780. break;
  6781. case GFC_ISYM_MAX:
  6782. if (expr->ts.type == BT_CHARACTER)
  6783. gfc_conv_intrinsic_minmax_char (se, expr, 1);
  6784. else
  6785. gfc_conv_intrinsic_minmax (se, expr, GT_EXPR);
  6786. break;
  6787. case GFC_ISYM_MAXLOC:
  6788. gfc_conv_intrinsic_minmaxloc (se, expr, GT_EXPR);
  6789. break;
  6790. case GFC_ISYM_MAXVAL:
  6791. gfc_conv_intrinsic_minmaxval (se, expr, GT_EXPR);
  6792. break;
  6793. case GFC_ISYM_MERGE:
  6794. gfc_conv_intrinsic_merge (se, expr);
  6795. break;
  6796. case GFC_ISYM_MERGE_BITS:
  6797. gfc_conv_intrinsic_merge_bits (se, expr);
  6798. break;
  6799. case GFC_ISYM_MIN:
  6800. if (expr->ts.type == BT_CHARACTER)
  6801. gfc_conv_intrinsic_minmax_char (se, expr, -1);
  6802. else
  6803. gfc_conv_intrinsic_minmax (se, expr, LT_EXPR);
  6804. break;
  6805. case GFC_ISYM_MINLOC:
  6806. gfc_conv_intrinsic_minmaxloc (se, expr, LT_EXPR);
  6807. break;
  6808. case GFC_ISYM_MINVAL:
  6809. gfc_conv_intrinsic_minmaxval (se, expr, LT_EXPR);
  6810. break;
  6811. case GFC_ISYM_NEAREST:
  6812. gfc_conv_intrinsic_nearest (se, expr);
  6813. break;
  6814. case GFC_ISYM_NORM2:
  6815. gfc_conv_intrinsic_arith (se, expr, PLUS_EXPR, true);
  6816. break;
  6817. case GFC_ISYM_NOT:
  6818. gfc_conv_intrinsic_not (se, expr);
  6819. break;
  6820. case GFC_ISYM_OR:
  6821. gfc_conv_intrinsic_bitop (se, expr, BIT_IOR_EXPR);
  6822. break;
  6823. case GFC_ISYM_PARITY:
  6824. gfc_conv_intrinsic_arith (se, expr, NE_EXPR, false);
  6825. break;
  6826. case GFC_ISYM_PRESENT:
  6827. gfc_conv_intrinsic_present (se, expr);
  6828. break;
  6829. case GFC_ISYM_PRODUCT:
  6830. gfc_conv_intrinsic_arith (se, expr, MULT_EXPR, false);
  6831. break;
  6832. case GFC_ISYM_RANK:
  6833. gfc_conv_intrinsic_rank (se, expr);
  6834. break;
  6835. case GFC_ISYM_RRSPACING:
  6836. gfc_conv_intrinsic_rrspacing (se, expr);
  6837. break;
  6838. case GFC_ISYM_SET_EXPONENT:
  6839. gfc_conv_intrinsic_set_exponent (se, expr);
  6840. break;
  6841. case GFC_ISYM_SCALE:
  6842. gfc_conv_intrinsic_scale (se, expr);
  6843. break;
  6844. case GFC_ISYM_SIGN:
  6845. gfc_conv_intrinsic_sign (se, expr);
  6846. break;
  6847. case GFC_ISYM_SIZE:
  6848. gfc_conv_intrinsic_size (se, expr);
  6849. break;
  6850. case GFC_ISYM_SIZEOF:
  6851. case GFC_ISYM_C_SIZEOF:
  6852. gfc_conv_intrinsic_sizeof (se, expr);
  6853. break;
  6854. case GFC_ISYM_STORAGE_SIZE:
  6855. gfc_conv_intrinsic_storage_size (se, expr);
  6856. break;
  6857. case GFC_ISYM_SPACING:
  6858. gfc_conv_intrinsic_spacing (se, expr);
  6859. break;
  6860. case GFC_ISYM_STRIDE:
  6861. conv_intrinsic_stride (se, expr);
  6862. break;
  6863. case GFC_ISYM_SUM:
  6864. gfc_conv_intrinsic_arith (se, expr, PLUS_EXPR, false);
  6865. break;
  6866. case GFC_ISYM_TRANSFER:
  6867. if (se->ss && se->ss->info->useflags)
  6868. /* Access the previously obtained result. */
  6869. gfc_conv_tmp_array_ref (se);
  6870. else
  6871. gfc_conv_intrinsic_transfer (se, expr);
  6872. break;
  6873. case GFC_ISYM_TTYNAM:
  6874. gfc_conv_intrinsic_ttynam (se, expr);
  6875. break;
  6876. case GFC_ISYM_UBOUND:
  6877. gfc_conv_intrinsic_bound (se, expr, 1);
  6878. break;
  6879. case GFC_ISYM_UCOBOUND:
  6880. conv_intrinsic_cobound (se, expr);
  6881. break;
  6882. case GFC_ISYM_XOR:
  6883. gfc_conv_intrinsic_bitop (se, expr, BIT_XOR_EXPR);
  6884. break;
  6885. case GFC_ISYM_LOC:
  6886. gfc_conv_intrinsic_loc (se, expr);
  6887. break;
  6888. case GFC_ISYM_THIS_IMAGE:
  6889. /* For num_images() == 1, handle as LCOBOUND. */
  6890. if (expr->value.function.actual->expr
  6891. && flag_coarray == GFC_FCOARRAY_SINGLE)
  6892. conv_intrinsic_cobound (se, expr);
  6893. else
  6894. trans_this_image (se, expr);
  6895. break;
  6896. case GFC_ISYM_IMAGE_INDEX:
  6897. trans_image_index (se, expr);
  6898. break;
  6899. case GFC_ISYM_NUM_IMAGES:
  6900. trans_num_images (se, expr);
  6901. break;
  6902. case GFC_ISYM_ACCESS:
  6903. case GFC_ISYM_CHDIR:
  6904. case GFC_ISYM_CHMOD:
  6905. case GFC_ISYM_DTIME:
  6906. case GFC_ISYM_ETIME:
  6907. case GFC_ISYM_EXTENDS_TYPE_OF:
  6908. case GFC_ISYM_FGET:
  6909. case GFC_ISYM_FGETC:
  6910. case GFC_ISYM_FNUM:
  6911. case GFC_ISYM_FPUT:
  6912. case GFC_ISYM_FPUTC:
  6913. case GFC_ISYM_FSTAT:
  6914. case GFC_ISYM_FTELL:
  6915. case GFC_ISYM_GETCWD:
  6916. case GFC_ISYM_GETGID:
  6917. case GFC_ISYM_GETPID:
  6918. case GFC_ISYM_GETUID:
  6919. case GFC_ISYM_HOSTNM:
  6920. case GFC_ISYM_KILL:
  6921. case GFC_ISYM_IERRNO:
  6922. case GFC_ISYM_IRAND:
  6923. case GFC_ISYM_ISATTY:
  6924. case GFC_ISYM_JN2:
  6925. case GFC_ISYM_LINK:
  6926. case GFC_ISYM_LSTAT:
  6927. case GFC_ISYM_MALLOC:
  6928. case GFC_ISYM_MATMUL:
  6929. case GFC_ISYM_MCLOCK:
  6930. case GFC_ISYM_MCLOCK8:
  6931. case GFC_ISYM_RAND:
  6932. case GFC_ISYM_RENAME:
  6933. case GFC_ISYM_SECOND:
  6934. case GFC_ISYM_SECNDS:
  6935. case GFC_ISYM_SIGNAL:
  6936. case GFC_ISYM_STAT:
  6937. case GFC_ISYM_SYMLNK:
  6938. case GFC_ISYM_SYSTEM:
  6939. case GFC_ISYM_TIME:
  6940. case GFC_ISYM_TIME8:
  6941. case GFC_ISYM_UMASK:
  6942. case GFC_ISYM_UNLINK:
  6943. case GFC_ISYM_YN2:
  6944. gfc_conv_intrinsic_funcall (se, expr);
  6945. break;
  6946. case GFC_ISYM_EOSHIFT:
  6947. case GFC_ISYM_PACK:
  6948. case GFC_ISYM_RESHAPE:
  6949. /* For those, expr->rank should always be >0 and thus the if above the
  6950. switch should have matched. */
  6951. gcc_unreachable ();
  6952. break;
  6953. default:
  6954. gfc_conv_intrinsic_lib_function (se, expr);
  6955. break;
  6956. }
  6957. }
  6958. static gfc_ss *
  6959. walk_inline_intrinsic_transpose (gfc_ss *ss, gfc_expr *expr)
  6960. {
  6961. gfc_ss *arg_ss, *tmp_ss;
  6962. gfc_actual_arglist *arg;
  6963. arg = expr->value.function.actual;
  6964. gcc_assert (arg->expr);
  6965. arg_ss = gfc_walk_subexpr (gfc_ss_terminator, arg->expr);
  6966. gcc_assert (arg_ss != gfc_ss_terminator);
  6967. for (tmp_ss = arg_ss; ; tmp_ss = tmp_ss->next)
  6968. {
  6969. if (tmp_ss->info->type != GFC_SS_SCALAR
  6970. && tmp_ss->info->type != GFC_SS_REFERENCE)
  6971. {
  6972. int tmp_dim;
  6973. gcc_assert (tmp_ss->dimen == 2);
  6974. /* We just invert dimensions. */
  6975. tmp_dim = tmp_ss->dim[0];
  6976. tmp_ss->dim[0] = tmp_ss->dim[1];
  6977. tmp_ss->dim[1] = tmp_dim;
  6978. }
  6979. /* Stop when tmp_ss points to the last valid element of the chain... */
  6980. if (tmp_ss->next == gfc_ss_terminator)
  6981. break;
  6982. }
  6983. /* ... so that we can attach the rest of the chain to it. */
  6984. tmp_ss->next = ss;
  6985. return arg_ss;
  6986. }
  6987. /* Move the given dimension of the given gfc_ss list to a nested gfc_ss list.
  6988. This has the side effect of reversing the nested list, so there is no
  6989. need to call gfc_reverse_ss on it (the given list is assumed not to be
  6990. reversed yet). */
  6991. static gfc_ss *
  6992. nest_loop_dimension (gfc_ss *ss, int dim)
  6993. {
  6994. int ss_dim, i;
  6995. gfc_ss *new_ss, *prev_ss = gfc_ss_terminator;
  6996. gfc_loopinfo *new_loop;
  6997. gcc_assert (ss != gfc_ss_terminator);
  6998. for (; ss != gfc_ss_terminator; ss = ss->next)
  6999. {
  7000. new_ss = gfc_get_ss ();
  7001. new_ss->next = prev_ss;
  7002. new_ss->parent = ss;
  7003. new_ss->info = ss->info;
  7004. new_ss->info->refcount++;
  7005. if (ss->dimen != 0)
  7006. {
  7007. gcc_assert (ss->info->type != GFC_SS_SCALAR
  7008. && ss->info->type != GFC_SS_REFERENCE);
  7009. new_ss->dimen = 1;
  7010. new_ss->dim[0] = ss->dim[dim];
  7011. gcc_assert (dim < ss->dimen);
  7012. ss_dim = --ss->dimen;
  7013. for (i = dim; i < ss_dim; i++)
  7014. ss->dim[i] = ss->dim[i + 1];
  7015. ss->dim[ss_dim] = 0;
  7016. }
  7017. prev_ss = new_ss;
  7018. if (ss->nested_ss)
  7019. {
  7020. ss->nested_ss->parent = new_ss;
  7021. new_ss->nested_ss = ss->nested_ss;
  7022. }
  7023. ss->nested_ss = new_ss;
  7024. }
  7025. new_loop = gfc_get_loopinfo ();
  7026. gfc_init_loopinfo (new_loop);
  7027. gcc_assert (prev_ss != NULL);
  7028. gcc_assert (prev_ss != gfc_ss_terminator);
  7029. gfc_add_ss_to_loop (new_loop, prev_ss);
  7030. return new_ss->parent;
  7031. }
  7032. /* Create the gfc_ss list for the SUM/PRODUCT arguments when the function
  7033. is to be inlined. */
  7034. static gfc_ss *
  7035. walk_inline_intrinsic_arith (gfc_ss *ss, gfc_expr *expr)
  7036. {
  7037. gfc_ss *tmp_ss, *tail, *array_ss;
  7038. gfc_actual_arglist *arg1, *arg2, *arg3;
  7039. int sum_dim;
  7040. bool scalar_mask = false;
  7041. /* The rank of the result will be determined later. */
  7042. arg1 = expr->value.function.actual;
  7043. arg2 = arg1->next;
  7044. arg3 = arg2->next;
  7045. gcc_assert (arg3 != NULL);
  7046. if (expr->rank == 0)
  7047. return ss;
  7048. tmp_ss = gfc_ss_terminator;
  7049. if (arg3->expr)
  7050. {
  7051. gfc_ss *mask_ss;
  7052. mask_ss = gfc_walk_subexpr (tmp_ss, arg3->expr);
  7053. if (mask_ss == tmp_ss)
  7054. scalar_mask = 1;
  7055. tmp_ss = mask_ss;
  7056. }
  7057. array_ss = gfc_walk_subexpr (tmp_ss, arg1->expr);
  7058. gcc_assert (array_ss != tmp_ss);
  7059. /* Odd thing: If the mask is scalar, it is used by the frontend after
  7060. the array (to make an if around the nested loop). Thus it shall
  7061. be after array_ss once the gfc_ss list is reversed. */
  7062. if (scalar_mask)
  7063. tmp_ss = gfc_get_scalar_ss (array_ss, arg3->expr);
  7064. else
  7065. tmp_ss = array_ss;
  7066. /* "Hide" the dimension on which we will sum in the first arg's scalarization
  7067. chain. */
  7068. sum_dim = mpz_get_si (arg2->expr->value.integer) - 1;
  7069. tail = nest_loop_dimension (tmp_ss, sum_dim);
  7070. tail->next = ss;
  7071. return tmp_ss;
  7072. }
  7073. static gfc_ss *
  7074. walk_inline_intrinsic_function (gfc_ss * ss, gfc_expr * expr)
  7075. {
  7076. switch (expr->value.function.isym->id)
  7077. {
  7078. case GFC_ISYM_PRODUCT:
  7079. case GFC_ISYM_SUM:
  7080. return walk_inline_intrinsic_arith (ss, expr);
  7081. case GFC_ISYM_TRANSPOSE:
  7082. return walk_inline_intrinsic_transpose (ss, expr);
  7083. default:
  7084. gcc_unreachable ();
  7085. }
  7086. gcc_unreachable ();
  7087. }
  7088. /* This generates code to execute before entering the scalarization loop.
  7089. Currently does nothing. */
  7090. void
  7091. gfc_add_intrinsic_ss_code (gfc_loopinfo * loop ATTRIBUTE_UNUSED, gfc_ss * ss)
  7092. {
  7093. switch (ss->info->expr->value.function.isym->id)
  7094. {
  7095. case GFC_ISYM_UBOUND:
  7096. case GFC_ISYM_LBOUND:
  7097. case GFC_ISYM_UCOBOUND:
  7098. case GFC_ISYM_LCOBOUND:
  7099. case GFC_ISYM_THIS_IMAGE:
  7100. break;
  7101. default:
  7102. gcc_unreachable ();
  7103. }
  7104. }
  7105. /* The LBOUND, LCOBOUND, UBOUND and UCOBOUND intrinsics with one parameter
  7106. are expanded into code inside the scalarization loop. */
  7107. static gfc_ss *
  7108. gfc_walk_intrinsic_bound (gfc_ss * ss, gfc_expr * expr)
  7109. {
  7110. if (expr->value.function.actual->expr->ts.type == BT_CLASS)
  7111. gfc_add_class_array_ref (expr->value.function.actual->expr);
  7112. /* The two argument version returns a scalar. */
  7113. if (expr->value.function.actual->next->expr)
  7114. return ss;
  7115. return gfc_get_array_ss (ss, expr, 1, GFC_SS_INTRINSIC);
  7116. }
  7117. /* Walk an intrinsic array libcall. */
  7118. static gfc_ss *
  7119. gfc_walk_intrinsic_libfunc (gfc_ss * ss, gfc_expr * expr)
  7120. {
  7121. gcc_assert (expr->rank > 0);
  7122. return gfc_get_array_ss (ss, expr, expr->rank, GFC_SS_FUNCTION);
  7123. }
  7124. /* Return whether the function call expression EXPR will be expanded
  7125. inline by gfc_conv_intrinsic_function. */
  7126. bool
  7127. gfc_inline_intrinsic_function_p (gfc_expr *expr)
  7128. {
  7129. gfc_actual_arglist *args;
  7130. if (!expr->value.function.isym)
  7131. return false;
  7132. switch (expr->value.function.isym->id)
  7133. {
  7134. case GFC_ISYM_PRODUCT:
  7135. case GFC_ISYM_SUM:
  7136. /* Disable inline expansion if code size matters. */
  7137. if (optimize_size)
  7138. return false;
  7139. args = expr->value.function.actual;
  7140. /* We need to be able to subset the SUM argument at compile-time. */
  7141. if (args->next->expr && args->next->expr->expr_type != EXPR_CONSTANT)
  7142. return false;
  7143. return true;
  7144. case GFC_ISYM_TRANSPOSE:
  7145. return true;
  7146. default:
  7147. return false;
  7148. }
  7149. }
  7150. /* Returns nonzero if the specified intrinsic function call maps directly to
  7151. an external library call. Should only be used for functions that return
  7152. arrays. */
  7153. int
  7154. gfc_is_intrinsic_libcall (gfc_expr * expr)
  7155. {
  7156. gcc_assert (expr->expr_type == EXPR_FUNCTION && expr->value.function.isym);
  7157. gcc_assert (expr->rank > 0);
  7158. if (gfc_inline_intrinsic_function_p (expr))
  7159. return 0;
  7160. switch (expr->value.function.isym->id)
  7161. {
  7162. case GFC_ISYM_ALL:
  7163. case GFC_ISYM_ANY:
  7164. case GFC_ISYM_COUNT:
  7165. case GFC_ISYM_JN2:
  7166. case GFC_ISYM_IANY:
  7167. case GFC_ISYM_IALL:
  7168. case GFC_ISYM_IPARITY:
  7169. case GFC_ISYM_MATMUL:
  7170. case GFC_ISYM_MAXLOC:
  7171. case GFC_ISYM_MAXVAL:
  7172. case GFC_ISYM_MINLOC:
  7173. case GFC_ISYM_MINVAL:
  7174. case GFC_ISYM_NORM2:
  7175. case GFC_ISYM_PARITY:
  7176. case GFC_ISYM_PRODUCT:
  7177. case GFC_ISYM_SUM:
  7178. case GFC_ISYM_SHAPE:
  7179. case GFC_ISYM_SPREAD:
  7180. case GFC_ISYM_YN2:
  7181. /* Ignore absent optional parameters. */
  7182. return 1;
  7183. case GFC_ISYM_RESHAPE:
  7184. case GFC_ISYM_CSHIFT:
  7185. case GFC_ISYM_EOSHIFT:
  7186. case GFC_ISYM_PACK:
  7187. case GFC_ISYM_UNPACK:
  7188. /* Pass absent optional parameters. */
  7189. return 2;
  7190. default:
  7191. return 0;
  7192. }
  7193. }
  7194. /* Walk an intrinsic function. */
  7195. gfc_ss *
  7196. gfc_walk_intrinsic_function (gfc_ss * ss, gfc_expr * expr,
  7197. gfc_intrinsic_sym * isym)
  7198. {
  7199. gcc_assert (isym);
  7200. if (isym->elemental)
  7201. return gfc_walk_elemental_function_args (ss, expr->value.function.actual,
  7202. NULL, GFC_SS_SCALAR);
  7203. if (expr->rank == 0)
  7204. return ss;
  7205. if (gfc_inline_intrinsic_function_p (expr))
  7206. return walk_inline_intrinsic_function (ss, expr);
  7207. if (gfc_is_intrinsic_libcall (expr))
  7208. return gfc_walk_intrinsic_libfunc (ss, expr);
  7209. /* Special cases. */
  7210. switch (isym->id)
  7211. {
  7212. case GFC_ISYM_LBOUND:
  7213. case GFC_ISYM_LCOBOUND:
  7214. case GFC_ISYM_UBOUND:
  7215. case GFC_ISYM_UCOBOUND:
  7216. case GFC_ISYM_THIS_IMAGE:
  7217. return gfc_walk_intrinsic_bound (ss, expr);
  7218. case GFC_ISYM_TRANSFER:
  7219. case GFC_ISYM_CAF_GET:
  7220. return gfc_walk_intrinsic_libfunc (ss, expr);
  7221. default:
  7222. /* This probably meant someone forgot to add an intrinsic to the above
  7223. list(s) when they implemented it, or something's gone horribly
  7224. wrong. */
  7225. gcc_unreachable ();
  7226. }
  7227. }
  7228. static tree
  7229. conv_co_collective (gfc_code *code)
  7230. {
  7231. gfc_se argse;
  7232. stmtblock_t block, post_block;
  7233. tree fndecl, array, strlen, image_index, stat, errmsg, errmsg_len;
  7234. gfc_expr *image_idx_expr, *stat_expr, *errmsg_expr, *opr_expr;
  7235. gfc_start_block (&block);
  7236. gfc_init_block (&post_block);
  7237. if (code->resolved_isym->id == GFC_ISYM_CO_REDUCE)
  7238. {
  7239. opr_expr = code->ext.actual->next->expr;
  7240. image_idx_expr = code->ext.actual->next->next->expr;
  7241. stat_expr = code->ext.actual->next->next->next->expr;
  7242. errmsg_expr = code->ext.actual->next->next->next->next->expr;
  7243. }
  7244. else
  7245. {
  7246. opr_expr = NULL;
  7247. image_idx_expr = code->ext.actual->next->expr;
  7248. stat_expr = code->ext.actual->next->next->expr;
  7249. errmsg_expr = code->ext.actual->next->next->next->expr;
  7250. }
  7251. /* stat. */
  7252. if (stat_expr)
  7253. {
  7254. gfc_init_se (&argse, NULL);
  7255. gfc_conv_expr (&argse, stat_expr);
  7256. gfc_add_block_to_block (&block, &argse.pre);
  7257. gfc_add_block_to_block (&post_block, &argse.post);
  7258. stat = argse.expr;
  7259. if (flag_coarray != GFC_FCOARRAY_SINGLE)
  7260. stat = gfc_build_addr_expr (NULL_TREE, stat);
  7261. }
  7262. else if (flag_coarray == GFC_FCOARRAY_SINGLE)
  7263. stat = NULL_TREE;
  7264. else
  7265. stat = null_pointer_node;
  7266. /* Early exit for GFC_FCOARRAY_SINGLE. */
  7267. if (flag_coarray == GFC_FCOARRAY_SINGLE)
  7268. {
  7269. if (stat != NULL_TREE)
  7270. gfc_add_modify (&block, stat,
  7271. fold_convert (TREE_TYPE (stat), integer_zero_node));
  7272. return gfc_finish_block (&block);
  7273. }
  7274. /* Handle the array. */
  7275. gfc_init_se (&argse, NULL);
  7276. if (code->ext.actual->expr->rank == 0)
  7277. {
  7278. symbol_attribute attr;
  7279. gfc_clear_attr (&attr);
  7280. gfc_init_se (&argse, NULL);
  7281. gfc_conv_expr (&argse, code->ext.actual->expr);
  7282. gfc_add_block_to_block (&block, &argse.pre);
  7283. gfc_add_block_to_block (&post_block, &argse.post);
  7284. array = gfc_conv_scalar_to_descriptor (&argse, argse.expr, attr);
  7285. array = gfc_build_addr_expr (NULL_TREE, array);
  7286. }
  7287. else
  7288. {
  7289. argse.want_pointer = 1;
  7290. gfc_conv_expr_descriptor (&argse, code->ext.actual->expr);
  7291. array = argse.expr;
  7292. }
  7293. gfc_add_block_to_block (&block, &argse.pre);
  7294. gfc_add_block_to_block (&post_block, &argse.post);
  7295. if (code->ext.actual->expr->ts.type == BT_CHARACTER)
  7296. strlen = argse.string_length;
  7297. else
  7298. strlen = integer_zero_node;
  7299. /* image_index. */
  7300. if (image_idx_expr)
  7301. {
  7302. gfc_init_se (&argse, NULL);
  7303. gfc_conv_expr (&argse, image_idx_expr);
  7304. gfc_add_block_to_block (&block, &argse.pre);
  7305. gfc_add_block_to_block (&post_block, &argse.post);
  7306. image_index = fold_convert (integer_type_node, argse.expr);
  7307. }
  7308. else
  7309. image_index = integer_zero_node;
  7310. /* errmsg. */
  7311. if (errmsg_expr)
  7312. {
  7313. gfc_init_se (&argse, NULL);
  7314. gfc_conv_expr (&argse, errmsg_expr);
  7315. gfc_add_block_to_block (&block, &argse.pre);
  7316. gfc_add_block_to_block (&post_block, &argse.post);
  7317. errmsg = argse.expr;
  7318. errmsg_len = fold_convert (integer_type_node, argse.string_length);
  7319. }
  7320. else
  7321. {
  7322. errmsg = null_pointer_node;
  7323. errmsg_len = integer_zero_node;
  7324. }
  7325. /* Generate the function call. */
  7326. switch (code->resolved_isym->id)
  7327. {
  7328. case GFC_ISYM_CO_BROADCAST:
  7329. fndecl = gfor_fndecl_co_broadcast;
  7330. break;
  7331. case GFC_ISYM_CO_MAX:
  7332. fndecl = gfor_fndecl_co_max;
  7333. break;
  7334. case GFC_ISYM_CO_MIN:
  7335. fndecl = gfor_fndecl_co_min;
  7336. break;
  7337. case GFC_ISYM_CO_REDUCE:
  7338. fndecl = gfor_fndecl_co_reduce;
  7339. break;
  7340. case GFC_ISYM_CO_SUM:
  7341. fndecl = gfor_fndecl_co_sum;
  7342. break;
  7343. default:
  7344. gcc_unreachable ();
  7345. }
  7346. if (code->resolved_isym->id == GFC_ISYM_CO_SUM
  7347. || code->resolved_isym->id == GFC_ISYM_CO_BROADCAST)
  7348. fndecl = build_call_expr_loc (input_location, fndecl, 5, array,
  7349. image_index, stat, errmsg, errmsg_len);
  7350. else if (code->resolved_isym->id != GFC_ISYM_CO_REDUCE)
  7351. fndecl = build_call_expr_loc (input_location, fndecl, 6, array, image_index,
  7352. stat, errmsg, strlen, errmsg_len);
  7353. else
  7354. {
  7355. tree opr, opr_flags;
  7356. // FIXME: Handle TS29113's bind(C) strings with descriptor.
  7357. int opr_flag_int;
  7358. if (gfc_is_proc_ptr_comp (opr_expr))
  7359. {
  7360. gfc_symbol *sym = gfc_get_proc_ptr_comp (opr_expr)->ts.interface;
  7361. opr_flag_int = sym->attr.dimension
  7362. || (sym->ts.type == BT_CHARACTER
  7363. && !sym->attr.is_bind_c)
  7364. ? GFC_CAF_BYREF : 0;
  7365. opr_flag_int |= opr_expr->ts.type == BT_CHARACTER
  7366. && !sym->attr.is_bind_c
  7367. ? GFC_CAF_HIDDENLEN : 0;
  7368. opr_flag_int |= sym->formal->sym->attr.value ? GFC_CAF_ARG_VALUE : 0;
  7369. }
  7370. else
  7371. {
  7372. opr_flag_int = gfc_return_by_reference (opr_expr->symtree->n.sym)
  7373. ? GFC_CAF_BYREF : 0;
  7374. opr_flag_int |= opr_expr->ts.type == BT_CHARACTER
  7375. && !opr_expr->symtree->n.sym->attr.is_bind_c
  7376. ? GFC_CAF_HIDDENLEN : 0;
  7377. opr_flag_int |= opr_expr->symtree->n.sym->formal->sym->attr.value
  7378. ? GFC_CAF_ARG_VALUE : 0;
  7379. }
  7380. opr_flags = build_int_cst (integer_type_node, opr_flag_int);
  7381. gfc_conv_expr (&argse, opr_expr);
  7382. opr = gfc_build_addr_expr (NULL_TREE, argse.expr);
  7383. fndecl = build_call_expr_loc (input_location, fndecl, 8, array, opr, opr_flags,
  7384. image_index, stat, errmsg, strlen, errmsg_len);
  7385. }
  7386. gfc_add_expr_to_block (&block, fndecl);
  7387. gfc_add_block_to_block (&block, &post_block);
  7388. return gfc_finish_block (&block);
  7389. }
  7390. static tree
  7391. conv_intrinsic_atomic_op (gfc_code *code)
  7392. {
  7393. gfc_se argse;
  7394. tree tmp, atom, value, old = NULL_TREE, stat = NULL_TREE;
  7395. stmtblock_t block, post_block;
  7396. gfc_expr *atom_expr = code->ext.actual->expr;
  7397. gfc_expr *stat_expr;
  7398. built_in_function fn;
  7399. if (atom_expr->expr_type == EXPR_FUNCTION
  7400. && atom_expr->value.function.isym
  7401. && atom_expr->value.function.isym->id == GFC_ISYM_CAF_GET)
  7402. atom_expr = atom_expr->value.function.actual->expr;
  7403. gfc_start_block (&block);
  7404. gfc_init_block (&post_block);
  7405. gfc_init_se (&argse, NULL);
  7406. argse.want_pointer = 1;
  7407. gfc_conv_expr (&argse, atom_expr);
  7408. gfc_add_block_to_block (&block, &argse.pre);
  7409. gfc_add_block_to_block (&post_block, &argse.post);
  7410. atom = argse.expr;
  7411. gfc_init_se (&argse, NULL);
  7412. if (flag_coarray == GFC_FCOARRAY_LIB
  7413. && code->ext.actual->next->expr->ts.kind == atom_expr->ts.kind)
  7414. argse.want_pointer = 1;
  7415. gfc_conv_expr (&argse, code->ext.actual->next->expr);
  7416. gfc_add_block_to_block (&block, &argse.pre);
  7417. gfc_add_block_to_block (&post_block, &argse.post);
  7418. value = argse.expr;
  7419. switch (code->resolved_isym->id)
  7420. {
  7421. case GFC_ISYM_ATOMIC_ADD:
  7422. case GFC_ISYM_ATOMIC_AND:
  7423. case GFC_ISYM_ATOMIC_DEF:
  7424. case GFC_ISYM_ATOMIC_OR:
  7425. case GFC_ISYM_ATOMIC_XOR:
  7426. stat_expr = code->ext.actual->next->next->expr;
  7427. if (flag_coarray == GFC_FCOARRAY_LIB)
  7428. old = null_pointer_node;
  7429. break;
  7430. default:
  7431. gfc_init_se (&argse, NULL);
  7432. if (flag_coarray == GFC_FCOARRAY_LIB)
  7433. argse.want_pointer = 1;
  7434. gfc_conv_expr (&argse, code->ext.actual->next->next->expr);
  7435. gfc_add_block_to_block (&block, &argse.pre);
  7436. gfc_add_block_to_block (&post_block, &argse.post);
  7437. old = argse.expr;
  7438. stat_expr = code->ext.actual->next->next->next->expr;
  7439. }
  7440. /* STAT= */
  7441. if (stat_expr != NULL)
  7442. {
  7443. gcc_assert (stat_expr->expr_type == EXPR_VARIABLE);
  7444. gfc_init_se (&argse, NULL);
  7445. if (flag_coarray == GFC_FCOARRAY_LIB)
  7446. argse.want_pointer = 1;
  7447. gfc_conv_expr_val (&argse, stat_expr);
  7448. gfc_add_block_to_block (&block, &argse.pre);
  7449. gfc_add_block_to_block (&post_block, &argse.post);
  7450. stat = argse.expr;
  7451. }
  7452. else if (flag_coarray == GFC_FCOARRAY_LIB)
  7453. stat = null_pointer_node;
  7454. if (flag_coarray == GFC_FCOARRAY_LIB)
  7455. {
  7456. tree image_index, caf_decl, offset, token;
  7457. int op;
  7458. switch (code->resolved_isym->id)
  7459. {
  7460. case GFC_ISYM_ATOMIC_ADD:
  7461. case GFC_ISYM_ATOMIC_FETCH_ADD:
  7462. op = (int) GFC_CAF_ATOMIC_ADD;
  7463. break;
  7464. case GFC_ISYM_ATOMIC_AND:
  7465. case GFC_ISYM_ATOMIC_FETCH_AND:
  7466. op = (int) GFC_CAF_ATOMIC_AND;
  7467. break;
  7468. case GFC_ISYM_ATOMIC_OR:
  7469. case GFC_ISYM_ATOMIC_FETCH_OR:
  7470. op = (int) GFC_CAF_ATOMIC_OR;
  7471. break;
  7472. case GFC_ISYM_ATOMIC_XOR:
  7473. case GFC_ISYM_ATOMIC_FETCH_XOR:
  7474. op = (int) GFC_CAF_ATOMIC_XOR;
  7475. break;
  7476. case GFC_ISYM_ATOMIC_DEF:
  7477. op = 0; /* Unused. */
  7478. break;
  7479. default:
  7480. gcc_unreachable ();
  7481. }
  7482. caf_decl = gfc_get_tree_for_caf_expr (atom_expr);
  7483. if (TREE_CODE (TREE_TYPE (caf_decl)) == REFERENCE_TYPE)
  7484. caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
  7485. if (gfc_is_coindexed (atom_expr))
  7486. image_index = gfc_caf_get_image_index (&block, atom_expr, caf_decl);
  7487. else
  7488. image_index = integer_zero_node;
  7489. if (!POINTER_TYPE_P (TREE_TYPE (value)))
  7490. {
  7491. tmp = gfc_create_var (TREE_TYPE (TREE_TYPE (atom)), "value");
  7492. gfc_add_modify (&block, tmp, fold_convert (TREE_TYPE (tmp), value));
  7493. value = gfc_build_addr_expr (NULL_TREE, tmp);
  7494. }
  7495. gfc_get_caf_token_offset (&token, &offset, caf_decl, atom, atom_expr);
  7496. if (code->resolved_isym->id == GFC_ISYM_ATOMIC_DEF)
  7497. tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_atomic_def, 7,
  7498. token, offset, image_index, value, stat,
  7499. build_int_cst (integer_type_node,
  7500. (int) atom_expr->ts.type),
  7501. build_int_cst (integer_type_node,
  7502. (int) atom_expr->ts.kind));
  7503. else
  7504. tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_atomic_op, 9,
  7505. build_int_cst (integer_type_node, op),
  7506. token, offset, image_index, value, old, stat,
  7507. build_int_cst (integer_type_node,
  7508. (int) atom_expr->ts.type),
  7509. build_int_cst (integer_type_node,
  7510. (int) atom_expr->ts.kind));
  7511. gfc_add_expr_to_block (&block, tmp);
  7512. gfc_add_block_to_block (&block, &post_block);
  7513. return gfc_finish_block (&block);
  7514. }
  7515. switch (code->resolved_isym->id)
  7516. {
  7517. case GFC_ISYM_ATOMIC_ADD:
  7518. case GFC_ISYM_ATOMIC_FETCH_ADD:
  7519. fn = BUILT_IN_ATOMIC_FETCH_ADD_N;
  7520. break;
  7521. case GFC_ISYM_ATOMIC_AND:
  7522. case GFC_ISYM_ATOMIC_FETCH_AND:
  7523. fn = BUILT_IN_ATOMIC_FETCH_AND_N;
  7524. break;
  7525. case GFC_ISYM_ATOMIC_DEF:
  7526. fn = BUILT_IN_ATOMIC_STORE_N;
  7527. break;
  7528. case GFC_ISYM_ATOMIC_OR:
  7529. case GFC_ISYM_ATOMIC_FETCH_OR:
  7530. fn = BUILT_IN_ATOMIC_FETCH_OR_N;
  7531. break;
  7532. case GFC_ISYM_ATOMIC_XOR:
  7533. case GFC_ISYM_ATOMIC_FETCH_XOR:
  7534. fn = BUILT_IN_ATOMIC_FETCH_XOR_N;
  7535. break;
  7536. default:
  7537. gcc_unreachable ();
  7538. }
  7539. tmp = TREE_TYPE (TREE_TYPE (atom));
  7540. fn = (built_in_function) ((int) fn
  7541. + exact_log2 (tree_to_uhwi (TYPE_SIZE_UNIT (tmp)))
  7542. + 1);
  7543. tmp = builtin_decl_explicit (fn);
  7544. tree itype = TREE_TYPE (TREE_TYPE (atom));
  7545. tmp = builtin_decl_explicit (fn);
  7546. switch (code->resolved_isym->id)
  7547. {
  7548. case GFC_ISYM_ATOMIC_ADD:
  7549. case GFC_ISYM_ATOMIC_AND:
  7550. case GFC_ISYM_ATOMIC_DEF:
  7551. case GFC_ISYM_ATOMIC_OR:
  7552. case GFC_ISYM_ATOMIC_XOR:
  7553. tmp = build_call_expr_loc (input_location, tmp, 3, atom,
  7554. fold_convert (itype, value),
  7555. build_int_cst (NULL, MEMMODEL_RELAXED));
  7556. gfc_add_expr_to_block (&block, tmp);
  7557. break;
  7558. default:
  7559. tmp = build_call_expr_loc (input_location, tmp, 3, atom,
  7560. fold_convert (itype, value),
  7561. build_int_cst (NULL, MEMMODEL_RELAXED));
  7562. gfc_add_modify (&block, old, fold_convert (TREE_TYPE (old), tmp));
  7563. break;
  7564. }
  7565. if (stat != NULL_TREE)
  7566. gfc_add_modify (&block, stat, build_int_cst (TREE_TYPE (stat), 0));
  7567. gfc_add_block_to_block (&block, &post_block);
  7568. return gfc_finish_block (&block);
  7569. }
  7570. static tree
  7571. conv_intrinsic_atomic_ref (gfc_code *code)
  7572. {
  7573. gfc_se argse;
  7574. tree tmp, atom, value, stat = NULL_TREE;
  7575. stmtblock_t block, post_block;
  7576. built_in_function fn;
  7577. gfc_expr *atom_expr = code->ext.actual->next->expr;
  7578. if (atom_expr->expr_type == EXPR_FUNCTION
  7579. && atom_expr->value.function.isym
  7580. && atom_expr->value.function.isym->id == GFC_ISYM_CAF_GET)
  7581. atom_expr = atom_expr->value.function.actual->expr;
  7582. gfc_start_block (&block);
  7583. gfc_init_block (&post_block);
  7584. gfc_init_se (&argse, NULL);
  7585. argse.want_pointer = 1;
  7586. gfc_conv_expr (&argse, atom_expr);
  7587. gfc_add_block_to_block (&block, &argse.pre);
  7588. gfc_add_block_to_block (&post_block, &argse.post);
  7589. atom = argse.expr;
  7590. gfc_init_se (&argse, NULL);
  7591. if (flag_coarray == GFC_FCOARRAY_LIB
  7592. && code->ext.actual->expr->ts.kind == atom_expr->ts.kind)
  7593. argse.want_pointer = 1;
  7594. gfc_conv_expr (&argse, code->ext.actual->expr);
  7595. gfc_add_block_to_block (&block, &argse.pre);
  7596. gfc_add_block_to_block (&post_block, &argse.post);
  7597. value = argse.expr;
  7598. /* STAT= */
  7599. if (code->ext.actual->next->next->expr != NULL)
  7600. {
  7601. gcc_assert (code->ext.actual->next->next->expr->expr_type
  7602. == EXPR_VARIABLE);
  7603. gfc_init_se (&argse, NULL);
  7604. if (flag_coarray == GFC_FCOARRAY_LIB)
  7605. argse.want_pointer = 1;
  7606. gfc_conv_expr_val (&argse, code->ext.actual->next->next->expr);
  7607. gfc_add_block_to_block (&block, &argse.pre);
  7608. gfc_add_block_to_block (&post_block, &argse.post);
  7609. stat = argse.expr;
  7610. }
  7611. else if (flag_coarray == GFC_FCOARRAY_LIB)
  7612. stat = null_pointer_node;
  7613. if (flag_coarray == GFC_FCOARRAY_LIB)
  7614. {
  7615. tree image_index, caf_decl, offset, token;
  7616. tree orig_value = NULL_TREE, vardecl = NULL_TREE;
  7617. caf_decl = gfc_get_tree_for_caf_expr (atom_expr);
  7618. if (TREE_CODE (TREE_TYPE (caf_decl)) == REFERENCE_TYPE)
  7619. caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
  7620. if (gfc_is_coindexed (atom_expr))
  7621. image_index = gfc_caf_get_image_index (&block, atom_expr, caf_decl);
  7622. else
  7623. image_index = integer_zero_node;
  7624. gfc_get_caf_token_offset (&token, &offset, caf_decl, atom, atom_expr);
  7625. /* Different type, need type conversion. */
  7626. if (!POINTER_TYPE_P (TREE_TYPE (value)))
  7627. {
  7628. vardecl = gfc_create_var (TREE_TYPE (TREE_TYPE (atom)), "value");
  7629. orig_value = value;
  7630. value = gfc_build_addr_expr (NULL_TREE, vardecl);
  7631. }
  7632. tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_atomic_ref, 7,
  7633. token, offset, image_index, value, stat,
  7634. build_int_cst (integer_type_node,
  7635. (int) atom_expr->ts.type),
  7636. build_int_cst (integer_type_node,
  7637. (int) atom_expr->ts.kind));
  7638. gfc_add_expr_to_block (&block, tmp);
  7639. if (vardecl != NULL_TREE)
  7640. gfc_add_modify (&block, orig_value,
  7641. fold_convert (TREE_TYPE (orig_value), vardecl));
  7642. gfc_add_block_to_block (&block, &post_block);
  7643. return gfc_finish_block (&block);
  7644. }
  7645. tmp = TREE_TYPE (TREE_TYPE (atom));
  7646. fn = (built_in_function) ((int) BUILT_IN_ATOMIC_LOAD_N
  7647. + exact_log2 (tree_to_uhwi (TYPE_SIZE_UNIT (tmp)))
  7648. + 1);
  7649. tmp = builtin_decl_explicit (fn);
  7650. tmp = build_call_expr_loc (input_location, tmp, 2, atom,
  7651. build_int_cst (integer_type_node,
  7652. MEMMODEL_RELAXED));
  7653. gfc_add_modify (&block, value, fold_convert (TREE_TYPE (value), tmp));
  7654. if (stat != NULL_TREE)
  7655. gfc_add_modify (&block, stat, build_int_cst (TREE_TYPE (stat), 0));
  7656. gfc_add_block_to_block (&block, &post_block);
  7657. return gfc_finish_block (&block);
  7658. }
  7659. static tree
  7660. conv_intrinsic_atomic_cas (gfc_code *code)
  7661. {
  7662. gfc_se argse;
  7663. tree tmp, atom, old, new_val, comp, stat = NULL_TREE;
  7664. stmtblock_t block, post_block;
  7665. built_in_function fn;
  7666. gfc_expr *atom_expr = code->ext.actual->expr;
  7667. if (atom_expr->expr_type == EXPR_FUNCTION
  7668. && atom_expr->value.function.isym
  7669. && atom_expr->value.function.isym->id == GFC_ISYM_CAF_GET)
  7670. atom_expr = atom_expr->value.function.actual->expr;
  7671. gfc_init_block (&block);
  7672. gfc_init_block (&post_block);
  7673. gfc_init_se (&argse, NULL);
  7674. argse.want_pointer = 1;
  7675. gfc_conv_expr (&argse, atom_expr);
  7676. atom = argse.expr;
  7677. gfc_init_se (&argse, NULL);
  7678. if (flag_coarray == GFC_FCOARRAY_LIB)
  7679. argse.want_pointer = 1;
  7680. gfc_conv_expr (&argse, code->ext.actual->next->expr);
  7681. gfc_add_block_to_block (&block, &argse.pre);
  7682. gfc_add_block_to_block (&post_block, &argse.post);
  7683. old = argse.expr;
  7684. gfc_init_se (&argse, NULL);
  7685. if (flag_coarray == GFC_FCOARRAY_LIB)
  7686. argse.want_pointer = 1;
  7687. gfc_conv_expr (&argse, code->ext.actual->next->next->expr);
  7688. gfc_add_block_to_block (&block, &argse.pre);
  7689. gfc_add_block_to_block (&post_block, &argse.post);
  7690. comp = argse.expr;
  7691. gfc_init_se (&argse, NULL);
  7692. if (flag_coarray == GFC_FCOARRAY_LIB
  7693. && code->ext.actual->next->next->next->expr->ts.kind
  7694. == atom_expr->ts.kind)
  7695. argse.want_pointer = 1;
  7696. gfc_conv_expr (&argse, code->ext.actual->next->next->next->expr);
  7697. gfc_add_block_to_block (&block, &argse.pre);
  7698. gfc_add_block_to_block (&post_block, &argse.post);
  7699. new_val = argse.expr;
  7700. /* STAT= */
  7701. if (code->ext.actual->next->next->next->next->expr != NULL)
  7702. {
  7703. gcc_assert (code->ext.actual->next->next->next->next->expr->expr_type
  7704. == EXPR_VARIABLE);
  7705. gfc_init_se (&argse, NULL);
  7706. if (flag_coarray == GFC_FCOARRAY_LIB)
  7707. argse.want_pointer = 1;
  7708. gfc_conv_expr_val (&argse,
  7709. code->ext.actual->next->next->next->next->expr);
  7710. gfc_add_block_to_block (&block, &argse.pre);
  7711. gfc_add_block_to_block (&post_block, &argse.post);
  7712. stat = argse.expr;
  7713. }
  7714. else if (flag_coarray == GFC_FCOARRAY_LIB)
  7715. stat = null_pointer_node;
  7716. if (flag_coarray == GFC_FCOARRAY_LIB)
  7717. {
  7718. tree image_index, caf_decl, offset, token;
  7719. caf_decl = gfc_get_tree_for_caf_expr (atom_expr);
  7720. if (TREE_CODE (TREE_TYPE (caf_decl)) == REFERENCE_TYPE)
  7721. caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
  7722. if (gfc_is_coindexed (atom_expr))
  7723. image_index = gfc_caf_get_image_index (&block, atom_expr, caf_decl);
  7724. else
  7725. image_index = integer_zero_node;
  7726. if (TREE_TYPE (TREE_TYPE (new_val)) != TREE_TYPE (TREE_TYPE (old)))
  7727. {
  7728. tmp = gfc_create_var (TREE_TYPE (TREE_TYPE (old)), "new");
  7729. gfc_add_modify (&block, tmp, fold_convert (TREE_TYPE (tmp), new_val));
  7730. new_val = gfc_build_addr_expr (NULL_TREE, tmp);
  7731. }
  7732. /* Convert a constant to a pointer. */
  7733. if (!POINTER_TYPE_P (TREE_TYPE (comp)))
  7734. {
  7735. tmp = gfc_create_var (TREE_TYPE (TREE_TYPE (old)), "comp");
  7736. gfc_add_modify (&block, tmp, fold_convert (TREE_TYPE (tmp), comp));
  7737. comp = gfc_build_addr_expr (NULL_TREE, tmp);
  7738. }
  7739. gfc_get_caf_token_offset (&token, &offset, caf_decl, atom, atom_expr);
  7740. tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_atomic_cas, 9,
  7741. token, offset, image_index, old, comp, new_val,
  7742. stat, build_int_cst (integer_type_node,
  7743. (int) atom_expr->ts.type),
  7744. build_int_cst (integer_type_node,
  7745. (int) atom_expr->ts.kind));
  7746. gfc_add_expr_to_block (&block, tmp);
  7747. gfc_add_block_to_block (&block, &post_block);
  7748. return gfc_finish_block (&block);
  7749. }
  7750. tmp = TREE_TYPE (TREE_TYPE (atom));
  7751. fn = (built_in_function) ((int) BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N
  7752. + exact_log2 (tree_to_uhwi (TYPE_SIZE_UNIT (tmp)))
  7753. + 1);
  7754. tmp = builtin_decl_explicit (fn);
  7755. gfc_add_modify (&block, old, comp);
  7756. tmp = build_call_expr_loc (input_location, tmp, 6, atom,
  7757. gfc_build_addr_expr (NULL, old),
  7758. fold_convert (TREE_TYPE (old), new_val),
  7759. boolean_false_node,
  7760. build_int_cst (NULL, MEMMODEL_RELAXED),
  7761. build_int_cst (NULL, MEMMODEL_RELAXED));
  7762. gfc_add_expr_to_block (&block, tmp);
  7763. if (stat != NULL_TREE)
  7764. gfc_add_modify (&block, stat, build_int_cst (TREE_TYPE (stat), 0));
  7765. gfc_add_block_to_block (&block, &post_block);
  7766. return gfc_finish_block (&block);
  7767. }
  7768. static tree
  7769. conv_intrinsic_move_alloc (gfc_code *code)
  7770. {
  7771. stmtblock_t block;
  7772. gfc_expr *from_expr, *to_expr;
  7773. gfc_expr *to_expr2, *from_expr2 = NULL;
  7774. gfc_se from_se, to_se;
  7775. tree tmp;
  7776. bool coarray;
  7777. gfc_start_block (&block);
  7778. from_expr = code->ext.actual->expr;
  7779. to_expr = code->ext.actual->next->expr;
  7780. gfc_init_se (&from_se, NULL);
  7781. gfc_init_se (&to_se, NULL);
  7782. gcc_assert (from_expr->ts.type != BT_CLASS
  7783. || to_expr->ts.type == BT_CLASS);
  7784. coarray = gfc_get_corank (from_expr) != 0;
  7785. if (from_expr->rank == 0 && !coarray)
  7786. {
  7787. if (from_expr->ts.type != BT_CLASS)
  7788. from_expr2 = from_expr;
  7789. else
  7790. {
  7791. from_expr2 = gfc_copy_expr (from_expr);
  7792. gfc_add_data_component (from_expr2);
  7793. }
  7794. if (to_expr->ts.type != BT_CLASS)
  7795. to_expr2 = to_expr;
  7796. else
  7797. {
  7798. to_expr2 = gfc_copy_expr (to_expr);
  7799. gfc_add_data_component (to_expr2);
  7800. }
  7801. from_se.want_pointer = 1;
  7802. to_se.want_pointer = 1;
  7803. gfc_conv_expr (&from_se, from_expr2);
  7804. gfc_conv_expr (&to_se, to_expr2);
  7805. gfc_add_block_to_block (&block, &from_se.pre);
  7806. gfc_add_block_to_block (&block, &to_se.pre);
  7807. /* Deallocate "to". */
  7808. tmp = gfc_deallocate_scalar_with_status (to_se.expr, NULL_TREE, true,
  7809. to_expr, to_expr->ts);
  7810. gfc_add_expr_to_block (&block, tmp);
  7811. /* Assign (_data) pointers. */
  7812. gfc_add_modify_loc (input_location, &block, to_se.expr,
  7813. fold_convert (TREE_TYPE (to_se.expr), from_se.expr));
  7814. /* Set "from" to NULL. */
  7815. gfc_add_modify_loc (input_location, &block, from_se.expr,
  7816. fold_convert (TREE_TYPE (from_se.expr), null_pointer_node));
  7817. gfc_add_block_to_block (&block, &from_se.post);
  7818. gfc_add_block_to_block (&block, &to_se.post);
  7819. /* Set _vptr. */
  7820. if (to_expr->ts.type == BT_CLASS)
  7821. {
  7822. gfc_symbol *vtab;
  7823. gfc_free_expr (to_expr2);
  7824. gfc_init_se (&to_se, NULL);
  7825. to_se.want_pointer = 1;
  7826. gfc_add_vptr_component (to_expr);
  7827. gfc_conv_expr (&to_se, to_expr);
  7828. if (from_expr->ts.type == BT_CLASS)
  7829. {
  7830. if (UNLIMITED_POLY (from_expr))
  7831. vtab = NULL;
  7832. else
  7833. {
  7834. vtab = gfc_find_derived_vtab (from_expr->ts.u.derived);
  7835. gcc_assert (vtab);
  7836. }
  7837. gfc_free_expr (from_expr2);
  7838. gfc_init_se (&from_se, NULL);
  7839. from_se.want_pointer = 1;
  7840. gfc_add_vptr_component (from_expr);
  7841. gfc_conv_expr (&from_se, from_expr);
  7842. gfc_add_modify_loc (input_location, &block, to_se.expr,
  7843. fold_convert (TREE_TYPE (to_se.expr),
  7844. from_se.expr));
  7845. /* Reset _vptr component to declared type. */
  7846. if (vtab == NULL)
  7847. /* Unlimited polymorphic. */
  7848. gfc_add_modify_loc (input_location, &block, from_se.expr,
  7849. fold_convert (TREE_TYPE (from_se.expr),
  7850. null_pointer_node));
  7851. else
  7852. {
  7853. tmp = gfc_build_addr_expr (NULL_TREE, gfc_get_symbol_decl (vtab));
  7854. gfc_add_modify_loc (input_location, &block, from_se.expr,
  7855. fold_convert (TREE_TYPE (from_se.expr), tmp));
  7856. }
  7857. }
  7858. else
  7859. {
  7860. vtab = gfc_find_vtab (&from_expr->ts);
  7861. gcc_assert (vtab);
  7862. tmp = gfc_build_addr_expr (NULL_TREE, gfc_get_symbol_decl (vtab));
  7863. gfc_add_modify_loc (input_location, &block, to_se.expr,
  7864. fold_convert (TREE_TYPE (to_se.expr), tmp));
  7865. }
  7866. }
  7867. return gfc_finish_block (&block);
  7868. }
  7869. /* Update _vptr component. */
  7870. if (to_expr->ts.type == BT_CLASS)
  7871. {
  7872. gfc_symbol *vtab;
  7873. to_se.want_pointer = 1;
  7874. to_expr2 = gfc_copy_expr (to_expr);
  7875. gfc_add_vptr_component (to_expr2);
  7876. gfc_conv_expr (&to_se, to_expr2);
  7877. if (from_expr->ts.type == BT_CLASS)
  7878. {
  7879. if (UNLIMITED_POLY (from_expr))
  7880. vtab = NULL;
  7881. else
  7882. {
  7883. vtab = gfc_find_derived_vtab (from_expr->ts.u.derived);
  7884. gcc_assert (vtab);
  7885. }
  7886. from_se.want_pointer = 1;
  7887. from_expr2 = gfc_copy_expr (from_expr);
  7888. gfc_add_vptr_component (from_expr2);
  7889. gfc_conv_expr (&from_se, from_expr2);
  7890. gfc_add_modify_loc (input_location, &block, to_se.expr,
  7891. fold_convert (TREE_TYPE (to_se.expr),
  7892. from_se.expr));
  7893. /* Reset _vptr component to declared type. */
  7894. if (vtab == NULL)
  7895. /* Unlimited polymorphic. */
  7896. gfc_add_modify_loc (input_location, &block, from_se.expr,
  7897. fold_convert (TREE_TYPE (from_se.expr),
  7898. null_pointer_node));
  7899. else
  7900. {
  7901. tmp = gfc_build_addr_expr (NULL_TREE, gfc_get_symbol_decl (vtab));
  7902. gfc_add_modify_loc (input_location, &block, from_se.expr,
  7903. fold_convert (TREE_TYPE (from_se.expr), tmp));
  7904. }
  7905. }
  7906. else
  7907. {
  7908. vtab = gfc_find_vtab (&from_expr->ts);
  7909. gcc_assert (vtab);
  7910. tmp = gfc_build_addr_expr (NULL_TREE, gfc_get_symbol_decl (vtab));
  7911. gfc_add_modify_loc (input_location, &block, to_se.expr,
  7912. fold_convert (TREE_TYPE (to_se.expr), tmp));
  7913. }
  7914. gfc_free_expr (to_expr2);
  7915. gfc_init_se (&to_se, NULL);
  7916. if (from_expr->ts.type == BT_CLASS)
  7917. {
  7918. gfc_free_expr (from_expr2);
  7919. gfc_init_se (&from_se, NULL);
  7920. }
  7921. }
  7922. /* Deallocate "to". */
  7923. if (from_expr->rank == 0)
  7924. {
  7925. to_se.want_coarray = 1;
  7926. from_se.want_coarray = 1;
  7927. }
  7928. gfc_conv_expr_descriptor (&to_se, to_expr);
  7929. gfc_conv_expr_descriptor (&from_se, from_expr);
  7930. /* For coarrays, call SYNC ALL if TO is already deallocated as MOVE_ALLOC
  7931. is an image control "statement", cf. IR F08/0040 in 12-006A. */
  7932. if (coarray && flag_coarray == GFC_FCOARRAY_LIB)
  7933. {
  7934. tree cond;
  7935. tmp = gfc_deallocate_with_status (to_se.expr, NULL_TREE, NULL_TREE,
  7936. NULL_TREE, NULL_TREE, true, to_expr,
  7937. true);
  7938. gfc_add_expr_to_block (&block, tmp);
  7939. tmp = gfc_conv_descriptor_data_get (to_se.expr);
  7940. cond = fold_build2_loc (input_location, EQ_EXPR,
  7941. boolean_type_node, tmp,
  7942. fold_convert (TREE_TYPE (tmp),
  7943. null_pointer_node));
  7944. tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_sync_all,
  7945. 3, null_pointer_node, null_pointer_node,
  7946. build_int_cst (integer_type_node, 0));
  7947. tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
  7948. tmp, build_empty_stmt (input_location));
  7949. gfc_add_expr_to_block (&block, tmp);
  7950. }
  7951. else
  7952. {
  7953. tmp = gfc_conv_descriptor_data_get (to_se.expr);
  7954. tmp = gfc_deallocate_with_status (tmp, NULL_TREE, NULL_TREE, NULL_TREE,
  7955. NULL_TREE, true, to_expr, false);
  7956. gfc_add_expr_to_block (&block, tmp);
  7957. }
  7958. /* Move the pointer and update the array descriptor data. */
  7959. gfc_add_modify_loc (input_location, &block, to_se.expr, from_se.expr);
  7960. /* Set "from" to NULL. */
  7961. tmp = gfc_conv_descriptor_data_get (from_se.expr);
  7962. gfc_add_modify_loc (input_location, &block, tmp,
  7963. fold_convert (TREE_TYPE (tmp), null_pointer_node));
  7964. return gfc_finish_block (&block);
  7965. }
  7966. tree
  7967. gfc_conv_intrinsic_subroutine (gfc_code *code)
  7968. {
  7969. tree res;
  7970. gcc_assert (code->resolved_isym);
  7971. switch (code->resolved_isym->id)
  7972. {
  7973. case GFC_ISYM_MOVE_ALLOC:
  7974. res = conv_intrinsic_move_alloc (code);
  7975. break;
  7976. case GFC_ISYM_ATOMIC_CAS:
  7977. res = conv_intrinsic_atomic_cas (code);
  7978. break;
  7979. case GFC_ISYM_ATOMIC_ADD:
  7980. case GFC_ISYM_ATOMIC_AND:
  7981. case GFC_ISYM_ATOMIC_DEF:
  7982. case GFC_ISYM_ATOMIC_OR:
  7983. case GFC_ISYM_ATOMIC_XOR:
  7984. case GFC_ISYM_ATOMIC_FETCH_ADD:
  7985. case GFC_ISYM_ATOMIC_FETCH_AND:
  7986. case GFC_ISYM_ATOMIC_FETCH_OR:
  7987. case GFC_ISYM_ATOMIC_FETCH_XOR:
  7988. res = conv_intrinsic_atomic_op (code);
  7989. break;
  7990. case GFC_ISYM_ATOMIC_REF:
  7991. res = conv_intrinsic_atomic_ref (code);
  7992. break;
  7993. case GFC_ISYM_C_F_POINTER:
  7994. case GFC_ISYM_C_F_PROCPOINTER:
  7995. res = conv_isocbinding_subroutine (code);
  7996. break;
  7997. case GFC_ISYM_CAF_SEND:
  7998. res = conv_caf_send (code);
  7999. break;
  8000. case GFC_ISYM_CO_BROADCAST:
  8001. case GFC_ISYM_CO_MIN:
  8002. case GFC_ISYM_CO_MAX:
  8003. case GFC_ISYM_CO_REDUCE:
  8004. case GFC_ISYM_CO_SUM:
  8005. res = conv_co_collective (code);
  8006. break;
  8007. case GFC_ISYM_SYSTEM_CLOCK:
  8008. res = conv_intrinsic_system_clock (code);
  8009. break;
  8010. default:
  8011. res = NULL_TREE;
  8012. break;
  8013. }
  8014. return res;
  8015. }
  8016. #include "gt-fortran-trans-intrinsic.h"