ninl.pas 182 KB

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