ninl.pas 112 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875
  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 floating_point_range_check_error then
  1112. begin
  1113. result:=crealconstnode.create(0,pbestrealtype^);
  1114. CGMessage(type_e_wrong_math_argument)
  1115. end
  1116. else
  1117. begin
  1118. if r=0.0 then
  1119. result:=crealconstnode.create(MathQNaN.Value,pbestrealtype^)
  1120. else
  1121. result:=crealconstnode.create(MathNegInf.Value,pbestrealtype^)
  1122. end
  1123. else
  1124. result:=crealconstnode.create(ln(r),pbestrealtype^)
  1125. end;
  1126. function handle_sqrt_const(r : bestreal) : tnode;
  1127. begin
  1128. if r<0.0 then
  1129. if floating_point_range_check_error then
  1130. begin
  1131. result:=crealconstnode.create(0,pbestrealtype^);
  1132. CGMessage(type_e_wrong_math_argument)
  1133. end
  1134. else
  1135. result:=crealconstnode.create(MathQNaN.Value,pbestrealtype^)
  1136. else
  1137. result:=crealconstnode.create(sqrt(r),pbestrealtype^)
  1138. end;
  1139. var
  1140. hp : tnode;
  1141. vl,vl2 : TConstExprInt;
  1142. vr : bestreal;
  1143. checkrange: boolean;
  1144. begin { simplify }
  1145. result:=nil;
  1146. { handle intern constant functions in separate case }
  1147. if nf_inlineconst in flags then
  1148. begin
  1149. { no parameters? }
  1150. if not assigned(left) then
  1151. internalerror(200501231)
  1152. else
  1153. begin
  1154. vl:=0;
  1155. vl2:=0; { second parameter Ex: ptr(vl,vl2) }
  1156. case left.nodetype of
  1157. realconstn :
  1158. begin
  1159. { Real functions are all handled with internproc below }
  1160. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename)
  1161. end;
  1162. ordconstn :
  1163. vl:=tordconstnode(left).value;
  1164. callparan :
  1165. begin
  1166. { both exists, else it was not generated }
  1167. vl:=tordconstnode(tcallparanode(left).left).value;
  1168. vl2:=tordconstnode(tcallparanode(tcallparanode(left).right).left).value;
  1169. end;
  1170. else
  1171. CGMessage(parser_e_illegal_expression);
  1172. end;
  1173. case inlinenumber of
  1174. in_const_abs :
  1175. hp:=genintconstnode(abs(vl));
  1176. in_const_sqr :
  1177. hp:=genintconstnode(sqr(vl));
  1178. in_const_odd :
  1179. hp:=cordconstnode.create(byte(odd(vl)),booltype,true);
  1180. in_const_swap_word :
  1181. hp:=cordconstnode.create((vl and $ff) shl 8+(vl shr 8),left.resultdef,true);
  1182. in_const_swap_long :
  1183. hp:=cordconstnode.create((vl and $ffff) shl 16+(vl shr 16),left.resultdef,true);
  1184. in_const_swap_qword :
  1185. hp:=cordconstnode.create((vl and $ffff) shl 32+(vl shr 32),left.resultdef,true);
  1186. in_const_ptr :
  1187. hp:=cpointerconstnode.create((vl2 shl 4)+vl,voidfarpointertype);
  1188. else
  1189. internalerror(88);
  1190. end;
  1191. end;
  1192. if hp=nil then
  1193. hp:=cerrornode.create;
  1194. result:=hp;
  1195. end
  1196. else
  1197. begin
  1198. case inlinenumber of
  1199. in_lo_long,
  1200. in_hi_long,
  1201. in_lo_qword,
  1202. in_hi_qword,
  1203. in_lo_word,
  1204. in_hi_word :
  1205. begin
  1206. if left.nodetype=ordconstn then
  1207. begin
  1208. case inlinenumber of
  1209. in_lo_word :
  1210. result:=cordconstnode.create(tordconstnode(left).value and $ff,u8inttype,true);
  1211. in_hi_word :
  1212. result:=cordconstnode.create(tordconstnode(left).value shr 8,u8inttype,true);
  1213. in_lo_long :
  1214. result:=cordconstnode.create(tordconstnode(left).value and $ffff,u16inttype,true);
  1215. in_hi_long :
  1216. result:=cordconstnode.create(tordconstnode(left).value shr 16,u16inttype,true);
  1217. in_lo_qword :
  1218. result:=cordconstnode.create(tordconstnode(left).value and $ffffffff,u32inttype,true);
  1219. in_hi_qword :
  1220. result:=cordconstnode.create(tordconstnode(left).value shr 32,u32inttype,true);
  1221. end;
  1222. end;
  1223. end;
  1224. in_ord_x:
  1225. begin
  1226. case left.resultdef.typ of
  1227. orddef :
  1228. begin
  1229. case torddef(left.resultdef).ordtype of
  1230. bool8bit,
  1231. uchar:
  1232. begin
  1233. { change to byte() }
  1234. result:=ctypeconvnode.create_internal(left,u8inttype);
  1235. left:=nil;
  1236. end;
  1237. bool16bit,
  1238. uwidechar :
  1239. begin
  1240. { change to word() }
  1241. result:=ctypeconvnode.create_internal(left,u16inttype);
  1242. left:=nil;
  1243. end;
  1244. bool32bit :
  1245. begin
  1246. { change to dword() }
  1247. result:=ctypeconvnode.create_internal(left,u32inttype);
  1248. left:=nil;
  1249. end;
  1250. bool64bit :
  1251. begin
  1252. { change to qword() }
  1253. result:=ctypeconvnode.create_internal(left,u64inttype);
  1254. left:=nil;
  1255. end;
  1256. uvoid :
  1257. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  1258. else
  1259. begin
  1260. { all other orddef need no transformation }
  1261. result:=left;
  1262. left:=nil;
  1263. end;
  1264. end;
  1265. end;
  1266. enumdef :
  1267. begin
  1268. result:=ctypeconvnode.create_internal(left,s32inttype);
  1269. left:=nil;
  1270. end;
  1271. pointerdef :
  1272. begin
  1273. if m_mac in current_settings.modeswitches then
  1274. begin
  1275. result:=ctypeconvnode.create_internal(left,ptruinttype);
  1276. left:=nil;
  1277. end
  1278. end;
  1279. end;
  1280. (*
  1281. if (left.nodetype=ordconstn) then
  1282. begin
  1283. result:=cordconstnode.create(
  1284. tordconstnode(left).value,sinttype,true);
  1285. end
  1286. else if (m_mac in current_settings.modeswitches) and
  1287. (left.ndoetype=pointerconstn) then
  1288. result:=cordconstnode.create(
  1289. tpointerconstnode(left).value,ptruinttype,true);
  1290. *)
  1291. end;
  1292. in_chr_byte:
  1293. begin
  1294. { convert to explicit char() }
  1295. result:=ctypeconvnode.create_internal(left,cchartype);
  1296. left:=nil;
  1297. end;
  1298. in_length_x:
  1299. begin
  1300. case left.resultdef.typ of
  1301. stringdef :
  1302. begin
  1303. if (left.nodetype=stringconstn) then
  1304. begin
  1305. result:=cordconstnode.create(
  1306. tstringconstnode(left).len,sinttype,true);
  1307. end;
  1308. end;
  1309. orddef :
  1310. begin
  1311. { length of char is always one }
  1312. if is_char(left.resultdef) or
  1313. is_widechar(left.resultdef) then
  1314. begin
  1315. result:=cordconstnode.create(1,sinttype,false);
  1316. end
  1317. end;
  1318. arraydef :
  1319. begin
  1320. if not is_open_array(left.resultdef) and
  1321. not is_array_of_const(left.resultdef) and
  1322. not is_dynamic_array(left.resultdef) then
  1323. result:=cordconstnode.create(tarraydef(left.resultdef).highrange-
  1324. tarraydef(left.resultdef).lowrange+1,
  1325. sinttype,true);
  1326. end;
  1327. end;
  1328. end;
  1329. in_assigned_x:
  1330. begin
  1331. if is_constnode(tcallparanode(left).left) or
  1332. (tcallparanode(left).left.nodetype = pointerconstn) then
  1333. begin
  1334. { let an add node figure it out }
  1335. result:=caddnode.create(unequaln,tcallparanode(left).left,cnilnode.create);
  1336. tcallparanode(left).left := nil;
  1337. end;
  1338. end;
  1339. in_pred_x,
  1340. in_succ_x:
  1341. begin
  1342. { only perform range checking if the result is an enum }
  1343. checkrange:=(resultdef.typ=enumdef);
  1344. if (left.nodetype=ordconstn) then
  1345. begin
  1346. if (inlinenumber=in_succ_x) then
  1347. result:=cordconstnode.create(tordconstnode(left).value+1,left.resultdef,checkrange)
  1348. else
  1349. result:=cordconstnode.create(tordconstnode(left).value-1,left.resultdef,checkrange);
  1350. end;
  1351. end;
  1352. in_low_x,
  1353. in_high_x:
  1354. begin
  1355. case left.resultdef.typ of
  1356. orddef,
  1357. enumdef:
  1358. begin
  1359. result:=do_lowhigh(left.resultdef);
  1360. end;
  1361. setdef:
  1362. begin
  1363. result:=do_lowhigh(tsetdef(left.resultdef).elementdef);
  1364. end;
  1365. arraydef:
  1366. begin
  1367. if (inlinenumber=in_low_x) then
  1368. begin
  1369. result:=cordconstnode.create(int64(tarraydef(
  1370. left.resultdef).lowrange),tarraydef(left.resultdef).rangedef,true);
  1371. end
  1372. else if not is_open_array(left.resultdef) and
  1373. not is_array_of_const(left.resultdef) and
  1374. not is_dynamic_array(left.resultdef) then
  1375. result:=cordconstnode.create(int64(tarraydef(left.resultdef).highrange),
  1376. tarraydef(left.resultdef).rangedef,true);
  1377. end;
  1378. stringdef:
  1379. begin
  1380. if inlinenumber=in_low_x then
  1381. begin
  1382. result:=cordconstnode.create(0,u8inttype,false);
  1383. end
  1384. else if not is_ansistring(left.resultdef) and
  1385. not is_widestring(left.resultdef) then
  1386. result:=cordconstnode.create(tstringdef(left.resultdef).len,u8inttype,true)
  1387. end;
  1388. end;
  1389. end;
  1390. in_exp_real :
  1391. begin
  1392. if left.nodetype in [ordconstn,realconstn] then
  1393. begin
  1394. result:=crealconstnode.create(exp(getconstrealvalue),pbestrealtype^);
  1395. if (trealconstnode(result).value_real=MathInf.Value) and
  1396. floating_point_range_check_error then
  1397. begin
  1398. result:=crealconstnode.create(0,pbestrealtype^);
  1399. CGMessage(parser_e_range_check_error);
  1400. end;
  1401. end
  1402. end;
  1403. in_trunc_real :
  1404. begin
  1405. if left.nodetype in [ordconstn,realconstn] then
  1406. begin
  1407. vr:=getconstrealvalue;
  1408. if (vr>=9223372036854775807.5) or (vr<=-9223372036854775808.5) then
  1409. begin
  1410. CGMessage(parser_e_range_check_error);
  1411. result:=cordconstnode.create(1,s64inttype,false)
  1412. end
  1413. else
  1414. result:=cordconstnode.create(trunc(vr),s64inttype,true)
  1415. end
  1416. end;
  1417. in_round_real :
  1418. begin
  1419. if left.nodetype in [ordconstn,realconstn] then
  1420. begin
  1421. vr:=getconstrealvalue;
  1422. if (vr>=9223372036854775807.5) or (vr<=-9223372036854775808.5) then
  1423. begin
  1424. CGMessage(parser_e_range_check_error);
  1425. result:=cordconstnode.create(1,s64inttype,false)
  1426. end
  1427. else
  1428. result:=cordconstnode.create(round(vr),s64inttype,true)
  1429. end
  1430. end;
  1431. in_frac_real :
  1432. begin
  1433. if left.nodetype in [ordconstn,realconstn] then
  1434. setconstrealvalue(frac(getconstrealvalue))
  1435. end;
  1436. in_int_real :
  1437. begin
  1438. if left.nodetype in [ordconstn,realconstn] then
  1439. setconstrealvalue(int(getconstrealvalue));
  1440. end;
  1441. in_pi_real :
  1442. begin
  1443. if block_type=bt_const then
  1444. setconstrealvalue(getpi)
  1445. end;
  1446. in_cos_real :
  1447. begin
  1448. if left.nodetype in [ordconstn,realconstn] then
  1449. setconstrealvalue(cos(getconstrealvalue))
  1450. end;
  1451. in_sin_real :
  1452. begin
  1453. if left.nodetype in [ordconstn,realconstn] then
  1454. setconstrealvalue(sin(getconstrealvalue))
  1455. end;
  1456. in_arctan_real :
  1457. begin
  1458. if left.nodetype in [ordconstn,realconstn] then
  1459. setconstrealvalue(arctan(getconstrealvalue))
  1460. end;
  1461. in_abs_real :
  1462. begin
  1463. if left.nodetype in [ordconstn,realconstn] then
  1464. setconstrealvalue(abs(getconstrealvalue))
  1465. end;
  1466. in_sqr_real :
  1467. begin
  1468. if left.nodetype in [ordconstn,realconstn] then
  1469. setconstrealvalue(sqr(getconstrealvalue))
  1470. end;
  1471. in_sqrt_real :
  1472. begin
  1473. if left.nodetype in [ordconstn,realconstn] then
  1474. begin
  1475. vr:=getconstrealvalue;
  1476. if vr<0.0 then
  1477. result:=handle_sqrt_const(vr)
  1478. else
  1479. setconstrealvalue(sqrt(vr));
  1480. end
  1481. end;
  1482. in_ln_real :
  1483. begin
  1484. if left.nodetype in [ordconstn,realconstn] then
  1485. begin
  1486. vr:=getconstrealvalue;
  1487. if vr<=0.0 then
  1488. result:=handle_ln_const(vr)
  1489. else
  1490. setconstrealvalue(ln(vr));
  1491. end
  1492. end;
  1493. in_assert_x_y :
  1494. begin
  1495. if not(cs_do_assertion in current_settings.localswitches) then
  1496. { we need a valid node, so insert a nothingn }
  1497. result:=cnothingnode.create;
  1498. end;
  1499. end;
  1500. end;
  1501. end;
  1502. function tinlinenode.pass_typecheck:tnode;
  1503. procedure setfloatresultdef;
  1504. begin
  1505. if (left.resultdef.typ=floatdef) and
  1506. (tfloatdef(left.resultdef).floattype in [s32real,s64real,s80real,s128real]) then
  1507. resultdef:=left.resultdef
  1508. else
  1509. begin
  1510. if (left.nodetype <> ordconstn) then
  1511. inserttypeconv(left,pbestrealtype^);
  1512. resultdef:=pbestrealtype^;
  1513. end;
  1514. end;
  1515. procedure handle_pack_unpack;
  1516. var
  1517. source, target, index: tcallparanode;
  1518. unpackedarraydef, packedarraydef: tarraydef;
  1519. tempindex: TConstExprInt;
  1520. begin
  1521. resultdef:=voidtype;
  1522. unpackedarraydef := nil;
  1523. packedarraydef := nil;
  1524. source := tcallparanode(left);
  1525. if (inlinenumber = in_unpack_x_y_z) then
  1526. begin
  1527. target := tcallparanode(source.right);
  1528. index := tcallparanode(target.right);
  1529. { source must be a packed array }
  1530. if not is_packed_array(source.left.resultdef) then
  1531. CGMessagePos2(source.left.fileinfo,type_e_got_expected_packed_array,'1',source.left.resultdef.GetTypeName)
  1532. else
  1533. packedarraydef := tarraydef(source.left.resultdef);
  1534. { target can be any kind of array, as long as it's not packed }
  1535. if (target.left.resultdef.typ <> arraydef) or
  1536. is_packed_array(target.left.resultdef) then
  1537. CGMessagePos2(target.left.fileinfo,type_e_got_expected_unpacked_array,'2',target.left.resultdef.GetTypeName)
  1538. else
  1539. unpackedarraydef := tarraydef(target.left.resultdef);
  1540. end
  1541. else
  1542. begin
  1543. index := tcallparanode(source.right);
  1544. target := tcallparanode(index.right);
  1545. { source can be any kind of array, as long as it's not packed }
  1546. if (source.left.resultdef.typ <> arraydef) or
  1547. is_packed_array(source.left.resultdef) then
  1548. CGMessagePos2(source.left.fileinfo,type_e_got_expected_unpacked_array,'1',source.left.resultdef.GetTypeName)
  1549. else
  1550. unpackedarraydef := tarraydef(source.left.resultdef);
  1551. { target must be a packed array }
  1552. if not is_packed_array(target.left.resultdef) then
  1553. CGMessagePos2(target.left.fileinfo,type_e_got_expected_packed_array,'3',target.left.resultdef.GetTypeName)
  1554. else
  1555. packedarraydef := tarraydef(target.left.resultdef);
  1556. end;
  1557. if assigned(unpackedarraydef) then
  1558. begin
  1559. { index must be compatible with the unpacked array's indextype }
  1560. inserttypeconv(index.left,unpackedarraydef.rangedef);
  1561. { range check at compile time if possible }
  1562. if assigned(packedarraydef) and
  1563. (index.left.nodetype = ordconstn) and
  1564. not is_special_array(unpackedarraydef) then
  1565. begin
  1566. testrange(index.left.resultdef,unpackedarraydef,tordconstnode(index.left).value,false);
  1567. tempindex := tordconstnode(index.left).value + packedarraydef.highrange-packedarraydef.lowrange;
  1568. testrange(index.left.resultdef,unpackedarraydef,tempindex,false);
  1569. end;
  1570. end;
  1571. { source array is read and must be valid }
  1572. set_varstate(source.left,vs_read,[vsf_must_be_valid]);
  1573. { target array is written }
  1574. valid_for_assignment(target.left,true);
  1575. set_varstate(target.left,vs_written,[]);
  1576. { index in the unpacked array is read and must be valid }
  1577. set_varstate(index.left,vs_read,[vsf_must_be_valid]);
  1578. { if the size of the arrays is 0 (array of empty records), }
  1579. { do nothing }
  1580. if (source.resultdef.size = 0) then
  1581. result:=cnothingnode.create;
  1582. end;
  1583. var
  1584. hightree,
  1585. hp : tnode;
  1586. begin
  1587. result:=nil;
  1588. { when handling writeln "left" contains no valid address }
  1589. if assigned(left) then
  1590. begin
  1591. if left.nodetype=callparan then
  1592. tcallparanode(left).get_paratype
  1593. else
  1594. typecheckpass(left);
  1595. end;
  1596. if not(nf_inlineconst in flags) then
  1597. begin
  1598. case inlinenumber of
  1599. in_lo_long,
  1600. in_hi_long,
  1601. in_lo_qword,
  1602. in_hi_qword,
  1603. in_lo_word,
  1604. in_hi_word :
  1605. begin
  1606. { give warning for incompatibility with tp and delphi }
  1607. if (inlinenumber in [in_lo_long,in_hi_long,in_lo_qword,in_hi_qword]) and
  1608. ((m_tp7 in current_settings.modeswitches) or
  1609. (m_delphi in current_settings.modeswitches)) then
  1610. CGMessage(type_w_maybe_wrong_hi_lo);
  1611. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1612. if not is_integer(left.resultdef) then
  1613. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename);
  1614. case inlinenumber of
  1615. in_lo_word,
  1616. in_hi_word :
  1617. resultdef:=u8inttype;
  1618. in_lo_long,
  1619. in_hi_long :
  1620. resultdef:=u16inttype;
  1621. in_lo_qword,
  1622. in_hi_qword :
  1623. resultdef:=u32inttype;
  1624. end;
  1625. end;
  1626. in_sizeof_x:
  1627. begin
  1628. { the constant evaluation of in_sizeof_x happens in pexpr where possible }
  1629. set_varstate(left,vs_read,[]);
  1630. if paramanager.push_high_param(vs_value,left.resultdef,current_procinfo.procdef.proccalloption) then
  1631. begin
  1632. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  1633. if assigned(hightree) then
  1634. begin
  1635. hp:=caddnode.create(addn,hightree,
  1636. cordconstnode.create(1,sinttype,false));
  1637. if (left.resultdef.typ=arraydef) then
  1638. if not is_packed_array(tarraydef(left.resultdef)) then
  1639. begin
  1640. if (tarraydef(left.resultdef).elesize<>1) then
  1641. hp:=caddnode.create(muln,hp,cordconstnode.create(tarraydef(
  1642. left.resultdef).elesize,sinttype,true));
  1643. end
  1644. else if (tarraydef(left.resultdef).elepackedbitsize <> 8) then
  1645. begin
  1646. { no packed open array support yet }
  1647. if (hp.nodetype <> ordconstn) then
  1648. internalerror(2006081511);
  1649. hp.free;
  1650. hp := cordconstnode.create(left.resultdef.size,sinttype,true);
  1651. {
  1652. hp:=
  1653. ctypeconvnode.create_explicit(sinttype,
  1654. cmoddivnode.create(divn,
  1655. caddnode.create(addn,
  1656. caddnode.create(muln,hp,cordconstnode.create(tarraydef(
  1657. left.resultdef).elepackedbitsize,s64inttype,true)),
  1658. cordconstnode.create(a,s64inttype,true)),
  1659. cordconstnode.create(8,s64inttype,true)),
  1660. sinttype);
  1661. }
  1662. end;
  1663. result:=hp;
  1664. end;
  1665. end
  1666. else
  1667. resultdef:=sinttype;
  1668. end;
  1669. in_typeof_x:
  1670. begin
  1671. set_varstate(left,vs_read,[]);
  1672. resultdef:=voidpointertype;
  1673. end;
  1674. in_ord_x:
  1675. begin
  1676. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1677. case left.resultdef.typ of
  1678. orddef,
  1679. enumdef :
  1680. ;
  1681. pointerdef :
  1682. begin
  1683. if not(m_mac in current_settings.modeswitches) then
  1684. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  1685. end
  1686. else
  1687. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  1688. end;
  1689. end;
  1690. in_chr_byte:
  1691. begin
  1692. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1693. end;
  1694. in_length_x:
  1695. begin
  1696. if ((left.resultdef.typ=arraydef) and
  1697. (not is_special_array(left.resultdef) or
  1698. is_open_array(left.resultdef))) or
  1699. (left.resultdef.typ=orddef) then
  1700. set_varstate(left,vs_read,[])
  1701. else
  1702. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1703. case left.resultdef.typ of
  1704. variantdef:
  1705. begin
  1706. inserttypeconv(left,cansistringtype);
  1707. end;
  1708. stringdef :
  1709. begin
  1710. { we don't need string convertions here, }
  1711. { except if from widestring to ansistring }
  1712. { and vice versa (that can change the }
  1713. { length) }
  1714. if (left.nodetype=typeconvn) and
  1715. (ttypeconvnode(left).left.resultdef.typ=stringdef) and
  1716. not(is_widestring(left.resultdef) xor
  1717. is_widestring(ttypeconvnode(left).left.resultdef)) then
  1718. begin
  1719. hp:=ttypeconvnode(left).left;
  1720. ttypeconvnode(left).left:=nil;
  1721. left.free;
  1722. left:=hp;
  1723. end;
  1724. end;
  1725. orddef :
  1726. begin
  1727. { will be handled in simplify }
  1728. if not is_char(left.resultdef) and
  1729. not is_widechar(left.resultdef) then
  1730. CGMessage(type_e_mismatch);
  1731. end;
  1732. pointerdef :
  1733. begin
  1734. if is_pchar(left.resultdef) then
  1735. begin
  1736. hp := ccallparanode.create(left,nil);
  1737. result := ccallnode.createintern('fpc_pchar_length',hp);
  1738. { make sure the left node doesn't get disposed, since it's }
  1739. { reused in the new node (JM) }
  1740. left:=nil;
  1741. exit;
  1742. end
  1743. else if is_pwidechar(left.resultdef) then
  1744. begin
  1745. hp := ccallparanode.create(left,nil);
  1746. result := ccallnode.createintern('fpc_pwidechar_length',hp);
  1747. { make sure the left node doesn't get disposed, since it's }
  1748. { reused in the new node (JM) }
  1749. left:=nil;
  1750. exit;
  1751. end
  1752. else
  1753. CGMessage(type_e_mismatch);
  1754. end;
  1755. arraydef :
  1756. begin
  1757. if is_open_array(left.resultdef) or
  1758. is_array_of_const(left.resultdef) then
  1759. begin
  1760. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  1761. if assigned(hightree) then
  1762. result:=caddnode.create(addn,hightree,
  1763. cordconstnode.create(1,sinttype,false));
  1764. exit;
  1765. end
  1766. else if is_dynamic_array(left.resultdef) then
  1767. begin
  1768. hp := ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil);
  1769. result := ccallnode.createintern('fpc_dynarray_length',hp);
  1770. { make sure the left node doesn't get disposed, since it's }
  1771. { reused in the new node (JM) }
  1772. left:=nil;
  1773. exit;
  1774. end
  1775. else
  1776. begin
  1777. { will be handled in simplify }
  1778. end;
  1779. end
  1780. else
  1781. CGMessage(type_e_mismatch);
  1782. end;
  1783. { shortstring return an 8 bit value as the length
  1784. is the first byte of the string }
  1785. if is_shortstring(left.resultdef) then
  1786. resultdef:=u8inttype
  1787. else
  1788. resultdef:=sinttype;
  1789. end;
  1790. in_typeinfo_x:
  1791. begin
  1792. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1793. resultdef:=voidpointertype;
  1794. end;
  1795. in_assigned_x:
  1796. begin
  1797. { the parser has already made sure the expression is valid }
  1798. { there could be a procvar, which is 2*sizeof(pointer), while we }
  1799. { must only check the first pointer -> can't just convert to an }
  1800. { add node in all cases }
  1801. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  1802. resultdef:=booltype;
  1803. end;
  1804. in_ofs_x :
  1805. internalerror(2000101001);
  1806. in_seg_x :
  1807. begin
  1808. set_varstate(left,vs_read,[]);
  1809. result:=cordconstnode.create(0,s32inttype,false);
  1810. end;
  1811. in_pred_x,
  1812. in_succ_x:
  1813. begin
  1814. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1815. resultdef:=left.resultdef;
  1816. if not is_ordinal(resultdef) then
  1817. CGMessage(type_e_ordinal_expr_expected)
  1818. else
  1819. begin
  1820. if (resultdef.typ=enumdef) and
  1821. (tenumdef(resultdef).has_jumps) and
  1822. not(m_delphi in current_settings.modeswitches) then
  1823. CGMessage(type_e_succ_and_pred_enums_with_assign_not_possible);
  1824. end;
  1825. end;
  1826. in_initialize_x,
  1827. in_finalize_x,
  1828. in_setlength_x:
  1829. begin
  1830. { inlined from pinline }
  1831. internalerror(200204231);
  1832. end;
  1833. in_inc_x,
  1834. in_dec_x:
  1835. begin
  1836. resultdef:=voidtype;
  1837. if assigned(left) then
  1838. begin
  1839. { first param must be var }
  1840. valid_for_var(tcallparanode(left).left,true);
  1841. set_varstate(tcallparanode(left).left,vs_readwritten,[vsf_must_be_valid]);
  1842. if (left.resultdef.typ in [enumdef,pointerdef]) or
  1843. is_ordinal(left.resultdef) or
  1844. is_currency(left.resultdef) then
  1845. begin
  1846. { value of left gets changed -> must be unique }
  1847. set_unique(tcallparanode(left).left);
  1848. { two paras ? }
  1849. if assigned(tcallparanode(left).right) then
  1850. begin
  1851. if is_integer(tcallparanode(left).right.resultdef) then
  1852. begin
  1853. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  1854. inserttypeconv_internal(tcallparanode(tcallparanode(left).right).left,tcallparanode(left).left.resultdef);
  1855. if assigned(tcallparanode(tcallparanode(left).right).right) then
  1856. { should be handled in the parser (JM) }
  1857. internalerror(2006020901);
  1858. end
  1859. else
  1860. CGMessagePos(tcallparanode(left).right.fileinfo,type_e_ordinal_expr_expected);
  1861. end;
  1862. end
  1863. else
  1864. CGMessagePos(left.fileinfo,type_e_ordinal_expr_expected);
  1865. end
  1866. else
  1867. CGMessagePos(fileinfo,type_e_mismatch);
  1868. end;
  1869. in_read_x,
  1870. in_readln_x,
  1871. in_write_x,
  1872. in_writeln_x :
  1873. begin
  1874. result := handle_read_write;
  1875. end;
  1876. in_settextbuf_file_x :
  1877. begin
  1878. resultdef:=voidtype;
  1879. { now we know the type of buffer }
  1880. hp:=ccallparanode.create(cordconstnode.create(
  1881. tcallparanode(left).left.resultdef.size,s32inttype,true),left);
  1882. result:=ccallnode.createintern('SETTEXTBUF',hp);
  1883. left:=nil;
  1884. end;
  1885. { the firstpass of the arg has been done in firstcalln ? }
  1886. in_reset_typedfile,
  1887. in_rewrite_typedfile :
  1888. begin
  1889. result := handle_reset_rewrite_typed;
  1890. end;
  1891. in_str_x_string :
  1892. begin
  1893. result:=handle_str;
  1894. end;
  1895. in_val_x :
  1896. begin
  1897. result:=handle_val;
  1898. end;
  1899. in_include_x_y,
  1900. in_exclude_x_y:
  1901. begin
  1902. resultdef:=voidtype;
  1903. { the parser already checks whether we have two (and exactly two) }
  1904. { parameters (JM) }
  1905. { first param must be var }
  1906. valid_for_var(tcallparanode(left).left,true);
  1907. set_varstate(tcallparanode(left).left,vs_readwritten,[vsf_must_be_valid]);
  1908. { check type }
  1909. if (left.resultdef.typ=setdef) then
  1910. begin
  1911. { insert a type conversion }
  1912. { to the type of the set elements }
  1913. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  1914. inserttypeconv(tcallparanode(tcallparanode(left).right).left,
  1915. tsetdef(left.resultdef).elementdef);
  1916. end
  1917. else
  1918. CGMessage(type_e_mismatch);
  1919. end;
  1920. in_pack_x_y_z,
  1921. in_unpack_x_y_z :
  1922. begin
  1923. handle_pack_unpack;
  1924. end;
  1925. in_slice_x:
  1926. begin
  1927. result:=nil;
  1928. resultdef:=tcallparanode(left).left.resultdef;
  1929. if (resultdef.typ <> arraydef) then
  1930. CGMessagePos(left.fileinfo,type_e_mismatch)
  1931. else if is_packed_array(resultdef) then
  1932. CGMessagePos2(left.fileinfo,type_e_got_expected_unpacked_array,'1',resultdef.typename);
  1933. if not(is_integer(tcallparanode(tcallparanode(left).right).left.resultdef)) then
  1934. CGMessagePos1(tcallparanode(left).right.fileinfo,
  1935. type_e_integer_expr_expected,
  1936. tcallparanode(tcallparanode(left).right).left.resultdef.typename);
  1937. end;
  1938. in_low_x,
  1939. in_high_x:
  1940. begin
  1941. case left.resultdef.typ of
  1942. orddef,
  1943. enumdef,
  1944. setdef:
  1945. ;
  1946. arraydef:
  1947. begin
  1948. if (inlinenumber=in_low_x) then
  1949. set_varstate(left,vs_read,[])
  1950. else
  1951. begin
  1952. if is_open_array(left.resultdef) or
  1953. is_array_of_const(left.resultdef) then
  1954. begin
  1955. set_varstate(left,vs_read,[]);
  1956. result:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  1957. end
  1958. else
  1959. if is_dynamic_array(left.resultdef) then
  1960. begin
  1961. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1962. { can't use inserttypeconv because we need }
  1963. { an explicit type conversion (JM) }
  1964. hp := ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil);
  1965. result := ccallnode.createintern('fpc_dynarray_high',hp);
  1966. { make sure the left node doesn't get disposed, since it's }
  1967. { reused in the new node (JM) }
  1968. left:=nil;
  1969. end
  1970. else
  1971. begin
  1972. set_varstate(left,vs_read,[]);
  1973. end;
  1974. end;
  1975. end;
  1976. stringdef:
  1977. begin
  1978. if inlinenumber=in_low_x then
  1979. begin
  1980. set_varstate(left,vs_read,[]);
  1981. end
  1982. else
  1983. begin
  1984. if is_open_string(left.resultdef) then
  1985. begin
  1986. set_varstate(left,vs_read,[]);
  1987. result:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry))
  1988. end
  1989. else if is_ansistring(left.resultdef) or
  1990. is_widestring(left.resultdef) then
  1991. CGMessage(type_e_mismatch)
  1992. end;
  1993. end;
  1994. else
  1995. CGMessage(type_e_mismatch);
  1996. end;
  1997. end;
  1998. in_exp_real,
  1999. in_frac_real,
  2000. in_int_real,
  2001. in_cos_real,
  2002. in_sin_real,
  2003. in_arctan_real,
  2004. in_abs_real,
  2005. in_ln_real :
  2006. begin
  2007. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2008. { converting an int64 to double on platforms without }
  2009. { extended can cause precision loss }
  2010. if not(left.nodetype in [ordconstn,realconstn]) then
  2011. inserttypeconv(left,pbestrealtype^);
  2012. resultdef:=pbestrealtype^;
  2013. end;
  2014. in_trunc_real,
  2015. in_round_real :
  2016. begin
  2017. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2018. { for direct float rounding, no best real type cast should be necessary }
  2019. if not((left.resultdef.typ=floatdef) and
  2020. (tfloatdef(left.resultdef).floattype in [s32real,s64real,s80real,s128real])) and
  2021. { converting an int64 to double on platforms without }
  2022. { extended can cause precision loss }
  2023. not(left.nodetype in [ordconstn,realconstn]) then
  2024. inserttypeconv(left,pbestrealtype^);
  2025. resultdef:=s64inttype;
  2026. end;
  2027. in_pi_real :
  2028. begin
  2029. resultdef:=pbestrealtype^;
  2030. end;
  2031. in_sqr_real,
  2032. in_sqrt_real :
  2033. begin
  2034. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2035. setfloatresultdef;
  2036. end;
  2037. {$ifdef SUPPORT_MMX}
  2038. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  2039. begin
  2040. end;
  2041. {$endif SUPPORT_MMX}
  2042. {$ifdef SUPPORT_UNALIGNED}
  2043. in_unaligned_x:
  2044. begin
  2045. resultdef:=left.resultdef;
  2046. end;
  2047. {$endif SUPPORT_UNALIGNED}
  2048. in_assert_x_y :
  2049. begin
  2050. resultdef:=voidtype;
  2051. if assigned(left) then
  2052. begin
  2053. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  2054. { check type }
  2055. if is_boolean(left.resultdef) then
  2056. begin
  2057. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  2058. { must always be a string }
  2059. inserttypeconv(tcallparanode(tcallparanode(left).right).left,cshortstringtype);
  2060. end
  2061. else
  2062. CGMessage1(type_e_boolean_expr_expected,left.resultdef.typename);
  2063. end
  2064. else
  2065. CGMessage(type_e_mismatch);
  2066. if (cs_do_assertion in current_settings.localswitches) then
  2067. include(current_procinfo.flags,pi_do_call);
  2068. end;
  2069. in_prefetch_var:
  2070. resultdef:=voidtype;
  2071. in_get_frame,
  2072. in_get_caller_frame,
  2073. in_get_caller_addr:
  2074. begin
  2075. resultdef:=voidpointertype;
  2076. end;
  2077. else
  2078. internalerror(8);
  2079. end;
  2080. end;
  2081. if not assigned(result) and not
  2082. codegenerror then
  2083. result:=simplify;
  2084. dec(parsing_para_level);
  2085. end;
  2086. function tinlinenode.pass_1 : tnode;
  2087. var
  2088. hp,hpp,resultnode : tnode;
  2089. shiftconst: longint;
  2090. tempnode: ttempcreatenode;
  2091. newstatement: tstatementnode;
  2092. newblock: tblocknode;
  2093. begin
  2094. result:=nil;
  2095. { if we handle writeln; left contains no valid address }
  2096. if assigned(left) then
  2097. begin
  2098. if left.nodetype=callparan then
  2099. tcallparanode(left).firstcallparan
  2100. else
  2101. firstpass(left);
  2102. left_max;
  2103. end;
  2104. inc(parsing_para_level);
  2105. { intern const should already be handled }
  2106. if nf_inlineconst in flags then
  2107. internalerror(200104044);
  2108. case inlinenumber of
  2109. in_lo_qword,
  2110. in_hi_qword,
  2111. in_lo_long,
  2112. in_hi_long,
  2113. in_lo_word,
  2114. in_hi_word:
  2115. begin
  2116. shiftconst := 0;
  2117. case inlinenumber of
  2118. in_hi_qword:
  2119. shiftconst := 32;
  2120. in_hi_long:
  2121. shiftconst := 16;
  2122. in_hi_word:
  2123. shiftconst := 8;
  2124. end;
  2125. if shiftconst <> 0 then
  2126. result := ctypeconvnode.create_internal(cshlshrnode.create(shrn,left,
  2127. cordconstnode.create(shiftconst,u32inttype,false)),resultdef)
  2128. else
  2129. result := ctypeconvnode.create_internal(left,resultdef);
  2130. left := nil;
  2131. firstpass(result);
  2132. end;
  2133. in_sizeof_x:
  2134. begin
  2135. if registersint<1 then
  2136. registersint:=1;
  2137. expectloc:=LOC_REGISTER;
  2138. end;
  2139. in_typeof_x:
  2140. begin
  2141. if registersint<1 then
  2142. registersint:=1;
  2143. expectloc:=LOC_REGISTER;
  2144. end;
  2145. in_length_x:
  2146. begin
  2147. if is_shortstring(left.resultdef) then
  2148. expectloc:=left.expectloc
  2149. else
  2150. begin
  2151. { ansi/wide string }
  2152. if registersint<1 then
  2153. registersint:=1;
  2154. expectloc:=LOC_REGISTER;
  2155. end;
  2156. end;
  2157. in_typeinfo_x:
  2158. begin
  2159. expectloc:=LOC_REGISTER;
  2160. registersint:=1;
  2161. end;
  2162. in_assigned_x:
  2163. begin
  2164. expectloc := LOC_JUMP;
  2165. registersint:=1;
  2166. end;
  2167. in_pred_x,
  2168. in_succ_x:
  2169. begin
  2170. if is_64bit(resultdef) then
  2171. begin
  2172. if (registersint<2) then
  2173. registersint:=2
  2174. end
  2175. else
  2176. begin
  2177. if (registersint<1) then
  2178. registersint:=1;
  2179. end;
  2180. expectloc:=LOC_REGISTER;
  2181. end;
  2182. in_setlength_x,
  2183. in_initialize_x,
  2184. in_finalize_x:
  2185. begin
  2186. expectloc:=LOC_VOID;
  2187. end;
  2188. in_inc_x,
  2189. in_dec_x:
  2190. begin
  2191. expectloc:=LOC_VOID;
  2192. { range/overflow checking doesn't work properly }
  2193. { with the inc/dec code that's generated (JM) }
  2194. if (current_settings.localswitches * [cs_check_overflow,cs_check_range] <> []) and
  2195. { No overflow check for pointer operations, because inc(pointer,-1) will always
  2196. trigger an overflow. For uint32 it works because then the operation is done
  2197. in 64bit. Range checking is not applicable to pointers either }
  2198. (tcallparanode(left).left.resultdef.typ<>pointerdef) then
  2199. { convert to simple add (JM) }
  2200. begin
  2201. newblock := internalstatements(newstatement);
  2202. { extra parameter? }
  2203. if assigned(tcallparanode(left).right) then
  2204. begin
  2205. { Yes, use for add node }
  2206. hpp := tcallparanode(tcallparanode(left).right).left;
  2207. tcallparanode(tcallparanode(left).right).left := nil;
  2208. if assigned(tcallparanode(tcallparanode(left).right).right) then
  2209. CGMessage(parser_e_illegal_expression);
  2210. end
  2211. else
  2212. begin
  2213. { no, create constant 1 }
  2214. hpp := cordconstnode.create(1,tcallparanode(left).left.resultdef,false);
  2215. end;
  2216. typecheckpass(hpp);
  2217. if not((hpp.resultdef.typ=orddef) and
  2218. {$ifndef cpu64bit}
  2219. (torddef(hpp.resultdef).ordtype<>u32bit)) then
  2220. {$else not cpu64bit}
  2221. (torddef(hpp.resultdef).ordtype<>u64bit)) then
  2222. {$endif not cpu64bit}
  2223. inserttypeconv_internal(hpp,sinttype);
  2224. { make sure we don't call functions part of the left node twice (and generally }
  2225. { optimize the code generation) }
  2226. if node_complexity(tcallparanode(left).left) > 1 then
  2227. begin
  2228. tempnode := ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  2229. addstatement(newstatement,tempnode);
  2230. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  2231. caddrnode.create_internal(tcallparanode(left).left.getcopy)));
  2232. hp := cderefnode.create(ctemprefnode.create(tempnode));
  2233. inserttypeconv_internal(hp,tcallparanode(left).left.resultdef);
  2234. end
  2235. else
  2236. begin
  2237. hp := tcallparanode(left).left.getcopy;
  2238. tempnode := nil;
  2239. end;
  2240. resultnode := hp.getcopy;
  2241. { avoid type errors from the addn/subn }
  2242. if not is_integer(resultnode.resultdef) then
  2243. begin
  2244. inserttypeconv_internal(hp,sinttype);
  2245. inserttypeconv_internal(hpp,sinttype);
  2246. end;
  2247. { addition/substraction depending on inc/dec }
  2248. if inlinenumber = in_inc_x then
  2249. hpp := caddnode.create(addn,hp,hpp)
  2250. else
  2251. hpp := caddnode.create(subn,hp,hpp);
  2252. { assign result of addition }
  2253. if not(is_integer(resultnode.resultdef)) then
  2254. inserttypeconv(hpp,torddef.create(
  2255. {$ifdef cpu64bit}
  2256. s64bit,
  2257. {$else cpu64bit}
  2258. s32bit,
  2259. {$endif cpu64bit}
  2260. get_min_value(resultnode.resultdef),
  2261. get_max_value(resultnode.resultdef)))
  2262. else
  2263. inserttypeconv(hpp,resultnode.resultdef);
  2264. { avoid any possible warnings }
  2265. inserttypeconv_internal(hpp,resultnode.resultdef);
  2266. addstatement(newstatement,cassignmentnode.create(resultnode,hpp));
  2267. { deallocate the temp }
  2268. if assigned(tempnode) then
  2269. addstatement(newstatement,ctempdeletenode.create(tempnode));
  2270. { firstpass it }
  2271. firstpass(newblock);
  2272. { return new node }
  2273. result := newblock;
  2274. end
  2275. else if (left.resultdef.typ in [enumdef,pointerdef]) or
  2276. is_ordinal(left.resultdef) then
  2277. begin
  2278. { two paras ? }
  2279. if assigned(tcallparanode(left).right) then
  2280. begin
  2281. { need we an additional register ? }
  2282. if not(is_constintnode(tcallparanode(tcallparanode(left).right).left)) and
  2283. (tcallparanode(tcallparanode(left).right).left.expectloc in [LOC_CREFERENCE,LOC_REFERENCE]) and
  2284. (tcallparanode(tcallparanode(left).right).left.registersint<=1) then
  2285. inc(registersint);
  2286. { do we need an additional register to restore the first parameter? }
  2287. if tcallparanode(tcallparanode(left).right).left.registersint>=registersint then
  2288. inc(registersint);
  2289. end;
  2290. end;
  2291. end;
  2292. in_include_x_y,
  2293. in_exclude_x_y:
  2294. begin
  2295. expectloc:=LOC_VOID;
  2296. registersint:=left.registersint;
  2297. registersfpu:=left.registersfpu;
  2298. {$ifdef SUPPORT_MMX}
  2299. registersmmx:=left.registersmmx;
  2300. {$endif SUPPORT_MMX}
  2301. end;
  2302. in_pack_x_y_z,
  2303. in_unpack_x_y_z:
  2304. begin
  2305. result:=first_pack_unpack;
  2306. end;
  2307. in_exp_real:
  2308. begin
  2309. result:= first_exp_real;
  2310. end;
  2311. in_round_real:
  2312. begin
  2313. result:= first_round_real;
  2314. end;
  2315. in_trunc_real:
  2316. begin
  2317. result:= first_trunc_real;
  2318. end;
  2319. in_int_real:
  2320. begin
  2321. result:= first_int_real;
  2322. end;
  2323. in_frac_real:
  2324. begin
  2325. result:= first_frac_real;
  2326. end;
  2327. in_cos_real:
  2328. begin
  2329. result:= first_cos_real;
  2330. end;
  2331. in_sin_real:
  2332. begin
  2333. result := first_sin_real;
  2334. end;
  2335. in_arctan_real:
  2336. begin
  2337. result := first_arctan_real;
  2338. end;
  2339. in_pi_real :
  2340. begin
  2341. result := first_pi;
  2342. end;
  2343. in_abs_real:
  2344. begin
  2345. result := first_abs_real;
  2346. end;
  2347. in_sqr_real:
  2348. begin
  2349. result := first_sqr_real;
  2350. end;
  2351. in_sqrt_real:
  2352. begin
  2353. result := first_sqrt_real;
  2354. end;
  2355. in_ln_real:
  2356. begin
  2357. result := first_ln_real;
  2358. end;
  2359. {$ifdef SUPPORT_MMX}
  2360. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  2361. begin
  2362. end;
  2363. {$endif SUPPORT_MMX}
  2364. in_assert_x_y :
  2365. begin
  2366. expectloc:=LOC_VOID;
  2367. registersint:=left.registersint;
  2368. registersfpu:=left.registersfpu;
  2369. {$ifdef SUPPORT_MMX}
  2370. registersmmx:=left.registersmmx;
  2371. {$endif SUPPORT_MMX}
  2372. end;
  2373. in_low_x,
  2374. in_high_x:
  2375. internalerror(200104047);
  2376. in_slice_x:
  2377. internalerror(2005101501);
  2378. in_ord_x,
  2379. in_chr_byte:
  2380. begin
  2381. { should not happend as it's converted to typeconv }
  2382. internalerror(200104045);
  2383. end;
  2384. in_ofs_x :
  2385. internalerror(2000101001);
  2386. in_seg_x :
  2387. internalerror(200104046);
  2388. in_settextbuf_file_x,
  2389. in_reset_typedfile,
  2390. in_rewrite_typedfile,
  2391. in_str_x_string,
  2392. in_val_x,
  2393. in_read_x,
  2394. in_readln_x,
  2395. in_write_x,
  2396. in_writeln_x :
  2397. begin
  2398. { should be handled by pass_typecheck }
  2399. internalerror(200108234);
  2400. end;
  2401. in_get_frame:
  2402. begin
  2403. include(current_procinfo.flags,pi_needs_stackframe);
  2404. expectloc:=LOC_CREGISTER;
  2405. end;
  2406. in_get_caller_frame:
  2407. begin
  2408. expectloc:=LOC_REGISTER;
  2409. registersint:=1;
  2410. end;
  2411. in_get_caller_addr:
  2412. begin
  2413. expectloc:=LOC_REGISTER;
  2414. registersint:=1;
  2415. end;
  2416. in_prefetch_var:
  2417. begin
  2418. expectloc:=LOC_VOID;
  2419. end;
  2420. {$ifdef SUPPORT_UNALIGNED}
  2421. in_unaligned_x:
  2422. begin
  2423. expectloc:=tcallparanode(left).left.expectloc;
  2424. end;
  2425. {$endif SUPPORT_UNALIGNED}
  2426. else
  2427. internalerror(89);
  2428. end;
  2429. dec(parsing_para_level);
  2430. end;
  2431. {$maxfpuregisters default}
  2432. function tinlinenode.docompare(p: tnode): boolean;
  2433. begin
  2434. docompare :=
  2435. inherited docompare(p) and
  2436. (inlinenumber = tinlinenode(p).inlinenumber);
  2437. end;
  2438. function tinlinenode.first_pi : tnode;
  2439. begin
  2440. result:=crealconstnode.create(getpi,pbestrealtype^);
  2441. end;
  2442. function tinlinenode.first_arctan_real : tnode;
  2443. begin
  2444. { create the call to the helper }
  2445. { on entry left node contains the parameter }
  2446. first_arctan_real := ccallnode.createintern('fpc_arctan_real',
  2447. ccallparanode.create(left,nil));
  2448. left := nil;
  2449. end;
  2450. function tinlinenode.first_abs_real : tnode;
  2451. begin
  2452. { create the call to the helper }
  2453. { on entry left node contains the parameter }
  2454. first_abs_real := ccallnode.createintern('fpc_abs_real',
  2455. ccallparanode.create(left,nil));
  2456. left := nil;
  2457. end;
  2458. function tinlinenode.first_sqr_real : tnode;
  2459. begin
  2460. { create the call to the helper }
  2461. { on entry left node contains the parameter }
  2462. first_sqr_real := ctypeconvnode.create(ccallnode.createintern('fpc_sqr_real',
  2463. ccallparanode.create(left,nil)),resultdef);
  2464. left := nil;
  2465. end;
  2466. function tinlinenode.first_sqrt_real : tnode;
  2467. begin
  2468. { create the call to the helper }
  2469. { on entry left node contains the parameter }
  2470. first_sqrt_real := ctypeconvnode.create(ccallnode.createintern('fpc_sqrt_real',
  2471. ccallparanode.create(left,nil)),resultdef);
  2472. left := nil;
  2473. end;
  2474. function tinlinenode.first_ln_real : tnode;
  2475. begin
  2476. { create the call to the helper }
  2477. { on entry left node contains the parameter }
  2478. first_ln_real := ccallnode.createintern('fpc_ln_real',
  2479. ccallparanode.create(left,nil));
  2480. left := nil;
  2481. end;
  2482. function tinlinenode.first_cos_real : tnode;
  2483. begin
  2484. { create the call to the helper }
  2485. { on entry left node contains the parameter }
  2486. first_cos_real := ccallnode.createintern('fpc_cos_real',
  2487. ccallparanode.create(left,nil));
  2488. left := nil;
  2489. end;
  2490. function tinlinenode.first_sin_real : tnode;
  2491. begin
  2492. { create the call to the helper }
  2493. { on entry left node contains the parameter }
  2494. first_sin_real := ccallnode.createintern('fpc_sin_real',
  2495. ccallparanode.create(left,nil));
  2496. left := nil;
  2497. end;
  2498. function tinlinenode.first_exp_real : tnode;
  2499. begin
  2500. { create the call to the helper }
  2501. { on entry left node contains the parameter }
  2502. result := ccallnode.createintern('fpc_exp_real',ccallparanode.create(left,nil));
  2503. left := nil;
  2504. end;
  2505. function tinlinenode.first_int_real : tnode;
  2506. begin
  2507. { create the call to the helper }
  2508. { on entry left node contains the parameter }
  2509. result := ccallnode.createintern('fpc_int_real',ccallparanode.create(left,nil));
  2510. left := nil;
  2511. end;
  2512. function tinlinenode.first_frac_real : tnode;
  2513. begin
  2514. { create the call to the helper }
  2515. { on entry left node contains the parameter }
  2516. result := ccallnode.createintern('fpc_frac_real',ccallparanode.create(left,nil));
  2517. left := nil;
  2518. end;
  2519. function tinlinenode.first_round_real : tnode;
  2520. begin
  2521. { create the call to the helper }
  2522. { on entry left node contains the parameter }
  2523. result := ccallnode.createintern('fpc_round_real',ccallparanode.create(left,nil));
  2524. left := nil;
  2525. end;
  2526. function tinlinenode.first_trunc_real : tnode;
  2527. begin
  2528. { create the call to the helper }
  2529. { on entry left node contains the parameter }
  2530. result := ccallnode.createintern('fpc_trunc_real',ccallparanode.create(left,nil));
  2531. left := nil;
  2532. end;
  2533. function tinlinenode.first_pack_unpack: tnode;
  2534. var
  2535. loopstatement : tstatementnode;
  2536. loop : tblocknode;
  2537. loopvar : ttempcreatenode;
  2538. tempnode,
  2539. source,
  2540. target,
  2541. index,
  2542. unpackednode,
  2543. packednode,
  2544. sourcevecindex,
  2545. targetvecindex,
  2546. loopbody : tnode;
  2547. temprangedef : tdef;
  2548. ulorange,
  2549. uhirange,
  2550. plorange,
  2551. phirange : TConstExprInt;
  2552. begin
  2553. { transform into a for loop which assigns the data of the (un)packed }
  2554. { array to the other one }
  2555. source := left;
  2556. if (inlinenumber = in_unpack_x_y_z) then
  2557. begin
  2558. target := tcallparanode(source).right;
  2559. index := tcallparanode(target).right;
  2560. packednode := tcallparanode(source).left;
  2561. unpackednode := tcallparanode(target).left;
  2562. end
  2563. else
  2564. begin
  2565. index := tcallparanode(source).right;
  2566. target := tcallparanode(index).right;
  2567. packednode := tcallparanode(target).left;
  2568. unpackednode := tcallparanode(source).left;
  2569. end;
  2570. source := tcallparanode(source).left;
  2571. target := tcallparanode(target).left;
  2572. index := tcallparanode(index).left;
  2573. loop := internalstatements(loopstatement);
  2574. loopvar := ctempcreatenode.create(
  2575. tarraydef(packednode.resultdef).rangedef,
  2576. tarraydef(packednode.resultdef).rangedef.size,
  2577. tt_persistent,true);
  2578. addstatement(loopstatement,loopvar);
  2579. { For range checking: we have to convert to an integer type (in case the index type }
  2580. { is an enum), add the index and loop variable together, convert the result }
  2581. { implicitly to an orddef with range equal to the rangedef to get range checking }
  2582. { and finally convert it explicitly back to the actual rangedef to avoid type }
  2583. { errors }
  2584. temprangedef:=nil;
  2585. getrange(unpackednode.resultdef,ulorange,uhirange);
  2586. getrange(packednode.resultdef,plorange,phirange);
  2587. temprangedef:=torddef.create(torddef(sinttype).ordtype,ulorange,uhirange);
  2588. sourcevecindex := ctemprefnode.create(loopvar);
  2589. targetvecindex := ctypeconvnode.create_internal(index.getcopy,sinttype);
  2590. targetvecindex := caddnode.create(subn,targetvecindex,cordconstnode.create(plorange,sinttype,true));
  2591. targetvecindex := caddnode.create(addn,targetvecindex,ctemprefnode.create(loopvar));
  2592. targetvecindex := ctypeconvnode.create(targetvecindex,temprangedef);
  2593. targetvecindex := ctypeconvnode.create_explicit(targetvecindex,tarraydef(unpackednode.resultdef).rangedef);
  2594. if (inlinenumber = in_pack_x_y_z) then
  2595. begin
  2596. { swap source and target vec indices }
  2597. tempnode := sourcevecindex;
  2598. sourcevecindex := targetvecindex;
  2599. targetvecindex := tempnode;
  2600. end;
  2601. { create the assignment in the loop body }
  2602. loopbody :=
  2603. cassignmentnode.create(
  2604. cvecnode.create(target.getcopy,targetvecindex),
  2605. cvecnode.create(source.getcopy,sourcevecindex)
  2606. );
  2607. { create the actual for loop }
  2608. tempnode := cfornode.create(
  2609. ctemprefnode.create(loopvar),
  2610. cinlinenode.create(in_low_x,false,packednode.getcopy),
  2611. cinlinenode.create(in_high_x,false,packednode.getcopy),
  2612. loopbody,
  2613. false);
  2614. addstatement(loopstatement,tempnode);
  2615. { free the loop counter }
  2616. addstatement(loopstatement,ctempdeletenode.create(loopvar));
  2617. result := loop;
  2618. end;
  2619. begin
  2620. cinlinenode:=tinlinenode;
  2621. end.