ninl.pas 210 KB

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