ninl.pas 218 KB

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