ninl.pas 162 KB

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