ninl.pas 160 KB

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