ninl.pas 218 KB

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