ninl.pas 174 KB

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