ninl.pas 224 KB

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