2
0

ninl.pas 209 KB

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