ninl.pas 168 KB

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