ninl.pas 176 KB

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