ninl.pas 155 KB

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