ninl.pas 167 KB

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