ninl.pas 186 KB

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