ninl.pas 223 KB

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