ninl.pas 166 KB

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