ninl.pas 207 KB

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