ninl.pas 219 KB

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