ninl.pas 152 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936
  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. typecheckpass(left);
  2341. set_varstate(left,vs_read,[]);
  2342. if (left.resultdef.typ=objectdef) and
  2343. not(oo_has_vmt in tobjectdef(left.resultdef).objectoptions) then
  2344. message(type_e_typeof_requires_vmt);
  2345. resultdef:=voidpointertype;
  2346. end;
  2347. in_ord_x:
  2348. begin
  2349. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2350. case left.resultdef.typ of
  2351. orddef,
  2352. enumdef :
  2353. ;
  2354. pointerdef :
  2355. begin
  2356. if not(m_mac in current_settings.modeswitches) then
  2357. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  2358. end
  2359. else
  2360. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  2361. end;
  2362. end;
  2363. in_chr_byte:
  2364. begin
  2365. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2366. end;
  2367. in_length_x:
  2368. begin
  2369. if ((left.resultdef.typ=arraydef) and
  2370. (not is_special_array(left.resultdef) or
  2371. is_open_array(left.resultdef))) or
  2372. (left.resultdef.typ=orddef) then
  2373. set_varstate(left,vs_read,[])
  2374. else
  2375. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2376. case left.resultdef.typ of
  2377. variantdef:
  2378. begin
  2379. inserttypeconv(left,getansistringdef);
  2380. end;
  2381. stringdef :
  2382. begin
  2383. { we don't need string convertions here, }
  2384. { except if from widestring to ansistring }
  2385. { and vice versa (that can change the }
  2386. { length) }
  2387. if (left.nodetype=typeconvn) and
  2388. (ttypeconvnode(left).left.resultdef.typ=stringdef) and
  2389. not(is_wide_or_unicode_string(left.resultdef) xor
  2390. is_wide_or_unicode_string(ttypeconvnode(left).left.resultdef)) then
  2391. begin
  2392. hp:=ttypeconvnode(left).left;
  2393. ttypeconvnode(left).left:=nil;
  2394. left.free;
  2395. left:=hp;
  2396. end;
  2397. end;
  2398. orddef :
  2399. begin
  2400. { will be handled in simplify }
  2401. if not is_char(left.resultdef) and
  2402. not is_widechar(left.resultdef) then
  2403. CGMessage(type_e_mismatch);
  2404. end;
  2405. pointerdef :
  2406. begin
  2407. if is_pchar(left.resultdef) then
  2408. begin
  2409. hp := ccallparanode.create(left,nil);
  2410. result := ccallnode.createintern('fpc_pchar_length',hp);
  2411. { make sure the left node doesn't get disposed, since it's }
  2412. { reused in the new node (JM) }
  2413. left:=nil;
  2414. exit;
  2415. end
  2416. else if is_pwidechar(left.resultdef) then
  2417. begin
  2418. hp := ccallparanode.create(left,nil);
  2419. result := ccallnode.createintern('fpc_pwidechar_length',hp);
  2420. { make sure the left node doesn't get disposed, since it's }
  2421. { reused in the new node (JM) }
  2422. left:=nil;
  2423. exit;
  2424. end
  2425. else
  2426. CGMessage(type_e_mismatch);
  2427. end;
  2428. arraydef :
  2429. begin
  2430. if is_open_array(left.resultdef) or
  2431. is_array_of_const(left.resultdef) then
  2432. begin
  2433. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  2434. if assigned(hightree) then
  2435. result:=caddnode.create(addn,hightree,
  2436. cordconstnode.create(1,sinttype,false));
  2437. exit;
  2438. end
  2439. else if is_dynamic_array(left.resultdef) then
  2440. begin
  2441. hp := ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil);
  2442. result := ccallnode.createintern('fpc_dynarray_length',hp);
  2443. { make sure the left node doesn't get disposed, since it's }
  2444. { reused in the new node (JM) }
  2445. left:=nil;
  2446. exit;
  2447. end
  2448. else
  2449. begin
  2450. { will be handled in simplify }
  2451. end;
  2452. end
  2453. else
  2454. CGMessage(type_e_mismatch);
  2455. end;
  2456. { shortstring return an 8 bit value as the length
  2457. is the first byte of the string }
  2458. if is_shortstring(left.resultdef) then
  2459. resultdef:=u8inttype
  2460. else
  2461. resultdef:=sinttype;
  2462. end;
  2463. in_typeinfo_x:
  2464. begin
  2465. if target_info.system in systems_managed_vm then
  2466. message(parser_e_feature_unsupported_for_vm);
  2467. if (left.resultdef.typ=enumdef) and
  2468. (tenumdef(left.resultdef).has_jumps) then
  2469. CGMessage(type_e_no_type_info);
  2470. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2471. resultdef:=voidpointertype;
  2472. end;
  2473. in_assigned_x:
  2474. begin
  2475. { the parser has already made sure the expression is valid }
  2476. { in case of a complex procvar, only check the "code" pointer }
  2477. if (tcallparanode(left).left.resultdef.typ=procvardef) and
  2478. not tprocvardef(tcallparanode(left).left.resultdef).is_addressonly then
  2479. begin
  2480. inserttypeconv_explicit(tcallparanode(left).left,search_system_type('TMETHOD').typedef);
  2481. tcallparanode(left).left:=csubscriptnode.create(tsym(tabstractrecorddef(tcallparanode(left).left.resultdef).symtable.find('CODE')),tcallparanode(left).left);
  2482. tcallparanode(left).get_paratype;
  2483. end;
  2484. { converting to an add node is tricky because of differences
  2485. in procvar handling between FPC and Delphi handling, so
  2486. handle specially }
  2487. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  2488. resultdef:=pasbool8type;
  2489. end;
  2490. in_ofs_x :
  2491. internalerror(2000101001);
  2492. in_seg_x :
  2493. begin
  2494. if target_info.system in systems_managed_vm then
  2495. message(parser_e_feature_unsupported_for_vm);
  2496. set_varstate(left,vs_read,[]);
  2497. result:=cordconstnode.create(0,s32inttype,false);
  2498. end;
  2499. in_pred_x,
  2500. in_succ_x:
  2501. begin
  2502. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2503. resultdef:=left.resultdef;
  2504. if not is_ordinal(resultdef) then
  2505. CGMessage(type_e_ordinal_expr_expected)
  2506. else
  2507. begin
  2508. if (resultdef.typ=enumdef) and
  2509. (tenumdef(resultdef).has_jumps) and
  2510. not(m_delphi in current_settings.modeswitches) then
  2511. CGMessage(type_e_succ_and_pred_enums_with_assign_not_possible);
  2512. end;
  2513. end;
  2514. in_copy_x:
  2515. result:=handle_copy;
  2516. in_initialize_x,
  2517. in_finalize_x:
  2518. begin
  2519. { inlined from pinline }
  2520. internalerror(200204231);
  2521. end;
  2522. in_setlength_x:
  2523. begin
  2524. result:=handle_setlength;
  2525. end;
  2526. in_inc_x,
  2527. in_dec_x:
  2528. begin
  2529. resultdef:=voidtype;
  2530. if assigned(left) then
  2531. begin
  2532. { first param must be var }
  2533. valid_for_var(tcallparanode(left).left,true);
  2534. set_varstate(tcallparanode(left).left,vs_readwritten,[vsf_must_be_valid]);
  2535. if (left.resultdef.typ in [enumdef,pointerdef]) or
  2536. is_ordinal(left.resultdef) or
  2537. is_currency(left.resultdef) then
  2538. begin
  2539. { value of left gets changed -> must be unique }
  2540. set_unique(tcallparanode(left).left);
  2541. { two paras ? }
  2542. if assigned(tcallparanode(left).right) then
  2543. begin
  2544. if is_integer(tcallparanode(left).right.resultdef) then
  2545. begin
  2546. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  2547. { when range/overflow checking is on, we
  2548. convert this to a regular add, and for proper
  2549. checking we need the original type }
  2550. if ([cs_check_range,cs_check_overflow]*current_settings.localswitches=[]) then
  2551. if (tcallparanode(left).left.resultdef.typ=pointerdef) then
  2552. begin
  2553. { don't convert values added to pointers into the pointer types themselves,
  2554. because that will turn signed values into unsigned ones, which then
  2555. goes wrong when they have to be multiplied with the size of the elements
  2556. to which the pointer points in ncginl (mantis #17342) }
  2557. if is_signed(tcallparanode(tcallparanode(left).right).left.resultdef) then
  2558. inserttypeconv(tcallparanode(tcallparanode(left).right).left,ptrsinttype)
  2559. else
  2560. inserttypeconv(tcallparanode(tcallparanode(left).right).left,ptruinttype)
  2561. end
  2562. else if is_integer(tcallparanode(left).left.resultdef) then
  2563. inserttypeconv(tcallparanode(tcallparanode(left).right).left,tcallparanode(left).left.resultdef)
  2564. else
  2565. inserttypeconv_internal(tcallparanode(tcallparanode(left).right).left,tcallparanode(left).left.resultdef);
  2566. if assigned(tcallparanode(tcallparanode(left).right).right) then
  2567. { should be handled in the parser (JM) }
  2568. internalerror(2006020901);
  2569. end
  2570. else
  2571. CGMessagePos(tcallparanode(left).right.fileinfo,type_e_ordinal_expr_expected);
  2572. end;
  2573. end
  2574. else
  2575. begin
  2576. hp:=self;
  2577. if isunaryoverloaded(hp) then
  2578. begin
  2579. { inc(rec) and dec(rec) assigns result value to argument }
  2580. result:=cassignmentnode.create(tcallparanode(left).left.getcopy,hp);
  2581. exit;
  2582. end
  2583. else
  2584. CGMessagePos(left.fileinfo,type_e_ordinal_expr_expected);
  2585. end;
  2586. end
  2587. else
  2588. CGMessagePos(fileinfo,type_e_mismatch);
  2589. end;
  2590. in_read_x,
  2591. in_readln_x,
  2592. in_readstr_x,
  2593. in_write_x,
  2594. in_writeln_x,
  2595. in_writestr_x :
  2596. begin
  2597. result := handle_read_write;
  2598. end;
  2599. in_settextbuf_file_x :
  2600. begin
  2601. if target_info.system in systems_managed_vm then
  2602. message(parser_e_feature_unsupported_for_vm);
  2603. resultdef:=voidtype;
  2604. { now we know the type of buffer }
  2605. hp:=ccallparanode.create(cordconstnode.create(
  2606. tcallparanode(left).left.resultdef.size,s32inttype,true),left);
  2607. result:=ccallnode.createintern('SETTEXTBUF',hp);
  2608. left:=nil;
  2609. end;
  2610. { the firstpass of the arg has been done in firstcalln ? }
  2611. in_reset_typedfile,
  2612. in_rewrite_typedfile :
  2613. begin
  2614. result := handle_reset_rewrite_typed;
  2615. end;
  2616. in_str_x_string :
  2617. begin
  2618. result:=handle_str;
  2619. end;
  2620. in_val_x :
  2621. begin
  2622. result:=handle_val;
  2623. end;
  2624. in_include_x_y,
  2625. in_exclude_x_y:
  2626. begin
  2627. resultdef:=voidtype;
  2628. { the parser already checks whether we have two (and exactly two) }
  2629. { parameters (JM) }
  2630. { first param must be var }
  2631. valid_for_var(tcallparanode(left).left,true);
  2632. set_varstate(tcallparanode(left).left,vs_readwritten,[vsf_must_be_valid]);
  2633. { check type }
  2634. if (left.resultdef.typ=setdef) then
  2635. begin
  2636. { insert a type conversion }
  2637. { to the type of the set elements }
  2638. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  2639. inserttypeconv(tcallparanode(tcallparanode(left).right).left,
  2640. tsetdef(left.resultdef).elementdef);
  2641. end
  2642. else
  2643. CGMessage(type_e_mismatch);
  2644. end;
  2645. in_pack_x_y_z,
  2646. in_unpack_x_y_z :
  2647. begin
  2648. handle_pack_unpack;
  2649. end;
  2650. in_slice_x:
  2651. begin
  2652. if target_info.system in systems_managed_vm then
  2653. message(parser_e_feature_unsupported_for_vm);
  2654. result:=nil;
  2655. resultdef:=tcallparanode(left).left.resultdef;
  2656. if (resultdef.typ <> arraydef) then
  2657. CGMessagePos(left.fileinfo,type_e_mismatch)
  2658. else if is_packed_array(resultdef) then
  2659. CGMessagePos2(left.fileinfo,type_e_got_expected_unpacked_array,'1',resultdef.typename);
  2660. if not(is_integer(tcallparanode(tcallparanode(left).right).left.resultdef)) then
  2661. CGMessagePos1(tcallparanode(left).right.fileinfo,
  2662. type_e_integer_expr_expected,
  2663. tcallparanode(tcallparanode(left).right).left.resultdef.typename);
  2664. end;
  2665. in_low_x,
  2666. in_high_x:
  2667. begin
  2668. case left.resultdef.typ of
  2669. orddef,
  2670. enumdef,
  2671. setdef:
  2672. ;
  2673. arraydef:
  2674. begin
  2675. if (inlinenumber=in_low_x) then
  2676. set_varstate(left,vs_read,[])
  2677. else
  2678. begin
  2679. if is_open_array(left.resultdef) or
  2680. is_array_of_const(left.resultdef) then
  2681. begin
  2682. set_varstate(left,vs_read,[]);
  2683. result:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  2684. end
  2685. else
  2686. if is_dynamic_array(left.resultdef) then
  2687. begin
  2688. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2689. { can't use inserttypeconv because we need }
  2690. { an explicit type conversion (JM) }
  2691. hp := ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil);
  2692. result := ccallnode.createintern('fpc_dynarray_high',hp);
  2693. { make sure the left node doesn't get disposed, since it's }
  2694. { reused in the new node (JM) }
  2695. left:=nil;
  2696. end
  2697. else
  2698. begin
  2699. set_varstate(left,vs_read,[]);
  2700. end;
  2701. end;
  2702. end;
  2703. stringdef:
  2704. begin
  2705. if inlinenumber=in_low_x then
  2706. begin
  2707. set_varstate(left,vs_read,[]);
  2708. end
  2709. else
  2710. begin
  2711. if is_open_string(left.resultdef) then
  2712. begin
  2713. set_varstate(left,vs_read,[]);
  2714. result:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry))
  2715. end
  2716. else if is_ansistring(left.resultdef) or
  2717. is_wide_or_unicode_string(left.resultdef) then
  2718. CGMessage(type_e_mismatch)
  2719. end;
  2720. end;
  2721. else
  2722. CGMessage(type_e_mismatch);
  2723. end;
  2724. end;
  2725. in_exp_real,
  2726. in_frac_real,
  2727. in_int_real,
  2728. in_cos_real,
  2729. in_sin_real,
  2730. in_arctan_real,
  2731. in_abs_real,
  2732. in_ln_real :
  2733. begin
  2734. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2735. { converting an int64 to double on platforms without }
  2736. { extended can cause precision loss }
  2737. if not(left.nodetype in [ordconstn,realconstn]) then
  2738. inserttypeconv(left,pbestrealtype^);
  2739. resultdef:=pbestrealtype^;
  2740. end;
  2741. in_trunc_real,
  2742. in_round_real :
  2743. begin
  2744. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2745. { for direct float rounding, no best real type cast should be necessary }
  2746. if not((left.resultdef.typ=floatdef) and
  2747. (tfloatdef(left.resultdef).floattype in [s32real,s64real,s80real,sc80real,s128real])) and
  2748. { converting an int64 to double on platforms without }
  2749. { extended can cause precision loss }
  2750. not(left.nodetype in [ordconstn,realconstn]) then
  2751. inserttypeconv(left,pbestrealtype^);
  2752. resultdef:=s64inttype;
  2753. end;
  2754. in_pi_real :
  2755. begin
  2756. resultdef:=pbestrealtype^;
  2757. end;
  2758. in_abs_long:
  2759. begin
  2760. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2761. inserttypeconv(left,s32inttype);
  2762. resultdef:=s32inttype;
  2763. end;
  2764. in_sqr_real,
  2765. in_sqrt_real :
  2766. begin
  2767. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2768. setfloatresultdef;
  2769. end;
  2770. {$ifdef SUPPORT_MMX}
  2771. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  2772. begin
  2773. end;
  2774. {$endif SUPPORT_MMX}
  2775. in_unaligned_x:
  2776. begin
  2777. resultdef:=left.resultdef;
  2778. end;
  2779. in_assert_x_y :
  2780. begin
  2781. resultdef:=voidtype;
  2782. if assigned(left) then
  2783. begin
  2784. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  2785. { check type }
  2786. if is_boolean(left.resultdef) then
  2787. begin
  2788. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  2789. { must always be a string }
  2790. inserttypeconv(tcallparanode(tcallparanode(left).right).left,cshortstringtype);
  2791. end
  2792. else
  2793. CGMessage1(type_e_boolean_expr_expected,left.resultdef.typename);
  2794. end
  2795. else
  2796. CGMessage(type_e_mismatch);
  2797. if (cs_do_assertion in current_settings.localswitches) then
  2798. include(current_procinfo.flags,pi_do_call);
  2799. end;
  2800. in_prefetch_var:
  2801. resultdef:=voidtype;
  2802. in_get_frame,
  2803. in_get_caller_frame,
  2804. in_get_caller_addr:
  2805. begin
  2806. resultdef:=voidpointertype;
  2807. end;
  2808. in_rol_x,
  2809. in_ror_x,
  2810. in_sar_x:
  2811. begin
  2812. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2813. resultdef:=left.resultdef;
  2814. end;
  2815. in_rol_x_y,
  2816. in_ror_x_y,
  2817. in_sar_x_y:
  2818. begin
  2819. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  2820. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  2821. resultdef:=tcallparanode(tcallparanode(left).right).left.resultdef;
  2822. end;
  2823. in_bsf_x,
  2824. in_bsr_x:
  2825. begin
  2826. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2827. if not is_integer(left.resultdef) then
  2828. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename);
  2829. if torddef(left.resultdef).ordtype in [u64bit, s64bit] then
  2830. resultdef:=u64inttype
  2831. else
  2832. resultdef:=u32inttype
  2833. end;
  2834. in_objc_selector_x:
  2835. begin
  2836. result:=cobjcselectornode.create(left);
  2837. { reused }
  2838. left:=nil;
  2839. end;
  2840. in_objc_protocol_x:
  2841. begin
  2842. result:=cobjcprotocolnode.create(left);
  2843. { reused }
  2844. left:=nil;
  2845. end;
  2846. in_objc_encode_x:
  2847. begin
  2848. result:=handle_objc_encode;
  2849. end;
  2850. in_default_x:
  2851. begin
  2852. result:=handle_default;
  2853. end;
  2854. in_box_x:
  2855. begin
  2856. result:=handle_box;
  2857. end;
  2858. in_unbox_x_y:
  2859. begin
  2860. result:=handle_unbox;
  2861. end;
  2862. else
  2863. internalerror(8);
  2864. end;
  2865. end;
  2866. if not assigned(result) and not
  2867. codegenerror then
  2868. result:=simplify(false);
  2869. end;
  2870. function tinlinenode.pass_1 : tnode;
  2871. var
  2872. hp,hpp,resultnode : tnode;
  2873. shiftconst: longint;
  2874. tempnode: ttempcreatenode;
  2875. newstatement: tstatementnode;
  2876. newblock: tblocknode;
  2877. begin
  2878. result:=nil;
  2879. { if we handle writeln; left contains no valid address }
  2880. if assigned(left) then
  2881. begin
  2882. if left.nodetype=callparan then
  2883. tcallparanode(left).firstcallparan
  2884. else
  2885. firstpass(left);
  2886. end;
  2887. { intern const should already be handled }
  2888. if nf_inlineconst in flags then
  2889. internalerror(200104044);
  2890. case inlinenumber of
  2891. in_lo_qword,
  2892. in_hi_qword,
  2893. in_lo_long,
  2894. in_hi_long,
  2895. in_lo_word,
  2896. in_hi_word:
  2897. begin
  2898. shiftconst := 0;
  2899. case inlinenumber of
  2900. in_hi_qword:
  2901. shiftconst := 32;
  2902. in_hi_long:
  2903. shiftconst := 16;
  2904. in_hi_word:
  2905. shiftconst := 8;
  2906. end;
  2907. if shiftconst <> 0 then
  2908. result := ctypeconvnode.create_internal(cshlshrnode.create(shrn,left,
  2909. cordconstnode.create(shiftconst,u32inttype,false)),resultdef)
  2910. else
  2911. result := ctypeconvnode.create_internal(left,resultdef);
  2912. left := nil;
  2913. firstpass(result);
  2914. end;
  2915. in_sizeof_x:
  2916. begin
  2917. expectloc:=LOC_REGISTER;
  2918. end;
  2919. in_typeof_x:
  2920. begin
  2921. expectloc:=LOC_REGISTER;
  2922. end;
  2923. in_length_x:
  2924. begin
  2925. result:=first_length;
  2926. end;
  2927. in_typeinfo_x:
  2928. begin
  2929. expectloc:=LOC_REGISTER;
  2930. end;
  2931. in_assigned_x:
  2932. begin
  2933. result:=first_assigned;
  2934. end;
  2935. in_pred_x,
  2936. in_succ_x:
  2937. begin
  2938. expectloc:=LOC_REGISTER;
  2939. { in case of range/overflow checking, use a regular addnode
  2940. because it's too complex to handle correctly otherwise }
  2941. {$ifndef jvm}
  2942. { enums are class instances in the JVM -> always need conversion }
  2943. if ([cs_check_overflow,cs_check_range]*current_settings.localswitches)<>[] then
  2944. {$endif}
  2945. begin
  2946. { create constant 1 }
  2947. hp:=cordconstnode.create(1,left.resultdef,false);
  2948. typecheckpass(hp);
  2949. if not is_integer(hp.resultdef) then
  2950. inserttypeconv_internal(hp,sinttype);
  2951. { avoid type errors from the addn/subn }
  2952. if not is_integer(left.resultdef) then
  2953. inserttypeconv_internal(left,sinttype);
  2954. { addition/substraction depending on succ/pred }
  2955. if inlinenumber=in_succ_x then
  2956. hp:=caddnode.create(addn,left,hp)
  2957. else
  2958. hp:=caddnode.create(subn,left,hp);
  2959. { assign result of addition }
  2960. if not(is_integer(resultdef)) then
  2961. inserttypeconv(hp,torddef.create(
  2962. {$ifdef cpu64bitaddr}
  2963. s64bit,
  2964. {$else cpu64bitaddr}
  2965. s32bit,
  2966. {$endif cpu64bitaddr}
  2967. get_min_value(resultdef),
  2968. get_max_value(resultdef)))
  2969. else
  2970. inserttypeconv(hp,resultdef);
  2971. { avoid any possible errors/warnings }
  2972. inserttypeconv_internal(hp,resultdef);
  2973. { firstpass it }
  2974. firstpass(hp);
  2975. { left is reused }
  2976. left:=nil;
  2977. { return new node }
  2978. result:=hp;
  2979. end;
  2980. end;
  2981. in_setlength_x:
  2982. result:=first_setlength;
  2983. in_copy_x:
  2984. result:=first_copy;
  2985. in_initialize_x,
  2986. in_finalize_x:
  2987. begin
  2988. expectloc:=LOC_VOID;
  2989. end;
  2990. in_inc_x,
  2991. in_dec_x:
  2992. begin
  2993. expectloc:=LOC_VOID;
  2994. { range/overflow checking doesn't work properly }
  2995. { with the inc/dec code that's generated (JM) }
  2996. if ((current_settings.localswitches * [cs_check_overflow,cs_check_range] <> []) and
  2997. { No overflow check for pointer operations, because inc(pointer,-1) will always
  2998. trigger an overflow. For uint32 it works because then the operation is done
  2999. in 64bit. Range checking is not applicable to pointers either }
  3000. (tcallparanode(left).left.resultdef.typ<>pointerdef))
  3001. {$ifdef jvm}
  3002. { enums are class instances on the JVM -> special treatment }
  3003. or (tcallparanode(left).left.resultdef.typ=enumdef)
  3004. {$endif}
  3005. then
  3006. { convert to simple add (JM) }
  3007. begin
  3008. newblock := internalstatements(newstatement);
  3009. { extra parameter? }
  3010. if assigned(tcallparanode(left).right) then
  3011. begin
  3012. { Yes, use for add node }
  3013. hpp := tcallparanode(tcallparanode(left).right).left;
  3014. tcallparanode(tcallparanode(left).right).left := nil;
  3015. if assigned(tcallparanode(tcallparanode(left).right).right) then
  3016. CGMessage(parser_e_illegal_expression);
  3017. end
  3018. else
  3019. begin
  3020. { no, create constant 1 }
  3021. hpp := cordconstnode.create(1,tcallparanode(left).left.resultdef,false);
  3022. end;
  3023. typecheckpass(hpp);
  3024. { make sure we don't call functions part of the left node twice (and generally }
  3025. { optimize the code generation) }
  3026. if node_complexity(tcallparanode(left).left) > 1 then
  3027. begin
  3028. tempnode := ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  3029. addstatement(newstatement,tempnode);
  3030. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  3031. caddrnode.create_internal(tcallparanode(left).left.getcopy)));
  3032. hp := cderefnode.create(ctemprefnode.create(tempnode));
  3033. inserttypeconv_internal(hp,tcallparanode(left).left.resultdef);
  3034. end
  3035. else
  3036. begin
  3037. hp := tcallparanode(left).left.getcopy;
  3038. tempnode := nil;
  3039. end;
  3040. resultnode := hp.getcopy;
  3041. { avoid type errors from the addn/subn }
  3042. if not is_integer(resultnode.resultdef) then
  3043. begin
  3044. inserttypeconv_internal(hp,sinttype);
  3045. inserttypeconv_internal(hpp,sinttype);
  3046. end;
  3047. { addition/substraction depending on inc/dec }
  3048. if inlinenumber = in_inc_x then
  3049. hpp := caddnode.create(addn,hp,hpp)
  3050. else
  3051. hpp := caddnode.create(subn,hp,hpp);
  3052. { assign result of addition }
  3053. if not(is_integer(resultnode.resultdef)) then
  3054. inserttypeconv(hpp,torddef.create(
  3055. {$ifdef cpu64bitaddr}
  3056. s64bit,
  3057. {$else cpu64bitaddr}
  3058. s32bit,
  3059. {$endif cpu64bitaddr}
  3060. get_min_value(resultnode.resultdef),
  3061. get_max_value(resultnode.resultdef)))
  3062. else
  3063. inserttypeconv(hpp,resultnode.resultdef);
  3064. { avoid any possible warnings }
  3065. inserttypeconv_internal(hpp,resultnode.resultdef);
  3066. addstatement(newstatement,cassignmentnode.create(resultnode,hpp));
  3067. { deallocate the temp }
  3068. if assigned(tempnode) then
  3069. addstatement(newstatement,ctempdeletenode.create(tempnode));
  3070. { firstpass it }
  3071. firstpass(tnode(newblock));
  3072. { return new node }
  3073. result := newblock;
  3074. end;
  3075. end;
  3076. in_include_x_y,
  3077. in_exclude_x_y:
  3078. begin
  3079. result:=first_IncludeExclude;
  3080. end;
  3081. in_pack_x_y_z,
  3082. in_unpack_x_y_z:
  3083. begin
  3084. result:=first_pack_unpack;
  3085. end;
  3086. in_exp_real:
  3087. begin
  3088. result:= first_exp_real;
  3089. end;
  3090. in_round_real:
  3091. begin
  3092. result:= first_round_real;
  3093. end;
  3094. in_trunc_real:
  3095. begin
  3096. result:= first_trunc_real;
  3097. end;
  3098. in_int_real:
  3099. begin
  3100. result:= first_int_real;
  3101. end;
  3102. in_frac_real:
  3103. begin
  3104. result:= first_frac_real;
  3105. end;
  3106. in_cos_real:
  3107. begin
  3108. result:= first_cos_real;
  3109. end;
  3110. in_sin_real:
  3111. begin
  3112. result := first_sin_real;
  3113. end;
  3114. in_arctan_real:
  3115. begin
  3116. result := first_arctan_real;
  3117. end;
  3118. in_pi_real :
  3119. begin
  3120. result := first_pi;
  3121. end;
  3122. in_abs_real:
  3123. begin
  3124. result := first_abs_real;
  3125. end;
  3126. in_abs_long:
  3127. begin
  3128. result := first_abs_long;
  3129. end;
  3130. in_sqr_real:
  3131. begin
  3132. result := first_sqr_real;
  3133. end;
  3134. in_sqrt_real:
  3135. begin
  3136. result := first_sqrt_real;
  3137. end;
  3138. in_ln_real:
  3139. begin
  3140. result := first_ln_real;
  3141. end;
  3142. {$ifdef SUPPORT_MMX}
  3143. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  3144. begin
  3145. end;
  3146. {$endif SUPPORT_MMX}
  3147. in_assert_x_y :
  3148. begin
  3149. result:=first_assert;
  3150. end;
  3151. in_low_x,
  3152. in_high_x:
  3153. internalerror(200104047);
  3154. in_slice_x:
  3155. internalerror(2005101501);
  3156. in_ord_x,
  3157. in_chr_byte:
  3158. begin
  3159. { should not happend as it's converted to typeconv }
  3160. internalerror(200104045);
  3161. end;
  3162. in_ofs_x :
  3163. internalerror(2000101001);
  3164. in_seg_x :
  3165. internalerror(200104046);
  3166. in_settextbuf_file_x,
  3167. in_reset_typedfile,
  3168. in_rewrite_typedfile,
  3169. in_str_x_string,
  3170. in_val_x,
  3171. in_read_x,
  3172. in_readln_x,
  3173. in_write_x,
  3174. in_writeln_x :
  3175. begin
  3176. { should be handled by pass_typecheck }
  3177. internalerror(200108234);
  3178. end;
  3179. in_get_frame:
  3180. begin
  3181. result:=first_get_frame;
  3182. end;
  3183. in_get_caller_frame:
  3184. begin
  3185. expectloc:=LOC_REGISTER;
  3186. end;
  3187. in_get_caller_addr:
  3188. begin
  3189. expectloc:=LOC_REGISTER;
  3190. end;
  3191. in_prefetch_var:
  3192. begin
  3193. expectloc:=LOC_VOID;
  3194. end;
  3195. in_unaligned_x:
  3196. begin
  3197. expectloc:=tcallparanode(left).left.expectloc;
  3198. end;
  3199. in_rol_x,
  3200. in_rol_x_y,
  3201. in_ror_x,
  3202. in_ror_x_y,
  3203. in_sar_x,
  3204. in_sar_x_y,
  3205. in_bsf_x,
  3206. in_bsr_x:
  3207. expectloc:=LOC_REGISTER;
  3208. in_new_x:
  3209. result:=first_new;
  3210. in_box_x:
  3211. result:=first_box;
  3212. in_unbox_x_y:
  3213. result:=first_unbox;
  3214. else
  3215. internalerror(89);
  3216. end;
  3217. end;
  3218. {$maxfpuregisters default}
  3219. function tinlinenode.docompare(p: tnode): boolean;
  3220. begin
  3221. docompare :=
  3222. inherited docompare(p) and
  3223. (inlinenumber = tinlinenode(p).inlinenumber);
  3224. end;
  3225. function tinlinenode.first_pi : tnode;
  3226. begin
  3227. result:=crealconstnode.create(getpi,pbestrealtype^);
  3228. end;
  3229. function tinlinenode.first_arctan_real : tnode;
  3230. begin
  3231. { create the call to the helper }
  3232. { on entry left node contains the parameter }
  3233. first_arctan_real := ccallnode.createintern('fpc_arctan_real',
  3234. ccallparanode.create(left,nil));
  3235. left := nil;
  3236. end;
  3237. function tinlinenode.first_abs_real : tnode;
  3238. begin
  3239. { create the call to the helper }
  3240. { on entry left node contains the parameter }
  3241. first_abs_real := ccallnode.createintern('fpc_abs_real',
  3242. ccallparanode.create(left,nil));
  3243. left := nil;
  3244. end;
  3245. function tinlinenode.first_sqr_real : tnode;
  3246. begin
  3247. {$ifndef cpufpemu}
  3248. { this procedure might be only used for cpus definining cpufpemu else
  3249. the optimizer might go into an endless loop when doing x*x -> changes }
  3250. internalerror(2011092401);
  3251. {$endif cpufpemu}
  3252. { create the call to the helper }
  3253. { on entry left node contains the parameter }
  3254. first_sqr_real := ctypeconvnode.create(ccallnode.createintern('fpc_sqr_real',
  3255. ccallparanode.create(left,nil)),resultdef);
  3256. left := nil;
  3257. end;
  3258. function tinlinenode.first_sqrt_real : tnode;
  3259. begin
  3260. { create the call to the helper }
  3261. { on entry left node contains the parameter }
  3262. first_sqrt_real := ctypeconvnode.create(ccallnode.createintern('fpc_sqrt_real',
  3263. ccallparanode.create(left,nil)),resultdef);
  3264. left := nil;
  3265. end;
  3266. function tinlinenode.first_ln_real : tnode;
  3267. begin
  3268. { create the call to the helper }
  3269. { on entry left node contains the parameter }
  3270. first_ln_real := ccallnode.createintern('fpc_ln_real',
  3271. ccallparanode.create(left,nil));
  3272. left := nil;
  3273. end;
  3274. function tinlinenode.first_cos_real : tnode;
  3275. begin
  3276. { create the call to the helper }
  3277. { on entry left node contains the parameter }
  3278. first_cos_real := ccallnode.createintern('fpc_cos_real',
  3279. ccallparanode.create(left,nil));
  3280. left := nil;
  3281. end;
  3282. function tinlinenode.first_sin_real : tnode;
  3283. begin
  3284. { create the call to the helper }
  3285. { on entry left node contains the parameter }
  3286. first_sin_real := ccallnode.createintern('fpc_sin_real',
  3287. ccallparanode.create(left,nil));
  3288. left := nil;
  3289. end;
  3290. function tinlinenode.first_exp_real : tnode;
  3291. begin
  3292. { create the call to the helper }
  3293. { on entry left node contains the parameter }
  3294. result := ccallnode.createintern('fpc_exp_real',ccallparanode.create(left,nil));
  3295. left := nil;
  3296. end;
  3297. function tinlinenode.first_int_real : tnode;
  3298. begin
  3299. { create the call to the helper }
  3300. { on entry left node contains the parameter }
  3301. result := ccallnode.createintern('fpc_int_real',ccallparanode.create(left,nil));
  3302. left := nil;
  3303. end;
  3304. function tinlinenode.first_frac_real : tnode;
  3305. begin
  3306. { create the call to the helper }
  3307. { on entry left node contains the parameter }
  3308. result := ccallnode.createintern('fpc_frac_real',ccallparanode.create(left,nil));
  3309. left := nil;
  3310. end;
  3311. function tinlinenode.first_round_real : tnode;
  3312. begin
  3313. { create the call to the helper }
  3314. { on entry left node contains the parameter }
  3315. result := ccallnode.createintern('fpc_round_real',ccallparanode.create(left,nil));
  3316. left := nil;
  3317. end;
  3318. function tinlinenode.first_trunc_real : tnode;
  3319. begin
  3320. { create the call to the helper }
  3321. { on entry left node contains the parameter }
  3322. result := ccallnode.createintern('fpc_trunc_real',ccallparanode.create(left,nil));
  3323. left := nil;
  3324. end;
  3325. function tinlinenode.first_abs_long : tnode;
  3326. begin
  3327. expectloc:=LOC_REGISTER;
  3328. result:=nil;
  3329. end;
  3330. function tinlinenode.first_IncludeExclude: tnode;
  3331. begin
  3332. result:=nil;
  3333. expectloc:=LOC_VOID;
  3334. end;
  3335. function tinlinenode.first_get_frame: tnode;
  3336. begin
  3337. include(current_procinfo.flags,pi_needs_stackframe);
  3338. expectloc:=LOC_CREGISTER;
  3339. result:=nil;
  3340. end;
  3341. function tinlinenode.first_setlength: tnode;
  3342. var
  3343. paras : tnode;
  3344. npara,
  3345. ppn : tcallparanode;
  3346. dims,
  3347. counter : integer;
  3348. isarray : boolean;
  3349. destppn : tnode;
  3350. newstatement : tstatementnode;
  3351. temp : ttempcreatenode;
  3352. newblock : tnode;
  3353. begin
  3354. paras:=left;
  3355. ppn:=tcallparanode(paras);
  3356. dims:=0;
  3357. while assigned(ppn.right) do
  3358. begin
  3359. inc(dims);
  3360. ppn:=tcallparanode(ppn.right);
  3361. end;
  3362. destppn:=ppn.left;
  3363. isarray:=is_dynamic_array(destppn.resultdef);
  3364. { first param must be a string or dynamic array ...}
  3365. if isarray then
  3366. begin
  3367. { create statements with call initialize the arguments and
  3368. call fpc_dynarr_setlength }
  3369. newblock:=internalstatements(newstatement);
  3370. { get temp for array of lengths }
  3371. temp:=ctempcreatenode.create(sinttype,dims*sinttype.size,tt_persistent,false);
  3372. addstatement(newstatement,temp);
  3373. { load array of lengths }
  3374. ppn:=tcallparanode(paras);
  3375. counter:=dims-1;
  3376. while assigned(ppn.right) do
  3377. begin
  3378. addstatement(newstatement,cassignmentnode.create(
  3379. ctemprefnode.create_offset(temp,counter*sinttype.size),
  3380. ppn.left));
  3381. ppn.left:=nil;
  3382. dec(counter);
  3383. ppn:=tcallparanode(ppn.right);
  3384. end;
  3385. { destppn is also reused }
  3386. ppn.left:=nil;
  3387. { create call to fpc_dynarr_setlength }
  3388. npara:=ccallparanode.create(caddrnode.create_internal
  3389. (ctemprefnode.create(temp)),
  3390. ccallparanode.create(cordconstnode.create
  3391. (dims,sinttype,true),
  3392. ccallparanode.create(caddrnode.create_internal
  3393. (crttinode.create(tstoreddef(destppn.resultdef),initrtti,rdt_normal)),
  3394. ccallparanode.create(ctypeconvnode.create_internal(destppn,voidpointertype),nil))));
  3395. addstatement(newstatement,ccallnode.createintern('fpc_dynarray_setlength',npara));
  3396. addstatement(newstatement,ctempdeletenode.create(temp));
  3397. end
  3398. else if is_ansistring(destppn.resultdef) then
  3399. begin
  3400. newblock:=ccallnode.createintern(
  3401. 'fpc_'+tstringdef(destppn.resultdef).stringtypname+'_setlength',
  3402. ccallparanode.create(
  3403. cordconstnode.create(getparaencoding(destppn.resultdef),u16inttype,true),
  3404. paras
  3405. )
  3406. );
  3407. { we reused the parameters, make sure we don't release them }
  3408. left:=nil;
  3409. end
  3410. else
  3411. begin
  3412. { we can reuse the supplied parameters }
  3413. newblock:=ccallnode.createintern(
  3414. 'fpc_'+tstringdef(destppn.resultdef).stringtypname+'_setlength',paras);
  3415. { we reused the parameters, make sure we don't release them }
  3416. left:=nil;
  3417. end;
  3418. result:=newblock;
  3419. end;
  3420. function tinlinenode.first_copy: tnode;
  3421. var
  3422. lowppn,
  3423. highppn,
  3424. npara,
  3425. paras : tnode;
  3426. ppn : tcallparanode;
  3427. paradef : tdef;
  3428. counter : integer;
  3429. begin
  3430. { determine copy function to use based on the first argument,
  3431. also count the number of arguments in this loop }
  3432. counter:=1;
  3433. paras:=left;
  3434. ppn:=tcallparanode(paras);
  3435. while assigned(ppn.right) do
  3436. begin
  3437. inc(counter);
  3438. ppn:=tcallparanode(ppn.right);
  3439. end;
  3440. paradef:=ppn.left.resultdef;
  3441. if is_ansistring(resultdef) then
  3442. { keep the specific kind of ansistringdef as result }
  3443. result:=ccallnode.createinternres('fpc_ansistr_copy',paras,resultdef)
  3444. else if is_widestring(resultdef) then
  3445. result:=ccallnode.createintern('fpc_widestr_copy',paras)
  3446. else if is_unicodestring(resultdef) then
  3447. result:=ccallnode.createintern('fpc_unicodestr_copy',paras)
  3448. { can't check for resultdef = cansichartype, because resultdef=
  3449. cshortstringtype here }
  3450. else if is_char(paradef) then
  3451. result:=ccallnode.createintern('fpc_char_copy',paras)
  3452. else if is_dynamic_array(resultdef) then
  3453. begin
  3454. { create statements with call }
  3455. if (counter=3) then
  3456. begin
  3457. highppn:=tcallparanode(paras).left.getcopy;
  3458. lowppn:=tcallparanode(tcallparanode(paras).right).left.getcopy;
  3459. end
  3460. else
  3461. begin
  3462. { copy the whole array using [0..high(sizeint)] range }
  3463. highppn:=cordconstnode.create(torddef(sinttype).high,sinttype,false);
  3464. lowppn:=cordconstnode.create(0,sinttype,false);
  3465. end;
  3466. { create call to fpc_dynarray_copy }
  3467. npara:=ccallparanode.create(highppn,
  3468. ccallparanode.create(lowppn,
  3469. ccallparanode.create(caddrnode.create_internal
  3470. (crttinode.create(tstoreddef(paradef),initrtti,rdt_normal)),
  3471. ccallparanode.create
  3472. (ctypeconvnode.create_internal(ppn.left,voidpointertype),nil))));
  3473. result:=ccallnode.createinternres('fpc_dynarray_copy',npara,paradef);
  3474. ppn.left:=nil;
  3475. paras.free;
  3476. end
  3477. else
  3478. result:=ccallnode.createintern('fpc_shortstr_copy',paras);
  3479. { parameters are reused }
  3480. left:=nil;
  3481. end;
  3482. function tinlinenode.first_new: tnode;
  3483. begin
  3484. internalerror(2011012201);
  3485. result:=nil;
  3486. end;
  3487. function tinlinenode.first_length: tnode;
  3488. begin
  3489. result:=nil;
  3490. if is_shortstring(left.resultdef) then
  3491. expectloc:=left.expectloc
  3492. else
  3493. begin
  3494. { ansi/wide string }
  3495. expectloc:=LOC_REGISTER;
  3496. end;
  3497. end;
  3498. function tinlinenode.first_assigned: tnode;
  3499. begin
  3500. result:=nil;
  3501. expectloc := LOC_JUMP;
  3502. end;
  3503. function tinlinenode.first_assert: tnode;
  3504. var
  3505. paras: tcallparanode;
  3506. begin
  3507. paras:=tcallparanode(tcallparanode(left).right);
  3508. paras:=ccallparanode.create(cstringconstnode.createstr(current_module.sourcefiles.get_file_name(current_filepos.fileindex)),paras);
  3509. paras:=ccallparanode.create(genintconstnode(fileinfo.line),paras);
  3510. {$if defined(x86) or defined(arm) or defined(jvm)}
  3511. paras:=ccallparanode.create(geninlinenode(in_get_frame,false,nil),paras);
  3512. {$else}
  3513. paras:=ccallparanode.create(ccallnode.createinternfromunit('SYSTEM','GET_FRAME',nil),paras);
  3514. {$endif}
  3515. result:=cifnode.create(cnotnode.create(tcallparanode(left).left),
  3516. ccallnode.createintern('fpc_assert',paras),nil);
  3517. tcallparanode(left).left:=nil;
  3518. tcallparanode(left).right:=nil;
  3519. end;
  3520. function tinlinenode.handle_box: tnode;
  3521. begin
  3522. result:=nil;
  3523. if not assigned(left) or
  3524. assigned(tcallparanode(left).right) then
  3525. CGMessage1(parser_e_wrong_parameter_size,'FpcInternalBox');
  3526. resultdef:=class_tobject;
  3527. end;
  3528. function tinlinenode.handle_unbox: tnode;
  3529. begin
  3530. result:=nil;
  3531. if not assigned(left) or
  3532. not assigned(tcallparanode(left).right) or
  3533. assigned(tcallparanode(tcallparanode(left).right).right) then
  3534. CGMessage1(parser_e_wrong_parameter_size,'FpcInternalUnBox');
  3535. if tcallparanode(left).left.nodetype<>typen then
  3536. internalerror(2011071701);
  3537. ttypenode(tcallparanode(left).left).allowed:=true;
  3538. resultdef:=tcallparanode(left).left.resultdef;
  3539. end;
  3540. function tinlinenode.first_pack_unpack: tnode;
  3541. var
  3542. loopstatement : tstatementnode;
  3543. loop : tblocknode;
  3544. loopvar : ttempcreatenode;
  3545. tempnode,
  3546. source,
  3547. target,
  3548. index,
  3549. unpackednode,
  3550. packednode,
  3551. sourcevecindex,
  3552. targetvecindex,
  3553. loopbody : tnode;
  3554. temprangedef : tdef;
  3555. ulorange,
  3556. uhirange,
  3557. plorange,
  3558. phirange : TConstExprInt;
  3559. begin
  3560. { transform into a for loop which assigns the data of the (un)packed }
  3561. { array to the other one }
  3562. source := left;
  3563. if (inlinenumber = in_unpack_x_y_z) then
  3564. begin
  3565. target := tcallparanode(source).right;
  3566. index := tcallparanode(target).right;
  3567. packednode := tcallparanode(source).left;
  3568. unpackednode := tcallparanode(target).left;
  3569. end
  3570. else
  3571. begin
  3572. index := tcallparanode(source).right;
  3573. target := tcallparanode(index).right;
  3574. packednode := tcallparanode(target).left;
  3575. unpackednode := tcallparanode(source).left;
  3576. end;
  3577. source := tcallparanode(source).left;
  3578. target := tcallparanode(target).left;
  3579. index := tcallparanode(index).left;
  3580. loop := internalstatements(loopstatement);
  3581. loopvar := ctempcreatenode.create(
  3582. tarraydef(packednode.resultdef).rangedef,
  3583. tarraydef(packednode.resultdef).rangedef.size,
  3584. tt_persistent,true);
  3585. addstatement(loopstatement,loopvar);
  3586. { For range checking: we have to convert to an integer type (in case the index type }
  3587. { is an enum), add the index and loop variable together, convert the result }
  3588. { implicitly to an orddef with range equal to the rangedef to get range checking }
  3589. { and finally convert it explicitly back to the actual rangedef to avoid type }
  3590. { errors }
  3591. temprangedef:=nil;
  3592. getrange(unpackednode.resultdef,ulorange,uhirange);
  3593. getrange(packednode.resultdef,plorange,phirange);
  3594. temprangedef:=torddef.create(torddef(sinttype).ordtype,ulorange,uhirange);
  3595. sourcevecindex := ctemprefnode.create(loopvar);
  3596. targetvecindex := ctypeconvnode.create_internal(index.getcopy,sinttype);
  3597. targetvecindex := caddnode.create(subn,targetvecindex,cordconstnode.create(plorange,sinttype,true));
  3598. targetvecindex := caddnode.create(addn,targetvecindex,ctemprefnode.create(loopvar));
  3599. targetvecindex := ctypeconvnode.create(targetvecindex,temprangedef);
  3600. targetvecindex := ctypeconvnode.create_explicit(targetvecindex,tarraydef(unpackednode.resultdef).rangedef);
  3601. if (inlinenumber = in_pack_x_y_z) then
  3602. begin
  3603. { swap source and target vec indices }
  3604. tempnode := sourcevecindex;
  3605. sourcevecindex := targetvecindex;
  3606. targetvecindex := tempnode;
  3607. end;
  3608. { create the assignment in the loop body }
  3609. loopbody :=
  3610. cassignmentnode.create(
  3611. cvecnode.create(target.getcopy,targetvecindex),
  3612. cvecnode.create(source.getcopy,sourcevecindex)
  3613. );
  3614. { create the actual for loop }
  3615. tempnode := cfornode.create(
  3616. ctemprefnode.create(loopvar),
  3617. cinlinenode.create(in_low_x,false,packednode.getcopy),
  3618. cinlinenode.create(in_high_x,false,packednode.getcopy),
  3619. loopbody,
  3620. false);
  3621. addstatement(loopstatement,tempnode);
  3622. { free the loop counter }
  3623. addstatement(loopstatement,ctempdeletenode.create(loopvar));
  3624. result := loop;
  3625. end;
  3626. end.