ninl.pas 186 KB

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