ninl.pas 168 KB

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