ninl.pas 190 KB

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