ninl.pas 157 KB

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