ninl.pas 154 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989
  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 in [0..255], both longints }
  1259. { and cardinals are fine. Since the formal code para type is }
  1260. { longint, insert a typecoversion to longint for cardinal para's }
  1261. begin
  1262. codepara.left := ctypeconvnode.create_internal(codepara.left,ptrsinttype);
  1263. { make it explicit, oterwise you may get a nonsense range }
  1264. { check error if the cardinal already contained a value }
  1265. { > $7fffffff }
  1266. codepara.get_paratype;
  1267. end;
  1268. { create the procedure name }
  1269. procname := 'fpc_val_';
  1270. case destpara.resultdef.typ of
  1271. orddef:
  1272. begin
  1273. case torddef(destpara.resultdef).ordtype of
  1274. {$ifdef cpu64bitaddr}
  1275. s64bit,
  1276. {$endif cpu64bitaddr}
  1277. s8bit,
  1278. s16bit,
  1279. s32bit:
  1280. begin
  1281. suffix := 'sint_';
  1282. { we also need a destsize para in this case }
  1283. sizepara := ccallparanode.create(cordconstnode.create
  1284. (destpara.resultdef.size,s32inttype,true),nil);
  1285. end;
  1286. {$ifdef cpu64bitaddr}
  1287. u64bit,
  1288. {$endif cpu64bitaddr}
  1289. u8bit,
  1290. u16bit,
  1291. u32bit:
  1292. suffix := 'uint_';
  1293. {$ifndef cpu64bitaddr}
  1294. s64bit: suffix := 'int64_';
  1295. u64bit: suffix := 'qword_';
  1296. {$endif not cpu64bitaddr}
  1297. scurrency: suffix := 'currency_';
  1298. else
  1299. internalerror(200304225);
  1300. end;
  1301. end;
  1302. floatdef:
  1303. suffix:='real_';
  1304. enumdef:
  1305. begin
  1306. suffix:='enum_';
  1307. sizepara:=Ccallparanode.create(Caddrnode.create_internal(
  1308. Crttinode.create(Tenumdef(destpara.resultdef),fullrtti,rdt_str2ord)
  1309. ),nil);
  1310. end;
  1311. end;
  1312. procname := procname + suffix;
  1313. { play a trick to have tcallnode handle invalid source parameters: }
  1314. { the shortstring-longint val routine by default }
  1315. if (sourcepara.resultdef.typ = stringdef) then
  1316. procname := procname + tstringdef(sourcepara.resultdef).stringtypname
  1317. { zero-based arrays (of char) can be implicitely converted to ansistring }
  1318. else if is_zero_based_array(sourcepara.resultdef) then
  1319. procname := procname + 'ansistr'
  1320. else
  1321. procname := procname + 'shortstr';
  1322. { set up the correct parameters for the call: the code para... }
  1323. newparas := codepara;
  1324. { and the source para }
  1325. codepara.right := sourcepara;
  1326. { sizepara either contains nil if none is needed (which is ok, since }
  1327. { then the next statement severes any possible links with other paras }
  1328. { that sourcepara may have) or it contains the necessary size para and }
  1329. { its right field is nil }
  1330. sourcepara.right := sizepara;
  1331. { create the call and assign the result to dest (val helpers are functions).
  1332. Use a trick to prevent a type size mismatch warning to be generated by the
  1333. assignment node. First convert implicitly to the resultdef. This will insert
  1334. the range check. The Second conversion is done explicitly to hide the implicit conversion
  1335. for the assignment node and therefor preventing the warning (PFV)
  1336. The implicit conversion is avoided for enums because implicit conversion between
  1337. longint (which is what fpc_val_enum_shortstr returns) and enumerations is not
  1338. possible. (DM).
  1339. The implicit conversion is also avoided for COMP type if it is handled by FPU (x86)
  1340. to prevent warning about automatic type conversion. }
  1341. if (destpara.resultdef.typ=enumdef) or
  1342. ((destpara.resultdef.typ=floatdef) and (tfloatdef(destpara.resultdef).floattype=s64comp))
  1343. then
  1344. tc:=ccallnode.createintern(procname,newparas)
  1345. else
  1346. tc:=ctypeconvnode.create(ccallnode.createintern(procname,newparas),destpara.left.resultdef);
  1347. addstatement(newstatement,cassignmentnode.create(
  1348. destpara.left,ctypeconvnode.create_internal(tc,destpara.left.resultdef)));
  1349. { dispose of the enclosing paranode of the destination }
  1350. destpara.left := nil;
  1351. destpara.right := nil;
  1352. destpara.free;
  1353. { check if we used a temp for code and whether we have to store }
  1354. { it to the real code parameter }
  1355. if assigned(orgcode) then
  1356. addstatement(newstatement,cassignmentnode.create(
  1357. orgcode,
  1358. ctypeconvnode.create_internal(
  1359. ctemprefnode.create(tempcode),orgcode.resultdef)));
  1360. { release the temp if we allocated one }
  1361. if assigned(tempcode) then
  1362. addstatement(newstatement,ctempdeletenode.create(tempcode));
  1363. { free the errornode }
  1364. result.free;
  1365. { and return it }
  1366. result := newblock;
  1367. end;
  1368. function tinlinenode.handle_setlength: tnode;
  1369. var
  1370. def: tdef;
  1371. destppn,
  1372. paras: tnode;
  1373. newstatement: tstatementnode;
  1374. ppn: tcallparanode;
  1375. counter,
  1376. dims: longint;
  1377. isarray: boolean;
  1378. begin
  1379. { for easy exiting if something goes wrong }
  1380. result:=cerrornode.create;
  1381. resultdef:=voidtype;
  1382. paras:=left;
  1383. dims:=0;
  1384. if assigned(paras) then
  1385. begin
  1386. { check type of lengths }
  1387. ppn:=tcallparanode(paras);
  1388. while assigned(ppn.right) do
  1389. begin
  1390. set_varstate(ppn.left,vs_read,[vsf_must_be_valid]);
  1391. inserttypeconv(ppn.left,sinttype);
  1392. inc(dims);
  1393. ppn:=tcallparanode(ppn.right);
  1394. end;
  1395. end;
  1396. if dims=0 then
  1397. begin
  1398. CGMessage1(parser_e_wrong_parameter_size,'SetLength');
  1399. exit;
  1400. end;
  1401. { last param must be var }
  1402. destppn:=ppn.left;
  1403. valid_for_var(destppn,true);
  1404. set_varstate(destppn,vs_written,[]);
  1405. { first param must be a string or dynamic array ...}
  1406. isarray:=is_dynamic_array(destppn.resultdef);
  1407. if not((destppn.resultdef.typ=stringdef) or
  1408. isarray) then
  1409. begin
  1410. { possibly generic involved? }
  1411. if df_generic in current_procinfo.procdef.defoptions then
  1412. result:=internalstatements(newstatement)
  1413. else
  1414. CGMessage(type_e_mismatch);
  1415. exit;
  1416. end;
  1417. { only dynamic arrays accept more dimensions }
  1418. if (dims>1) then
  1419. begin
  1420. if (not isarray) then
  1421. CGMessage(type_e_mismatch)
  1422. else
  1423. begin
  1424. { check if the amount of dimensions is valid }
  1425. def:=tarraydef(destppn.resultdef).elementdef;
  1426. counter:=dims;
  1427. while counter > 1 do
  1428. begin
  1429. if not(is_dynamic_array(def)) then
  1430. begin
  1431. CGMessage1(parser_e_wrong_parameter_size,'SetLength');
  1432. break;
  1433. end;
  1434. dec(counter);
  1435. def:=tarraydef(def).elementdef;
  1436. end;
  1437. end;
  1438. end;
  1439. result.free;
  1440. result:=nil;
  1441. end;
  1442. function tinlinenode.handle_copy: tnode;
  1443. var
  1444. paras : tnode;
  1445. ppn : tcallparanode;
  1446. paradef : tdef;
  1447. counter : integer;
  1448. begin
  1449. result:=nil;
  1450. { determine copy function to use based on the first argument,
  1451. also count the number of arguments in this loop }
  1452. counter:=1;
  1453. paras:=left;
  1454. ppn:=tcallparanode(paras);
  1455. while assigned(ppn.right) do
  1456. begin
  1457. inc(counter);
  1458. set_varstate(ppn.left,vs_read,[vsf_must_be_valid]);
  1459. ppn:=tcallparanode(ppn.right);
  1460. end;
  1461. set_varstate(ppn.left,vs_read,[vsf_must_be_valid]);
  1462. paradef:=ppn.left.resultdef;
  1463. if is_ansistring(paradef) then
  1464. // set resultdef to argument def
  1465. resultdef:=paradef
  1466. else if (is_chararray(paradef) and (paradef.size>255)) or
  1467. ((cs_refcountedstrings in current_settings.localswitches) and is_pchar(paradef)) then
  1468. // set resultdef to ansistring type since result will be in ansistring codepage
  1469. resultdef:=getansistringdef
  1470. else
  1471. if is_widestring(paradef) then
  1472. resultdef:=cwidestringtype
  1473. else
  1474. if is_unicodestring(paradef) or
  1475. is_widechararray(paradef) or
  1476. is_pwidechar(paradef) then
  1477. resultdef:=cunicodestringtype
  1478. else
  1479. if is_char(paradef) then
  1480. resultdef:=cshortstringtype
  1481. else
  1482. if is_dynamic_array(paradef) then
  1483. begin
  1484. { Only allow 1 or 3 arguments }
  1485. if not(counter in [1..3]) then
  1486. begin
  1487. CGMessage1(parser_e_wrong_parameter_size,'Copy');
  1488. exit;
  1489. end;
  1490. resultdef:=paradef;
  1491. end
  1492. else
  1493. begin
  1494. { generic fallback that will give an error if a wrong
  1495. type is passed }
  1496. if (counter=3) then
  1497. resultdef:=cshortstringtype
  1498. else
  1499. CGMessagePos(ppn.left.fileinfo,type_e_mismatch);
  1500. end;
  1501. end;
  1502. {$maxfpuregisters 0}
  1503. function getpi : bestreal;
  1504. begin
  1505. {$ifdef x86}
  1506. { x86 has pi in hardware }
  1507. result:=pi;
  1508. {$else x86}
  1509. {$ifdef cpuextended}
  1510. result:=MathPiExtended.Value;
  1511. {$else cpuextended}
  1512. result:=MathPi.Value;
  1513. {$endif cpuextended}
  1514. {$endif x86}
  1515. end;
  1516. function tinlinenode.simplify(forinline : boolean): tnode;
  1517. function do_lowhigh(def:tdef) : tnode;
  1518. var
  1519. v : tconstexprint;
  1520. enum : tenumsym;
  1521. hp : tnode;
  1522. i : integer;
  1523. begin
  1524. case def.typ of
  1525. orddef:
  1526. begin
  1527. set_varstate(left,vs_read,[]);
  1528. if inlinenumber=in_low_x then
  1529. v:=torddef(def).low
  1530. else
  1531. v:=torddef(def).high;
  1532. hp:=cordconstnode.create(v,def,true);
  1533. typecheckpass(hp);
  1534. do_lowhigh:=hp;
  1535. end;
  1536. enumdef:
  1537. begin
  1538. set_varstate(left,vs_read,[]);
  1539. if inlinenumber=in_high_x then
  1540. v:=tenumdef(def).maxval
  1541. else
  1542. v:=tenumdef(def).minval;
  1543. enum:=nil;
  1544. for i := 0 to tenumdef(def).symtable.SymList.Count - 1 do
  1545. if tenumsym(tenumdef(def).symtable.SymList[i]).value=v then
  1546. begin
  1547. enum:=tenumsym(tenumdef(def).symtable.SymList[i]);
  1548. break;
  1549. end;
  1550. if not assigned(enum) then
  1551. internalerror(309993)
  1552. else
  1553. hp:=genenumnode(enum);
  1554. do_lowhigh:=hp;
  1555. end;
  1556. else
  1557. internalerror(87);
  1558. end;
  1559. end;
  1560. function getconstrealvalue : bestreal;
  1561. begin
  1562. case left.nodetype of
  1563. ordconstn:
  1564. getconstrealvalue:=tordconstnode(left).value;
  1565. realconstn:
  1566. getconstrealvalue:=trealconstnode(left).value_real;
  1567. else
  1568. internalerror(309992);
  1569. end;
  1570. end;
  1571. procedure setconstrealvalue(r : bestreal);
  1572. begin
  1573. result:=crealconstnode.create(r,pbestrealtype^);
  1574. end;
  1575. function handle_ln_const(r : bestreal) : tnode;
  1576. begin
  1577. if r<=0.0 then
  1578. if floating_point_range_check_error then
  1579. begin
  1580. result:=crealconstnode.create(0,pbestrealtype^);
  1581. CGMessage(type_e_wrong_math_argument)
  1582. end
  1583. else
  1584. begin
  1585. if r=0.0 then
  1586. result:=crealconstnode.create(MathQNaN.Value,pbestrealtype^)
  1587. else
  1588. result:=crealconstnode.create(MathNegInf.Value,pbestrealtype^)
  1589. end
  1590. else
  1591. result:=crealconstnode.create(ln(r),pbestrealtype^)
  1592. end;
  1593. function handle_sqrt_const(r : bestreal) : tnode;
  1594. begin
  1595. if r<0.0 then
  1596. if floating_point_range_check_error then
  1597. begin
  1598. result:=crealconstnode.create(0,pbestrealtype^);
  1599. CGMessage(type_e_wrong_math_argument)
  1600. end
  1601. else
  1602. result:=crealconstnode.create(MathQNaN.Value,pbestrealtype^)
  1603. else
  1604. result:=crealconstnode.create(sqrt(r),pbestrealtype^)
  1605. end;
  1606. function handle_const_sar : tnode;
  1607. var
  1608. vl,vl2 : TConstExprInt;
  1609. bits,shift: integer;
  1610. mask : qword;
  1611. def : tdef;
  1612. begin
  1613. result:=nil;
  1614. if (left.nodetype=ordconstn) or ((left.nodetype=callparan) and (tcallparanode(left).left.nodetype=ordconstn)) then
  1615. begin
  1616. if (left.nodetype=callparan) and
  1617. assigned(tcallparanode(left).right) then
  1618. begin
  1619. if (tcallparanode(tcallparanode(left).right).left.nodetype=ordconstn) then
  1620. begin
  1621. def:=tcallparanode(tcallparanode(left).right).left.resultdef;
  1622. vl:=tordconstnode(tcallparanode(left).left).value;
  1623. vl2:=tordconstnode(tcallparanode(tcallparanode(left).right).left).value;
  1624. end
  1625. else
  1626. exit;
  1627. end
  1628. else
  1629. begin
  1630. def:=left.resultdef;
  1631. vl:=1;
  1632. vl2:=tordconstnode(left).value;
  1633. end;
  1634. bits:=def.size*8;
  1635. shift:=vl.svalue and (bits-1);
  1636. case bits of
  1637. 8:
  1638. mask:=$ff;
  1639. 16:
  1640. mask:=$ffff;
  1641. 32:
  1642. mask:=$ffffffff;
  1643. 64:
  1644. mask:=qword($ffffffffffffffff);
  1645. else
  1646. mask:=qword(1 shl bits)-1;
  1647. end;
  1648. {$push}
  1649. {$r-,q-}
  1650. if shift=0 then
  1651. result:=cordconstnode.create(vl2.svalue,def,false)
  1652. else if vl2.svalue<0 then
  1653. result:=cordconstnode.create(((vl2.svalue shr shift) or (mask shl (bits-shift))) and mask,def,false)
  1654. else
  1655. result:=cordconstnode.create((vl2.svalue shr shift) and mask,def,false);
  1656. {$pop}
  1657. end
  1658. else
  1659. end;
  1660. function handle_const_rox : tnode;
  1661. var
  1662. vl,vl2 : TConstExprInt;
  1663. bits,shift: integer;
  1664. def : tdef;
  1665. begin
  1666. result:=nil;
  1667. if (left.nodetype=ordconstn) or ((left.nodetype=callparan) and (tcallparanode(left).left.nodetype=ordconstn)) then
  1668. begin
  1669. if (left.nodetype=callparan) and
  1670. assigned(tcallparanode(left).right) then
  1671. begin
  1672. if (tcallparanode(tcallparanode(left).right).left.nodetype=ordconstn) then
  1673. begin
  1674. def:=tcallparanode(tcallparanode(left).right).left.resultdef;
  1675. vl:=tordconstnode(tcallparanode(left).left).value;
  1676. vl2:=tordconstnode(tcallparanode(tcallparanode(left).right).left).value;
  1677. end
  1678. else
  1679. exit;
  1680. end
  1681. else
  1682. begin
  1683. def:=left.resultdef;
  1684. vl:=1;
  1685. vl2:=tordconstnode(left).value;
  1686. end;
  1687. bits:=def.size*8;
  1688. shift:=vl.svalue and (bits-1);
  1689. {$push}
  1690. {$r-,q-}
  1691. if shift=0 then
  1692. result:=cordconstnode.create(vl2.svalue,def,false)
  1693. else
  1694. case inlinenumber of
  1695. in_ror_x,in_ror_x_y:
  1696. case def.size of
  1697. 1:
  1698. result:=cordconstnode.create(RorByte(Byte(vl2.svalue),shift),def,false);
  1699. 2:
  1700. result:=cordconstnode.create(RorWord(Word(vl2.svalue),shift),def,false);
  1701. 4:
  1702. result:=cordconstnode.create(RorDWord(DWord(vl2.svalue),shift),def,false);
  1703. 8:
  1704. result:=cordconstnode.create(RorQWord(QWord(vl2.svalue),shift),def,false);
  1705. else
  1706. internalerror(2011061903);
  1707. end;
  1708. in_rol_x,in_rol_x_y:
  1709. case def.size of
  1710. 1:
  1711. result:=cordconstnode.create(RolByte(Byte(vl2.svalue),shift),def,false);
  1712. 2:
  1713. result:=cordconstnode.create(RolWord(Word(vl2.svalue),shift),def,false);
  1714. 4:
  1715. result:=cordconstnode.create(RolDWord(DWord(vl2.svalue),shift),def,false);
  1716. 8:
  1717. result:=cordconstnode.create(RolQWord(QWord(vl2.svalue),shift),def,false);
  1718. else
  1719. internalerror(2011061902);
  1720. end;
  1721. else
  1722. internalerror(2011061901);
  1723. end;
  1724. end;
  1725. end;
  1726. var
  1727. hp : tnode;
  1728. vl,vl2 : TConstExprInt;
  1729. vr : bestreal;
  1730. begin { simplify }
  1731. result:=nil;
  1732. { handle intern constant functions in separate case }
  1733. if nf_inlineconst in flags then
  1734. begin
  1735. { no parameters? }
  1736. if not assigned(left) then
  1737. internalerror(200501231)
  1738. else
  1739. begin
  1740. vl:=0;
  1741. vl2:=0; { second parameter Ex: ptr(vl,vl2) }
  1742. case left.nodetype of
  1743. realconstn :
  1744. begin
  1745. { Real functions are all handled with internproc below }
  1746. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename)
  1747. end;
  1748. ordconstn :
  1749. vl:=tordconstnode(left).value;
  1750. callparan :
  1751. begin
  1752. { both exists, else it was not generated }
  1753. vl:=tordconstnode(tcallparanode(left).left).value;
  1754. vl2:=tordconstnode(tcallparanode(tcallparanode(left).right).left).value;
  1755. end;
  1756. else
  1757. CGMessage(parser_e_illegal_expression);
  1758. end;
  1759. case inlinenumber of
  1760. in_const_abs :
  1761. if vl.signed then
  1762. hp:=create_simplified_ord_const(abs(vl.svalue),resultdef,forinline)
  1763. else
  1764. hp:=create_simplified_ord_const(vl.uvalue,resultdef,forinline);
  1765. in_const_sqr:
  1766. if vl.signed then
  1767. hp:=create_simplified_ord_const(sqr(vl.svalue),resultdef,forinline)
  1768. else
  1769. hp:=create_simplified_ord_const(sqr(vl.uvalue),resultdef,forinline);
  1770. in_const_odd :
  1771. hp:=cordconstnode.create(qword(odd(int64(vl))),pasbool8type,true);
  1772. in_const_swap_word :
  1773. hp:=cordconstnode.create((vl and $ff) shl 8+(vl shr 8),left.resultdef,true);
  1774. in_const_swap_long :
  1775. hp:=cordconstnode.create((vl and $ffff) shl 16+(vl shr 16),left.resultdef,true);
  1776. in_const_swap_qword :
  1777. hp:=cordconstnode.create((vl and $ffff) shl 32+(vl shr 32),left.resultdef,true);
  1778. in_const_ptr:
  1779. begin
  1780. {Don't construct pointers from negative values.}
  1781. if (vl.signed and (vl.svalue<0)) or (vl2.signed and (vl2.svalue<0)) then
  1782. cgmessage(parser_e_range_check_error);
  1783. hp:=cpointerconstnode.create((vl2.uvalue shl 4)+vl.uvalue,voidfarpointertype);
  1784. end
  1785. else
  1786. internalerror(88);
  1787. end;
  1788. end;
  1789. if hp=nil then
  1790. hp:=cerrornode.create;
  1791. result:=hp;
  1792. end
  1793. else
  1794. begin
  1795. case inlinenumber of
  1796. in_lo_long,
  1797. in_hi_long,
  1798. in_lo_qword,
  1799. in_hi_qword,
  1800. in_lo_word,
  1801. in_hi_word :
  1802. begin
  1803. if left.nodetype=ordconstn then
  1804. begin
  1805. case inlinenumber of
  1806. in_lo_word :
  1807. result:=cordconstnode.create(tordconstnode(left).value and $ff,u8inttype,true);
  1808. in_hi_word :
  1809. result:=cordconstnode.create(tordconstnode(left).value shr 8,u8inttype,true);
  1810. in_lo_long :
  1811. result:=cordconstnode.create(tordconstnode(left).value and $ffff,u16inttype,true);
  1812. in_hi_long :
  1813. result:=cordconstnode.create(tordconstnode(left).value shr 16,u16inttype,true);
  1814. in_lo_qword :
  1815. result:=cordconstnode.create(tordconstnode(left).value and $ffffffff,u32inttype,true);
  1816. in_hi_qword :
  1817. result:=cordconstnode.create(tordconstnode(left).value shr 32,u32inttype,true);
  1818. end;
  1819. end;
  1820. end;
  1821. in_ord_x:
  1822. begin
  1823. case left.resultdef.typ of
  1824. orddef :
  1825. begin
  1826. case torddef(left.resultdef).ordtype of
  1827. pasbool8,
  1828. uchar:
  1829. begin
  1830. { change to byte() }
  1831. result:=ctypeconvnode.create_internal(left,u8inttype);
  1832. left:=nil;
  1833. end;
  1834. pasbool16,
  1835. uwidechar :
  1836. begin
  1837. { change to word() }
  1838. result:=ctypeconvnode.create_internal(left,u16inttype);
  1839. left:=nil;
  1840. end;
  1841. pasbool32 :
  1842. begin
  1843. { change to dword() }
  1844. result:=ctypeconvnode.create_internal(left,u32inttype);
  1845. left:=nil;
  1846. end;
  1847. pasbool64 :
  1848. begin
  1849. { change to qword() }
  1850. result:=ctypeconvnode.create_internal(left,u64inttype);
  1851. left:=nil;
  1852. end;
  1853. bool8bit:
  1854. begin
  1855. { change to shortint() }
  1856. result:=ctypeconvnode.create_internal(left,s8inttype);
  1857. left:=nil;
  1858. end;
  1859. bool16bit :
  1860. begin
  1861. { change to smallint() }
  1862. result:=ctypeconvnode.create_internal(left,s16inttype);
  1863. left:=nil;
  1864. end;
  1865. bool32bit :
  1866. begin
  1867. { change to longint() }
  1868. result:=ctypeconvnode.create_internal(left,s32inttype);
  1869. left:=nil;
  1870. end;
  1871. bool64bit :
  1872. begin
  1873. { change to int64() }
  1874. result:=ctypeconvnode.create_internal(left,s64inttype);
  1875. left:=nil;
  1876. end;
  1877. uvoid :
  1878. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  1879. else
  1880. begin
  1881. { all other orddef need no transformation }
  1882. result:=left;
  1883. left:=nil;
  1884. end;
  1885. end;
  1886. end;
  1887. enumdef :
  1888. begin
  1889. result:=ctypeconvnode.create_internal(left,s32inttype);
  1890. left:=nil;
  1891. end;
  1892. pointerdef :
  1893. begin
  1894. if m_mac in current_settings.modeswitches then
  1895. begin
  1896. result:=ctypeconvnode.create_internal(left,ptruinttype);
  1897. left:=nil;
  1898. end
  1899. end;
  1900. end;
  1901. (*
  1902. if (left.nodetype=ordconstn) then
  1903. begin
  1904. result:=cordconstnode.create(
  1905. tordconstnode(left).value,sinttype,true);
  1906. end
  1907. else if (m_mac in current_settings.modeswitches) and
  1908. (left.ndoetype=pointerconstn) then
  1909. result:=cordconstnode.create(
  1910. tpointerconstnode(left).value,ptruinttype,true);
  1911. *)
  1912. end;
  1913. in_chr_byte:
  1914. begin
  1915. { convert to explicit char() }
  1916. result:=ctypeconvnode.create_internal(left,cansichartype);
  1917. left:=nil;
  1918. end;
  1919. in_length_x:
  1920. begin
  1921. case left.resultdef.typ of
  1922. stringdef :
  1923. begin
  1924. if (left.nodetype=stringconstn) then
  1925. begin
  1926. result:=cordconstnode.create(
  1927. tstringconstnode(left).len,sinttype,true);
  1928. end;
  1929. end;
  1930. orddef :
  1931. begin
  1932. { length of char is always one }
  1933. if is_char(left.resultdef) or
  1934. is_widechar(left.resultdef) then
  1935. begin
  1936. result:=cordconstnode.create(1,sinttype,false);
  1937. end
  1938. end;
  1939. arraydef :
  1940. begin
  1941. if (left.nodetype=stringconstn) then
  1942. begin
  1943. result:=cordconstnode.create(
  1944. tstringconstnode(left).len,sinttype,true);
  1945. end
  1946. else if not is_open_array(left.resultdef) and
  1947. not is_array_of_const(left.resultdef) and
  1948. not is_dynamic_array(left.resultdef) then
  1949. result:=cordconstnode.create(tarraydef(left.resultdef).highrange-
  1950. tarraydef(left.resultdef).lowrange+1,
  1951. sinttype,true);
  1952. end;
  1953. end;
  1954. end;
  1955. in_assigned_x:
  1956. begin
  1957. if is_constnode(tcallparanode(left).left) or
  1958. (tcallparanode(left).left.nodetype = pointerconstn) then
  1959. begin
  1960. { let an add node figure it out }
  1961. result:=caddnode.create(unequaln,tcallparanode(left).left,cnilnode.create);
  1962. tcallparanode(left).left := nil;
  1963. end;
  1964. end;
  1965. in_pred_x,
  1966. in_succ_x:
  1967. begin
  1968. if (left.nodetype=ordconstn) then
  1969. begin
  1970. if (inlinenumber=in_succ_x) then
  1971. vl:=tordconstnode(left).value+1
  1972. else
  1973. vl:=tordconstnode(left).value-1;
  1974. if is_integer(left.resultdef) then
  1975. { the type of the original integer constant is irrelevant,
  1976. it should be automatically adapted to the new value
  1977. (except when inlining) }
  1978. result:=create_simplified_ord_const(vl,resultdef,forinline)
  1979. else
  1980. { check the range for enums, chars, booleans }
  1981. result:=cordconstnode.create(vl,left.resultdef,true)
  1982. end
  1983. end;
  1984. in_low_x,
  1985. in_high_x:
  1986. begin
  1987. case left.resultdef.typ of
  1988. orddef,
  1989. enumdef:
  1990. begin
  1991. result:=do_lowhigh(left.resultdef);
  1992. end;
  1993. setdef:
  1994. begin
  1995. result:=do_lowhigh(tsetdef(left.resultdef).elementdef);
  1996. end;
  1997. arraydef:
  1998. begin
  1999. if (inlinenumber=in_low_x) then
  2000. begin
  2001. result:=cordconstnode.create(int64(tarraydef(
  2002. left.resultdef).lowrange),tarraydef(left.resultdef).rangedef,true);
  2003. end
  2004. else if not is_open_array(left.resultdef) and
  2005. not is_array_of_const(left.resultdef) and
  2006. not is_dynamic_array(left.resultdef) then
  2007. result:=cordconstnode.create(int64(tarraydef(left.resultdef).highrange),
  2008. tarraydef(left.resultdef).rangedef,true);
  2009. end;
  2010. stringdef:
  2011. begin
  2012. if inlinenumber=in_low_x then
  2013. begin
  2014. if is_dynamicstring(left.resultdef) and
  2015. not(cs_zerobasedstrings in current_settings.localswitches) then
  2016. result:=cordconstnode.create(1,u8inttype,false)
  2017. else
  2018. result:=cordconstnode.create(0,u8inttype,false);
  2019. end
  2020. else if not is_dynamicstring(left.resultdef) then
  2021. result:=cordconstnode.create(tstringdef(left.resultdef).len,u8inttype,true)
  2022. end;
  2023. end;
  2024. end;
  2025. in_exp_real :
  2026. begin
  2027. if left.nodetype in [ordconstn,realconstn] then
  2028. begin
  2029. result:=crealconstnode.create(exp(getconstrealvalue),pbestrealtype^);
  2030. if (trealconstnode(result).value_real=MathInf.Value) and
  2031. floating_point_range_check_error then
  2032. begin
  2033. result:=crealconstnode.create(0,pbestrealtype^);
  2034. CGMessage(parser_e_range_check_error);
  2035. end;
  2036. end
  2037. end;
  2038. in_trunc_real :
  2039. begin
  2040. if left.nodetype in [ordconstn,realconstn] then
  2041. begin
  2042. vr:=getconstrealvalue;
  2043. if (vr>=9223372036854775807.99) or (vr<=-9223372036854775808.0) then
  2044. begin
  2045. message3(type_e_range_check_error_bounds,realtostr(vr),'-9223372036854775808.0','9223372036854775807.99..');
  2046. result:=cordconstnode.create(1,s64inttype,false)
  2047. end
  2048. else
  2049. result:=cordconstnode.create(trunc(vr),s64inttype,true)
  2050. end
  2051. end;
  2052. in_round_real :
  2053. begin
  2054. { can't evaluate while inlining, may depend on fpu setting }
  2055. if (not forinline) and
  2056. (left.nodetype in [ordconstn,realconstn]) then
  2057. begin
  2058. vr:=getconstrealvalue;
  2059. if (vr>=9223372036854775807.5) or (vr<=-9223372036854775808.5) then
  2060. begin
  2061. message3(type_e_range_check_error_bounds,realtostr(vr),'-9223372036854775808.49..','9223372036854775807.49..');
  2062. result:=cordconstnode.create(1,s64inttype,false)
  2063. end
  2064. else
  2065. result:=cordconstnode.create(round(vr),s64inttype,true)
  2066. end
  2067. end;
  2068. in_frac_real :
  2069. begin
  2070. if left.nodetype in [ordconstn,realconstn] then
  2071. setconstrealvalue(frac(getconstrealvalue))
  2072. end;
  2073. in_int_real :
  2074. begin
  2075. if left.nodetype in [ordconstn,realconstn] then
  2076. setconstrealvalue(int(getconstrealvalue));
  2077. end;
  2078. in_pi_real :
  2079. begin
  2080. if block_type=bt_const then
  2081. setconstrealvalue(getpi)
  2082. end;
  2083. in_cos_real :
  2084. begin
  2085. if left.nodetype in [ordconstn,realconstn] then
  2086. setconstrealvalue(cos(getconstrealvalue))
  2087. end;
  2088. in_sin_real :
  2089. begin
  2090. if left.nodetype in [ordconstn,realconstn] then
  2091. setconstrealvalue(sin(getconstrealvalue))
  2092. end;
  2093. in_arctan_real :
  2094. begin
  2095. if left.nodetype in [ordconstn,realconstn] then
  2096. setconstrealvalue(arctan(getconstrealvalue))
  2097. end;
  2098. in_abs_real :
  2099. begin
  2100. if left.nodetype in [ordconstn,realconstn] then
  2101. setconstrealvalue(abs(getconstrealvalue))
  2102. end;
  2103. in_abs_long:
  2104. begin
  2105. if left.nodetype=ordconstn then
  2106. begin
  2107. if tordconstnode(left).value<0 then
  2108. result:=cordconstnode.create((-tordconstnode(left).value),s32inttype,false)
  2109. else
  2110. result:=cordconstnode.create((tordconstnode(left).value),s32inttype,false);
  2111. end
  2112. end;
  2113. in_sqr_real :
  2114. begin
  2115. if left.nodetype in [ordconstn,realconstn] then
  2116. setconstrealvalue(sqr(getconstrealvalue))
  2117. end;
  2118. in_sqrt_real :
  2119. begin
  2120. if left.nodetype in [ordconstn,realconstn] then
  2121. begin
  2122. vr:=getconstrealvalue;
  2123. if vr<0.0 then
  2124. result:=handle_sqrt_const(vr)
  2125. else
  2126. setconstrealvalue(sqrt(vr));
  2127. end
  2128. end;
  2129. in_ln_real :
  2130. begin
  2131. if left.nodetype in [ordconstn,realconstn] then
  2132. begin
  2133. vr:=getconstrealvalue;
  2134. if vr<=0.0 then
  2135. result:=handle_ln_const(vr)
  2136. else
  2137. setconstrealvalue(ln(vr));
  2138. end
  2139. end;
  2140. in_assert_x_y :
  2141. begin
  2142. if not(cs_do_assertion in current_settings.localswitches) then
  2143. { we need a valid node, so insert a nothingn }
  2144. result:=cnothingnode.create;
  2145. end;
  2146. in_sar_x,
  2147. in_sar_x_y :
  2148. begin
  2149. result:=handle_const_sar;
  2150. end;
  2151. in_rol_x,
  2152. in_rol_x_y,
  2153. in_ror_x,
  2154. in_ror_x_y :
  2155. result:=handle_const_rox;
  2156. end;
  2157. end;
  2158. end;
  2159. function tinlinenode.pass_typecheck:tnode;
  2160. procedure setfloatresultdef;
  2161. begin
  2162. if (left.resultdef.typ=floatdef) and
  2163. (tfloatdef(left.resultdef).floattype in [s32real,s64real,s80real,sc80real,s128real]) then
  2164. resultdef:=left.resultdef
  2165. else
  2166. begin
  2167. if (left.nodetype <> ordconstn) then
  2168. inserttypeconv(left,pbestrealtype^);
  2169. resultdef:=pbestrealtype^;
  2170. end;
  2171. end;
  2172. procedure handle_pack_unpack;
  2173. var
  2174. source, target, index: tcallparanode;
  2175. unpackedarraydef, packedarraydef: tarraydef;
  2176. tempindex: TConstExprInt;
  2177. begin
  2178. resultdef:=voidtype;
  2179. unpackedarraydef := nil;
  2180. packedarraydef := nil;
  2181. source := tcallparanode(left);
  2182. if (inlinenumber = in_unpack_x_y_z) then
  2183. begin
  2184. target := tcallparanode(source.right);
  2185. index := tcallparanode(target.right);
  2186. { source must be a packed array }
  2187. if not is_packed_array(source.left.resultdef) then
  2188. CGMessagePos2(source.left.fileinfo,type_e_got_expected_packed_array,'1',source.left.resultdef.typename)
  2189. else
  2190. packedarraydef := tarraydef(source.left.resultdef);
  2191. { target can be any kind of array, as long as it's not packed }
  2192. if (target.left.resultdef.typ <> arraydef) or
  2193. is_packed_array(target.left.resultdef) then
  2194. CGMessagePos2(target.left.fileinfo,type_e_got_expected_unpacked_array,'2',target.left.resultdef.typename)
  2195. else
  2196. unpackedarraydef := tarraydef(target.left.resultdef);
  2197. end
  2198. else
  2199. begin
  2200. index := tcallparanode(source.right);
  2201. target := tcallparanode(index.right);
  2202. { source can be any kind of array, as long as it's not packed }
  2203. if (source.left.resultdef.typ <> arraydef) or
  2204. is_packed_array(source.left.resultdef) then
  2205. CGMessagePos2(source.left.fileinfo,type_e_got_expected_unpacked_array,'1',source.left.resultdef.typename)
  2206. else
  2207. unpackedarraydef := tarraydef(source.left.resultdef);
  2208. { target must be a packed array }
  2209. if not is_packed_array(target.left.resultdef) then
  2210. CGMessagePos2(target.left.fileinfo,type_e_got_expected_packed_array,'3',target.left.resultdef.typename)
  2211. else
  2212. packedarraydef := tarraydef(target.left.resultdef);
  2213. end;
  2214. if assigned(unpackedarraydef) then
  2215. begin
  2216. { index must be compatible with the unpacked array's indextype }
  2217. inserttypeconv(index.left,unpackedarraydef.rangedef);
  2218. { range check at compile time if possible }
  2219. if assigned(packedarraydef) and
  2220. (index.left.nodetype = ordconstn) and
  2221. not is_special_array(unpackedarraydef) then
  2222. begin
  2223. testrange(unpackedarraydef,tordconstnode(index.left).value,false,false);
  2224. tempindex := tordconstnode(index.left).value + packedarraydef.highrange-packedarraydef.lowrange;
  2225. testrange(unpackedarraydef,tempindex,false,false);
  2226. end;
  2227. end;
  2228. { source array is read and must be valid }
  2229. set_varstate(source.left,vs_read,[vsf_must_be_valid]);
  2230. { target array is written }
  2231. valid_for_assignment(target.left,true);
  2232. set_varstate(target.left,vs_written,[]);
  2233. { index in the unpacked array is read and must be valid }
  2234. set_varstate(index.left,vs_read,[vsf_must_be_valid]);
  2235. { if the size of the arrays is 0 (array of empty records), }
  2236. { do nothing }
  2237. if (source.resultdef.size = 0) then
  2238. result:=cnothingnode.create;
  2239. end;
  2240. function handle_objc_encode: tnode;
  2241. var
  2242. encodedtype: ansistring;
  2243. errordef: tdef;
  2244. begin
  2245. encodedtype:='';
  2246. if not objctryencodetype(left.resultdef,encodedtype,errordef) then
  2247. Message1(type_e_objc_type_unsupported,errordef.typename);
  2248. result:=cstringconstnode.createpchar(ansistring2pchar(encodedtype),length(encodedtype));
  2249. end;
  2250. var
  2251. hightree,
  2252. hp : tnode;
  2253. begin
  2254. result:=nil;
  2255. { when handling writeln "left" contains no valid address }
  2256. if assigned(left) then
  2257. begin
  2258. if left.nodetype=callparan then
  2259. tcallparanode(left).get_paratype
  2260. else
  2261. typecheckpass(left);
  2262. end;
  2263. if not(nf_inlineconst in flags) then
  2264. begin
  2265. case inlinenumber of
  2266. in_lo_long,
  2267. in_hi_long,
  2268. in_lo_qword,
  2269. in_hi_qword,
  2270. in_lo_word,
  2271. in_hi_word :
  2272. begin
  2273. { give warning for incompatibility with tp and delphi }
  2274. if (inlinenumber in [in_lo_long,in_hi_long,in_lo_qword,in_hi_qword]) and
  2275. ((m_tp7 in current_settings.modeswitches) or
  2276. (m_delphi in current_settings.modeswitches)) then
  2277. CGMessage(type_w_maybe_wrong_hi_lo);
  2278. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2279. if not is_integer(left.resultdef) then
  2280. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename);
  2281. case inlinenumber of
  2282. in_lo_word,
  2283. in_hi_word :
  2284. resultdef:=u8inttype;
  2285. in_lo_long,
  2286. in_hi_long :
  2287. resultdef:=u16inttype;
  2288. in_lo_qword,
  2289. in_hi_qword :
  2290. resultdef:=u32inttype;
  2291. end;
  2292. end;
  2293. in_sizeof_x:
  2294. begin
  2295. { the constant evaluation of in_sizeof_x happens in pexpr where possible }
  2296. set_varstate(left,vs_read,[]);
  2297. if (left.resultdef.typ<>undefineddef) and
  2298. paramanager.push_high_param(vs_value,left.resultdef,current_procinfo.procdef.proccalloption) then
  2299. begin
  2300. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  2301. if assigned(hightree) then
  2302. begin
  2303. hp:=caddnode.create(addn,hightree,
  2304. cordconstnode.create(1,sinttype,false));
  2305. if (left.resultdef.typ=arraydef) then
  2306. if not is_packed_array(tarraydef(left.resultdef)) then
  2307. begin
  2308. if (tarraydef(left.resultdef).elesize<>1) then
  2309. hp:=caddnode.create(muln,hp,cordconstnode.create(tarraydef(
  2310. left.resultdef).elesize,sinttype,true));
  2311. end
  2312. else if (tarraydef(left.resultdef).elepackedbitsize <> 8) then
  2313. begin
  2314. { no packed open array support yet }
  2315. if (hp.nodetype <> ordconstn) then
  2316. internalerror(2006081511);
  2317. hp.free;
  2318. hp := cordconstnode.create(left.resultdef.size,sinttype,true);
  2319. {
  2320. hp:=
  2321. ctypeconvnode.create_explicit(sinttype,
  2322. cmoddivnode.create(divn,
  2323. caddnode.create(addn,
  2324. caddnode.create(muln,hp,cordconstnode.create(tarraydef(
  2325. left.resultdef).elepackedbitsize,s64inttype,true)),
  2326. cordconstnode.create(a,s64inttype,true)),
  2327. cordconstnode.create(8,s64inttype,true)),
  2328. sinttype);
  2329. }
  2330. end;
  2331. result:=hp;
  2332. end;
  2333. end
  2334. else
  2335. resultdef:=sinttype;
  2336. end;
  2337. in_typeof_x:
  2338. begin
  2339. if target_info.system in systems_managed_vm then
  2340. message(parser_e_feature_unsupported_for_vm);
  2341. typecheckpass(left);
  2342. set_varstate(left,vs_read,[]);
  2343. if (left.resultdef.typ=objectdef) and
  2344. not(oo_has_vmt in tobjectdef(left.resultdef).objectoptions) then
  2345. message(type_e_typeof_requires_vmt);
  2346. resultdef:=voidpointertype;
  2347. end;
  2348. in_ord_x:
  2349. begin
  2350. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2351. case left.resultdef.typ of
  2352. orddef,
  2353. enumdef :
  2354. ;
  2355. pointerdef :
  2356. begin
  2357. if not(m_mac in current_settings.modeswitches) then
  2358. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  2359. end
  2360. else
  2361. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  2362. end;
  2363. end;
  2364. in_chr_byte:
  2365. begin
  2366. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2367. end;
  2368. in_length_x:
  2369. begin
  2370. if ((left.resultdef.typ=arraydef) and
  2371. (not is_special_array(left.resultdef) or
  2372. is_open_array(left.resultdef))) or
  2373. (left.resultdef.typ=orddef) then
  2374. set_varstate(left,vs_read,[])
  2375. else
  2376. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2377. case left.resultdef.typ of
  2378. variantdef:
  2379. begin
  2380. inserttypeconv(left,getansistringdef);
  2381. end;
  2382. stringdef :
  2383. begin
  2384. { we don't need string convertions here, }
  2385. { except if from widestring to ansistring }
  2386. { and vice versa (that can change the }
  2387. { length) }
  2388. if (left.nodetype=typeconvn) and
  2389. (ttypeconvnode(left).left.resultdef.typ=stringdef) and
  2390. not(is_wide_or_unicode_string(left.resultdef) xor
  2391. is_wide_or_unicode_string(ttypeconvnode(left).left.resultdef)) then
  2392. begin
  2393. hp:=ttypeconvnode(left).left;
  2394. ttypeconvnode(left).left:=nil;
  2395. left.free;
  2396. left:=hp;
  2397. end;
  2398. end;
  2399. orddef :
  2400. begin
  2401. { will be handled in simplify }
  2402. if not is_char(left.resultdef) and
  2403. not is_widechar(left.resultdef) then
  2404. CGMessage(type_e_mismatch);
  2405. end;
  2406. pointerdef :
  2407. begin
  2408. if is_pchar(left.resultdef) then
  2409. begin
  2410. hp := ccallparanode.create(left,nil);
  2411. result := ccallnode.createintern('fpc_pchar_length',hp);
  2412. { make sure the left node doesn't get disposed, since it's }
  2413. { reused in the new node (JM) }
  2414. left:=nil;
  2415. exit;
  2416. end
  2417. else if is_pwidechar(left.resultdef) then
  2418. begin
  2419. hp := ccallparanode.create(left,nil);
  2420. result := ccallnode.createintern('fpc_pwidechar_length',hp);
  2421. { make sure the left node doesn't get disposed, since it's }
  2422. { reused in the new node (JM) }
  2423. left:=nil;
  2424. exit;
  2425. end
  2426. else
  2427. CGMessage(type_e_mismatch);
  2428. end;
  2429. arraydef :
  2430. begin
  2431. if is_open_array(left.resultdef) or
  2432. is_array_of_const(left.resultdef) then
  2433. begin
  2434. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  2435. if assigned(hightree) then
  2436. result:=caddnode.create(addn,hightree,
  2437. cordconstnode.create(1,sinttype,false));
  2438. exit;
  2439. end
  2440. else if is_dynamic_array(left.resultdef) then
  2441. begin
  2442. hp := ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil);
  2443. result := ccallnode.createintern('fpc_dynarray_length',hp);
  2444. { make sure the left node doesn't get disposed, since it's }
  2445. { reused in the new node (JM) }
  2446. left:=nil;
  2447. exit;
  2448. end
  2449. else
  2450. begin
  2451. { will be handled in simplify }
  2452. end;
  2453. end
  2454. else
  2455. CGMessage(type_e_mismatch);
  2456. end;
  2457. { shortstring return an 8 bit value as the length
  2458. is the first byte of the string }
  2459. if is_shortstring(left.resultdef) then
  2460. resultdef:=u8inttype
  2461. else
  2462. resultdef:=sinttype;
  2463. end;
  2464. in_typeinfo_x:
  2465. begin
  2466. if target_info.system in systems_managed_vm then
  2467. message(parser_e_feature_unsupported_for_vm);
  2468. if (left.resultdef.typ=enumdef) and
  2469. (tenumdef(left.resultdef).has_jumps) then
  2470. CGMessage(type_e_no_type_info);
  2471. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2472. resultdef:=voidpointertype;
  2473. end;
  2474. in_assigned_x:
  2475. begin
  2476. { the parser has already made sure the expression is valid }
  2477. { in case of a complex procvar, only check the "code" pointer }
  2478. if (tcallparanode(left).left.resultdef.typ=procvardef) and
  2479. not tprocvardef(tcallparanode(left).left.resultdef).is_addressonly then
  2480. begin
  2481. inserttypeconv_explicit(tcallparanode(left).left,search_system_type('TMETHOD').typedef);
  2482. tcallparanode(left).left:=csubscriptnode.create(tsym(tabstractrecorddef(tcallparanode(left).left.resultdef).symtable.find('CODE')),tcallparanode(left).left);
  2483. tcallparanode(left).get_paratype;
  2484. end;
  2485. { converting to an add node is tricky because of differences
  2486. in procvar handling between FPC and Delphi handling, so
  2487. handle specially }
  2488. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  2489. resultdef:=pasbool8type;
  2490. end;
  2491. in_ofs_x :
  2492. internalerror(2000101001);
  2493. in_seg_x :
  2494. begin
  2495. if target_info.system in systems_managed_vm then
  2496. message(parser_e_feature_unsupported_for_vm);
  2497. set_varstate(left,vs_read,[]);
  2498. result:=cordconstnode.create(0,s32inttype,false);
  2499. end;
  2500. in_pred_x,
  2501. in_succ_x:
  2502. begin
  2503. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2504. resultdef:=left.resultdef;
  2505. if is_ordinal(resultdef) or is_typeparam(resultdef) then
  2506. begin
  2507. if (resultdef.typ=enumdef) and
  2508. (tenumdef(resultdef).has_jumps) and
  2509. not(m_delphi in current_settings.modeswitches) then
  2510. CGMessage(type_e_succ_and_pred_enums_with_assign_not_possible);
  2511. end
  2512. else
  2513. CGMessage(type_e_ordinal_expr_expected)
  2514. end;
  2515. in_copy_x:
  2516. result:=handle_copy;
  2517. in_initialize_x,
  2518. in_finalize_x:
  2519. begin
  2520. { inlined from pinline }
  2521. internalerror(200204231);
  2522. end;
  2523. in_setlength_x:
  2524. begin
  2525. result:=handle_setlength;
  2526. end;
  2527. in_inc_x,
  2528. in_dec_x:
  2529. begin
  2530. resultdef:=voidtype;
  2531. if assigned(left) then
  2532. begin
  2533. { first param must be var }
  2534. valid_for_var(tcallparanode(left).left,true);
  2535. set_varstate(tcallparanode(left).left,vs_readwritten,[vsf_must_be_valid]);
  2536. if (left.resultdef.typ in [enumdef,pointerdef]) or
  2537. is_ordinal(left.resultdef) or
  2538. is_currency(left.resultdef) then
  2539. begin
  2540. { value of left gets changed -> must be unique }
  2541. set_unique(tcallparanode(left).left);
  2542. { two paras ? }
  2543. if assigned(tcallparanode(left).right) then
  2544. begin
  2545. if is_integer(tcallparanode(left).right.resultdef) then
  2546. begin
  2547. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  2548. { when range/overflow checking is on, we
  2549. convert this to a regular add, and for proper
  2550. checking we need the original type }
  2551. if ([cs_check_range,cs_check_overflow]*current_settings.localswitches=[]) then
  2552. if (tcallparanode(left).left.resultdef.typ=pointerdef) then
  2553. begin
  2554. { don't convert values added to pointers into the pointer types themselves,
  2555. because that will turn signed values into unsigned ones, which then
  2556. goes wrong when they have to be multiplied with the size of the elements
  2557. to which the pointer points in ncginl (mantis #17342) }
  2558. if is_signed(tcallparanode(tcallparanode(left).right).left.resultdef) then
  2559. inserttypeconv(tcallparanode(tcallparanode(left).right).left,ptrsinttype)
  2560. else
  2561. inserttypeconv(tcallparanode(tcallparanode(left).right).left,ptruinttype)
  2562. end
  2563. else if is_integer(tcallparanode(left).left.resultdef) then
  2564. inserttypeconv(tcallparanode(tcallparanode(left).right).left,tcallparanode(left).left.resultdef)
  2565. else
  2566. inserttypeconv_internal(tcallparanode(tcallparanode(left).right).left,tcallparanode(left).left.resultdef);
  2567. if assigned(tcallparanode(tcallparanode(left).right).right) then
  2568. { should be handled in the parser (JM) }
  2569. internalerror(2006020901);
  2570. end
  2571. else
  2572. CGMessagePos(tcallparanode(left).right.fileinfo,type_e_ordinal_expr_expected);
  2573. end;
  2574. end
  2575. { generic type parameter? }
  2576. else if is_typeparam(left.resultdef) then
  2577. begin
  2578. result:=cnothingnode.create;
  2579. exit;
  2580. end
  2581. else
  2582. begin
  2583. hp:=self;
  2584. if isunaryoverloaded(hp) then
  2585. begin
  2586. { inc(rec) and dec(rec) assigns result value to argument }
  2587. result:=cassignmentnode.create(tcallparanode(left).left.getcopy,hp);
  2588. exit;
  2589. end
  2590. else
  2591. CGMessagePos(left.fileinfo,type_e_ordinal_expr_expected);
  2592. end;
  2593. end
  2594. else
  2595. CGMessagePos(fileinfo,type_e_mismatch);
  2596. end;
  2597. in_read_x,
  2598. in_readln_x,
  2599. in_readstr_x,
  2600. in_write_x,
  2601. in_writeln_x,
  2602. in_writestr_x :
  2603. begin
  2604. result := handle_read_write;
  2605. end;
  2606. in_settextbuf_file_x :
  2607. begin
  2608. if target_info.system in systems_managed_vm then
  2609. message(parser_e_feature_unsupported_for_vm);
  2610. resultdef:=voidtype;
  2611. { now we know the type of buffer }
  2612. hp:=ccallparanode.create(cordconstnode.create(
  2613. tcallparanode(left).left.resultdef.size,s32inttype,true),left);
  2614. result:=ccallnode.createintern('SETTEXTBUF',hp);
  2615. left:=nil;
  2616. end;
  2617. { the firstpass of the arg has been done in firstcalln ? }
  2618. in_reset_typedfile,
  2619. in_rewrite_typedfile :
  2620. begin
  2621. result := handle_reset_rewrite_typed;
  2622. end;
  2623. in_str_x_string :
  2624. begin
  2625. result:=handle_str;
  2626. end;
  2627. in_val_x :
  2628. begin
  2629. result:=handle_val;
  2630. end;
  2631. in_include_x_y,
  2632. in_exclude_x_y:
  2633. begin
  2634. resultdef:=voidtype;
  2635. { the parser already checks whether we have two (and exactly two) }
  2636. { parameters (JM) }
  2637. { first param must be var }
  2638. valid_for_var(tcallparanode(left).left,true);
  2639. set_varstate(tcallparanode(left).left,vs_readwritten,[vsf_must_be_valid]);
  2640. { check type }
  2641. if (left.resultdef.typ=setdef) then
  2642. begin
  2643. { insert a type conversion }
  2644. { to the type of the set elements }
  2645. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  2646. inserttypeconv(tcallparanode(tcallparanode(left).right).left,
  2647. tsetdef(left.resultdef).elementdef);
  2648. end
  2649. else
  2650. CGMessage(type_e_mismatch);
  2651. end;
  2652. in_pack_x_y_z,
  2653. in_unpack_x_y_z :
  2654. begin
  2655. handle_pack_unpack;
  2656. end;
  2657. in_slice_x:
  2658. begin
  2659. if target_info.system in systems_managed_vm then
  2660. message(parser_e_feature_unsupported_for_vm);
  2661. result:=nil;
  2662. resultdef:=tcallparanode(left).left.resultdef;
  2663. if (resultdef.typ <> arraydef) then
  2664. CGMessagePos(left.fileinfo,type_e_mismatch)
  2665. else if is_packed_array(resultdef) then
  2666. CGMessagePos2(left.fileinfo,type_e_got_expected_unpacked_array,'1',resultdef.typename);
  2667. if not(is_integer(tcallparanode(tcallparanode(left).right).left.resultdef)) then
  2668. CGMessagePos1(tcallparanode(left).right.fileinfo,
  2669. type_e_integer_expr_expected,
  2670. tcallparanode(tcallparanode(left).right).left.resultdef.typename);
  2671. end;
  2672. in_low_x,
  2673. in_high_x:
  2674. begin
  2675. case left.resultdef.typ of
  2676. orddef,
  2677. enumdef,
  2678. setdef:
  2679. ;
  2680. arraydef:
  2681. begin
  2682. if (inlinenumber=in_low_x) then
  2683. set_varstate(left,vs_read,[])
  2684. else
  2685. begin
  2686. if is_open_array(left.resultdef) or
  2687. is_array_of_const(left.resultdef) then
  2688. begin
  2689. set_varstate(left,vs_read,[]);
  2690. result:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  2691. end
  2692. else
  2693. if is_dynamic_array(left.resultdef) then
  2694. begin
  2695. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2696. { can't use inserttypeconv because we need }
  2697. { an explicit type conversion (JM) }
  2698. hp := ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil);
  2699. result := ccallnode.createintern('fpc_dynarray_high',hp);
  2700. { make sure the left node doesn't get disposed, since it's }
  2701. { reused in the new node (JM) }
  2702. left:=nil;
  2703. end
  2704. else
  2705. begin
  2706. set_varstate(left,vs_read,[]);
  2707. end;
  2708. end;
  2709. end;
  2710. stringdef:
  2711. begin
  2712. if inlinenumber=in_low_x then
  2713. begin
  2714. set_varstate(left,vs_read,[]);
  2715. end
  2716. else
  2717. begin
  2718. if is_open_string(left.resultdef) then
  2719. begin
  2720. set_varstate(left,vs_read,[]);
  2721. result:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry))
  2722. end
  2723. else if is_dynamicstring(left.resultdef) then
  2724. begin
  2725. result:=cinlinenode.create(in_length_x,false,left);
  2726. if cs_zerobasedstrings in current_settings.localswitches then
  2727. result:=caddnode.create(subn,result,cordconstnode.create(1,sinttype,false));
  2728. { make sure the left node doesn't get disposed, since it's }
  2729. { reused in the new node (JM) }
  2730. left:=nil;
  2731. end
  2732. end;
  2733. end;
  2734. else
  2735. CGMessage(type_e_mismatch);
  2736. end;
  2737. end;
  2738. in_exp_real,
  2739. in_frac_real,
  2740. in_int_real,
  2741. in_cos_real,
  2742. in_sin_real,
  2743. in_arctan_real,
  2744. in_abs_real,
  2745. in_ln_real :
  2746. begin
  2747. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2748. { converting an int64 to double on platforms without }
  2749. { extended can cause precision loss }
  2750. if not(left.nodetype in [ordconstn,realconstn]) then
  2751. inserttypeconv(left,pbestrealtype^);
  2752. resultdef:=pbestrealtype^;
  2753. end;
  2754. in_trunc_real,
  2755. in_round_real :
  2756. begin
  2757. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2758. { for direct float rounding, no best real type cast should be necessary }
  2759. if not((left.resultdef.typ=floatdef) and
  2760. (tfloatdef(left.resultdef).floattype in [s32real,s64real,s80real,sc80real,s128real])) and
  2761. { converting an int64 to double on platforms without }
  2762. { extended can cause precision loss }
  2763. not(left.nodetype in [ordconstn,realconstn]) then
  2764. inserttypeconv(left,pbestrealtype^);
  2765. resultdef:=s64inttype;
  2766. end;
  2767. in_pi_real :
  2768. begin
  2769. resultdef:=pbestrealtype^;
  2770. end;
  2771. in_abs_long:
  2772. begin
  2773. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2774. inserttypeconv(left,s32inttype);
  2775. resultdef:=s32inttype;
  2776. end;
  2777. in_sqr_real,
  2778. in_sqrt_real :
  2779. begin
  2780. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2781. setfloatresultdef;
  2782. end;
  2783. {$ifdef SUPPORT_MMX}
  2784. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  2785. begin
  2786. end;
  2787. {$endif SUPPORT_MMX}
  2788. in_aligned_x,
  2789. in_unaligned_x:
  2790. begin
  2791. resultdef:=left.resultdef;
  2792. end;
  2793. in_assert_x_y :
  2794. begin
  2795. resultdef:=voidtype;
  2796. if assigned(left) then
  2797. begin
  2798. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  2799. { check type }
  2800. if is_boolean(left.resultdef) then
  2801. begin
  2802. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  2803. { must always be a string }
  2804. inserttypeconv(tcallparanode(tcallparanode(left).right).left,cshortstringtype);
  2805. end
  2806. else
  2807. CGMessage1(type_e_boolean_expr_expected,left.resultdef.typename);
  2808. end
  2809. else
  2810. CGMessage(type_e_mismatch);
  2811. if (cs_do_assertion in current_settings.localswitches) then
  2812. include(current_procinfo.flags,pi_do_call);
  2813. end;
  2814. in_prefetch_var:
  2815. resultdef:=voidtype;
  2816. in_get_frame,
  2817. in_get_caller_frame,
  2818. in_get_caller_addr:
  2819. begin
  2820. resultdef:=voidpointertype;
  2821. end;
  2822. in_rol_x,
  2823. in_ror_x,
  2824. in_sar_x:
  2825. begin
  2826. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2827. resultdef:=left.resultdef;
  2828. end;
  2829. in_rol_x_y,
  2830. in_ror_x_y,
  2831. in_sar_x_y:
  2832. begin
  2833. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  2834. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  2835. resultdef:=tcallparanode(tcallparanode(left).right).left.resultdef;
  2836. end;
  2837. in_bsf_x,
  2838. in_bsr_x:
  2839. begin
  2840. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2841. if not is_integer(left.resultdef) then
  2842. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename);
  2843. if torddef(left.resultdef).ordtype in [u64bit, s64bit] then
  2844. resultdef:=u64inttype
  2845. else
  2846. resultdef:=u32inttype
  2847. end;
  2848. in_popcnt_x:
  2849. begin
  2850. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2851. if not is_integer(left.resultdef) then
  2852. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename);
  2853. resultdef:=left.resultdef;
  2854. end;
  2855. in_objc_selector_x:
  2856. begin
  2857. result:=cobjcselectornode.create(left);
  2858. { reused }
  2859. left:=nil;
  2860. end;
  2861. in_objc_protocol_x:
  2862. begin
  2863. result:=cobjcprotocolnode.create(left);
  2864. { reused }
  2865. left:=nil;
  2866. end;
  2867. in_objc_encode_x:
  2868. begin
  2869. result:=handle_objc_encode;
  2870. end;
  2871. in_default_x:
  2872. begin
  2873. result:=handle_default;
  2874. end;
  2875. in_box_x:
  2876. begin
  2877. result:=handle_box;
  2878. end;
  2879. in_unbox_x_y:
  2880. begin
  2881. result:=handle_unbox;
  2882. end;
  2883. else
  2884. internalerror(8);
  2885. end;
  2886. end;
  2887. if not assigned(result) and not
  2888. codegenerror then
  2889. result:=simplify(false);
  2890. end;
  2891. function tinlinenode.pass_1 : tnode;
  2892. var
  2893. hp,hpp,resultnode : tnode;
  2894. shiftconst: longint;
  2895. tempnode: ttempcreatenode;
  2896. newstatement: tstatementnode;
  2897. newblock: tblocknode;
  2898. begin
  2899. result:=nil;
  2900. { if we handle writeln; left contains no valid address }
  2901. if assigned(left) then
  2902. begin
  2903. if left.nodetype=callparan then
  2904. tcallparanode(left).firstcallparan
  2905. else
  2906. firstpass(left);
  2907. end;
  2908. { intern const should already be handled }
  2909. if nf_inlineconst in flags then
  2910. internalerror(200104044);
  2911. case inlinenumber of
  2912. in_lo_qword,
  2913. in_hi_qword,
  2914. in_lo_long,
  2915. in_hi_long,
  2916. in_lo_word,
  2917. in_hi_word:
  2918. begin
  2919. shiftconst := 0;
  2920. case inlinenumber of
  2921. in_hi_qword:
  2922. shiftconst := 32;
  2923. in_hi_long:
  2924. shiftconst := 16;
  2925. in_hi_word:
  2926. shiftconst := 8;
  2927. end;
  2928. if shiftconst <> 0 then
  2929. result := ctypeconvnode.create_internal(cshlshrnode.create(shrn,left,
  2930. cordconstnode.create(shiftconst,u32inttype,false)),resultdef)
  2931. else
  2932. result := ctypeconvnode.create_internal(left,resultdef);
  2933. left := nil;
  2934. firstpass(result);
  2935. end;
  2936. in_sizeof_x:
  2937. begin
  2938. expectloc:=LOC_REGISTER;
  2939. end;
  2940. in_typeof_x:
  2941. begin
  2942. expectloc:=LOC_REGISTER;
  2943. end;
  2944. in_length_x:
  2945. begin
  2946. result:=first_length;
  2947. end;
  2948. in_typeinfo_x:
  2949. begin
  2950. expectloc:=LOC_REGISTER;
  2951. end;
  2952. in_assigned_x:
  2953. begin
  2954. result:=first_assigned;
  2955. end;
  2956. in_pred_x,
  2957. in_succ_x:
  2958. begin
  2959. expectloc:=LOC_REGISTER;
  2960. { in case of range/overflow checking, use a regular addnode
  2961. because it's too complex to handle correctly otherwise }
  2962. {$ifndef jvm}
  2963. { enums are class instances in the JVM -> always need conversion }
  2964. if ([cs_check_overflow,cs_check_range]*current_settings.localswitches)<>[] then
  2965. {$endif}
  2966. begin
  2967. { create constant 1 }
  2968. hp:=cordconstnode.create(1,left.resultdef,false);
  2969. typecheckpass(hp);
  2970. if not is_integer(hp.resultdef) then
  2971. inserttypeconv_internal(hp,sinttype);
  2972. { avoid type errors from the addn/subn }
  2973. if not is_integer(left.resultdef) then
  2974. inserttypeconv_internal(left,sinttype);
  2975. { addition/substraction depending on succ/pred }
  2976. if inlinenumber=in_succ_x then
  2977. hp:=caddnode.create(addn,left,hp)
  2978. else
  2979. hp:=caddnode.create(subn,left,hp);
  2980. { assign result of addition }
  2981. if not(is_integer(resultdef)) then
  2982. inserttypeconv(hp,torddef.create(
  2983. {$ifdef cpu64bitaddr}
  2984. s64bit,
  2985. {$else cpu64bitaddr}
  2986. s32bit,
  2987. {$endif cpu64bitaddr}
  2988. get_min_value(resultdef),
  2989. get_max_value(resultdef)))
  2990. else
  2991. inserttypeconv(hp,resultdef);
  2992. { avoid any possible errors/warnings }
  2993. inserttypeconv_internal(hp,resultdef);
  2994. { firstpass it }
  2995. firstpass(hp);
  2996. { left is reused }
  2997. left:=nil;
  2998. { return new node }
  2999. result:=hp;
  3000. end;
  3001. end;
  3002. in_setlength_x:
  3003. result:=first_setlength;
  3004. in_copy_x:
  3005. result:=first_copy;
  3006. in_initialize_x,
  3007. in_finalize_x:
  3008. begin
  3009. expectloc:=LOC_VOID;
  3010. end;
  3011. in_inc_x,
  3012. in_dec_x:
  3013. begin
  3014. expectloc:=LOC_VOID;
  3015. { range/overflow checking doesn't work properly }
  3016. { with the inc/dec code that's generated (JM) }
  3017. if ((current_settings.localswitches * [cs_check_overflow,cs_check_range] <> []) and
  3018. { No overflow check for pointer operations, because inc(pointer,-1) will always
  3019. trigger an overflow. For uint32 it works because then the operation is done
  3020. in 64bit. Range checking is not applicable to pointers either }
  3021. (tcallparanode(left).left.resultdef.typ<>pointerdef))
  3022. {$ifdef jvm}
  3023. { enums are class instances on the JVM -> special treatment }
  3024. or (tcallparanode(left).left.resultdef.typ=enumdef)
  3025. {$endif}
  3026. then
  3027. { convert to simple add (JM) }
  3028. begin
  3029. newblock := internalstatements(newstatement);
  3030. { extra parameter? }
  3031. if assigned(tcallparanode(left).right) then
  3032. begin
  3033. { Yes, use for add node }
  3034. hpp := tcallparanode(tcallparanode(left).right).left;
  3035. tcallparanode(tcallparanode(left).right).left := nil;
  3036. if assigned(tcallparanode(tcallparanode(left).right).right) then
  3037. CGMessage(parser_e_illegal_expression);
  3038. end
  3039. else
  3040. begin
  3041. { no, create constant 1 }
  3042. hpp := cordconstnode.create(1,tcallparanode(left).left.resultdef,false);
  3043. end;
  3044. typecheckpass(hpp);
  3045. { make sure we don't call functions part of the left node twice (and generally }
  3046. { optimize the code generation) }
  3047. if node_complexity(tcallparanode(left).left) > 1 then
  3048. begin
  3049. tempnode := ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  3050. addstatement(newstatement,tempnode);
  3051. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  3052. caddrnode.create_internal(tcallparanode(left).left.getcopy)));
  3053. hp := cderefnode.create(ctemprefnode.create(tempnode));
  3054. inserttypeconv_internal(hp,tcallparanode(left).left.resultdef);
  3055. end
  3056. else
  3057. begin
  3058. hp := tcallparanode(left).left.getcopy;
  3059. tempnode := nil;
  3060. end;
  3061. resultnode := hp.getcopy;
  3062. { avoid type errors from the addn/subn }
  3063. if not is_integer(resultnode.resultdef) then
  3064. begin
  3065. inserttypeconv_internal(hp,sinttype);
  3066. inserttypeconv_internal(hpp,sinttype);
  3067. end;
  3068. { addition/substraction depending on inc/dec }
  3069. if inlinenumber = in_inc_x then
  3070. hpp := caddnode.create(addn,hp,hpp)
  3071. else
  3072. hpp := caddnode.create(subn,hp,hpp);
  3073. { assign result of addition }
  3074. if not(is_integer(resultnode.resultdef)) then
  3075. inserttypeconv(hpp,torddef.create(
  3076. {$ifdef cpu64bitaddr}
  3077. s64bit,
  3078. {$else cpu64bitaddr}
  3079. s32bit,
  3080. {$endif cpu64bitaddr}
  3081. get_min_value(resultnode.resultdef),
  3082. get_max_value(resultnode.resultdef)))
  3083. else
  3084. inserttypeconv(hpp,resultnode.resultdef);
  3085. { avoid any possible warnings }
  3086. inserttypeconv_internal(hpp,resultnode.resultdef);
  3087. addstatement(newstatement,cassignmentnode.create(resultnode,hpp));
  3088. { deallocate the temp }
  3089. if assigned(tempnode) then
  3090. addstatement(newstatement,ctempdeletenode.create(tempnode));
  3091. { firstpass it }
  3092. firstpass(tnode(newblock));
  3093. { return new node }
  3094. result := newblock;
  3095. end;
  3096. end;
  3097. in_include_x_y,
  3098. in_exclude_x_y:
  3099. begin
  3100. result:=first_IncludeExclude;
  3101. end;
  3102. in_pack_x_y_z,
  3103. in_unpack_x_y_z:
  3104. begin
  3105. result:=first_pack_unpack;
  3106. end;
  3107. in_exp_real:
  3108. begin
  3109. result:= first_exp_real;
  3110. end;
  3111. in_round_real:
  3112. begin
  3113. result:= first_round_real;
  3114. end;
  3115. in_trunc_real:
  3116. begin
  3117. result:= first_trunc_real;
  3118. end;
  3119. in_int_real:
  3120. begin
  3121. result:= first_int_real;
  3122. end;
  3123. in_frac_real:
  3124. begin
  3125. result:= first_frac_real;
  3126. end;
  3127. in_cos_real:
  3128. begin
  3129. result:= first_cos_real;
  3130. end;
  3131. in_sin_real:
  3132. begin
  3133. result := first_sin_real;
  3134. end;
  3135. in_arctan_real:
  3136. begin
  3137. result := first_arctan_real;
  3138. end;
  3139. in_pi_real :
  3140. begin
  3141. result := first_pi;
  3142. end;
  3143. in_abs_real:
  3144. begin
  3145. result := first_abs_real;
  3146. end;
  3147. in_abs_long:
  3148. begin
  3149. result := first_abs_long;
  3150. end;
  3151. in_sqr_real:
  3152. begin
  3153. result := first_sqr_real;
  3154. end;
  3155. in_sqrt_real:
  3156. begin
  3157. result := first_sqrt_real;
  3158. end;
  3159. in_ln_real:
  3160. begin
  3161. result := first_ln_real;
  3162. end;
  3163. {$ifdef SUPPORT_MMX}
  3164. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  3165. begin
  3166. end;
  3167. {$endif SUPPORT_MMX}
  3168. in_assert_x_y :
  3169. begin
  3170. result:=first_assert;
  3171. end;
  3172. in_low_x,
  3173. in_high_x:
  3174. internalerror(200104047);
  3175. in_slice_x:
  3176. internalerror(2005101501);
  3177. in_ord_x,
  3178. in_chr_byte:
  3179. begin
  3180. { should not happend as it's converted to typeconv }
  3181. internalerror(200104045);
  3182. end;
  3183. in_ofs_x :
  3184. internalerror(2000101001);
  3185. in_seg_x :
  3186. internalerror(200104046);
  3187. in_settextbuf_file_x,
  3188. in_reset_typedfile,
  3189. in_rewrite_typedfile,
  3190. in_str_x_string,
  3191. in_val_x,
  3192. in_read_x,
  3193. in_readln_x,
  3194. in_write_x,
  3195. in_writeln_x :
  3196. begin
  3197. { should be handled by pass_typecheck }
  3198. internalerror(200108234);
  3199. end;
  3200. in_get_frame:
  3201. begin
  3202. result:=first_get_frame;
  3203. end;
  3204. in_get_caller_frame:
  3205. begin
  3206. expectloc:=LOC_REGISTER;
  3207. end;
  3208. in_get_caller_addr:
  3209. begin
  3210. expectloc:=LOC_REGISTER;
  3211. end;
  3212. in_prefetch_var:
  3213. begin
  3214. expectloc:=LOC_VOID;
  3215. end;
  3216. in_aligned_x,
  3217. in_unaligned_x:
  3218. begin
  3219. expectloc:=tcallparanode(left).left.expectloc;
  3220. end;
  3221. in_rol_x,
  3222. in_rol_x_y,
  3223. in_ror_x,
  3224. in_ror_x_y,
  3225. in_sar_x,
  3226. in_sar_x_y,
  3227. in_bsf_x,
  3228. in_bsr_x:
  3229. expectloc:=LOC_REGISTER;
  3230. in_popcnt_x:
  3231. result:=first_popcnt;
  3232. in_new_x:
  3233. result:=first_new;
  3234. in_box_x:
  3235. result:=first_box;
  3236. in_unbox_x_y:
  3237. result:=first_unbox;
  3238. else
  3239. internalerror(89);
  3240. end;
  3241. end;
  3242. {$maxfpuregisters default}
  3243. function tinlinenode.docompare(p: tnode): boolean;
  3244. begin
  3245. docompare :=
  3246. inherited docompare(p) and
  3247. (inlinenumber = tinlinenode(p).inlinenumber);
  3248. end;
  3249. function tinlinenode.first_pi : tnode;
  3250. begin
  3251. result:=crealconstnode.create(getpi,pbestrealtype^);
  3252. end;
  3253. function tinlinenode.first_arctan_real : tnode;
  3254. begin
  3255. { create the call to the helper }
  3256. { on entry left node contains the parameter }
  3257. first_arctan_real := ccallnode.createintern('fpc_arctan_real',
  3258. ccallparanode.create(left,nil));
  3259. left := nil;
  3260. end;
  3261. function tinlinenode.first_abs_real : tnode;
  3262. begin
  3263. { create the call to the helper }
  3264. { on entry left node contains the parameter }
  3265. first_abs_real := ccallnode.createintern('fpc_abs_real',
  3266. ccallparanode.create(left,nil));
  3267. left := nil;
  3268. end;
  3269. function tinlinenode.first_sqr_real : tnode;
  3270. begin
  3271. {$ifndef cpufpemu}
  3272. { this procedure might be only used for cpus definining cpufpemu else
  3273. the optimizer might go into an endless loop when doing x*x -> changes }
  3274. internalerror(2011092401);
  3275. {$endif cpufpemu}
  3276. { create the call to the helper }
  3277. { on entry left node contains the parameter }
  3278. first_sqr_real := ctypeconvnode.create(ccallnode.createintern('fpc_sqr_real',
  3279. ccallparanode.create(left,nil)),resultdef);
  3280. left := nil;
  3281. end;
  3282. function tinlinenode.first_sqrt_real : tnode;
  3283. begin
  3284. { create the call to the helper }
  3285. { on entry left node contains the parameter }
  3286. first_sqrt_real := ctypeconvnode.create(ccallnode.createintern('fpc_sqrt_real',
  3287. ccallparanode.create(left,nil)),resultdef);
  3288. left := nil;
  3289. end;
  3290. function tinlinenode.first_ln_real : tnode;
  3291. begin
  3292. { create the call to the helper }
  3293. { on entry left node contains the parameter }
  3294. first_ln_real := ccallnode.createintern('fpc_ln_real',
  3295. ccallparanode.create(left,nil));
  3296. left := nil;
  3297. end;
  3298. function tinlinenode.first_cos_real : tnode;
  3299. begin
  3300. { create the call to the helper }
  3301. { on entry left node contains the parameter }
  3302. first_cos_real := ccallnode.createintern('fpc_cos_real',
  3303. ccallparanode.create(left,nil));
  3304. left := nil;
  3305. end;
  3306. function tinlinenode.first_sin_real : tnode;
  3307. begin
  3308. { create the call to the helper }
  3309. { on entry left node contains the parameter }
  3310. first_sin_real := ccallnode.createintern('fpc_sin_real',
  3311. ccallparanode.create(left,nil));
  3312. left := nil;
  3313. end;
  3314. function tinlinenode.first_exp_real : tnode;
  3315. begin
  3316. { create the call to the helper }
  3317. { on entry left node contains the parameter }
  3318. result := ccallnode.createintern('fpc_exp_real',ccallparanode.create(left,nil));
  3319. left := nil;
  3320. end;
  3321. function tinlinenode.first_int_real : tnode;
  3322. begin
  3323. { create the call to the helper }
  3324. { on entry left node contains the parameter }
  3325. result := ccallnode.createintern('fpc_int_real',ccallparanode.create(left,nil));
  3326. left := nil;
  3327. end;
  3328. function tinlinenode.first_frac_real : tnode;
  3329. begin
  3330. { create the call to the helper }
  3331. { on entry left node contains the parameter }
  3332. result := ccallnode.createintern('fpc_frac_real',ccallparanode.create(left,nil));
  3333. left := nil;
  3334. end;
  3335. function tinlinenode.first_round_real : tnode;
  3336. begin
  3337. { create the call to the helper }
  3338. { on entry left node contains the parameter }
  3339. result := ccallnode.createintern('fpc_round_real',ccallparanode.create(left,nil));
  3340. left := nil;
  3341. end;
  3342. function tinlinenode.first_trunc_real : tnode;
  3343. begin
  3344. { create the call to the helper }
  3345. { on entry left node contains the parameter }
  3346. result := ccallnode.createintern('fpc_trunc_real',ccallparanode.create(left,nil));
  3347. left := nil;
  3348. end;
  3349. function tinlinenode.first_abs_long : tnode;
  3350. begin
  3351. expectloc:=LOC_REGISTER;
  3352. result:=nil;
  3353. end;
  3354. function tinlinenode.first_IncludeExclude: tnode;
  3355. begin
  3356. result:=nil;
  3357. expectloc:=LOC_VOID;
  3358. end;
  3359. function tinlinenode.first_get_frame: tnode;
  3360. begin
  3361. include(current_procinfo.flags,pi_needs_stackframe);
  3362. expectloc:=LOC_CREGISTER;
  3363. result:=nil;
  3364. end;
  3365. function tinlinenode.first_setlength: tnode;
  3366. var
  3367. paras : tnode;
  3368. npara,
  3369. ppn : tcallparanode;
  3370. dims,
  3371. counter : integer;
  3372. isarray : boolean;
  3373. destppn : tnode;
  3374. newstatement : tstatementnode;
  3375. temp : ttempcreatenode;
  3376. newblock : tnode;
  3377. begin
  3378. paras:=left;
  3379. ppn:=tcallparanode(paras);
  3380. dims:=0;
  3381. while assigned(ppn.right) do
  3382. begin
  3383. inc(dims);
  3384. ppn:=tcallparanode(ppn.right);
  3385. end;
  3386. destppn:=ppn.left;
  3387. isarray:=is_dynamic_array(destppn.resultdef);
  3388. { first param must be a string or dynamic array ...}
  3389. if isarray then
  3390. begin
  3391. { create statements with call initialize the arguments and
  3392. call fpc_dynarr_setlength }
  3393. newblock:=internalstatements(newstatement);
  3394. { get temp for array of lengths }
  3395. temp:=ctempcreatenode.create(sinttype,dims*sinttype.size,tt_persistent,false);
  3396. addstatement(newstatement,temp);
  3397. { load array of lengths }
  3398. ppn:=tcallparanode(paras);
  3399. counter:=dims-1;
  3400. while assigned(ppn.right) do
  3401. begin
  3402. addstatement(newstatement,cassignmentnode.create(
  3403. ctemprefnode.create_offset(temp,counter*sinttype.size),
  3404. ppn.left));
  3405. ppn.left:=nil;
  3406. dec(counter);
  3407. ppn:=tcallparanode(ppn.right);
  3408. end;
  3409. { destppn is also reused }
  3410. ppn.left:=nil;
  3411. { create call to fpc_dynarr_setlength }
  3412. npara:=ccallparanode.create(caddrnode.create_internal
  3413. (ctemprefnode.create(temp)),
  3414. ccallparanode.create(cordconstnode.create
  3415. (dims,sinttype,true),
  3416. ccallparanode.create(caddrnode.create_internal
  3417. (crttinode.create(tstoreddef(destppn.resultdef),initrtti,rdt_normal)),
  3418. ccallparanode.create(ctypeconvnode.create_internal(destppn,voidpointertype),nil))));
  3419. addstatement(newstatement,ccallnode.createintern('fpc_dynarray_setlength',npara));
  3420. addstatement(newstatement,ctempdeletenode.create(temp));
  3421. end
  3422. else if is_ansistring(destppn.resultdef) then
  3423. begin
  3424. newblock:=ccallnode.createintern(
  3425. 'fpc_'+tstringdef(destppn.resultdef).stringtypname+'_setlength',
  3426. ccallparanode.create(
  3427. cordconstnode.create(getparaencoding(destppn.resultdef),u16inttype,true),
  3428. paras
  3429. )
  3430. );
  3431. { we reused the parameters, make sure we don't release them }
  3432. left:=nil;
  3433. end
  3434. else
  3435. begin
  3436. { we can reuse the supplied parameters }
  3437. newblock:=ccallnode.createintern(
  3438. 'fpc_'+tstringdef(destppn.resultdef).stringtypname+'_setlength',paras);
  3439. { we reused the parameters, make sure we don't release them }
  3440. left:=nil;
  3441. end;
  3442. result:=newblock;
  3443. end;
  3444. function tinlinenode.first_copy: tnode;
  3445. var
  3446. lowppn,
  3447. highppn,
  3448. npara,
  3449. paras : tnode;
  3450. ppn : tcallparanode;
  3451. paradef : tdef;
  3452. counter : integer;
  3453. begin
  3454. { determine copy function to use based on the first argument,
  3455. also count the number of arguments in this loop }
  3456. counter:=1;
  3457. paras:=left;
  3458. ppn:=tcallparanode(paras);
  3459. while assigned(ppn.right) do
  3460. begin
  3461. inc(counter);
  3462. ppn:=tcallparanode(ppn.right);
  3463. end;
  3464. paradef:=ppn.left.resultdef;
  3465. { fill up third parameter }
  3466. if counter=2 then
  3467. begin
  3468. paras:=ccallparanode.create(cordconstnode.create(torddef(sinttype).high,sinttype,false),paras);
  3469. counter:=3;
  3470. end;
  3471. if is_ansistring(resultdef) then
  3472. { keep the specific kind of ansistringdef as result }
  3473. result:=ccallnode.createinternres('fpc_ansistr_copy',paras,resultdef)
  3474. else if is_widestring(resultdef) then
  3475. result:=ccallnode.createintern('fpc_widestr_copy',paras)
  3476. else if is_unicodestring(resultdef) then
  3477. result:=ccallnode.createintern('fpc_unicodestr_copy',paras)
  3478. { can't check for resultdef = cansichartype, because resultdef=
  3479. cshortstringtype here }
  3480. else if is_char(paradef) then
  3481. result:=ccallnode.createintern('fpc_char_copy',paras)
  3482. else if is_dynamic_array(resultdef) then
  3483. begin
  3484. { create statements with call }
  3485. case counter of
  3486. 1:
  3487. begin
  3488. { copy the whole array using [0..high(sizeint)] range }
  3489. highppn:=cordconstnode.create(torddef(sinttype).high,sinttype,false);
  3490. lowppn:=cordconstnode.create(0,sinttype,false);
  3491. end;
  3492. 3:
  3493. begin
  3494. highppn:=tcallparanode(paras).left.getcopy;
  3495. lowppn:=tcallparanode(tcallparanode(paras).right).left.getcopy;
  3496. end;
  3497. else
  3498. internalerror(2012100701);
  3499. end;
  3500. { create call to fpc_dynarray_copy }
  3501. npara:=ccallparanode.create(highppn,
  3502. ccallparanode.create(lowppn,
  3503. ccallparanode.create(caddrnode.create_internal
  3504. (crttinode.create(tstoreddef(paradef),initrtti,rdt_normal)),
  3505. ccallparanode.create
  3506. (ctypeconvnode.create_internal(ppn.left,voidpointertype),nil))));
  3507. result:=ccallnode.createinternres('fpc_dynarray_copy',npara,paradef);
  3508. ppn.left:=nil;
  3509. paras.free;
  3510. end
  3511. else
  3512. result:=ccallnode.createintern('fpc_shortstr_copy',paras);
  3513. { parameters are reused }
  3514. left:=nil;
  3515. end;
  3516. function tinlinenode.first_new: tnode;
  3517. begin
  3518. internalerror(2011012201);
  3519. result:=nil;
  3520. end;
  3521. function tinlinenode.first_length: tnode;
  3522. begin
  3523. result:=nil;
  3524. if is_shortstring(left.resultdef) then
  3525. expectloc:=left.expectloc
  3526. else
  3527. begin
  3528. { ansi/wide string }
  3529. expectloc:=LOC_REGISTER;
  3530. end;
  3531. end;
  3532. function tinlinenode.first_assigned: tnode;
  3533. begin
  3534. result:=nil;
  3535. expectloc := LOC_JUMP;
  3536. end;
  3537. function tinlinenode.first_assert: tnode;
  3538. var
  3539. paras: tcallparanode;
  3540. begin
  3541. paras:=tcallparanode(tcallparanode(left).right);
  3542. paras:=ccallparanode.create(cstringconstnode.createstr(current_module.sourcefiles.get_file_name(current_filepos.fileindex)),paras);
  3543. paras:=ccallparanode.create(genintconstnode(fileinfo.line),paras);
  3544. {$if defined(x86) or defined(arm) or defined(jvm)}
  3545. paras:=ccallparanode.create(geninlinenode(in_get_frame,false,nil),paras);
  3546. {$else}
  3547. paras:=ccallparanode.create(ccallnode.createinternfromunit('SYSTEM','GET_FRAME',nil),paras);
  3548. {$endif}
  3549. result:=cifnode.create(cnotnode.create(tcallparanode(left).left),
  3550. ccallnode.createintern('fpc_assert',paras),nil);
  3551. tcallparanode(left).left:=nil;
  3552. tcallparanode(left).right:=nil;
  3553. end;
  3554. function tinlinenode.first_popcnt: tnode;
  3555. var
  3556. suffix : string;
  3557. begin
  3558. case torddef(left.resultdef).ordtype of
  3559. u8bit: suffix:='byte';
  3560. u16bit: suffix:='word';
  3561. u32bit: suffix:='dword';
  3562. u64bit: suffix:='qword';
  3563. else
  3564. internalerror(2012082601);
  3565. end;
  3566. result:=ccallnode.createintern('fpc_popcnt_'+suffix,ccallparanode.create(left,nil));
  3567. left:=nil;
  3568. end;
  3569. function tinlinenode.handle_box: tnode;
  3570. begin
  3571. result:=nil;
  3572. if not assigned(left) or
  3573. assigned(tcallparanode(left).right) then
  3574. CGMessage1(parser_e_wrong_parameter_size,'FpcInternalBox');
  3575. resultdef:=class_tobject;
  3576. end;
  3577. function tinlinenode.handle_unbox: tnode;
  3578. begin
  3579. result:=nil;
  3580. if not assigned(left) or
  3581. not assigned(tcallparanode(left).right) or
  3582. assigned(tcallparanode(tcallparanode(left).right).right) then
  3583. CGMessage1(parser_e_wrong_parameter_size,'FpcInternalUnBox');
  3584. if tcallparanode(left).left.nodetype<>typen then
  3585. internalerror(2011071701);
  3586. ttypenode(tcallparanode(left).left).allowed:=true;
  3587. resultdef:=tcallparanode(left).left.resultdef;
  3588. end;
  3589. function tinlinenode.first_pack_unpack: tnode;
  3590. var
  3591. loopstatement : tstatementnode;
  3592. loop : tblocknode;
  3593. loopvar : ttempcreatenode;
  3594. tempnode,
  3595. source,
  3596. target,
  3597. index,
  3598. unpackednode,
  3599. packednode,
  3600. sourcevecindex,
  3601. targetvecindex,
  3602. loopbody : tnode;
  3603. temprangedef : tdef;
  3604. ulorange,
  3605. uhirange,
  3606. plorange,
  3607. phirange : TConstExprInt;
  3608. begin
  3609. { transform into a for loop which assigns the data of the (un)packed }
  3610. { array to the other one }
  3611. source := left;
  3612. if (inlinenumber = in_unpack_x_y_z) then
  3613. begin
  3614. target := tcallparanode(source).right;
  3615. index := tcallparanode(target).right;
  3616. packednode := tcallparanode(source).left;
  3617. unpackednode := tcallparanode(target).left;
  3618. end
  3619. else
  3620. begin
  3621. index := tcallparanode(source).right;
  3622. target := tcallparanode(index).right;
  3623. packednode := tcallparanode(target).left;
  3624. unpackednode := tcallparanode(source).left;
  3625. end;
  3626. source := tcallparanode(source).left;
  3627. target := tcallparanode(target).left;
  3628. index := tcallparanode(index).left;
  3629. loop := internalstatements(loopstatement);
  3630. loopvar := ctempcreatenode.create(
  3631. tarraydef(packednode.resultdef).rangedef,
  3632. tarraydef(packednode.resultdef).rangedef.size,
  3633. tt_persistent,true);
  3634. addstatement(loopstatement,loopvar);
  3635. { For range checking: we have to convert to an integer type (in case the index type }
  3636. { is an enum), add the index and loop variable together, convert the result }
  3637. { implicitly to an orddef with range equal to the rangedef to get range checking }
  3638. { and finally convert it explicitly back to the actual rangedef to avoid type }
  3639. { errors }
  3640. temprangedef:=nil;
  3641. getrange(unpackednode.resultdef,ulorange,uhirange);
  3642. getrange(packednode.resultdef,plorange,phirange);
  3643. temprangedef:=torddef.create(torddef(sinttype).ordtype,ulorange,uhirange);
  3644. sourcevecindex := ctemprefnode.create(loopvar);
  3645. targetvecindex := ctypeconvnode.create_internal(index.getcopy,sinttype);
  3646. targetvecindex := caddnode.create(subn,targetvecindex,cordconstnode.create(plorange,sinttype,true));
  3647. targetvecindex := caddnode.create(addn,targetvecindex,ctemprefnode.create(loopvar));
  3648. targetvecindex := ctypeconvnode.create(targetvecindex,temprangedef);
  3649. targetvecindex := ctypeconvnode.create_explicit(targetvecindex,tarraydef(unpackednode.resultdef).rangedef);
  3650. if (inlinenumber = in_pack_x_y_z) then
  3651. begin
  3652. { swap source and target vec indices }
  3653. tempnode := sourcevecindex;
  3654. sourcevecindex := targetvecindex;
  3655. targetvecindex := tempnode;
  3656. end;
  3657. { create the assignment in the loop body }
  3658. loopbody :=
  3659. cassignmentnode.create(
  3660. cvecnode.create(target.getcopy,targetvecindex),
  3661. cvecnode.create(source.getcopy,sourcevecindex)
  3662. );
  3663. { create the actual for loop }
  3664. tempnode := cfornode.create(
  3665. ctemprefnode.create(loopvar),
  3666. cinlinenode.create(in_low_x,false,packednode.getcopy),
  3667. cinlinenode.create(in_high_x,false,packednode.getcopy),
  3668. loopbody,
  3669. false);
  3670. addstatement(loopstatement,tempnode);
  3671. { free the loop counter }
  3672. addstatement(loopstatement,ctempdeletenode.create(loopvar));
  3673. result := loop;
  3674. end;
  3675. end.