ninl.pas 117 KB

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