ninl.pas 136 KB

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