ninl.pas 166 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311
  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. { this should be an open array or array of const, both of
  2443. which can only be simple load nodes of parameters }
  2444. if left.nodetype<>loadn then
  2445. internalerror(2014120701);
  2446. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  2447. if assigned(hightree) then
  2448. begin
  2449. hp:=caddnode.create(addn,hightree,
  2450. cordconstnode.create(1,sinttype,false));
  2451. if (left.resultdef.typ=arraydef) then
  2452. if not is_packed_array(tarraydef(left.resultdef)) then
  2453. begin
  2454. if (tarraydef(left.resultdef).elesize<>1) then
  2455. hp:=caddnode.create(muln,hp,cordconstnode.create(tarraydef(
  2456. left.resultdef).elesize,sinttype,true));
  2457. end
  2458. else if (tarraydef(left.resultdef).elepackedbitsize <> 8) then
  2459. begin
  2460. { no packed open array support yet }
  2461. if (hp.nodetype <> ordconstn) then
  2462. internalerror(2006081511);
  2463. hp.free;
  2464. hp := cordconstnode.create(left.resultdef.size,sinttype,true);
  2465. {
  2466. hp:=
  2467. ctypeconvnode.create_explicit(sinttype,
  2468. cmoddivnode.create(divn,
  2469. caddnode.create(addn,
  2470. caddnode.create(muln,hp,cordconstnode.create(tarraydef(
  2471. left.resultdef).elepackedbitsize,s64inttype,true)),
  2472. cordconstnode.create(a,s64inttype,true)),
  2473. cordconstnode.create(8,s64inttype,true)),
  2474. sinttype);
  2475. }
  2476. end;
  2477. result:=hp;
  2478. end;
  2479. end
  2480. else
  2481. resultdef:=sinttype;
  2482. end;
  2483. in_typeof_x:
  2484. begin
  2485. if target_info.system in systems_managed_vm then
  2486. message(parser_e_feature_unsupported_for_vm);
  2487. typecheckpass(left);
  2488. set_varstate(left,vs_read,[]);
  2489. if (left.resultdef.typ=objectdef) and
  2490. not(oo_has_vmt in tobjectdef(left.resultdef).objectoptions) then
  2491. message(type_e_typeof_requires_vmt);
  2492. resultdef:=voidpointertype;
  2493. end;
  2494. in_ord_x:
  2495. begin
  2496. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2497. case left.resultdef.typ of
  2498. orddef,
  2499. enumdef :
  2500. ;
  2501. pointerdef :
  2502. begin
  2503. if not(m_mac in current_settings.modeswitches) then
  2504. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  2505. end
  2506. else
  2507. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  2508. end;
  2509. end;
  2510. in_chr_byte:
  2511. begin
  2512. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2513. end;
  2514. in_length_x:
  2515. begin
  2516. if ((left.resultdef.typ=arraydef) and
  2517. (not is_special_array(left.resultdef) or
  2518. is_open_array(left.resultdef))) or
  2519. (left.resultdef.typ=orddef) then
  2520. set_varstate(left,vs_read,[])
  2521. else
  2522. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2523. case left.resultdef.typ of
  2524. variantdef:
  2525. begin
  2526. inserttypeconv(left,getansistringdef);
  2527. end;
  2528. stringdef :
  2529. begin
  2530. { we don't need string convertions here, }
  2531. { except if from widestring to ansistring }
  2532. { and vice versa (that can change the }
  2533. { length) }
  2534. if (left.nodetype=typeconvn) and
  2535. (ttypeconvnode(left).left.resultdef.typ=stringdef) and
  2536. not(is_wide_or_unicode_string(left.resultdef) xor
  2537. is_wide_or_unicode_string(ttypeconvnode(left).left.resultdef)) then
  2538. begin
  2539. hp:=ttypeconvnode(left).left;
  2540. ttypeconvnode(left).left:=nil;
  2541. left.free;
  2542. left:=hp;
  2543. end;
  2544. end;
  2545. orddef :
  2546. begin
  2547. { will be handled in simplify }
  2548. if not is_char(left.resultdef) and
  2549. not is_widechar(left.resultdef) then
  2550. CGMessage(type_e_mismatch);
  2551. end;
  2552. pointerdef :
  2553. begin
  2554. if is_pchar(left.resultdef) then
  2555. begin
  2556. hp := ccallparanode.create(left,nil);
  2557. result := ccallnode.createintern('fpc_pchar_length',hp);
  2558. { make sure the left node doesn't get disposed, since it's }
  2559. { reused in the new node (JM) }
  2560. left:=nil;
  2561. exit;
  2562. end
  2563. else if is_pwidechar(left.resultdef) then
  2564. begin
  2565. hp := ccallparanode.create(left,nil);
  2566. result := ccallnode.createintern('fpc_pwidechar_length',hp);
  2567. { make sure the left node doesn't get disposed, since it's }
  2568. { reused in the new node (JM) }
  2569. left:=nil;
  2570. exit;
  2571. end
  2572. else
  2573. CGMessage(type_e_mismatch);
  2574. end;
  2575. arraydef :
  2576. begin
  2577. if is_open_array(left.resultdef) or
  2578. is_array_of_const(left.resultdef) then
  2579. begin
  2580. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  2581. if assigned(hightree) then
  2582. result:=caddnode.create(addn,hightree,
  2583. cordconstnode.create(1,sinttype,false));
  2584. exit;
  2585. end
  2586. { Length() for dynamic arrays is inlined }
  2587. else
  2588. begin
  2589. { will be handled in simplify }
  2590. end;
  2591. end;
  2592. undefineddef :
  2593. begin
  2594. if not (df_generic in current_procinfo.procdef.defoptions) then
  2595. CGMessage(type_e_mismatch);
  2596. { otherwise nothing }
  2597. end;
  2598. else
  2599. CGMessage(type_e_mismatch);
  2600. end;
  2601. { shortstring return an 8 bit value as the length
  2602. is the first byte of the string }
  2603. if is_shortstring(left.resultdef) then
  2604. resultdef:=u8inttype
  2605. else
  2606. resultdef:=sinttype;
  2607. end;
  2608. in_typeinfo_x:
  2609. begin
  2610. if target_info.system in systems_managed_vm then
  2611. message(parser_e_feature_unsupported_for_vm);
  2612. if (left.resultdef.typ=enumdef) and
  2613. (tenumdef(left.resultdef).has_jumps) then
  2614. CGMessage(type_e_no_type_info);
  2615. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2616. resultdef:=voidpointertype;
  2617. end;
  2618. in_assigned_x:
  2619. begin
  2620. { the parser has already made sure the expression is valid }
  2621. { in case of a complex procvar, only check the "code" pointer }
  2622. if (tcallparanode(left).left.resultdef.typ=procvardef) and
  2623. not tprocvardef(tcallparanode(left).left.resultdef).is_addressonly then
  2624. begin
  2625. inserttypeconv_explicit(tcallparanode(left).left,search_system_type('TMETHOD').typedef);
  2626. tcallparanode(left).left:=csubscriptnode.create(tsym(tabstractrecorddef(tcallparanode(left).left.resultdef).symtable.find('CODE')),tcallparanode(left).left);
  2627. tcallparanode(left).get_paratype;
  2628. end;
  2629. { Postpone conversion into addnode until firstpass, so targets
  2630. may override first_assigned and insert specific code. }
  2631. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  2632. resultdef:=pasbool8type;
  2633. end;
  2634. in_ofs_x :
  2635. internalerror(2000101001);
  2636. in_seg_x :
  2637. begin
  2638. result := typecheck_seg;
  2639. end;
  2640. in_pred_x,
  2641. in_succ_x:
  2642. begin
  2643. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2644. resultdef:=left.resultdef;
  2645. if is_ordinal(resultdef) or is_typeparam(resultdef) then
  2646. begin
  2647. if (resultdef.typ=enumdef) and
  2648. (tenumdef(resultdef).has_jumps) and
  2649. not(m_delphi in current_settings.modeswitches) then
  2650. CGMessage(type_e_succ_and_pred_enums_with_assign_not_possible);
  2651. end
  2652. else
  2653. CGMessage(type_e_ordinal_expr_expected)
  2654. end;
  2655. in_copy_x:
  2656. result:=handle_copy;
  2657. in_initialize_x,
  2658. in_finalize_x:
  2659. begin
  2660. { inlined from pinline }
  2661. internalerror(200204231);
  2662. end;
  2663. in_setlength_x:
  2664. begin
  2665. result:=handle_setlength;
  2666. end;
  2667. in_inc_x,
  2668. in_dec_x:
  2669. begin
  2670. resultdef:=voidtype;
  2671. if not(df_generic in current_procinfo.procdef.defoptions) then
  2672. begin
  2673. if assigned(left) then
  2674. begin
  2675. { first param must be var }
  2676. valid_for_var(tcallparanode(left).left,true);
  2677. set_varstate(tcallparanode(left).left,vs_readwritten,[vsf_must_be_valid]);
  2678. if (left.resultdef.typ in [enumdef,pointerdef]) or
  2679. is_ordinal(left.resultdef) or
  2680. is_currency(left.resultdef) then
  2681. begin
  2682. { value of left gets changed -> must be unique }
  2683. set_unique(tcallparanode(left).left);
  2684. { two paras ? }
  2685. if assigned(tcallparanode(left).right) then
  2686. begin
  2687. if is_integer(tcallparanode(left).right.resultdef) then
  2688. begin
  2689. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  2690. { when range/overflow checking is on, we
  2691. convert this to a regular add, and for proper
  2692. checking we need the original type }
  2693. if ([cs_check_range,cs_check_overflow]*current_settings.localswitches=[]) then
  2694. if (tcallparanode(left).left.resultdef.typ=pointerdef) then
  2695. begin
  2696. { don't convert values added to pointers into the pointer types themselves,
  2697. because that will turn signed values into unsigned ones, which then
  2698. goes wrong when they have to be multiplied with the size of the elements
  2699. to which the pointer points in ncginl (mantis #17342) }
  2700. if is_signed(tcallparanode(tcallparanode(left).right).left.resultdef) then
  2701. inserttypeconv(tcallparanode(tcallparanode(left).right).left,ptrsinttype)
  2702. else
  2703. inserttypeconv(tcallparanode(tcallparanode(left).right).left,ptruinttype)
  2704. end
  2705. else if is_integer(tcallparanode(left).left.resultdef) then
  2706. inserttypeconv(tcallparanode(tcallparanode(left).right).left,tcallparanode(left).left.resultdef)
  2707. else
  2708. inserttypeconv_internal(tcallparanode(tcallparanode(left).right).left,tcallparanode(left).left.resultdef);
  2709. if assigned(tcallparanode(tcallparanode(left).right).right) then
  2710. { should be handled in the parser (JM) }
  2711. internalerror(2006020901);
  2712. end
  2713. else
  2714. CGMessagePos(tcallparanode(left).right.fileinfo,type_e_ordinal_expr_expected);
  2715. end;
  2716. end
  2717. { generic type parameter? }
  2718. else if is_typeparam(left.resultdef) then
  2719. begin
  2720. result:=cnothingnode.create;
  2721. exit;
  2722. end
  2723. else
  2724. begin
  2725. hp:=self;
  2726. if isunaryoverloaded(hp) then
  2727. begin
  2728. { inc(rec) and dec(rec) assigns result value to argument }
  2729. result:=cassignmentnode.create(tcallparanode(left).left.getcopy,hp);
  2730. exit;
  2731. end
  2732. else
  2733. CGMessagePos(left.fileinfo,type_e_ordinal_expr_expected);
  2734. end;
  2735. end
  2736. else
  2737. CGMessagePos(fileinfo,type_e_mismatch);
  2738. end;
  2739. end;
  2740. in_read_x,
  2741. in_readln_x,
  2742. in_readstr_x,
  2743. in_write_x,
  2744. in_writeln_x,
  2745. in_writestr_x :
  2746. begin
  2747. result := handle_read_write;
  2748. end;
  2749. in_settextbuf_file_x :
  2750. begin
  2751. if target_info.system in systems_managed_vm then
  2752. message(parser_e_feature_unsupported_for_vm);
  2753. resultdef:=voidtype;
  2754. { now we know the type of buffer }
  2755. hp:=ccallparanode.create(cordconstnode.create(
  2756. tcallparanode(left).left.resultdef.size,s32inttype,true),left);
  2757. result:=ccallnode.createintern('SETTEXTBUF',hp);
  2758. left:=nil;
  2759. end;
  2760. { the firstpass of the arg has been done in firstcalln ? }
  2761. in_reset_typedfile,
  2762. in_rewrite_typedfile :
  2763. begin
  2764. result := handle_reset_rewrite_typed;
  2765. end;
  2766. in_str_x_string :
  2767. begin
  2768. result:=handle_str;
  2769. end;
  2770. in_val_x :
  2771. begin
  2772. result:=handle_val;
  2773. end;
  2774. in_include_x_y,
  2775. in_exclude_x_y:
  2776. begin
  2777. resultdef:=voidtype;
  2778. { the parser already checks whether we have two (and exactly two) }
  2779. { parameters (JM) }
  2780. { first param must be var }
  2781. valid_for_var(tcallparanode(left).left,true);
  2782. set_varstate(tcallparanode(left).left,vs_readwritten,[vsf_must_be_valid]);
  2783. { check type }
  2784. if (left.resultdef.typ=setdef) then
  2785. begin
  2786. { insert a type conversion }
  2787. { to the type of the set elements }
  2788. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  2789. inserttypeconv(tcallparanode(tcallparanode(left).right).left,
  2790. tsetdef(left.resultdef).elementdef);
  2791. end
  2792. else
  2793. CGMessage(type_e_mismatch);
  2794. end;
  2795. in_pack_x_y_z,
  2796. in_unpack_x_y_z :
  2797. begin
  2798. handle_pack_unpack;
  2799. end;
  2800. in_slice_x:
  2801. begin
  2802. if target_info.system in systems_managed_vm then
  2803. message(parser_e_feature_unsupported_for_vm);
  2804. result:=nil;
  2805. resultdef:=tcallparanode(left).left.resultdef;
  2806. if (resultdef.typ <> arraydef) then
  2807. CGMessagePos(left.fileinfo,type_e_mismatch)
  2808. else if is_packed_array(resultdef) then
  2809. CGMessagePos2(left.fileinfo,type_e_got_expected_unpacked_array,'1',resultdef.typename);
  2810. if not(is_integer(tcallparanode(tcallparanode(left).right).left.resultdef)) then
  2811. CGMessagePos1(tcallparanode(left).right.fileinfo,
  2812. type_e_integer_expr_expected,
  2813. tcallparanode(tcallparanode(left).right).left.resultdef.typename);
  2814. end;
  2815. in_new_x:
  2816. resultdef:=left.resultdef;
  2817. in_low_x,
  2818. in_high_x:
  2819. begin
  2820. case left.resultdef.typ of
  2821. orddef,
  2822. enumdef,
  2823. setdef:
  2824. ;
  2825. arraydef:
  2826. begin
  2827. if (inlinenumber=in_low_x) then
  2828. set_varstate(left,vs_read,[])
  2829. else
  2830. begin
  2831. if is_open_array(left.resultdef) or
  2832. is_array_of_const(left.resultdef) then
  2833. begin
  2834. set_varstate(left,vs_read,[]);
  2835. result:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  2836. end
  2837. else
  2838. if is_dynamic_array(left.resultdef) then
  2839. begin
  2840. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2841. { can't use inserttypeconv because we need }
  2842. { an explicit type conversion (JM) }
  2843. hp := ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil);
  2844. result := ccallnode.createintern('fpc_dynarray_high',hp);
  2845. { make sure the left node doesn't get disposed, since it's }
  2846. { reused in the new node (JM) }
  2847. left:=nil;
  2848. end
  2849. else
  2850. begin
  2851. set_varstate(left,vs_read,[]);
  2852. end;
  2853. end;
  2854. end;
  2855. stringdef:
  2856. begin
  2857. if inlinenumber=in_low_x then
  2858. begin
  2859. set_varstate(left,vs_read,[]);
  2860. end
  2861. else
  2862. begin
  2863. if is_open_string(left.resultdef) then
  2864. begin
  2865. set_varstate(left,vs_read,[]);
  2866. result:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry))
  2867. end
  2868. else if is_dynamicstring(left.resultdef) then
  2869. begin
  2870. result:=cinlinenode.create(in_length_x,false,left);
  2871. if cs_zerobasedstrings in current_settings.localswitches then
  2872. result:=caddnode.create(subn,result,cordconstnode.create(1,sinttype,false));
  2873. { make sure the left node doesn't get disposed, since it's }
  2874. { reused in the new node (JM) }
  2875. left:=nil;
  2876. end
  2877. end;
  2878. end;
  2879. else
  2880. CGMessage(type_e_mismatch);
  2881. end;
  2882. end;
  2883. in_exp_real,
  2884. in_frac_real,
  2885. in_int_real,
  2886. in_cos_real,
  2887. in_sin_real,
  2888. in_arctan_real,
  2889. in_ln_real :
  2890. begin
  2891. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2892. { converting an int64 to double on platforms without }
  2893. { extended can cause precision loss }
  2894. if not(left.nodetype in [ordconstn,realconstn]) then
  2895. inserttypeconv(left,pbestrealtype^);
  2896. resultdef:=pbestrealtype^;
  2897. end;
  2898. in_trunc_real,
  2899. in_round_real :
  2900. begin
  2901. { on i8086, the int64 result is returned in a var param, because
  2902. it's too big to fit in a register or a pair of registers. In
  2903. that case we have 2 parameters and left.nodetype is a callparan. }
  2904. if left.nodetype = callparan then
  2905. temp_pnode := @tcallparanode(left).left
  2906. else
  2907. temp_pnode := @left;
  2908. set_varstate(temp_pnode^,vs_read,[vsf_must_be_valid]);
  2909. { for direct float rounding, no best real type cast should be necessary }
  2910. if not((temp_pnode^.resultdef.typ=floatdef) and
  2911. (tfloatdef(temp_pnode^.resultdef).floattype in [s32real,s64real,s80real,sc80real,s128real])) and
  2912. { converting an int64 to double on platforms without }
  2913. { extended can cause precision loss }
  2914. not(temp_pnode^.nodetype in [ordconstn,realconstn]) then
  2915. inserttypeconv(temp_pnode^,pbestrealtype^);
  2916. resultdef:=s64inttype;
  2917. end;
  2918. in_pi_real :
  2919. begin
  2920. resultdef:=pbestrealtype^;
  2921. end;
  2922. in_abs_long:
  2923. begin
  2924. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2925. resultdef:=left.resultdef;
  2926. end;
  2927. in_abs_real,
  2928. in_sqr_real,
  2929. in_sqrt_real :
  2930. begin
  2931. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2932. setfloatresultdef;
  2933. end;
  2934. {$ifdef SUPPORT_MMX}
  2935. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  2936. begin
  2937. end;
  2938. {$endif SUPPORT_MMX}
  2939. in_aligned_x,
  2940. in_unaligned_x:
  2941. begin
  2942. resultdef:=left.resultdef;
  2943. end;
  2944. in_assert_x_y :
  2945. begin
  2946. resultdef:=voidtype;
  2947. if assigned(left) then
  2948. begin
  2949. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  2950. { check type }
  2951. if is_boolean(left.resultdef) or
  2952. (
  2953. (left.resultdef.typ=undefineddef) and
  2954. (df_generic in current_procinfo.procdef.defoptions)
  2955. ) then
  2956. begin
  2957. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  2958. { must always be a string }
  2959. inserttypeconv(tcallparanode(tcallparanode(left).right).left,cshortstringtype);
  2960. end
  2961. else
  2962. CGMessage1(type_e_boolean_expr_expected,left.resultdef.typename);
  2963. end
  2964. else
  2965. CGMessage(type_e_mismatch);
  2966. if (cs_do_assertion in current_settings.localswitches) then
  2967. include(current_procinfo.flags,pi_do_call);
  2968. end;
  2969. in_prefetch_var:
  2970. resultdef:=voidtype;
  2971. in_get_frame,
  2972. in_get_caller_frame,
  2973. in_get_caller_addr:
  2974. begin
  2975. resultdef:=voidpointertype;
  2976. end;
  2977. in_rol_x,
  2978. in_ror_x,
  2979. in_sar_x:
  2980. begin
  2981. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2982. resultdef:=left.resultdef;
  2983. end;
  2984. in_rol_x_y,
  2985. in_ror_x_y,
  2986. in_sar_x_y:
  2987. begin
  2988. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  2989. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  2990. resultdef:=tcallparanode(tcallparanode(left).right).left.resultdef;
  2991. end;
  2992. in_bsf_x,
  2993. in_bsr_x:
  2994. begin
  2995. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2996. if not is_integer(left.resultdef) then
  2997. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename);
  2998. if torddef(left.resultdef).ordtype in [u64bit, s64bit] then
  2999. resultdef:=u64inttype
  3000. else
  3001. resultdef:=u32inttype
  3002. end;
  3003. in_popcnt_x:
  3004. begin
  3005. set_varstate(left,vs_read,[vsf_must_be_valid]);
  3006. if not is_integer(left.resultdef) then
  3007. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename);
  3008. resultdef:=left.resultdef;
  3009. end;
  3010. in_objc_selector_x:
  3011. begin
  3012. result:=cobjcselectornode.create(left);
  3013. { reused }
  3014. left:=nil;
  3015. end;
  3016. in_objc_protocol_x:
  3017. begin
  3018. result:=cobjcprotocolnode.create(left);
  3019. { reused }
  3020. left:=nil;
  3021. end;
  3022. in_objc_encode_x:
  3023. begin
  3024. result:=handle_objc_encode;
  3025. end;
  3026. in_default_x:
  3027. begin
  3028. result:=handle_default;
  3029. end;
  3030. in_box_x:
  3031. begin
  3032. result:=handle_box;
  3033. end;
  3034. in_unbox_x_y:
  3035. begin
  3036. result:=handle_unbox;
  3037. end;
  3038. in_fma_single,
  3039. in_fma_double,
  3040. in_fma_extended,
  3041. in_fma_float128:
  3042. begin
  3043. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  3044. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  3045. set_varstate(tcallparanode(tcallparanode(tcallparanode(left).right).right).left,vs_read,[vsf_must_be_valid]);
  3046. resultdef:=tcallparanode(left).left.resultdef;
  3047. end;
  3048. else
  3049. internalerror(8);
  3050. end;
  3051. end;
  3052. if not assigned(result) and not
  3053. codegenerror then
  3054. result:=simplify(false);
  3055. end;
  3056. function tinlinenode.pass_1 : tnode;
  3057. var
  3058. hp: tnode;
  3059. shiftconst: longint;
  3060. begin
  3061. result:=nil;
  3062. { if we handle writeln; left contains no valid address }
  3063. if assigned(left) then
  3064. begin
  3065. if left.nodetype=callparan then
  3066. tcallparanode(left).firstcallparan
  3067. else
  3068. firstpass(left);
  3069. end;
  3070. { intern const should already be handled }
  3071. if nf_inlineconst in flags then
  3072. internalerror(200104044);
  3073. case inlinenumber of
  3074. in_lo_qword,
  3075. in_hi_qword,
  3076. in_lo_long,
  3077. in_hi_long,
  3078. in_lo_word,
  3079. in_hi_word:
  3080. begin
  3081. shiftconst := 0;
  3082. case inlinenumber of
  3083. in_hi_qword:
  3084. shiftconst := 32;
  3085. in_hi_long:
  3086. shiftconst := 16;
  3087. in_hi_word:
  3088. shiftconst := 8;
  3089. end;
  3090. if shiftconst <> 0 then
  3091. result := ctypeconvnode.create_internal(cshlshrnode.create(shrn,left,
  3092. cordconstnode.create(shiftconst,sinttype,false)),resultdef)
  3093. else
  3094. result := ctypeconvnode.create_internal(left,resultdef);
  3095. left := nil;
  3096. firstpass(result);
  3097. end;
  3098. in_sizeof_x,
  3099. in_typeof_x:
  3100. begin
  3101. expectloc:=LOC_REGISTER;
  3102. if (left.nodetype=typen) and
  3103. (cs_create_pic in current_settings.moduleswitches) and
  3104. (tf_pic_uses_got in target_info.flags) then
  3105. include(current_procinfo.flags,pi_needs_got);
  3106. end;
  3107. in_length_x:
  3108. begin
  3109. result:=first_length;
  3110. end;
  3111. in_typeinfo_x:
  3112. begin
  3113. result:=caddrnode.create_internal(
  3114. crttinode.create(tstoreddef(left.resultdef),fullrtti,rdt_normal)
  3115. );
  3116. end;
  3117. in_assigned_x:
  3118. begin
  3119. result:=first_assigned;
  3120. end;
  3121. in_pred_x,
  3122. in_succ_x:
  3123. begin
  3124. expectloc:=LOC_REGISTER;
  3125. { in case of range/overflow checking, use a regular addnode
  3126. because it's too complex to handle correctly otherwise }
  3127. {$ifndef jvm}
  3128. { enums are class instances in the JVM -> always need conversion }
  3129. if ([cs_check_overflow,cs_check_range]*current_settings.localswitches)<>[] then
  3130. {$endif}
  3131. begin
  3132. { create constant 1 }
  3133. hp:=cordconstnode.create(1,left.resultdef,false);
  3134. typecheckpass(hp);
  3135. if not is_integer(hp.resultdef) then
  3136. inserttypeconv_internal(hp,sinttype);
  3137. { avoid type errors from the addn/subn }
  3138. if not is_integer(left.resultdef) then
  3139. inserttypeconv_internal(left,sinttype);
  3140. { addition/substraction depending on succ/pred }
  3141. if inlinenumber=in_succ_x then
  3142. hp:=caddnode.create(addn,left,hp)
  3143. else
  3144. hp:=caddnode.create(subn,left,hp);
  3145. { assign result of addition }
  3146. if not(is_integer(resultdef)) then
  3147. inserttypeconv(hp,corddef.create(
  3148. {$ifdef cpu64bitaddr}
  3149. s64bit,
  3150. {$else cpu64bitaddr}
  3151. s32bit,
  3152. {$endif cpu64bitaddr}
  3153. get_min_value(resultdef),
  3154. get_max_value(resultdef)))
  3155. else
  3156. inserttypeconv(hp,resultdef);
  3157. { avoid any possible errors/warnings }
  3158. inserttypeconv_internal(hp,resultdef);
  3159. { firstpass it }
  3160. firstpass(hp);
  3161. { left is reused }
  3162. left:=nil;
  3163. { return new node }
  3164. result:=hp;
  3165. end;
  3166. end;
  3167. in_setlength_x:
  3168. result:=first_setlength;
  3169. in_copy_x:
  3170. result:=first_copy;
  3171. in_initialize_x,
  3172. in_finalize_x:
  3173. begin
  3174. expectloc:=LOC_VOID;
  3175. end;
  3176. in_inc_x,
  3177. in_dec_x:
  3178. begin
  3179. result:=first_IncDec;
  3180. end;
  3181. in_include_x_y,
  3182. in_exclude_x_y:
  3183. begin
  3184. result:=first_IncludeExclude;
  3185. end;
  3186. in_pack_x_y_z,
  3187. in_unpack_x_y_z:
  3188. begin
  3189. result:=first_pack_unpack;
  3190. end;
  3191. in_exp_real:
  3192. begin
  3193. result:= first_exp_real;
  3194. end;
  3195. in_round_real:
  3196. begin
  3197. result:= first_round_real;
  3198. end;
  3199. in_trunc_real:
  3200. begin
  3201. result:= first_trunc_real;
  3202. end;
  3203. in_int_real:
  3204. begin
  3205. result:= first_int_real;
  3206. end;
  3207. in_frac_real:
  3208. begin
  3209. result:= first_frac_real;
  3210. end;
  3211. in_cos_real:
  3212. begin
  3213. result:= first_cos_real;
  3214. end;
  3215. in_sin_real:
  3216. begin
  3217. result := first_sin_real;
  3218. end;
  3219. in_arctan_real:
  3220. begin
  3221. result := first_arctan_real;
  3222. end;
  3223. in_pi_real :
  3224. begin
  3225. result := first_pi;
  3226. end;
  3227. in_abs_real:
  3228. begin
  3229. result := first_abs_real;
  3230. end;
  3231. in_abs_long:
  3232. begin
  3233. result := first_abs_long;
  3234. end;
  3235. in_sqr_real:
  3236. begin
  3237. result := first_sqr_real;
  3238. end;
  3239. in_sqrt_real:
  3240. begin
  3241. result := first_sqrt_real;
  3242. end;
  3243. in_ln_real:
  3244. begin
  3245. result := first_ln_real;
  3246. end;
  3247. {$ifdef SUPPORT_MMX}
  3248. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  3249. begin
  3250. end;
  3251. {$endif SUPPORT_MMX}
  3252. in_assert_x_y :
  3253. begin
  3254. result:=first_assert;
  3255. end;
  3256. in_low_x,
  3257. in_high_x:
  3258. internalerror(200104047);
  3259. in_slice_x:
  3260. internalerror(2005101501);
  3261. in_ord_x,
  3262. in_chr_byte:
  3263. begin
  3264. { should not happend as it's converted to typeconv }
  3265. internalerror(200104045);
  3266. end;
  3267. in_ofs_x :
  3268. internalerror(2000101001);
  3269. in_seg_x :
  3270. begin
  3271. result:=first_seg;
  3272. end;
  3273. in_settextbuf_file_x,
  3274. in_reset_typedfile,
  3275. in_rewrite_typedfile,
  3276. in_str_x_string,
  3277. in_val_x,
  3278. in_read_x,
  3279. in_readln_x,
  3280. in_write_x,
  3281. in_writeln_x :
  3282. begin
  3283. { should be handled by pass_typecheck }
  3284. internalerror(200108234);
  3285. end;
  3286. in_get_frame:
  3287. begin
  3288. result:=first_get_frame;
  3289. end;
  3290. in_get_caller_frame:
  3291. begin
  3292. expectloc:=LOC_REGISTER;
  3293. end;
  3294. in_get_caller_addr:
  3295. begin
  3296. expectloc:=LOC_REGISTER;
  3297. end;
  3298. in_prefetch_var:
  3299. begin
  3300. expectloc:=LOC_VOID;
  3301. end;
  3302. in_aligned_x,
  3303. in_unaligned_x:
  3304. begin
  3305. expectloc:=tcallparanode(left).left.expectloc;
  3306. end;
  3307. in_rol_x,
  3308. in_rol_x_y,
  3309. in_ror_x,
  3310. in_ror_x_y,
  3311. in_bsf_x,
  3312. in_bsr_x:
  3313. expectloc:=LOC_REGISTER;
  3314. in_sar_x,
  3315. in_sar_x_y:
  3316. result:=first_sar;
  3317. in_popcnt_x:
  3318. result:=first_popcnt;
  3319. in_new_x:
  3320. result:=first_new;
  3321. in_box_x:
  3322. result:=first_box;
  3323. in_unbox_x_y:
  3324. result:=first_unbox;
  3325. in_fma_single,
  3326. in_fma_double,
  3327. in_fma_extended,
  3328. in_fma_float128:
  3329. result:=first_fma;
  3330. else
  3331. internalerror(89);
  3332. end;
  3333. end;
  3334. {$maxfpuregisters default}
  3335. function tinlinenode.docompare(p: tnode): boolean;
  3336. begin
  3337. docompare :=
  3338. inherited docompare(p) and
  3339. (inlinenumber = tinlinenode(p).inlinenumber);
  3340. end;
  3341. function tinlinenode.first_pi : tnode;
  3342. begin
  3343. result:=crealconstnode.create(getpi,pbestrealtype^);
  3344. end;
  3345. function tinlinenode.first_arctan_real : tnode;
  3346. begin
  3347. { create the call to the helper }
  3348. { on entry left node contains the parameter }
  3349. first_arctan_real := ccallnode.createintern('fpc_arctan_real',
  3350. ccallparanode.create(left,nil));
  3351. left := nil;
  3352. end;
  3353. function tinlinenode.first_abs_real : tnode;
  3354. begin
  3355. { create the call to the helper }
  3356. { on entry left node contains the parameter }
  3357. first_abs_real := ctypeconvnode.create(ccallnode.createintern('fpc_abs_real',
  3358. ccallparanode.create(left,nil)),resultdef);
  3359. left := nil;
  3360. end;
  3361. function tinlinenode.first_sqr_real : tnode;
  3362. begin
  3363. {$ifndef cpufpemu}
  3364. { this procedure might be only used for cpus definining cpufpemu else
  3365. the optimizer might go into an endless loop when doing x*x -> changes }
  3366. internalerror(2011092401);
  3367. {$endif cpufpemu}
  3368. { create the call to the helper }
  3369. { on entry left node contains the parameter }
  3370. first_sqr_real := ctypeconvnode.create(ccallnode.createintern('fpc_sqr_real',
  3371. ccallparanode.create(left,nil)),resultdef);
  3372. left := nil;
  3373. end;
  3374. function tinlinenode.first_sqrt_real : tnode;
  3375. var
  3376. fdef: tdef;
  3377. procname: string[31];
  3378. begin
  3379. if ((cs_fp_emulation in current_settings.moduleswitches)
  3380. {$ifdef cpufpemu}
  3381. or (current_settings.fputype=fpu_soft)
  3382. {$endif cpufpemu}
  3383. ) and not (target_info.system in systems_wince) then
  3384. begin
  3385. case tfloatdef(left.resultdef).floattype of
  3386. s32real:
  3387. begin
  3388. fdef:=search_system_type('FLOAT32REC').typedef;
  3389. procname:='float32_sqrt';
  3390. end;
  3391. s64real:
  3392. begin
  3393. fdef:=search_system_type('FLOAT64').typedef;
  3394. procname:='float64_sqrt';
  3395. end;
  3396. {!!! not yet implemented
  3397. s128real:
  3398. }
  3399. else
  3400. internalerror(2014052101);
  3401. end;
  3402. first_sqrt_real:=ctypeconvnode.create_internal(ccallnode.createintern(procname,ccallparanode.create(
  3403. ctypeconvnode.create_internal(left,fdef),nil)),resultdef);
  3404. end
  3405. else
  3406. begin
  3407. { create the call to the helper }
  3408. { on entry left node contains the parameter }
  3409. first_sqrt_real := ctypeconvnode.create(ccallnode.createintern('fpc_sqrt_real',
  3410. ccallparanode.create(left,nil)),resultdef);
  3411. end;
  3412. left := nil;
  3413. end;
  3414. function tinlinenode.first_ln_real : tnode;
  3415. begin
  3416. { create the call to the helper }
  3417. { on entry left node contains the parameter }
  3418. first_ln_real := ccallnode.createintern('fpc_ln_real',
  3419. ccallparanode.create(left,nil));
  3420. left := nil;
  3421. end;
  3422. function tinlinenode.first_cos_real : tnode;
  3423. begin
  3424. { create the call to the helper }
  3425. { on entry left node contains the parameter }
  3426. first_cos_real := ccallnode.createintern('fpc_cos_real',
  3427. ccallparanode.create(left,nil));
  3428. left := nil;
  3429. end;
  3430. function tinlinenode.first_sin_real : tnode;
  3431. begin
  3432. { create the call to the helper }
  3433. { on entry left node contains the parameter }
  3434. first_sin_real := ccallnode.createintern('fpc_sin_real',
  3435. ccallparanode.create(left,nil));
  3436. left := nil;
  3437. end;
  3438. function tinlinenode.first_exp_real : tnode;
  3439. begin
  3440. { create the call to the helper }
  3441. { on entry left node contains the parameter }
  3442. result := ccallnode.createintern('fpc_exp_real',ccallparanode.create(left,nil));
  3443. left := nil;
  3444. end;
  3445. function tinlinenode.first_int_real : tnode;
  3446. begin
  3447. { create the call to the helper }
  3448. { on entry left node contains the parameter }
  3449. result := ccallnode.createintern('fpc_int_real',ccallparanode.create(left,nil));
  3450. left := nil;
  3451. end;
  3452. function tinlinenode.first_frac_real : tnode;
  3453. begin
  3454. { create the call to the helper }
  3455. { on entry left node contains the parameter }
  3456. result := ccallnode.createintern('fpc_frac_real',ccallparanode.create(left,nil));
  3457. left := nil;
  3458. end;
  3459. function tinlinenode.first_round_real : tnode;
  3460. begin
  3461. { create the call to the helper }
  3462. { on entry left node contains the parameter }
  3463. result := ccallnode.createintern('fpc_round_real',ccallparanode.create(left,nil));
  3464. left := nil;
  3465. end;
  3466. function tinlinenode.first_trunc_real : tnode;
  3467. begin
  3468. { create the call to the helper }
  3469. { on entry left node contains the parameter }
  3470. result := ccallnode.createintern('fpc_trunc_real',ccallparanode.create(left,nil));
  3471. left := nil;
  3472. end;
  3473. function tinlinenode.first_abs_long : tnode;
  3474. begin
  3475. expectloc:=LOC_REGISTER;
  3476. result:=nil;
  3477. end;
  3478. function tinlinenode.first_IncDec: tnode;
  3479. var
  3480. hp,hpp,resultnode : tnode;
  3481. tempnode: ttempcreatenode;
  3482. newstatement: tstatementnode;
  3483. newblock: tblocknode;
  3484. begin
  3485. expectloc:=LOC_VOID;
  3486. result:=nil;
  3487. { range/overflow checking doesn't work properly }
  3488. { with the inc/dec code that's generated (JM) }
  3489. if ((current_settings.localswitches * [cs_check_overflow,cs_check_range] <> []) and
  3490. { No overflow check for pointer operations, because inc(pointer,-1) will always
  3491. trigger an overflow. For uint32 it works because then the operation is done
  3492. in 64bit. Range checking is not applicable to pointers either }
  3493. (tcallparanode(left).left.resultdef.typ<>pointerdef))
  3494. {$ifdef jvm}
  3495. { enums are class instances on the JVM -> special treatment }
  3496. or (tcallparanode(left).left.resultdef.typ=enumdef)
  3497. {$endif}
  3498. then
  3499. { convert to simple add (JM) }
  3500. begin
  3501. newblock := internalstatements(newstatement);
  3502. { extra parameter? }
  3503. if assigned(tcallparanode(left).right) then
  3504. begin
  3505. { Yes, use for add node }
  3506. hpp := tcallparanode(tcallparanode(left).right).left;
  3507. tcallparanode(tcallparanode(left).right).left := nil;
  3508. if assigned(tcallparanode(tcallparanode(left).right).right) then
  3509. CGMessage(parser_e_illegal_expression);
  3510. end
  3511. else
  3512. begin
  3513. { no, create constant 1 }
  3514. hpp := cordconstnode.create(1,tcallparanode(left).left.resultdef,false);
  3515. end;
  3516. typecheckpass(hpp);
  3517. { make sure we don't call functions part of the left node twice (and generally }
  3518. { optimize the code generation) }
  3519. { Storing address is not always an optimization: alignment of left is not known
  3520. at this point, so we must assume the worst and use an unaligned pointer.
  3521. This results in larger and slower code on alignment-sensitive targets.
  3522. Therefore the complexity condition below is questionable, maybe just filtering
  3523. out calls with "= NODE_COMPLEXITY_INF" is sufficient.
  3524. Value of 3 corresponds to subscript nodes, i.e. record field. }
  3525. if node_complexity(tcallparanode(left).left) > 3 then
  3526. begin
  3527. tempnode := ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  3528. addstatement(newstatement,tempnode);
  3529. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  3530. caddrnode.create_internal(tcallparanode(left).left.getcopy)));
  3531. hp := cderefnode.create(ctemprefnode.create(tempnode));
  3532. inserttypeconv_internal(hp,tcallparanode(left).left.resultdef);
  3533. end
  3534. else
  3535. begin
  3536. hp := tcallparanode(left).left.getcopy;
  3537. tempnode := nil;
  3538. end;
  3539. resultnode := hp.getcopy;
  3540. { avoid type errors from the addn/subn }
  3541. if not is_integer(resultnode.resultdef) then
  3542. begin
  3543. inserttypeconv_internal(hp,sinttype);
  3544. inserttypeconv_internal(hpp,sinttype);
  3545. end;
  3546. { addition/substraction depending on inc/dec }
  3547. if inlinenumber = in_inc_x then
  3548. hpp := caddnode.create(addn,hp,hpp)
  3549. else
  3550. hpp := caddnode.create(subn,hp,hpp);
  3551. { assign result of addition }
  3552. if not(is_integer(resultnode.resultdef)) then
  3553. inserttypeconv(hpp,corddef.create(
  3554. {$ifdef cpu64bitaddr}
  3555. s64bit,
  3556. {$else cpu64bitaddr}
  3557. s32bit,
  3558. {$endif cpu64bitaddr}
  3559. get_min_value(resultnode.resultdef),
  3560. get_max_value(resultnode.resultdef)))
  3561. else
  3562. inserttypeconv(hpp,resultnode.resultdef);
  3563. { avoid any possible warnings }
  3564. inserttypeconv_internal(hpp,resultnode.resultdef);
  3565. addstatement(newstatement,cassignmentnode.create(resultnode,hpp));
  3566. { force pass 1, so copied tries get first pass'ed as well and flags like nf_write, nf_call_unique
  3567. get set right }
  3568. node_reset_flags(newstatement.statement,[nf_pass1_done]);
  3569. { firstpass it }
  3570. firstpass(tnode(newstatement.left));
  3571. { deallocate the temp }
  3572. if assigned(tempnode) then
  3573. addstatement(newstatement,ctempdeletenode.create(tempnode));
  3574. { firstpass it }
  3575. firstpass(tnode(newblock));
  3576. { return new node }
  3577. result := newblock;
  3578. end;
  3579. end;
  3580. function tinlinenode.first_IncludeExclude: tnode;
  3581. begin
  3582. result:=nil;
  3583. expectloc:=LOC_VOID;
  3584. end;
  3585. function tinlinenode.first_get_frame: tnode;
  3586. begin
  3587. include(current_procinfo.flags,pi_needs_stackframe);
  3588. expectloc:=LOC_CREGISTER;
  3589. result:=nil;
  3590. end;
  3591. function tinlinenode.first_setlength: tnode;
  3592. var
  3593. paras : tnode;
  3594. npara,
  3595. ppn : tcallparanode;
  3596. dims,
  3597. counter : integer;
  3598. isarray : boolean;
  3599. destppn : tnode;
  3600. newstatement : tstatementnode;
  3601. temp : ttempcreatenode;
  3602. newblock : tnode;
  3603. begin
  3604. paras:=left;
  3605. ppn:=tcallparanode(paras);
  3606. dims:=0;
  3607. while assigned(ppn.right) do
  3608. begin
  3609. inc(dims);
  3610. ppn:=tcallparanode(ppn.right);
  3611. end;
  3612. destppn:=ppn.left;
  3613. isarray:=is_dynamic_array(destppn.resultdef);
  3614. { first param must be a string or dynamic array ...}
  3615. if isarray then
  3616. begin
  3617. { create statements with call initialize the arguments and
  3618. call fpc_dynarr_setlength }
  3619. newblock:=internalstatements(newstatement);
  3620. { get temp for array of lengths }
  3621. temp:=ctempcreatenode.create(sinttype,dims*sinttype.size,tt_persistent,false);
  3622. addstatement(newstatement,temp);
  3623. { load array of lengths }
  3624. ppn:=tcallparanode(paras);
  3625. counter:=dims-1;
  3626. while assigned(ppn.right) do
  3627. begin
  3628. addstatement(newstatement,cassignmentnode.create(
  3629. ctemprefnode.create_offset(temp,counter*sinttype.size),
  3630. ppn.left));
  3631. ppn.left:=nil;
  3632. dec(counter);
  3633. ppn:=tcallparanode(ppn.right);
  3634. end;
  3635. { destppn is also reused }
  3636. ppn.left:=nil;
  3637. { create call to fpc_dynarr_setlength }
  3638. npara:=ccallparanode.create(caddrnode.create_internal
  3639. (ctemprefnode.create(temp)),
  3640. ccallparanode.create(cordconstnode.create
  3641. (dims,sinttype,true),
  3642. ccallparanode.create(caddrnode.create_internal
  3643. (crttinode.create(tstoreddef(destppn.resultdef),initrtti,rdt_normal)),
  3644. ccallparanode.create(ctypeconvnode.create_internal(destppn,voidpointertype),nil))));
  3645. addstatement(newstatement,ccallnode.createintern('fpc_dynarray_setlength',npara));
  3646. addstatement(newstatement,ctempdeletenode.create(temp));
  3647. end
  3648. else if is_ansistring(destppn.resultdef) then
  3649. begin
  3650. newblock:=ccallnode.createintern(
  3651. 'fpc_'+tstringdef(destppn.resultdef).stringtypname+'_setlength',
  3652. ccallparanode.create(
  3653. cordconstnode.create(getparaencoding(destppn.resultdef),u16inttype,true),
  3654. paras
  3655. )
  3656. );
  3657. { we reused the parameters, make sure we don't release them }
  3658. left:=nil;
  3659. end
  3660. else
  3661. begin
  3662. { we can reuse the supplied parameters }
  3663. newblock:=ccallnode.createintern(
  3664. 'fpc_'+tstringdef(destppn.resultdef).stringtypname+'_setlength',paras);
  3665. { we reused the parameters, make sure we don't release them }
  3666. left:=nil;
  3667. end;
  3668. result:=newblock;
  3669. end;
  3670. function tinlinenode.first_copy: tnode;
  3671. var
  3672. lowppn,
  3673. highppn,
  3674. npara,
  3675. paras : tnode;
  3676. ppn : tcallparanode;
  3677. paradef : tdef;
  3678. counter : integer;
  3679. begin
  3680. { determine copy function to use based on the first argument,
  3681. also count the number of arguments in this loop }
  3682. counter:=1;
  3683. paras:=left;
  3684. ppn:=tcallparanode(paras);
  3685. while assigned(ppn.right) do
  3686. begin
  3687. inc(counter);
  3688. ppn:=tcallparanode(ppn.right);
  3689. end;
  3690. paradef:=ppn.left.resultdef;
  3691. { fill up third parameter }
  3692. if counter=2 then
  3693. begin
  3694. paras:=ccallparanode.create(cordconstnode.create(torddef(sinttype).high,sinttype,false),paras);
  3695. counter:=3;
  3696. end;
  3697. if is_ansistring(resultdef) then
  3698. { keep the specific kind of ansistringdef as result }
  3699. result:=ccallnode.createinternres('fpc_ansistr_copy',paras,resultdef)
  3700. else if is_widestring(resultdef) then
  3701. result:=ccallnode.createintern('fpc_widestr_copy',paras)
  3702. else if is_unicodestring(resultdef) then
  3703. result:=ccallnode.createintern('fpc_unicodestr_copy',paras)
  3704. { can't check for resultdef = cansichartype, because resultdef=
  3705. cshortstringtype here }
  3706. else if is_char(paradef) then
  3707. result:=ccallnode.createintern('fpc_char_copy',paras)
  3708. else if is_dynamic_array(resultdef) then
  3709. begin
  3710. { create statements with call }
  3711. case counter of
  3712. 1:
  3713. begin
  3714. { copy the whole array using [0..high(sizeint)] range }
  3715. highppn:=cordconstnode.create(torddef(sinttype).high,sinttype,false);
  3716. lowppn:=cordconstnode.create(0,sinttype,false);
  3717. end;
  3718. 3:
  3719. begin
  3720. highppn:=tcallparanode(paras).left.getcopy;
  3721. lowppn:=tcallparanode(tcallparanode(paras).right).left.getcopy;
  3722. end;
  3723. else
  3724. internalerror(2012100701);
  3725. end;
  3726. { create call to fpc_dynarray_copy }
  3727. npara:=ccallparanode.create(highppn,
  3728. ccallparanode.create(lowppn,
  3729. ccallparanode.create(caddrnode.create_internal
  3730. (crttinode.create(tstoreddef(paradef),initrtti,rdt_normal)),
  3731. ccallparanode.create
  3732. (ctypeconvnode.create_internal(ppn.left,voidpointertype),nil))));
  3733. result:=ccallnode.createinternres('fpc_dynarray_copy',npara,paradef);
  3734. ppn.left:=nil;
  3735. paras.free;
  3736. end
  3737. else
  3738. result:=ccallnode.createintern('fpc_shortstr_copy',paras);
  3739. { parameters are reused }
  3740. left:=nil;
  3741. end;
  3742. function tinlinenode.first_new: tnode;
  3743. var
  3744. newstatement : tstatementnode;
  3745. newblock : tblocknode;
  3746. temp : ttempcreatenode;
  3747. para : tcallparanode;
  3748. begin
  3749. { create statements with call to getmem+initialize }
  3750. newblock:=internalstatements(newstatement);
  3751. { create temp for result }
  3752. temp := ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,true);
  3753. addstatement(newstatement,temp);
  3754. { create call to fpc_getmem }
  3755. para := ccallparanode.create(cordconstnode.create
  3756. (tpointerdef(left.resultdef).pointeddef.size,s32inttype,true),nil);
  3757. addstatement(newstatement,cassignmentnode.create(
  3758. ctemprefnode.create(temp),
  3759. ccallnode.createintern('fpc_getmem',para)));
  3760. { create call to fpc_initialize }
  3761. if is_managed_type(tpointerdef(left.resultdef).pointeddef) then
  3762. begin
  3763. para := ccallparanode.create(caddrnode.create_internal(crttinode.create
  3764. (tstoreddef(tpointerdef(left.resultdef).pointeddef),initrtti,rdt_normal)),
  3765. ccallparanode.create(ctemprefnode.create
  3766. (temp),nil));
  3767. addstatement(newstatement,ccallnode.createintern('fpc_initialize',para));
  3768. end;
  3769. { the last statement should return the value as
  3770. location and type, this is done be referencing the
  3771. temp and converting it first from a persistent temp to
  3772. normal temp }
  3773. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  3774. addstatement(newstatement,ctemprefnode.create(temp));
  3775. result:=newblock;
  3776. end;
  3777. function tinlinenode.first_length: tnode;
  3778. begin
  3779. result:=nil;
  3780. if is_shortstring(left.resultdef) then
  3781. expectloc:=left.expectloc
  3782. else
  3783. begin
  3784. { ansi/wide string }
  3785. expectloc:=LOC_REGISTER;
  3786. end;
  3787. end;
  3788. function tinlinenode.first_assigned: tnode;
  3789. begin
  3790. { Comparison must not call procvars, indicate that with nf_load_procvar flag }
  3791. result:=caddnode.create(unequaln,tcallparanode(left).left,cnilnode.create);
  3792. include(result.flags,nf_load_procvar);
  3793. tcallparanode(left).left:=nil;
  3794. end;
  3795. function tinlinenode.first_assert: tnode;
  3796. var
  3797. paras: tcallparanode;
  3798. begin
  3799. paras:=tcallparanode(tcallparanode(left).right);
  3800. paras:=ccallparanode.create(cstringconstnode.createstr(current_module.sourcefiles.get_file_name(current_filepos.fileindex)),paras);
  3801. paras:=ccallparanode.create(genintconstnode(fileinfo.line),paras);
  3802. {$ifdef SUPPORT_GET_FRAME}
  3803. paras:=ccallparanode.create(geninlinenode(in_get_frame,false,nil),paras);
  3804. {$else}
  3805. paras:=ccallparanode.create(ccallnode.createinternfromunit('SYSTEM','GET_FRAME',nil),paras);
  3806. {$endif}
  3807. result:=cifnode.create(cnotnode.create(tcallparanode(left).left),
  3808. ccallnode.createintern('fpc_assert',paras),nil);
  3809. include(result.flags,nf_internal);
  3810. tcallparanode(left).left:=nil;
  3811. tcallparanode(left).right:=nil;
  3812. end;
  3813. function tinlinenode.first_popcnt: tnode;
  3814. var
  3815. suffix : string;
  3816. begin
  3817. case torddef(left.resultdef).ordtype of
  3818. u8bit: suffix:='byte';
  3819. u16bit: suffix:='word';
  3820. u32bit: suffix:='dword';
  3821. u64bit: suffix:='qword';
  3822. else
  3823. internalerror(2012082601);
  3824. end;
  3825. result:=ccallnode.createintern('fpc_popcnt_'+suffix,ccallparanode.create(left,nil));
  3826. left:=nil;
  3827. end;
  3828. function tinlinenode.typecheck_seg: tnode;
  3829. begin
  3830. if target_info.system in systems_managed_vm then
  3831. message(parser_e_feature_unsupported_for_vm);
  3832. set_varstate(left,vs_read,[]);
  3833. result:=cordconstnode.create(0,s32inttype,false);
  3834. end;
  3835. function tinlinenode.first_seg: tnode;
  3836. begin
  3837. internalerror(200104046);
  3838. result:=nil;
  3839. end;
  3840. function tinlinenode.first_sar: tnode;
  3841. begin
  3842. result:=nil;
  3843. expectloc:=LOC_REGISTER;
  3844. {$ifndef cpu64bitalu}
  3845. if is_64bitint(resultdef) then
  3846. begin
  3847. if (inlinenumber=in_sar_x) then
  3848. left:=ccallparanode.create(cordconstnode.create(1,u8inttype,false),
  3849. ccallparanode.create(left,nil));
  3850. result:=ccallnode.createintern('fpc_sarint64',left);
  3851. left:=nil;
  3852. end;
  3853. {$endif cpu64bitalu}
  3854. end;
  3855. function tinlinenode.handle_box: tnode;
  3856. begin
  3857. result:=nil;
  3858. if not assigned(left) or
  3859. assigned(tcallparanode(left).right) then
  3860. CGMessage1(parser_e_wrong_parameter_size,'FpcInternalBox');
  3861. resultdef:=class_tobject;
  3862. end;
  3863. function tinlinenode.handle_unbox: tnode;
  3864. begin
  3865. result:=nil;
  3866. if not assigned(left) or
  3867. not assigned(tcallparanode(left).right) or
  3868. assigned(tcallparanode(tcallparanode(left).right).right) then
  3869. CGMessage1(parser_e_wrong_parameter_size,'FpcInternalUnBox');
  3870. if tcallparanode(left).left.nodetype<>typen then
  3871. internalerror(2011071701);
  3872. ttypenode(tcallparanode(left).left).allowed:=true;
  3873. resultdef:=tcallparanode(left).left.resultdef;
  3874. end;
  3875. function tinlinenode.first_pack_unpack: tnode;
  3876. var
  3877. loopstatement : tstatementnode;
  3878. loop : tblocknode;
  3879. loopvar : ttempcreatenode;
  3880. tempnode,
  3881. source,
  3882. target,
  3883. index,
  3884. unpackednode,
  3885. packednode,
  3886. sourcevecindex,
  3887. targetvecindex,
  3888. loopbody : tnode;
  3889. temprangedef : tdef;
  3890. ulorange,
  3891. uhirange,
  3892. plorange,
  3893. phirange : TConstExprInt;
  3894. begin
  3895. { transform into a for loop which assigns the data of the (un)packed }
  3896. { array to the other one }
  3897. source := left;
  3898. if (inlinenumber = in_unpack_x_y_z) then
  3899. begin
  3900. target := tcallparanode(source).right;
  3901. index := tcallparanode(target).right;
  3902. packednode := tcallparanode(source).left;
  3903. unpackednode := tcallparanode(target).left;
  3904. end
  3905. else
  3906. begin
  3907. index := tcallparanode(source).right;
  3908. target := tcallparanode(index).right;
  3909. packednode := tcallparanode(target).left;
  3910. unpackednode := tcallparanode(source).left;
  3911. end;
  3912. source := tcallparanode(source).left;
  3913. target := tcallparanode(target).left;
  3914. index := tcallparanode(index).left;
  3915. loop := internalstatements(loopstatement);
  3916. loopvar := ctempcreatenode.create(
  3917. tarraydef(packednode.resultdef).rangedef,
  3918. tarraydef(packednode.resultdef).rangedef.size,
  3919. tt_persistent,true);
  3920. addstatement(loopstatement,loopvar);
  3921. { For range checking: we have to convert to an integer type (in case the index type }
  3922. { is an enum), add the index and loop variable together, convert the result }
  3923. { implicitly to an orddef with range equal to the rangedef to get range checking }
  3924. { and finally convert it explicitly back to the actual rangedef to avoid type }
  3925. { errors }
  3926. temprangedef:=nil;
  3927. getrange(unpackednode.resultdef,ulorange,uhirange);
  3928. getrange(packednode.resultdef,plorange,phirange);
  3929. temprangedef:=corddef.create(torddef(sinttype).ordtype,ulorange,uhirange);
  3930. sourcevecindex := ctemprefnode.create(loopvar);
  3931. targetvecindex := ctypeconvnode.create_internal(index.getcopy,sinttype);
  3932. targetvecindex := caddnode.create(subn,targetvecindex,cordconstnode.create(plorange,sinttype,true));
  3933. targetvecindex := caddnode.create(addn,targetvecindex,ctemprefnode.create(loopvar));
  3934. targetvecindex := ctypeconvnode.create(targetvecindex,temprangedef);
  3935. targetvecindex := ctypeconvnode.create_explicit(targetvecindex,tarraydef(unpackednode.resultdef).rangedef);
  3936. if (inlinenumber = in_pack_x_y_z) then
  3937. begin
  3938. { swap source and target vec indices }
  3939. tempnode := sourcevecindex;
  3940. sourcevecindex := targetvecindex;
  3941. targetvecindex := tempnode;
  3942. end;
  3943. { create the assignment in the loop body }
  3944. loopbody :=
  3945. cassignmentnode.create(
  3946. cvecnode.create(target.getcopy,targetvecindex),
  3947. cvecnode.create(source.getcopy,sourcevecindex)
  3948. );
  3949. { create the actual for loop }
  3950. tempnode := cfornode.create(
  3951. ctemprefnode.create(loopvar),
  3952. cinlinenode.create(in_low_x,false,packednode.getcopy),
  3953. cinlinenode.create(in_high_x,false,packednode.getcopy),
  3954. loopbody,
  3955. false);
  3956. addstatement(loopstatement,tempnode);
  3957. { free the loop counter }
  3958. addstatement(loopstatement,ctempdeletenode.create(loopvar));
  3959. result := loop;
  3960. end;
  3961. function tinlinenode.first_fma: tnode;
  3962. begin
  3963. CGMessage1(cg_e_function_not_support_by_selected_instruction_set,'FMA');
  3964. result:=nil;
  3965. end;
  3966. end.