ninl.pas 205 KB

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