ninl.pas 137 KB

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