ninl.pas 223 KB

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