ninl.pas 172 KB

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