ninl.pas 166 KB

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