ninl.pas 179 KB

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