ninl.pas 119 KB

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