ninl.pas 127 KB

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