ninl.pas 160 KB

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