ninl.pas 205 KB

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