ninl.pas 132 KB

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