ninl.pas 163 KB

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