ninl.pas 205 KB

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