ninl.pas 152 KB

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