ninl.pas 151 KB

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