ninl.pas 112 KB

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