ninl.pas 137 KB

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