ninl.pas 215 KB

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