2
0

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