ninl.pas 142 KB

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