ninl.pas 196 KB

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