pass_1.pas 198 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324
  1. {
  2. $Id$
  3. Copyright (c) 1996-98 by Florian Klaempfl
  4. This unit implements the first pass of the code generator
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. {$ifdef tp}
  19. {$F+}
  20. {$endif tp}
  21. unit pass_1;
  22. interface
  23. uses tree;
  24. function do_firstpass(var p : ptree) : boolean;
  25. implementation
  26. uses
  27. scanner,cobjects,verbose,systems,globals,aasm,symtable,
  28. types,strings,hcodegen,files
  29. {$ifdef i386}
  30. ,i386
  31. ,tgeni386
  32. {$endif}
  33. {$ifdef m68k}
  34. ,m68k
  35. ,tgen68k
  36. {$endif}
  37. {$ifdef UseBrowser}
  38. ,browser
  39. {$endif UseBrowser}
  40. ;
  41. { firstcallparan without varspez
  42. we don't count the ref }
  43. const
  44. count_ref : boolean = true;
  45. procedure message(const t : tmsgconst);
  46. var
  47. olderrorcount : longint;
  48. begin
  49. if not(codegenerror) then
  50. begin
  51. olderrorcount:=status.errorcount;
  52. verbose.Message(t);
  53. codegenerror:=olderrorcount<>status.errorcount;
  54. end;
  55. end;
  56. procedure message1(const t : tmsgconst;const s : string);
  57. var
  58. olderrorcount : longint;
  59. begin
  60. if not(codegenerror) then
  61. begin
  62. olderrorcount:=status.errorcount;
  63. verbose.Message1(t,s);
  64. codegenerror:=olderrorcount<>status.errorcount;
  65. end;
  66. end;
  67. procedure message2(const t : tmsgconst;const s1,s2 : string);
  68. var
  69. olderrorcount : longint;
  70. begin
  71. if not(codegenerror) then
  72. begin
  73. olderrorcount:=status.errorcount;
  74. verbose.Message2(t,s1,s2);
  75. codegenerror:=olderrorcount<>status.errorcount;
  76. end;
  77. end;
  78. procedure message3(const t : tmsgconst;const s1,s2,s3 : string);
  79. var
  80. olderrorcount : longint;
  81. begin
  82. if not(codegenerror) then
  83. begin
  84. olderrorcount:=status.errorcount;
  85. verbose.Message3(t,s1,s2,s3);
  86. codegenerror:=olderrorcount<>status.errorcount;
  87. end;
  88. end;
  89. procedure firstpass(var p : ptree);forward;
  90. { marks an lvalue as "unregable" }
  91. procedure make_not_regable(p : ptree);
  92. begin
  93. case p^.treetype of
  94. typeconvn :
  95. make_not_regable(p^.left);
  96. loadn :
  97. if p^.symtableentry^.typ=varsym then
  98. pvarsym(p^.symtableentry)^.var_options :=
  99. pvarsym(p^.symtableentry)^.var_options and not vo_regable;
  100. end;
  101. end;
  102. procedure left_right_max(p : ptree);
  103. begin
  104. p^.registers32:=max(p^.left^.registers32,p^.right^.registers32);
  105. p^.registersfpu:=max(p^.left^.registersfpu,p^.right^.registersfpu);
  106. {$ifdef SUPPORT_MMX}
  107. p^.registersmmx:=max(p^.left^.registersmmx,p^.right^.registersmmx);
  108. {$endif SUPPORT_MMX}
  109. end;
  110. { calculates the needed registers for a binary operator }
  111. procedure calcregisters(p : ptree;r32,fpu,mmx : word);
  112. begin
  113. left_right_max(p);
  114. { Nur wenn links und rechts ein Unterschied < ben”tige Anzahl ist, }
  115. { wird ein zus„tzliches Register ben”tigt, da es dann keinen }
  116. { schwierigeren Ast gibt, welcher erst ausgewertet werden kann }
  117. if (abs(p^.left^.registers32-p^.right^.registers32)<r32) then
  118. inc(p^.registers32,r32);
  119. if (abs(p^.left^.registersfpu-p^.right^.registersfpu)<fpu) then
  120. inc(p^.registersfpu,fpu);
  121. {$ifdef SUPPORT_MMX}
  122. if (abs(p^.left^.registersmmx-p^.right^.registersmmx)<mmx) then
  123. inc(p^.registersmmx,mmx);
  124. {$endif SUPPORT_MMX}
  125. { error message, if more than 8 floating point }
  126. { registers are needed }
  127. if p^.registersfpu>8 then
  128. Message(cg_e_too_complex_expr);
  129. end;
  130. function both_rm(p : ptree) : boolean;
  131. begin
  132. both_rm:=(p^.left^.location.loc in [LOC_MEM,LOC_REFERENCE]) and
  133. (p^.right^.location.loc in [LOC_MEM,LOC_REFERENCE]);
  134. end;
  135. function is_assignment_overloaded(from_def,to_def : pdef) : boolean;forward;
  136. function isconvertable(def_from,def_to : pdef;
  137. var doconv : tconverttype;fromtreetype : ttreetyp;
  138. explicit : boolean) : boolean;
  139. { Tbasetype: uauto,uvoid,uchar,
  140. u8bit,u16bit,u32bit,
  141. s8bit,s16bit,s32,
  142. bool8bit,bool16bit,boot32bit }
  143. const
  144. basedefconverts : array[tbasetype,tbasetype] of tconverttype =
  145. {uauto}
  146. ((tc_not_possible,tc_not_possible,tc_not_possible,
  147. tc_not_possible,tc_not_possible,tc_not_possible,
  148. tc_not_possible,tc_not_possible,tc_not_possible,
  149. tc_not_possible,tc_not_possible,tc_not_possible),
  150. {uvoid}
  151. (tc_not_possible,tc_not_possible,tc_not_possible,
  152. tc_not_possible,tc_not_possible,tc_not_possible,
  153. tc_not_possible,tc_not_possible,tc_not_possible,
  154. tc_not_possible,tc_not_possible,tc_not_possible),
  155. {uchar}
  156. (tc_not_possible,tc_not_possible,tc_only_rangechecks32bit,
  157. tc_not_possible,tc_not_possible,tc_not_possible,
  158. tc_not_possible,tc_not_possible,tc_not_possible,
  159. tc_not_possible,tc_not_possible,tc_not_possible),
  160. {u8bit}
  161. (tc_not_possible,tc_not_possible,tc_not_possible,
  162. tc_only_rangechecks32bit,tc_u8bit_2_u16bit,tc_u8bit_2_u32bit,
  163. tc_only_rangechecks32bit,tc_u8bit_2_s16bit,tc_u8bit_2_s32bit,
  164. tc_int_2_bool,tc_int_2_bool,tc_int_2_bool),
  165. {u16bit}
  166. (tc_not_possible,tc_not_possible,tc_not_possible,
  167. tc_u16bit_2_u8bit,tc_only_rangechecks32bit,tc_u16bit_2_u32bit,
  168. tc_u16bit_2_s8bit,tc_only_rangechecks32bit,tc_u16bit_2_s32bit,
  169. tc_int_2_bool,tc_int_2_bool,tc_int_2_bool),
  170. {u32bit}
  171. (tc_not_possible,tc_not_possible,tc_not_possible,
  172. tc_u32bit_2_u8bit,tc_u32bit_2_u16bit,tc_only_rangechecks32bit,
  173. tc_u32bit_2_s8bit,tc_u32bit_2_s16bit,tc_only_rangechecks32bit,
  174. tc_int_2_bool,tc_int_2_bool,tc_int_2_bool),
  175. {s8bit}
  176. (tc_not_possible,tc_not_possible,tc_not_possible,
  177. tc_only_rangechecks32bit,tc_s8bit_2_u16bit,tc_s8bit_2_u32bit,
  178. tc_only_rangechecks32bit,tc_s8bit_2_s16bit,tc_s8bit_2_s32bit,
  179. tc_int_2_bool,tc_int_2_bool,tc_int_2_bool),
  180. {s16bit}
  181. (tc_not_possible,tc_not_possible,tc_not_possible,
  182. tc_s16bit_2_u8bit,tc_only_rangechecks32bit,tc_s16bit_2_u32bit,
  183. tc_s16bit_2_s8bit,tc_only_rangechecks32bit,tc_s16bit_2_s32bit,
  184. tc_int_2_bool,tc_int_2_bool,tc_int_2_bool),
  185. {s32bit}
  186. (tc_not_possible,tc_not_possible,tc_not_possible,
  187. tc_s32bit_2_u8bit,tc_s32bit_2_u16bit,tc_only_rangechecks32bit,
  188. tc_s32bit_2_s8bit,tc_s32bit_2_s16bit,tc_only_rangechecks32bit,
  189. tc_int_2_bool,tc_int_2_bool,tc_int_2_bool),
  190. {bool8bit}
  191. (tc_not_possible,tc_not_possible,tc_not_possible,
  192. tc_bool_2_int,tc_bool_2_int,tc_bool_2_int,
  193. tc_bool_2_int,tc_bool_2_int,tc_bool_2_int,
  194. tc_only_rangechecks32bit,tc_bool_2_int,tc_bool_2_int),
  195. {bool16bit}
  196. (tc_not_possible,tc_not_possible,tc_not_possible,
  197. tc_bool_2_int,tc_bool_2_int,tc_bool_2_int,
  198. tc_bool_2_int,tc_bool_2_int,tc_bool_2_int,
  199. tc_bool_2_int,tc_only_rangechecks32bit,tc_bool_2_int),
  200. {bool32bit}
  201. (tc_not_possible,tc_not_possible,tc_not_possible,
  202. tc_bool_2_int,tc_bool_2_int,tc_bool_2_int,
  203. tc_bool_2_int,tc_bool_2_int,tc_bool_2_int,
  204. tc_bool_2_int,tc_bool_2_int,tc_only_rangechecks32bit));
  205. var
  206. b : boolean;
  207. begin
  208. b:=false;
  209. if (not assigned(def_from)) or (not assigned(def_to)) then
  210. begin
  211. isconvertable:=false;
  212. exit;
  213. end;
  214. if (def_from^.deftype=orddef) and (def_to^.deftype=orddef) then
  215. begin
  216. doconv:=basedefconverts[porddef(def_from)^.typ,porddef(def_to)^.typ];
  217. if doconv<>tc_not_possible then
  218. b:=true;
  219. end
  220. else if (def_from^.deftype=orddef) and (def_to^.deftype=floatdef) then
  221. begin
  222. if pfloatdef(def_to)^.typ=f32bit then
  223. doconv:=tc_int_2_fix
  224. else
  225. doconv:=tc_int_2_real;
  226. b:=true;
  227. end
  228. else if (def_from^.deftype=floatdef) and (def_to^.deftype=floatdef) then
  229. begin
  230. if pfloatdef(def_from)^.typ=pfloatdef(def_to)^.typ then
  231. doconv:=tc_equal
  232. else
  233. begin
  234. if pfloatdef(def_from)^.typ=f32bit then
  235. doconv:=tc_fix_2_real
  236. else if pfloatdef(def_to)^.typ=f32bit then
  237. doconv:=tc_real_2_fix
  238. else
  239. doconv:=tc_real_2_real;
  240. { comp isn't a floating type }
  241. {$ifdef i386}
  242. if (pfloatdef(def_to)^.typ=s64bit) and
  243. (pfloatdef(def_from)^.typ<>s64bit) and
  244. not (explicit) then
  245. Message(parser_w_convert_real_2_comp);
  246. {$endif}
  247. end;
  248. b:=true;
  249. end
  250. { assignment overwritten ?? }
  251. else if is_assignment_overloaded(def_from,def_to) then
  252. b:=true
  253. else if (def_from^.deftype=pointerdef) and (def_to^.deftype=arraydef) and
  254. (parraydef(def_to)^.lowrange=0) and
  255. is_equal(ppointerdef(def_from)^.definition,
  256. parraydef(def_to)^.definition) then
  257. begin
  258. doconv:=tc_pointer_to_array;
  259. b:=true;
  260. end
  261. else if (def_from^.deftype=arraydef) and (def_to^.deftype=pointerdef) and
  262. (parraydef(def_from)^.lowrange=0) and
  263. is_equal(parraydef(def_from)^.definition,
  264. ppointerdef(def_to)^.definition) then
  265. begin
  266. doconv:=tc_array_to_pointer;
  267. b:=true;
  268. end
  269. { typed files are all equal to the abstract file type
  270. name TYPEDFILE in system.pp in is_equal in types.pas
  271. the problem is that it sholud be also compatible to FILE
  272. but this would leed to a problem for ASSIGN RESET and REWRITE
  273. when trying to find the good overloaded function !!
  274. so all file function are doubled in system.pp
  275. this is not very beautiful !!}
  276. else if (def_from^.deftype=filedef) and (def_to^.deftype=filedef) and
  277. (
  278. (
  279. (pfiledef(def_from)^.filetype = ft_typed) and
  280. (pfiledef(def_to)^.filetype = ft_typed) and
  281. (
  282. (pfiledef(def_from)^.typed_as = pdef(voiddef)) or
  283. (pfiledef(def_to)^.typed_as = pdef(voiddef))
  284. )
  285. ) or
  286. (
  287. (
  288. (pfiledef(def_from)^.filetype = ft_untyped) and
  289. (pfiledef(def_to)^.filetype = ft_typed)
  290. ) or
  291. (
  292. (pfiledef(def_from)^.filetype = ft_typed) and
  293. (pfiledef(def_to)^.filetype = ft_untyped)
  294. )
  295. )
  296. ) then
  297. begin
  298. doconv:=tc_equal;
  299. b:=true;
  300. end
  301. { object pascal objects }
  302. else if (def_from^.deftype=objectdef) and (def_to^.deftype=objectdef) {and
  303. pobjectdef(def_from)^.isclass and pobjectdef(def_to)^.isclass }then
  304. begin
  305. doconv:=tc_equal;
  306. b:=pobjectdef(def_from)^.isrelated(
  307. pobjectdef(def_to));
  308. end
  309. { class reference types }
  310. else if (def_from^.deftype=classrefdef) and (def_from^.deftype=classrefdef) then
  311. begin
  312. doconv:=tc_equal;
  313. b:=pobjectdef(pclassrefdef(def_from)^.definition)^.isrelated(
  314. pobjectdef(pclassrefdef(def_to)^.definition));
  315. end
  316. else if (def_from^.deftype=pointerdef) and (def_to^.deftype=pointerdef) then
  317. begin
  318. { child class pointer can be assigned to anchestor pointers }
  319. if (
  320. (ppointerdef(def_from)^.definition^.deftype=objectdef) and
  321. (ppointerdef(def_to)^.definition^.deftype=objectdef) and
  322. pobjectdef(ppointerdef(def_from)^.definition)^.isrelated(
  323. pobjectdef(ppointerdef(def_to)^.definition))
  324. ) or
  325. { all pointers can be assigned to void-pointer }
  326. is_equal(ppointerdef(def_to)^.definition,voiddef) or
  327. { in my opnion, is this not clean pascal }
  328. { well, but it's handy to use, it isn't ? (FK) }
  329. is_equal(ppointerdef(def_from)^.definition,voiddef) then
  330. begin
  331. doconv:=tc_equal;
  332. b:=true;
  333. end
  334. end
  335. else
  336. if (def_from^.deftype=stringdef) and (def_to^.deftype=stringdef) then
  337. begin
  338. doconv:=tc_string_to_string;
  339. b:=true;
  340. end
  341. else
  342. { char to string}
  343. if is_equal(def_from,cchardef) and
  344. (def_to^.deftype=stringdef) then
  345. begin
  346. doconv:=tc_char_to_string;
  347. b:=true;
  348. end
  349. else
  350. { string constant to zero terminated string constant }
  351. if (fromtreetype=stringconstn) and
  352. (
  353. (def_to^.deftype=pointerdef) and
  354. is_equal(Ppointerdef(def_to)^.definition,cchardef)
  355. ) then
  356. begin
  357. doconv:=tc_cstring_charpointer;
  358. b:=true;
  359. end
  360. else
  361. { array of char to string }
  362. { the length check is done by the firstpass of this node }
  363. if (def_from^.deftype=stringdef) and
  364. (
  365. (def_to^.deftype=arraydef) and
  366. is_equal(parraydef(def_to)^.definition,cchardef)
  367. ) then
  368. begin
  369. doconv:=tc_string_chararray;
  370. b:=true;
  371. end
  372. else
  373. { string to array of char }
  374. { the length check is done by the firstpass of this node }
  375. if (
  376. (def_from^.deftype=arraydef) and
  377. is_equal(parraydef(def_from)^.definition,cchardef)
  378. ) and
  379. (def_to^.deftype=stringdef) then
  380. begin
  381. doconv:=tc_chararray_2_string;
  382. b:=true;
  383. end
  384. else
  385. if (fromtreetype=ordconstn) and is_equal(def_from,cchardef) then
  386. begin
  387. if (def_to^.deftype=pointerdef) and
  388. is_equal(ppointerdef(def_to)^.definition,cchardef) then
  389. begin
  390. doconv:=tc_cchar_charpointer;
  391. b:=true;
  392. end;
  393. end
  394. else
  395. if (def_to^.deftype=procvardef) and (def_from^.deftype=procdef) then
  396. begin
  397. def_from^.deftype:=procvardef;
  398. doconv:=tc_proc2procvar;
  399. b:=is_equal(def_from,def_to);
  400. def_from^.deftype:=procdef;
  401. end
  402. else
  403. { nil is compatible with class instances }
  404. if (fromtreetype=niln) and (def_to^.deftype=objectdef)
  405. and (pobjectdef(def_to)^.isclass) then
  406. begin
  407. doconv:=tc_equal;
  408. b:=true;
  409. end
  410. else
  411. { nil is compatible with class references }
  412. if (fromtreetype=niln) and (def_to^.deftype=classrefdef) then
  413. begin
  414. doconv:=tc_equal;
  415. b:=true;
  416. end
  417. else
  418. { nil is compatible with procvars }
  419. if (fromtreetype=niln) and (def_to^.deftype=procvardef) then
  420. begin
  421. doconv:=tc_equal;
  422. b:=true;
  423. end
  424. else
  425. { nil is compatible with ansi- and wide strings }
  426. if (fromtreetype=niln) and (def_to^.deftype=stringdef)
  427. and (pstringdef(def_to)^.string_typ in [st_ansistring,st_widestring]) then
  428. begin
  429. doconv:=tc_equal;
  430. b:=true;
  431. end
  432. else
  433. { ansi- and wide strings can be assigned to void pointers }
  434. if (def_from^.deftype=stringdef) and
  435. (pstringdef(def_from)^.string_typ in [st_ansistring,st_widestring]) and
  436. (def_to^.deftype=pointerdef) and
  437. (ppointerdef(def_to)^.definition^.deftype=orddef) and
  438. (porddef(ppointerdef(def_to)^.definition)^.typ=uvoid) then
  439. begin
  440. doconv:=tc_equal;
  441. b:=true;
  442. end
  443. { procedure variable can be assigned to an void pointer }
  444. { Not anymore. Use the @ operator now.}
  445. else
  446. if not (cs_tp_compatible in aktswitches) then
  447. begin
  448. if (def_from^.deftype=procvardef) and
  449. (def_to^.deftype=pointerdef) and
  450. (ppointerdef(def_to)^.definition^.deftype=orddef) and
  451. (porddef(ppointerdef(def_to)^.definition)^.typ=uvoid) then
  452. begin
  453. doconv:=tc_equal;
  454. b:=true;
  455. end;
  456. end;
  457. isconvertable:=b;
  458. end;
  459. procedure firsterror(var p : ptree);
  460. begin
  461. p^.error:=true;
  462. codegenerror:=true;
  463. p^.resulttype:=generrordef;
  464. end;
  465. procedure firstload(var p : ptree);
  466. begin
  467. p^.location.loc:=LOC_REFERENCE;
  468. p^.registers32:=0;
  469. p^.registersfpu:=0;
  470. {$ifdef SUPPORT_MMX}
  471. p^.registersmmx:=0;
  472. {$endif SUPPORT_MMX}
  473. clear_reference(p^.location.reference);
  474. {$ifdef TEST_FUNCRET}
  475. if p^.symtableentry^.typ=funcretsym then
  476. begin
  477. putnode(p);
  478. p:=genzeronode(funcretn);
  479. p^.funcretprocinfo:=pprocinfo(pfuncretsym(p^.symtableentry)^.funcretprocinfo);
  480. p^.retdef:=pfuncretsym(p^.symtableentry)^.funcretdef;
  481. firstpass(p);
  482. exit;
  483. end;
  484. {$endif TEST_FUNCRET}
  485. if p^.symtableentry^.typ=absolutesym then
  486. begin
  487. p^.resulttype:=pabsolutesym(p^.symtableentry)^.definition;
  488. if pabsolutesym(p^.symtableentry)^.abstyp=tovar then
  489. p^.symtableentry:=pabsolutesym(p^.symtableentry)^.ref;
  490. p^.symtable:=p^.symtableentry^.owner;
  491. p^.is_absolute:=true;
  492. end;
  493. case p^.symtableentry^.typ of
  494. absolutesym :;
  495. varsym :
  496. begin
  497. if not(p^.is_absolute) and (p^.resulttype=nil) then
  498. p^.resulttype:=pvarsym(p^.symtableentry)^.definition;
  499. if ((p^.symtable^.symtabletype=parasymtable) or
  500. (p^.symtable^.symtabletype=localsymtable)) and
  501. (lexlevel>p^.symtable^.symtablelevel) then
  502. begin
  503. { sollte sich die Variable in einem anderen Stackframe }
  504. { befinden, so brauchen wir ein Register zum Dereferenceieren }
  505. if (p^.symtable^.symtablelevel)>0 then
  506. begin
  507. p^.registers32:=1;
  508. { auáerdem kann sie nicht mehr in ein Register
  509. geladen werden }
  510. pvarsym(p^.symtableentry)^.var_options :=
  511. pvarsym(p^.symtableentry)^.var_options and not vo_regable;
  512. end;
  513. end;
  514. if (pvarsym(p^.symtableentry)^.varspez=vs_const) then
  515. p^.location.loc:=LOC_MEM;
  516. { we need a register for call by reference parameters }
  517. if (pvarsym(p^.symtableentry)^.varspez=vs_var) or
  518. ((pvarsym(p^.symtableentry)^.varspez=vs_const) and
  519. dont_copy_const_param(pvarsym(p^.symtableentry)^.definition)
  520. ) or
  521. { call by value open arrays are also indirect addressed }
  522. is_open_array(pvarsym(p^.symtableentry)^.definition) then
  523. p^.registers32:=1;
  524. if p^.symtable^.symtabletype=withsymtable then
  525. p^.registers32:=1;
  526. { a class variable is a pointer !!!
  527. yes, but we have to resolve the reference in an
  528. appropriate tree node (FK)
  529. if (pvarsym(p^.symtableentry)^.definition^.deftype=objectdef) and
  530. ((pobjectdef(pvarsym(p^.symtableentry)^.definition)^.options and oois_class)<>0) then
  531. p^.registers32:=1;
  532. }
  533. { count variable references }
  534. if must_be_valid and p^.is_first then
  535. begin
  536. if pvarsym(p^.symtableentry)^.is_valid=2 then
  537. if (assigned(pvarsym(p^.symtableentry)^.owner) and assigned(aktprocsym)
  538. and (pvarsym(p^.symtableentry)^.owner = aktprocsym^.definition^.localst)) then
  539. Message1(sym_n_uninitialized_local_variable,pvarsym(p^.symtableentry)^.name);
  540. end;
  541. if count_ref then
  542. begin
  543. if (p^.is_first) then
  544. begin
  545. if (pvarsym(p^.symtableentry)^.is_valid=2) then
  546. pvarsym(p^.symtableentry)^.is_valid:=1;
  547. p^.is_first:=false;
  548. end;
  549. end;
  550. { this will create problem with local var set by
  551. under_procedures
  552. if (assigned(pvarsym(p^.symtableentry)^.owner) and assigned(aktprocsym)
  553. and ((pvarsym(p^.symtableentry)^.owner = aktprocsym^.definition^.localst)
  554. or (pvarsym(p^.symtableentry)^.owner = aktprocsym^.definition^.localst))) then }
  555. if t_times<1 then
  556. inc(pvarsym(p^.symtableentry)^.refs)
  557. else
  558. inc(pvarsym(p^.symtableentry)^.refs,t_times);
  559. end;
  560. typedconstsym :
  561. if not p^.is_absolute then
  562. p^.resulttype:=ptypedconstsym(p^.symtableentry)^.definition;
  563. procsym :
  564. begin
  565. if assigned(pprocsym(p^.symtableentry)^.definition^.nextoverloaded) then
  566. Message(parser_e_no_overloaded_procvars);
  567. p^.resulttype:=pprocsym(p^.symtableentry)^.definition;
  568. end;
  569. else internalerror(3);
  570. end;
  571. end;
  572. procedure firstadd(var p : ptree);
  573. procedure make_bool_equal_size(var p:ptree);
  574. begin
  575. if porddef(p^.left^.resulttype)^.typ>porddef(p^.right^.resulttype)^.typ then
  576. begin
  577. p^.right:=gentypeconvnode(p^.right,porddef(p^.left^.resulttype));
  578. p^.right^.convtyp:=tc_bool_2_int;
  579. p^.right^.explizit:=true;
  580. firstpass(p^.right);
  581. end
  582. else
  583. if porddef(p^.left^.resulttype)^.typ<porddef(p^.right^.resulttype)^.typ then
  584. begin
  585. p^.left:=gentypeconvnode(p^.left,porddef(p^.right^.resulttype));
  586. p^.left^.convtyp:=tc_bool_2_int;
  587. p^.left^.explizit:=true;
  588. firstpass(p^.left);
  589. end;
  590. end;
  591. var
  592. lt,rt : ttreetyp;
  593. t : ptree;
  594. rv,lv : longint;
  595. rvd,lvd : {double}bestreal;
  596. rd,ld : pdef;
  597. concatstrings : boolean;
  598. { to evalute const sets }
  599. resultset : pconstset;
  600. i : longint;
  601. b : boolean;
  602. {$ifndef UseAnsiString}
  603. s1,s2:^string;
  604. {$else UseAnsiString}
  605. s1,s2 : pchar;
  606. l1,l2 : longint;
  607. {$endif UseAnsiString}
  608. { this totally forgets to set the pi_do_call flag !! }
  609. label
  610. no_overload;
  611. begin
  612. { first do the two subtrees }
  613. firstpass(p^.left);
  614. firstpass(p^.right);
  615. lt:=p^.left^.treetype;
  616. rt:=p^.right^.treetype;
  617. rd:=p^.right^.resulttype;
  618. ld:=p^.left^.resulttype;
  619. if codegenerror then
  620. exit;
  621. { overloaded operator ? }
  622. if (p^.treetype=starstarn) or
  623. (ld^.deftype=recorddef) or
  624. { <> and = are defined for classes }
  625. ((ld^.deftype=objectdef) and
  626. (not(pobjectdef(ld)^.isclass) or
  627. not(p^.treetype in [equaln,unequaln])
  628. )
  629. ) or
  630. (rd^.deftype=recorddef) or
  631. { <> and = are defined for classes }
  632. ((rd^.deftype=objectdef) and
  633. (not(pobjectdef(rd)^.isclass) or
  634. not(p^.treetype in [equaln,unequaln])
  635. )
  636. ) then
  637. begin
  638. {!!!!!!!!! handle paras }
  639. case p^.treetype of
  640. { the nil as symtable signs firstcalln that this is
  641. an overloaded operator }
  642. addn:
  643. t:=gencallnode(overloaded_operators[plus],nil);
  644. subn:
  645. t:=gencallnode(overloaded_operators[minus],nil);
  646. muln:
  647. t:=gencallnode(overloaded_operators[star],nil);
  648. starstarn:
  649. t:=gencallnode(overloaded_operators[starstar],nil);
  650. slashn:
  651. t:=gencallnode(overloaded_operators[slash],nil);
  652. ltn:
  653. t:=gencallnode(overloaded_operators[globals.lt],nil);
  654. gtn:
  655. t:=gencallnode(overloaded_operators[gt],nil);
  656. lten:
  657. t:=gencallnode(overloaded_operators[lte],nil);
  658. gten:
  659. t:=gencallnode(overloaded_operators[gte],nil);
  660. equaln,unequaln :
  661. t:=gencallnode(overloaded_operators[equal],nil);
  662. else goto no_overload;
  663. end;
  664. { we have to convert p^.left and p^.right into
  665. callparanodes }
  666. t^.left:=gencallparanode(p^.left,nil);
  667. t^.left:=gencallparanode(p^.right,t^.left);
  668. if t^.symtableprocentry=nil then
  669. Message(parser_e_operator_not_overloaded);
  670. if p^.treetype=unequaln then
  671. t:=gensinglenode(notn,t);
  672. firstpass(t);
  673. putnode(p);
  674. p:=t;
  675. exit;
  676. end;
  677. no_overload:
  678. { compact consts }
  679. { convert int consts to real consts, if the }
  680. { other operand is a real const }
  681. if is_constintnode(p^.left) and
  682. (rt=realconstn) then
  683. begin
  684. t:=genrealconstnode(p^.left^.value);
  685. disposetree(p^.left);
  686. p^.left:=t;
  687. lt:=realconstn;
  688. end;
  689. if is_constintnode(p^.right) and
  690. (lt=realconstn) then
  691. begin
  692. t:=genrealconstnode(p^.right^.value);
  693. disposetree(p^.right);
  694. p^.right:=t;
  695. rt:=realconstn;
  696. end;
  697. if is_constintnode(p^.left) and
  698. is_constintnode(p^.right) then
  699. begin
  700. lv:=p^.left^.value;
  701. rv:=p^.right^.value;
  702. case p^.treetype of
  703. addn:
  704. t:=genordinalconstnode(lv+rv,s32bitdef);
  705. subn:
  706. t:=genordinalconstnode(lv-rv,s32bitdef);
  707. muln:
  708. t:=genordinalconstnode(lv*rv,s32bitdef);
  709. xorn:
  710. t:=genordinalconstnode(lv xor rv,s32bitdef);
  711. orn:
  712. t:=genordinalconstnode(lv or rv,s32bitdef);
  713. andn:
  714. t:=genordinalconstnode(lv and rv,s32bitdef);
  715. ltn:
  716. t:=genordinalconstnode(ord(lv<rv),booldef);
  717. lten:
  718. t:=genordinalconstnode(ord(lv<=rv),booldef);
  719. gtn:
  720. t:=genordinalconstnode(ord(lv>rv),booldef);
  721. gten:
  722. t:=genordinalconstnode(ord(lv>=rv),booldef);
  723. equaln:
  724. t:=genordinalconstnode(ord(lv=rv),booldef);
  725. unequaln:
  726. t:=genordinalconstnode(ord(lv<>rv),booldef);
  727. slashn :
  728. begin
  729. { int/int becomes a real }
  730. t:=genrealconstnode(int(lv)/int(rv));
  731. firstpass(t);
  732. end;
  733. else
  734. Message(sym_e_type_mismatch);
  735. end;
  736. disposetree(p);
  737. firstpass(t);
  738. p:=t;
  739. exit;
  740. end
  741. else
  742. { real constants }
  743. if (lt=realconstn) and (rt=realconstn) then
  744. begin
  745. lvd:=p^.left^.valued;
  746. rvd:=p^.right^.valued;
  747. case p^.treetype of
  748. addn:
  749. t:=genrealconstnode(lvd+rvd);
  750. subn:
  751. t:=genrealconstnode(lvd-rvd);
  752. muln:
  753. t:=genrealconstnode(lvd*rvd);
  754. caretn:
  755. t:=genrealconstnode(exp(ln(lvd)*rvd));
  756. slashn:
  757. t:=genrealconstnode(lvd/rvd);
  758. ltn:
  759. t:=genordinalconstnode(ord(lvd<rvd),booldef);
  760. lten:
  761. t:=genordinalconstnode(ord(lvd<=rvd),booldef);
  762. gtn:
  763. t:=genordinalconstnode(ord(lvd>rvd),booldef);
  764. gten:
  765. t:=genordinalconstnode(ord(lvd>=rvd),booldef);
  766. equaln:
  767. t:=genordinalconstnode(ord(lvd=rvd),booldef);
  768. unequaln:
  769. t:=genordinalconstnode(ord(lvd<>rvd),booldef);
  770. else
  771. Message(sym_e_type_mismatch);
  772. end;
  773. disposetree(p);
  774. p:=t;
  775. firstpass(p);
  776. exit;
  777. end;
  778. concatstrings:=false;
  779. {$ifdef UseAnsiString}
  780. s1:=nil;
  781. s2:=nil;
  782. {$else UseAnsiString}
  783. new(s1);
  784. new(s2);
  785. {$endif UseAnsiString}
  786. if (lt=ordconstn) and (rt=ordconstn) and
  787. (ld^.deftype=orddef) and
  788. (porddef(ld)^.typ=uchar) and
  789. (rd^.deftype=orddef) and
  790. (porddef(rd)^.typ=uchar) then
  791. begin
  792. {$ifdef UseAnsiString}
  793. s1:=strpnew(char(byte(p^.left^.value)));
  794. s2:=strpnew(char(byte(p^.right^.value)));
  795. l1:=1;l2:=1;
  796. {$else UseAnsiString}
  797. s1^:=char(byte(p^.left^.value));
  798. s2^:=char(byte(p^.right^.value));
  799. concatstrings:=true;
  800. {$endif UseAnsiString}
  801. end
  802. else if (lt=stringconstn) and (rt=ordconstn) and
  803. (rd^.deftype=orddef) and
  804. (porddef(rd)^.typ=uchar) then
  805. begin
  806. {$ifdef UseAnsiString}
  807. { here there is allways the damn #0 problem !! }
  808. s1:=getpcharcopy(p^.left);
  809. l1:=p^.left^.length;
  810. s2:=strpnew(char(byte(p^.right^.value)));
  811. l2:=1;
  812. {$else UseAnsiString}
  813. s1^:=p^.left^.values^;
  814. s2^:=char(byte(p^.right^.value));
  815. concatstrings:=true;
  816. {$endif UseAnsiString}
  817. end
  818. else if (lt=ordconstn) and (rt=stringconstn) and
  819. (ld^.deftype=orddef) and
  820. (porddef(ld)^.typ=uchar) then
  821. begin
  822. {$ifdef UseAnsiString}
  823. { here there is allways the damn #0 problem !! }
  824. s1:=strpnew(char(byte(p^.left^.value)));
  825. l1:=1;
  826. s2:=getpcharcopy(p^.right);
  827. l2:=p^.right^.length;
  828. {$else UseAnsiString}
  829. s1^:=char(byte(p^.left^.value));
  830. s2^:=p^.right^.values^;
  831. concatstrings:=true;
  832. {$endif UseAnsiString}
  833. end
  834. else if (lt=stringconstn) and (rt=stringconstn) then
  835. begin
  836. {$ifdef UseAnsiString}
  837. s1:=getpcharcopy(p^.left);
  838. l1:=p^.left^.length;
  839. s2:=getpcharcopy(p^.right);
  840. l2:=p^.right^.length;
  841. concatstrings:=true;
  842. {$else UseAnsiString}
  843. s1^:=p^.left^.values^;
  844. s2^:=p^.right^.values^;
  845. concatstrings:=true;
  846. {$endif UseAnsiString}
  847. end;
  848. { I will need to translate all this to ansistrings !!! }
  849. if concatstrings then
  850. begin
  851. case p^.treetype of
  852. {$ifndef UseAnsiString}
  853. addn : t:=genstringconstnode(s1^+s2^);
  854. ltn : t:=genordinalconstnode(byte(s1^<s2^),booldef);
  855. lten : t:=genordinalconstnode(byte(s1^<=s2^),booldef);
  856. gtn : t:=genordinalconstnode(byte(s1^>s2^),booldef);
  857. gten : t:=genordinalconstnode(byte(s1^>=s2^),booldef);
  858. equaln : t:=genordinalconstnode(byte(s1^=s2^),booldef);
  859. unequaln : t:=genordinalconstnode(byte(s1^<>s2^),booldef);
  860. {$else UseAnsiString}
  861. addn : t:=genpcharconstnode(
  862. concatansistrings(s1,s2,l1,l2),l1+l2);
  863. ltn : t:=genordinalconstnode(
  864. byte(compareansistrings(s1,s2,l1,l2)<0),booldef);
  865. lten : t:=genordinalconstnode(
  866. byte(compareansistrings(s1,s2,l1,l2)<=0),booldef);
  867. gtn : t:=genordinalconstnode(
  868. byte(compareansistrings(s1,s2,l1,l2)>0),booldef);
  869. gten : t:=genordinalconstnode(
  870. byte(compareansistrings(s1,s2,l1,l2)>=0),booldef);
  871. equaln : t:=genordinalconstnode(
  872. byte(compareansistrings(s1,s2,l1,l2)=0),booldef);
  873. unequaln : t:=genordinalconstnode(
  874. byte(compareansistrings(s1,s2,l1,l2)<>0),booldef);
  875. {$endif UseAnsiString}
  876. end;
  877. {$ifdef UseAnsiString}
  878. ansistringdispose(s1,l1);
  879. ansistringdispose(s2,l2);
  880. {$else UseAnsiString}
  881. dispose(s1);
  882. dispose(s2);
  883. {$endif UseAnsiString}
  884. disposetree(p);
  885. firstpass(t);
  886. p:=t;
  887. exit;
  888. end;
  889. {$ifdef UseAnsiString}
  890. ansistringdispose(s1,l1);
  891. ansistringdispose(s2,l2);
  892. {$else UseAnsiString}
  893. dispose(s1);
  894. dispose(s2);
  895. {$endif UseAnsiString}
  896. { we can set this globally but it not allways true }
  897. { procinfo.flags:=procinfo.flags or pi_do_call; }
  898. { if both are boolean: }
  899. if ((ld^.deftype=orddef) and
  900. (porddef(ld)^.typ in [bool8bit,bool16bit,bool32bit])) and
  901. ((rd^.deftype=orddef) and
  902. (porddef(rd)^.typ in [bool8bit,bool16bit,bool32bit])) then
  903. begin
  904. case p^.treetype of
  905. andn,orn : begin
  906. calcregisters(p,0,0,0);
  907. p^.location.loc:=LOC_JUMP;
  908. end;
  909. unequaln,
  910. equaln,xorn : begin
  911. make_bool_equal_size(p);
  912. calcregisters(p,1,0,0);
  913. end
  914. else
  915. Message(sym_e_type_mismatch);
  916. end;
  917. end
  918. { wenn beides vom Char dann keine Konvertiereung einf�gen }
  919. { h”chstens es handelt sich um einen +-Operator }
  920. else if ((rd^.deftype=orddef) and (porddef(rd)^.typ=uchar)) and
  921. ((ld^.deftype=orddef) and (porddef(ld)^.typ=uchar)) then
  922. begin
  923. if p^.treetype=addn then
  924. begin
  925. p^.left:=gentypeconvnode(p^.left,cstringdef);
  926. firstpass(p^.left);
  927. p^.right:=gentypeconvnode(p^.right,cstringdef);
  928. firstpass(p^.right);
  929. { here we call STRCOPY }
  930. procinfo.flags:=procinfo.flags or pi_do_call;
  931. calcregisters(p,0,0,0);
  932. p^.location.loc:=LOC_MEM;
  933. end
  934. else
  935. calcregisters(p,1,0,0);
  936. end
  937. { if string and character, then conver the character to a string }
  938. else if ((rd^.deftype=stringdef) and
  939. ((ld^.deftype=orddef) and (porddef(ld)^.typ=uchar))) or
  940. ((ld^.deftype=stringdef) and
  941. ((rd^.deftype=orddef) and (porddef(rd)^.typ=uchar))) then
  942. begin
  943. if ((ld^.deftype=orddef) and (porddef(ld)^.typ=uchar)) then
  944. p^.left:=gentypeconvnode(p^.left,cstringdef)
  945. else
  946. p^.right:=gentypeconvnode(p^.right,cstringdef);
  947. firstpass(p^.left);
  948. firstpass(p^.right);
  949. { here we call STRCONCAT or STRCMP }
  950. procinfo.flags:=procinfo.flags or pi_do_call;
  951. calcregisters(p,0,0,0);
  952. p^.location.loc:=LOC_MEM;
  953. end
  954. else
  955. if ((rd^.deftype=setdef) and (ld^.deftype=setdef)) then
  956. begin
  957. case p^.treetype of
  958. subn,symdifn,addn,muln,equaln,unequaln : ;
  959. else Message(sym_e_type_mismatch);
  960. end;
  961. if not(is_equal(rd,ld)) then
  962. Message(sym_e_set_element_are_not_comp);
  963. { why here its is alredy in entry of firstadd
  964. firstpass(p^.left);
  965. firstpass(p^.right); }
  966. { do constant evalution }
  967. { set constructor ? }
  968. if (p^.right^.treetype=setconstrn) and
  969. (p^.left^.treetype=setconstrn) and
  970. { and no variables ? }
  971. (p^.right^.left=nil) and
  972. (p^.left^.left=nil) then
  973. begin
  974. new(resultset);
  975. case p^.treetype of
  976. addn : begin
  977. for i:=0 to 31 do
  978. resultset^[i]:=
  979. p^.right^.constset^[i] or p^.left^.constset^[i];
  980. t:=gensetconstruktnode(resultset,psetdef(ld));
  981. end;
  982. muln : begin
  983. for i:=0 to 31 do
  984. resultset^[i]:=
  985. p^.right^.constset^[i] and p^.left^.constset^[i];
  986. t:=gensetconstruktnode(resultset,psetdef(ld));
  987. end;
  988. subn : begin
  989. for i:=0 to 31 do
  990. resultset^[i]:=
  991. p^.left^.constset^[i] and not(p^.right^.constset^[i]);
  992. t:=gensetconstruktnode(resultset,psetdef(ld));
  993. end;
  994. symdifn : begin
  995. for i:=0 to 31 do
  996. resultset^[i]:=
  997. p^.left^.constset^[i] xor p^.right^.constset^[i];
  998. t:=gensetconstruktnode(resultset,psetdef(ld));
  999. end;
  1000. unequaln : begin
  1001. b:=true;
  1002. for i:=0 to 31 do
  1003. if p^.right^.constset^[i]=p^.left^.constset^[i] then
  1004. begin
  1005. b:=false;
  1006. break;
  1007. end;
  1008. t:=genordinalconstnode(ord(b),booldef);
  1009. end;
  1010. equaln : begin
  1011. b:=true;
  1012. for i:=0 to 31 do
  1013. if p^.right^.constset^[i]<>p^.left^.constset^[i] then
  1014. begin
  1015. b:=false;
  1016. break;
  1017. end;
  1018. t:=genordinalconstnode(ord(b),booldef);
  1019. end;
  1020. end;
  1021. dispose(resultset);
  1022. disposetree(p);
  1023. p:=t;
  1024. firstpass(p);
  1025. exit;
  1026. end
  1027. else if psetdef(rd)^.settype=smallset then
  1028. begin
  1029. calcregisters(p,1,0,0);
  1030. p^.location.loc:=LOC_REGISTER;
  1031. end
  1032. else
  1033. begin
  1034. calcregisters(p,0,0,0);
  1035. { here we call SET... }
  1036. procinfo.flags:=procinfo.flags or pi_do_call;
  1037. p^.location.loc:=LOC_MEM;
  1038. end;
  1039. end
  1040. else
  1041. if ((rd^.deftype=stringdef) and (ld^.deftype=stringdef)) then
  1042. { here we call STR... }
  1043. procinfo.flags:=procinfo.flags or pi_do_call
  1044. { if there is a real float, convert both to float 80 bit }
  1045. else
  1046. if ((rd^.deftype=floatdef) and (pfloatdef(rd)^.typ<>f32bit)) or
  1047. ((ld^.deftype=floatdef) and (pfloatdef(ld)^.typ<>f32bit)) then
  1048. begin
  1049. p^.right:=gentypeconvnode(p^.right,c64floatdef);
  1050. p^.left:=gentypeconvnode(p^.left,c64floatdef);
  1051. firstpass(p^.left);
  1052. firstpass(p^.right);
  1053. calcregisters(p,1,1,0);
  1054. p^.location.loc:=LOC_FPU;
  1055. end
  1056. else
  1057. { if there is one fix comma number, convert both to 32 bit fixcomma }
  1058. if ((rd^.deftype=floatdef) and (pfloatdef(rd)^.typ=f32bit)) or
  1059. ((ld^.deftype=floatdef) and (pfloatdef(ld)^.typ=f32bit)) then
  1060. begin
  1061. if not(porddef(rd)^.typ in [u8bit,s8bit,u16bit,
  1062. s16bit,s32bit]) or (p^.treetype<>muln) then
  1063. p^.right:=gentypeconvnode(p^.right,s32fixeddef);
  1064. if not(porddef(rd)^.typ in [u8bit,s8bit,u16bit,
  1065. s16bit,s32bit]) or (p^.treetype<>muln) then
  1066. p^.left:=gentypeconvnode(p^.left,s32fixeddef);
  1067. firstpass(p^.left);
  1068. firstpass(p^.right);
  1069. calcregisters(p,1,0,0);
  1070. p^.location.loc:=LOC_REGISTER;
  1071. end
  1072. { pointer comperation and subtraction }
  1073. else if (rd^.deftype=pointerdef) and (ld^.deftype=pointerdef) then
  1074. begin
  1075. p^.location.loc:=LOC_REGISTER;
  1076. p^.right:=gentypeconvnode(p^.right,ld);
  1077. firstpass(p^.right);
  1078. calcregisters(p,1,0,0);
  1079. case p^.treetype of
  1080. equaln,unequaln : ;
  1081. ltn,lten,gtn,gten:
  1082. begin
  1083. if not(cs_extsyntax in aktswitches) then
  1084. Message(sym_e_type_mismatch);
  1085. end;
  1086. subn:
  1087. begin
  1088. if not(cs_extsyntax in aktswitches) then
  1089. Message(sym_e_type_mismatch);
  1090. p^.resulttype:=s32bitdef;
  1091. exit;
  1092. end;
  1093. else Message(sym_e_type_mismatch);
  1094. end;
  1095. end
  1096. else if (rd^.deftype=objectdef) and (ld^.deftype=objectdef) and
  1097. pobjectdef(rd)^.isclass and pobjectdef(ld)^.isclass then
  1098. begin
  1099. p^.location.loc:=LOC_REGISTER;
  1100. if pobjectdef(rd)^.isrelated(pobjectdef(ld)) then
  1101. p^.right:=gentypeconvnode(p^.right,ld)
  1102. else
  1103. p^.left:=gentypeconvnode(p^.left,rd);
  1104. firstpass(p^.right);
  1105. firstpass(p^.left);
  1106. calcregisters(p,1,0,0);
  1107. case p^.treetype of
  1108. equaln,unequaln : ;
  1109. else Message(sym_e_type_mismatch);
  1110. end;
  1111. end
  1112. else if (rd^.deftype=classrefdef) and (ld^.deftype=classrefdef) then
  1113. begin
  1114. p^.location.loc:=LOC_REGISTER;
  1115. if pobjectdef(pclassrefdef(rd)^.definition)^.isrelated(pobjectdef(
  1116. pclassrefdef(ld)^.definition)) then
  1117. p^.right:=gentypeconvnode(p^.right,ld)
  1118. else
  1119. p^.left:=gentypeconvnode(p^.left,rd);
  1120. firstpass(p^.right);
  1121. firstpass(p^.left);
  1122. calcregisters(p,1,0,0);
  1123. case p^.treetype of
  1124. equaln,unequaln : ;
  1125. else Message(sym_e_type_mismatch);
  1126. end;
  1127. end
  1128. { allows comperasion with nil pointer }
  1129. else if (rd^.deftype=objectdef) and
  1130. pobjectdef(rd)^.isclass then
  1131. begin
  1132. p^.location.loc:=LOC_REGISTER;
  1133. p^.left:=gentypeconvnode(p^.left,rd);
  1134. firstpass(p^.left);
  1135. calcregisters(p,1,0,0);
  1136. case p^.treetype of
  1137. equaln,unequaln : ;
  1138. else Message(sym_e_type_mismatch);
  1139. end;
  1140. end
  1141. else if (ld^.deftype=objectdef) and
  1142. pobjectdef(ld)^.isclass then
  1143. begin
  1144. p^.location.loc:=LOC_REGISTER;
  1145. p^.right:=gentypeconvnode(p^.right,ld);
  1146. firstpass(p^.right);
  1147. calcregisters(p,1,0,0);
  1148. case p^.treetype of
  1149. equaln,unequaln : ;
  1150. else Message(sym_e_type_mismatch);
  1151. end;
  1152. end
  1153. else if (rd^.deftype=classrefdef) then
  1154. begin
  1155. p^.left:=gentypeconvnode(p^.left,rd);
  1156. firstpass(p^.left);
  1157. calcregisters(p,1,0,0);
  1158. case p^.treetype of
  1159. equaln,unequaln : ;
  1160. else Message(sym_e_type_mismatch);
  1161. end;
  1162. end
  1163. else if (ld^.deftype=classrefdef) then
  1164. begin
  1165. p^.right:=gentypeconvnode(p^.right,ld);
  1166. firstpass(p^.right);
  1167. calcregisters(p,1,0,0);
  1168. case p^.treetype of
  1169. equaln,unequaln : ;
  1170. else Message(sym_e_type_mismatch);
  1171. end;
  1172. end
  1173. else if (rd^.deftype=pointerdef) then
  1174. begin
  1175. p^.location.loc:=LOC_REGISTER;
  1176. p^.left:=gentypeconvnode(p^.left,s32bitdef);
  1177. firstpass(p^.left);
  1178. calcregisters(p,1,0,0);
  1179. if p^.treetype=addn then
  1180. begin
  1181. if not(cs_extsyntax in aktswitches) then
  1182. Message(sym_e_type_mismatch);
  1183. end
  1184. else Message(sym_e_type_mismatch);
  1185. end
  1186. else if (ld^.deftype=pointerdef) then
  1187. begin
  1188. p^.location.loc:=LOC_REGISTER;
  1189. p^.right:=gentypeconvnode(p^.right,s32bitdef);
  1190. firstpass(p^.right);
  1191. calcregisters(p,1,0,0);
  1192. case p^.treetype of
  1193. addn,subn : if not(cs_extsyntax in aktswitches) then
  1194. Message(sym_e_type_mismatch);
  1195. else Message(sym_e_type_mismatch);
  1196. end;
  1197. end
  1198. else if (rd^.deftype=procvardef) and (ld^.deftype=procvardef) and
  1199. is_equal(rd,ld) then
  1200. begin
  1201. calcregisters(p,1,0,0);
  1202. p^.location.loc:=LOC_REGISTER;
  1203. case p^.treetype of
  1204. equaln,unequaln : ;
  1205. else Message(sym_e_type_mismatch);
  1206. end;
  1207. end
  1208. else if (ld^.deftype=enumdef) and (rd^.deftype=enumdef)
  1209. and (is_equal(ld,rd)) then
  1210. begin
  1211. calcregisters(p,1,0,0);
  1212. case p^.treetype of
  1213. equaln,unequaln,
  1214. ltn,lten,gtn,gten : ;
  1215. else Message(sym_e_type_mismatch);
  1216. end;
  1217. end
  1218. {$ifdef SUPPORT_MMX}
  1219. else if (cs_mmx in aktswitches) and is_mmx_able_array(ld)
  1220. and is_mmx_able_array(rd) and is_equal(ld,rd) then
  1221. begin
  1222. firstpass(p^.right);
  1223. firstpass(p^.left);
  1224. case p^.treetype of
  1225. addn,subn,xorn,orn,andn:
  1226. ;
  1227. { mul is a little bit restricted }
  1228. muln:
  1229. if not(mmx_type(p^.left^.resulttype) in
  1230. [mmxu16bit,mmxs16bit,mmxfixed16]) then
  1231. Message(sym_e_type_mismatch);
  1232. else
  1233. Message(sym_e_type_mismatch);
  1234. end;
  1235. p^.location.loc:=LOC_MMXREGISTER;
  1236. calcregisters(p,0,0,1);
  1237. end
  1238. {$endif SUPPORT_MMX}
  1239. { the general solution is to convert to 32 bit int }
  1240. else
  1241. begin
  1242. { but an int/int gives real/real! }
  1243. if p^.treetype=slashn then
  1244. begin
  1245. Message(parser_w_use_int_div_int_op);
  1246. p^.right:=gentypeconvnode(p^.right,c64floatdef);
  1247. p^.left:=gentypeconvnode(p^.left,c64floatdef);
  1248. firstpass(p^.left);
  1249. firstpass(p^.right);
  1250. { maybe we need an integer register to save }
  1251. { a reference }
  1252. if ((p^.left^.location.loc<>LOC_FPU) or
  1253. (p^.right^.location.loc<>LOC_FPU)) and
  1254. (p^.left^.registers32=p^.right^.registers32) then
  1255. calcregisters(p,1,1,0)
  1256. else
  1257. calcregisters(p,0,1,0);
  1258. p^.location.loc:=LOC_FPU;
  1259. end
  1260. else
  1261. begin
  1262. p^.right:=gentypeconvnode(p^.right,s32bitdef);
  1263. p^.left:=gentypeconvnode(p^.left,s32bitdef);
  1264. firstpass(p^.left);
  1265. firstpass(p^.right);
  1266. calcregisters(p,1,0,0);
  1267. p^.location.loc:=LOC_REGISTER;
  1268. end;
  1269. end;
  1270. if codegenerror then
  1271. exit;
  1272. { determines result type for comparions }
  1273. { here the is a problem with multiple passes }
  1274. { example length(s)+1 gets internal 'longint' type first }
  1275. { if it is a arg it is converted to 'LONGINT' }
  1276. { but a second first pass will reset this to 'longint' }
  1277. case p^.treetype of
  1278. ltn,lten,gtn,gten,equaln,unequaln:
  1279. begin
  1280. if not assigned(p^.resulttype) then
  1281. p^.resulttype:=booldef;
  1282. p^.location.loc:=LOC_FLAGS;
  1283. end;
  1284. xorn:
  1285. begin
  1286. if not assigned(p^.resulttype) then
  1287. p^.resulttype:=p^.left^.resulttype;
  1288. p^.location.loc:=LOC_REGISTER;
  1289. end;
  1290. addn:
  1291. begin
  1292. { the result of a string addition is a string of length 255 }
  1293. if (p^.left^.resulttype^.deftype=stringdef) or
  1294. (p^.right^.resulttype^.deftype=stringdef) then
  1295. begin
  1296. {$ifndef UseAnsiString}
  1297. if not assigned(p^.resulttype) then
  1298. p^.resulttype:=cstringdef
  1299. {$else UseAnsiString}
  1300. if is_ansistring(p^.left^.resulttype) or
  1301. is_ansistring(p^.right^.resulttype) then
  1302. p^.resulttype:=cansistringdef
  1303. else
  1304. p^.resulttype:=cstringdef;
  1305. {$endif UseAnsiString}
  1306. end
  1307. else
  1308. if not assigned(p^.resulttype) then
  1309. p^.resulttype:=p^.left^.resulttype;
  1310. end;
  1311. else if not assigned(p^.resulttype) then
  1312. p^.resulttype:=p^.left^.resulttype;
  1313. end;
  1314. end;
  1315. procedure firstmoddiv(var p : ptree);
  1316. var
  1317. t : ptree;
  1318. {power : longint; }
  1319. begin
  1320. firstpass(p^.left);
  1321. firstpass(p^.right);
  1322. if codegenerror then
  1323. exit;
  1324. if is_constintnode(p^.left) and is_constintnode(p^.right) then
  1325. begin
  1326. case p^.treetype of
  1327. modn : t:=genordinalconstnode(p^.left^.value mod p^.right^.value,s32bitdef);
  1328. divn : t:=genordinalconstnode(p^.left^.value div p^.right^.value,s32bitdef);
  1329. end;
  1330. disposetree(p);
  1331. firstpass(t);
  1332. p:=t;
  1333. exit;
  1334. end;
  1335. { !!!!!! u32bit }
  1336. p^.right:=gentypeconvnode(p^.right,s32bitdef);
  1337. p^.left:=gentypeconvnode(p^.left,s32bitdef);
  1338. firstpass(p^.left);
  1339. firstpass(p^.right);
  1340. if codegenerror then
  1341. exit;
  1342. left_right_max(p);
  1343. p^.resulttype:=s32bitdef;
  1344. p^.location.loc:=LOC_REGISTER;
  1345. end;
  1346. procedure firstshlshr(var p : ptree);
  1347. var
  1348. t : ptree;
  1349. begin
  1350. firstpass(p^.left);
  1351. firstpass(p^.right);
  1352. if codegenerror then
  1353. exit;
  1354. if is_constintnode(p^.left) and is_constintnode(p^.right) then
  1355. begin
  1356. case p^.treetype of
  1357. shrn : t:=genordinalconstnode(p^.left^.value shr p^.right^.value,s32bitdef);
  1358. shln : t:=genordinalconstnode(p^.left^.value shl p^.right^.value,s32bitdef);
  1359. end;
  1360. disposetree(p);
  1361. firstpass(t);
  1362. p:=t;
  1363. exit;
  1364. end;
  1365. p^.right:=gentypeconvnode(p^.right,s32bitdef);
  1366. p^.left:=gentypeconvnode(p^.left,s32bitdef);
  1367. firstpass(p^.left);
  1368. firstpass(p^.right);
  1369. if codegenerror then
  1370. exit;
  1371. calcregisters(p,2,0,0);
  1372. {
  1373. p^.registers32:=p^.left^.registers32;
  1374. if p^.registers32<p^.right^.registers32 then
  1375. p^.registers32:=p^.right^.registers32;
  1376. if p^.registers32<1 then p^.registers32:=1;
  1377. }
  1378. p^.resulttype:=s32bitdef;
  1379. p^.location.loc:=LOC_REGISTER;
  1380. end;
  1381. procedure firstrealconst(var p : ptree);
  1382. begin
  1383. p^.location.loc:=LOC_MEM;
  1384. end;
  1385. procedure firstfixconst(var p : ptree);
  1386. begin
  1387. p^.location.loc:=LOC_MEM;
  1388. end;
  1389. procedure firstordconst(var p : ptree);
  1390. begin
  1391. p^.location.loc:=LOC_MEM;
  1392. end;
  1393. procedure firstniln(var p : ptree);
  1394. begin
  1395. p^.resulttype:=voidpointerdef;
  1396. p^.location.loc:=LOC_MEM;
  1397. end;
  1398. procedure firststringconst(var p : ptree);
  1399. begin
  1400. {why this !!! lost of dummy type definitions
  1401. one per const string !!!
  1402. p^.resulttype:=new(pstringdef,init(length(p^.values^)));}
  1403. if cs_ansistrings in aktswitches then
  1404. p^.resulttype:=cansistringdef
  1405. else
  1406. p^.resulttype:=cstringdef;
  1407. p^.location.loc:=LOC_MEM;
  1408. end;
  1409. procedure firstumminus(var p : ptree);
  1410. var
  1411. t : ptree;
  1412. minusdef : pprocdef;
  1413. begin
  1414. firstpass(p^.left);
  1415. p^.registers32:=p^.left^.registers32;
  1416. p^.registersfpu:=p^.left^.registersfpu;
  1417. {$ifdef SUPPORT_MMX}
  1418. p^.registersmmx:=p^.left^.registersmmx;
  1419. {$endif SUPPORT_MMX}
  1420. p^.resulttype:=p^.left^.resulttype;
  1421. if codegenerror then
  1422. exit;
  1423. if is_constintnode(p^.left) then
  1424. begin
  1425. t:=genordinalconstnode(-p^.left^.value,s32bitdef);
  1426. disposetree(p);
  1427. firstpass(t);
  1428. p:=t;
  1429. exit;
  1430. end;
  1431. { nasm can not cope with negativ reals !! }
  1432. if is_constrealnode(p^.left)
  1433. {$ifdef i386}
  1434. and not(aktoutputformat in [as_nasmcoff,as_nasmelf,as_nasmobj])
  1435. {$endif}
  1436. then
  1437. begin
  1438. t:=genrealconstnode(-p^.left^.valued);
  1439. disposetree(p);
  1440. firstpass(t);
  1441. p:=t;
  1442. exit;
  1443. end;
  1444. if (p^.left^.resulttype^.deftype=floatdef) then
  1445. begin
  1446. if pfloatdef(p^.left^.resulttype)^.typ=f32bit then
  1447. begin
  1448. if (p^.left^.location.loc<>LOC_REGISTER) and
  1449. (p^.registers32<1) then
  1450. p^.registers32:=1;
  1451. p^.location.loc:=LOC_REGISTER;
  1452. end
  1453. else
  1454. p^.location.loc:=LOC_FPU;
  1455. end
  1456. {$ifdef SUPPORT_MMX}
  1457. else if (cs_mmx in aktswitches) and
  1458. is_mmx_able_array(p^.left^.resulttype) then
  1459. begin
  1460. if (p^.left^.location.loc<>LOC_MMXREGISTER) and
  1461. (p^.registersmmx<1) then
  1462. p^.registersmmx:=1;
  1463. { if saturation is on, p^.left^.resulttype isn't
  1464. "mmx able" (FK)
  1465. if (cs_mmx_saturation in aktswitches^) and
  1466. (porddef(parraydef(p^.resulttype)^.definition)^.typ in
  1467. [s32bit,u32bit]) then
  1468. Message(sym_e_type_mismatch);
  1469. }
  1470. end
  1471. {$endif SUPPORT_MMX}
  1472. else if (p^.left^.resulttype^.deftype=orddef) then
  1473. begin
  1474. p^.left:=gentypeconvnode(p^.left,s32bitdef);
  1475. firstpass(p^.left);
  1476. p^.registersfpu:=p^.left^.registersfpu;
  1477. {$ifdef SUPPORT_MMX}
  1478. p^.registersmmx:=p^.left^.registersmmx;
  1479. {$endif SUPPORT_MMX}
  1480. p^.registers32:=p^.left^.registers32;
  1481. if codegenerror then
  1482. exit;
  1483. if (p^.left^.location.loc<>LOC_REGISTER) and
  1484. (p^.registers32<1) then
  1485. p^.registers32:=1;
  1486. p^.location.loc:=LOC_REGISTER;
  1487. p^.resulttype:=p^.left^.resulttype;
  1488. end
  1489. else
  1490. begin
  1491. if assigned(overloaded_operators[minus]) then
  1492. minusdef:=overloaded_operators[minus]^.definition
  1493. else
  1494. minusdef:=nil;
  1495. while assigned(minusdef) do
  1496. begin
  1497. if (minusdef^.para1^.data=p^.left^.resulttype) and
  1498. (minusdef^.para1^.next=nil) then
  1499. begin
  1500. t:=gencallnode(overloaded_operators[minus],nil);
  1501. t^.left:=gencallparanode(p^.left,nil);
  1502. putnode(p);
  1503. p:=t;
  1504. firstpass(p);
  1505. exit;
  1506. end;
  1507. minusdef:=minusdef^.nextoverloaded;
  1508. end;
  1509. Message(sym_e_type_mismatch);
  1510. end;
  1511. end;
  1512. procedure firstaddr(var p : ptree);
  1513. var
  1514. hp : ptree;
  1515. hp2 : pdefcoll;
  1516. store_valid : boolean;
  1517. begin
  1518. make_not_regable(p^.left);
  1519. if not(assigned(p^.resulttype)) then
  1520. begin
  1521. if p^.left^.treetype=calln then
  1522. begin
  1523. hp:=genloadnode(pvarsym(p^.left^.symtableprocentry),p^.left^.symtableproc);
  1524. { result is a procedure variable }
  1525. { No, to be TP compatible, you must return a pointer to
  1526. the procedure that is stored in the procvar.}
  1527. if not(cs_tp_compatible in aktswitches) then
  1528. begin
  1529. p^.resulttype:=new(pprocvardef,init);
  1530. pprocvardef(p^.resulttype)^.options:=
  1531. p^.left^.symtableprocentry^.definition^.options;
  1532. pprocvardef(p^.resulttype)^.retdef:=
  1533. p^.left^.symtableprocentry^.definition^.retdef;
  1534. hp2:=p^.left^.symtableprocentry^.definition^.para1;
  1535. while assigned(hp2) do
  1536. begin
  1537. pprocvardef(p^.resulttype)^.concatdef(hp2^.data,hp2^.paratyp);
  1538. hp2:=hp2^.next;
  1539. end;
  1540. end
  1541. else
  1542. p^.resulttype:=voidpointerdef;
  1543. disposetree(p^.left);
  1544. p^.left:=hp;
  1545. end
  1546. else
  1547. begin
  1548. if not(cs_typed_addresses in aktswitches) then
  1549. p^.resulttype:=voidpointerdef
  1550. else p^.resulttype:=new(ppointerdef,init(p^.left^.resulttype));
  1551. end;
  1552. end;
  1553. store_valid:=must_be_valid;
  1554. must_be_valid:=false;
  1555. firstpass(p^.left);
  1556. must_be_valid:=store_valid;
  1557. if codegenerror then
  1558. exit;
  1559. { we should allow loc_mem for @string }
  1560. if (p^.left^.location.loc<>LOC_REFERENCE) and
  1561. (p^.left^.location.loc<>LOC_MEM) then
  1562. Message(cg_e_illegal_expression);
  1563. p^.registers32:=p^.left^.registers32;
  1564. p^.registersfpu:=p^.left^.registersfpu;
  1565. {$ifdef SUPPORT_MMX}
  1566. p^.registersmmx:=p^.left^.registersmmx;
  1567. {$endif SUPPORT_MMX}
  1568. if p^.registers32<1 then
  1569. p^.registers32:=1;
  1570. p^.location.loc:=LOC_REGISTER;
  1571. end;
  1572. procedure firstdoubleaddr(var p : ptree);
  1573. begin
  1574. make_not_regable(p^.left);
  1575. firstpass(p^.left);
  1576. if p^.resulttype=nil then
  1577. p^.resulttype:=voidpointerdef;
  1578. if (p^.left^.resulttype^.deftype)<>procvardef then
  1579. Message(cg_e_illegal_expression);
  1580. if codegenerror then
  1581. exit;
  1582. if (p^.left^.location.loc<>LOC_REFERENCE) then
  1583. Message(cg_e_illegal_expression);
  1584. p^.registers32:=p^.left^.registers32;
  1585. p^.registersfpu:=p^.left^.registersfpu;
  1586. {$ifdef SUPPORT_MMX}
  1587. p^.registersmmx:=p^.left^.registersmmx;
  1588. {$endif SUPPORT_MMX}
  1589. if p^.registers32<1 then
  1590. p^.registers32:=1;
  1591. p^.location.loc:=LOC_REGISTER;
  1592. end;
  1593. procedure firstnot(var p : ptree);
  1594. var
  1595. t : ptree;
  1596. begin
  1597. firstpass(p^.left);
  1598. if codegenerror then
  1599. exit;
  1600. if (p^.left^.treetype=ordconstn) then
  1601. begin
  1602. t:=genordinalconstnode(not(p^.left^.value),p^.left^.resulttype);
  1603. disposetree(p);
  1604. firstpass(t);
  1605. p:=t;
  1606. exit;
  1607. end;
  1608. p^.resulttype:=p^.left^.resulttype;
  1609. p^.location.loc:=p^.left^.location.loc;
  1610. {$ifdef SUPPORT_MMX}
  1611. p^.registersmmx:=p^.left^.registersmmx;
  1612. {$endif SUPPORT_MMX}
  1613. if is_equal(p^.resulttype,booldef) then
  1614. begin
  1615. p^.registers32:=p^.left^.registers32;
  1616. if ((p^.location.loc=LOC_REFERENCE) or
  1617. (p^.location.loc=LOC_CREGISTER)) and
  1618. (p^.registers32<1) then
  1619. p^.registers32:=1;
  1620. end
  1621. else
  1622. {$ifdef SUPPORT_MMX}
  1623. if (cs_mmx in aktswitches) and
  1624. is_mmx_able_array(p^.left^.resulttype) then
  1625. begin
  1626. if (p^.left^.location.loc<>LOC_MMXREGISTER) and
  1627. (p^.registersmmx<1) then
  1628. p^.registersmmx:=1;
  1629. end
  1630. else
  1631. {$endif SUPPORT_MMX}
  1632. begin
  1633. p^.left:=gentypeconvnode(p^.left,s32bitdef);
  1634. firstpass(p^.left);
  1635. if codegenerror then
  1636. exit;
  1637. p^.resulttype:=p^.left^.resulttype;
  1638. p^.registers32:=p^.left^.registers32;
  1639. {$ifdef SUPPORT_MMX}
  1640. p^.registersmmx:=p^.left^.registersmmx;
  1641. {$endif SUPPORT_MMX}
  1642. if (p^.left^.location.loc<>LOC_REGISTER) and
  1643. (p^.registers32<1) then
  1644. p^.registers32:=1;
  1645. p^.location.loc:=LOC_REGISTER;
  1646. end;
  1647. p^.registersfpu:=p^.left^.registersfpu;
  1648. end;
  1649. procedure firstnothing(var p : ptree);
  1650. begin
  1651. p^.resulttype:=voiddef;
  1652. end;
  1653. procedure firstassignment(var p : ptree);
  1654. var
  1655. store_valid : boolean;
  1656. hp : ptree;
  1657. begin
  1658. store_valid:=must_be_valid;
  1659. must_be_valid:=false;
  1660. firstpass(p^.left);
  1661. { assignements to open arrays aren't allowed }
  1662. if is_open_array(p^.left^.resulttype) then
  1663. Message(sym_e_type_mismatch);
  1664. { test if we can avoid copying string to temp
  1665. as in s:=s+...; (PM) }
  1666. {$ifdef dummyi386}
  1667. if ((p^.right^.treetype=addn) or (p^.right^.treetype=subn)) and
  1668. equal_trees(p^.left,p^.right^.left) and
  1669. (ret_in_acc(p^.left^.resulttype)) and
  1670. (not cs_rangechecking in aktswitches^) then
  1671. begin
  1672. disposetree(p^.right^.left);
  1673. hp:=p^.right;
  1674. p^.right:=p^.right^.right;
  1675. if hp^.treetype=addn then
  1676. p^.assigntyp:=at_plus
  1677. else
  1678. p^.assigntyp:=at_minus;
  1679. putnode(hp);
  1680. end;
  1681. if p^.assigntyp<>at_normal then
  1682. begin
  1683. { for fpu type there is no faster way }
  1684. if is_fpu(p^.left^.resulttype) then
  1685. case p^.assigntyp of
  1686. at_plus : p^.right:=gennode(addn,getcopy(p^.left),p^.right);
  1687. at_minus : p^.right:=gennode(subn,getcopy(p^.left),p^.right);
  1688. at_star : p^.right:=gennode(muln,getcopy(p^.left),p^.right);
  1689. at_slash : p^.right:=gennode(slashn,getcopy(p^.left),p^.right);
  1690. end;
  1691. end;
  1692. {$endif i386}
  1693. must_be_valid:=true;
  1694. firstpass(p^.right);
  1695. must_be_valid:=store_valid;
  1696. if codegenerror then
  1697. exit;
  1698. { some string functions don't need conversion, so treat them separatly }
  1699. if is_shortstring(p^.left^.resulttype) and (assigned(p^.right^.resulttype)) then
  1700. begin
  1701. if not ((p^.right^.resulttype^.deftype=stringdef) or
  1702. ((p^.right^.resulttype^.deftype=orddef) and (porddef(p^.right^.resulttype)^.typ=uchar))) then
  1703. begin
  1704. p^.right:=gentypeconvnode(p^.right,p^.left^.resulttype);
  1705. firstpass(p^.right);
  1706. if codegenerror then
  1707. exit;
  1708. end;
  1709. { we call STRCOPY }
  1710. procinfo.flags:=procinfo.flags or pi_do_call;
  1711. hp:=p^.right;
  1712. { test for s:=s+anything ... }
  1713. { the problem is for
  1714. s:=s+s+s;
  1715. this is broken here !! }
  1716. { while hp^.treetype=addn do hp:=hp^.left;
  1717. if equal_trees(p^.left,hp) then
  1718. begin
  1719. p^.concat_string:=true;
  1720. hp:=p^.right;
  1721. while hp^.treetype=addn do
  1722. begin
  1723. hp^.use_strconcat:=true;
  1724. hp:=hp^.left;
  1725. end;
  1726. end; }
  1727. end
  1728. else
  1729. begin
  1730. if (p^.right^.treetype=realconstn) then
  1731. begin
  1732. if p^.left^.resulttype^.deftype=floatdef then
  1733. begin
  1734. case pfloatdef(p^.left^.resulttype)^.typ of
  1735. s32real : p^.right^.realtyp:=ait_real_32bit;
  1736. s64real : p^.right^.realtyp:=ait_real_64bit;
  1737. s80real : p^.right^.realtyp:=ait_real_extended;
  1738. { what about f32bit and s64bit }
  1739. else
  1740. begin
  1741. p^.right:=gentypeconvnode(p^.right,p^.left^.resulttype);
  1742. { nochmal firstpass wegen der Typkonvertierung aufrufen }
  1743. firstpass(p^.right);
  1744. if codegenerror then
  1745. exit;
  1746. end;
  1747. end;
  1748. end;
  1749. end
  1750. else
  1751. begin
  1752. p^.right:=gentypeconvnode(p^.right,p^.left^.resulttype);
  1753. firstpass(p^.right);
  1754. if codegenerror then
  1755. exit;
  1756. end;
  1757. end;
  1758. p^.resulttype:=voiddef;
  1759. {
  1760. p^.registers32:=max(p^.left^.registers32,p^.right^.registers32);
  1761. p^.registersfpu:=max(p^.left^.registersfpu,p^.right^.registersfpu);
  1762. }
  1763. p^.registers32:=p^.left^.registers32+p^.right^.registers32;
  1764. p^.registersfpu:=max(p^.left^.registersfpu,p^.right^.registersfpu);
  1765. {$ifdef SUPPORT_MMX}
  1766. p^.registersmmx:=max(p^.left^.registersmmx,p^.right^.registersmmx);
  1767. {$endif SUPPORT_MMX}
  1768. end;
  1769. procedure firstlr(var p : ptree);
  1770. begin
  1771. firstpass(p^.left);
  1772. firstpass(p^.right);
  1773. end;
  1774. procedure firstderef(var p : ptree);
  1775. begin
  1776. firstpass(p^.left);
  1777. if codegenerror then
  1778. begin
  1779. p^.resulttype:=generrordef;
  1780. exit;
  1781. end;
  1782. p^.registers32:=max(p^.left^.registers32,1);
  1783. p^.registersfpu:=p^.left^.registersfpu;
  1784. {$ifdef SUPPORT_MMX}
  1785. p^.registersmmx:=p^.left^.registersmmx;
  1786. {$endif SUPPORT_MMX}
  1787. if p^.left^.resulttype^.deftype<>pointerdef then
  1788. Message(cg_e_invalid_qualifier);
  1789. p^.resulttype:=ppointerdef(p^.left^.resulttype)^.definition;
  1790. p^.location.loc:=LOC_REFERENCE;
  1791. end;
  1792. procedure firstrange(var p : ptree);
  1793. var
  1794. ct : tconverttype;
  1795. begin
  1796. firstpass(p^.left);
  1797. firstpass(p^.right);
  1798. if codegenerror then
  1799. exit;
  1800. { allow only ordinal constants }
  1801. if not((p^.left^.treetype=ordconstn) and
  1802. (p^.right^.treetype=ordconstn)) then
  1803. Message(cg_e_illegal_expression);
  1804. { upper limit must be greater or equal than lower limit }
  1805. { not if u32bit }
  1806. if (p^.left^.value>p^.right^.value) and
  1807. (( p^.left^.value<0) or (p^.right^.value>=0)) then
  1808. Message(cg_e_upper_lower_than_lower);
  1809. { both types must be compatible }
  1810. if not(isconvertable(p^.left^.resulttype,p^.right^.resulttype,
  1811. ct,ordconstn,false)) and
  1812. not(is_equal(p^.left^.resulttype,p^.right^.resulttype)) then
  1813. Message(sym_e_type_mismatch);
  1814. end;
  1815. {
  1816. begin
  1817. p^.registers32:=max(p^.left^.registers32,p^.right^.registers32);
  1818. if p^.right^.treetype<>ordconstn then
  1819. begin
  1820. case p^.right^.location.loc of
  1821. LOC_MEM,LOC_REFERENCE,
  1822. LOC_CREGISTER,LOC_FLAGS:
  1823. inc(p^.registers32);
  1824. end;
  1825. end;
  1826. end;
  1827. }
  1828. procedure firstvecn(var p : ptree);
  1829. var
  1830. harr : pdef;
  1831. ct : tconverttype;
  1832. begin
  1833. firstpass(p^.left);
  1834. firstpass(p^.right);
  1835. if codegenerror then
  1836. exit;
  1837. { range check only for arrays }
  1838. if (p^.left^.resulttype^.deftype=arraydef) then
  1839. begin
  1840. if not(isconvertable(p^.right^.resulttype,
  1841. parraydef(p^.left^.resulttype)^.rangedef,
  1842. ct,ordconstn,false)) and
  1843. not(is_equal(p^.right^.resulttype,
  1844. parraydef(p^.left^.resulttype)^.rangedef)) then
  1845. Message(sym_e_type_mismatch);
  1846. end;
  1847. { Never convert a boolean or a char !}
  1848. { maybe type conversion }
  1849. if (p^.right^.resulttype^.deftype<>enumdef) and
  1850. not ((p^.right^.resulttype^.deftype=orddef) and
  1851. (Porddef(p^.right^.resulttype)^.typ in [bool8bit,bool16bit,bool32bit,uchar])) then
  1852. begin
  1853. p^.right:=gentypeconvnode(p^.right,s32bitdef);
  1854. { once more firstpass }
  1855. {?? It's better to only firstpass when the tree has
  1856. changed, isn't it ?}
  1857. firstpass(p^.right);
  1858. end;
  1859. if codegenerror then
  1860. exit;
  1861. { determine return type }
  1862. if not assigned(p^.resulttype) then
  1863. if p^.left^.resulttype^.deftype=arraydef then
  1864. p^.resulttype:=parraydef(p^.left^.resulttype)^.definition
  1865. else if (p^.left^.resulttype^.deftype=pointerdef) then
  1866. begin
  1867. { convert pointer to array }
  1868. harr:=new(parraydef,init(0,$7fffffff,s32bitdef));
  1869. parraydef(harr)^.definition:=ppointerdef(p^.left^.resulttype)^.definition;
  1870. p^.left:=gentypeconvnode(p^.left,harr);
  1871. firstpass(p^.left);
  1872. if codegenerror then
  1873. exit;
  1874. p^.resulttype:=parraydef(harr)^.definition
  1875. end
  1876. else if p^.left^.resulttype^.deftype=stringdef then
  1877. begin
  1878. { indexed access to strings }
  1879. case pstringdef(p^.left^.resulttype)^.string_typ of
  1880. {
  1881. st_widestring : p^.resulttype:=cwchardef;
  1882. }
  1883. st_ansistring : p^.resulttype:=cchardef;
  1884. st_longstring : p^.resulttype:=cchardef;
  1885. st_shortstring : p^.resulttype:=cchardef;
  1886. end;
  1887. end
  1888. else
  1889. Message(sym_e_type_mismatch);
  1890. { the register calculation is easy if a const index is used }
  1891. if p^.right^.treetype=ordconstn then
  1892. begin
  1893. p^.registers32:=p^.left^.registers32;
  1894. { for ansi/wide strings, we need at least one register }
  1895. if is_ansistring(p^.left^.resulttype) or
  1896. is_widestring(p^.left^.resulttype) then
  1897. p^.registers32:=max(p^.registers32,1);
  1898. end
  1899. else
  1900. begin
  1901. { this rules are suboptimal, but they should give }
  1902. { good results }
  1903. p^.registers32:=max(p^.left^.registers32,p^.right^.registers32);
  1904. { for ansi/wide strings, we need at least one register }
  1905. if is_ansistring(p^.left^.resulttype) or
  1906. is_widestring(p^.left^.resulttype) then
  1907. p^.registers32:=max(p^.registers32,1);
  1908. { need we an extra register when doing the restore ? }
  1909. if (p^.left^.registers32<=p^.right^.registers32) and
  1910. { only if the node needs less than 3 registers }
  1911. { two for the right node and one for the }
  1912. { left address }
  1913. (p^.registers32<3) then
  1914. inc(p^.registers32);
  1915. { need we an extra register for the index ? }
  1916. if (p^.right^.location.loc<>LOC_REGISTER)
  1917. { only if the right node doesn't need a register }
  1918. and (p^.right^.registers32<1) then
  1919. inc(p^.registers32);
  1920. { not correct, but what works better ?
  1921. if p^.left^.registers32>0 then
  1922. p^.registers32:=max(p^.registers32,2)
  1923. else
  1924. min. one register
  1925. p^.registers32:=max(p^.registers32,1);
  1926. }
  1927. end;
  1928. p^.registersfpu:=max(p^.left^.registersfpu,p^.right^.registersfpu);
  1929. {$ifdef SUPPORT_MMX}
  1930. p^.registersmmx:=max(p^.left^.registersmmx,p^.right^.registersmmx);
  1931. {$endif SUPPORT_MMX}
  1932. p^.location.loc:=p^.left^.location.loc;
  1933. end;
  1934. type
  1935. tfirstconvproc = procedure(var p : ptree);
  1936. procedure first_bigger_smaller(var p : ptree);
  1937. begin
  1938. if (p^.left^.location.loc<>LOC_REGISTER) and (p^.registers32=0) then
  1939. p^.registers32:=1;
  1940. p^.location.loc:=LOC_REGISTER;
  1941. end;
  1942. procedure first_cstring_charpointer(var p : ptree);
  1943. begin
  1944. p^.registers32:=1;
  1945. p^.location.loc:=LOC_REGISTER;
  1946. end;
  1947. procedure first_string_chararray(var p : ptree);
  1948. begin
  1949. p^.registers32:=1;
  1950. p^.location.loc:=LOC_REGISTER;
  1951. end;
  1952. procedure first_string_string(var p : ptree);
  1953. begin
  1954. if pstringdef(p^.resulttype)^.string_typ<>
  1955. pstringdef(p^.left^.resulttype)^.string_typ then
  1956. begin
  1957. if p^.left^.treetype=stringconstn then
  1958. p^.left^.stringtype:=pstringdef(p^.resulttype)^.string_typ
  1959. else
  1960. procinfo.flags:=procinfo.flags or pi_do_call;
  1961. end;
  1962. { for simplicity lets first keep all ansistrings
  1963. as LOC_MEM, could also become LOC_REGISTER }
  1964. p^.location.loc:=LOC_MEM;
  1965. end;
  1966. procedure first_char_to_string(var p : ptree);
  1967. var
  1968. hp : ptree;
  1969. begin
  1970. if p^.left^.treetype=ordconstn then
  1971. begin
  1972. hp:=genstringconstnode(chr(p^.left^.value));
  1973. firstpass(hp);
  1974. disposetree(p);
  1975. p:=hp;
  1976. end
  1977. else
  1978. p^.location.loc:=LOC_MEM;
  1979. end;
  1980. procedure first_nothing(var p : ptree);
  1981. begin
  1982. p^.location.loc:=LOC_MEM;
  1983. end;
  1984. procedure first_array_to_pointer(var p : ptree);
  1985. begin
  1986. if p^.registers32<1 then
  1987. p^.registers32:=1;
  1988. p^.location.loc:=LOC_REGISTER;
  1989. end;
  1990. procedure first_int_real(var p : ptree);
  1991. var t : ptree;
  1992. begin
  1993. if p^.left^.treetype=ordconstn then
  1994. begin
  1995. { convert constants direct }
  1996. { not because of type conversion }
  1997. t:=genrealconstnode(p^.left^.value);
  1998. { do a first pass here
  1999. because firstpass of typeconv does
  2000. not redo it for left field !! }
  2001. firstpass(t);
  2002. { the type can be something else than s64real !!}
  2003. t:=gentypeconvnode(t,p^.resulttype);
  2004. firstpass(t);
  2005. disposetree(p);
  2006. p:=t;
  2007. exit;
  2008. end
  2009. else
  2010. begin
  2011. if p^.registersfpu<1 then
  2012. p^.registersfpu:=1;
  2013. p^.location.loc:=LOC_FPU;
  2014. end;
  2015. end;
  2016. procedure first_int_fix(var p : ptree);
  2017. begin
  2018. if p^.left^.treetype=ordconstn then
  2019. begin
  2020. { convert constants direct }
  2021. p^.treetype:=fixconstn;
  2022. p^.valuef:=p^.left^.value shl 16;
  2023. p^.disposetyp:=dt_nothing;
  2024. disposetree(p^.left);
  2025. p^.location.loc:=LOC_MEM;
  2026. end
  2027. else
  2028. begin
  2029. if p^.registers32<1 then
  2030. p^.registers32:=1;
  2031. p^.location.loc:=LOC_REGISTER;
  2032. end;
  2033. end;
  2034. procedure first_real_fix(var p : ptree);
  2035. begin
  2036. if p^.left^.treetype=realconstn then
  2037. begin
  2038. { convert constants direct }
  2039. p^.treetype:=fixconstn;
  2040. p^.valuef:=round(p^.left^.valued*65536);
  2041. p^.disposetyp:=dt_nothing;
  2042. disposetree(p^.left);
  2043. p^.location.loc:=LOC_MEM;
  2044. end
  2045. else
  2046. begin
  2047. { at least one fpu and int register needed }
  2048. if p^.registers32<1 then
  2049. p^.registers32:=1;
  2050. if p^.registersfpu<1 then
  2051. p^.registersfpu:=1;
  2052. p^.location.loc:=LOC_REGISTER;
  2053. end;
  2054. end;
  2055. procedure first_fix_real(var p : ptree);
  2056. begin
  2057. if p^.left^.treetype=fixconstn then
  2058. begin
  2059. { convert constants direct }
  2060. p^.treetype:=realconstn;
  2061. p^.valued:=round(p^.left^.valuef/65536.0);
  2062. p^.disposetyp:=dt_nothing;
  2063. disposetree(p^.left);
  2064. p^.location.loc:=LOC_MEM;
  2065. end
  2066. else
  2067. begin
  2068. if p^.registersfpu<1 then
  2069. p^.registersfpu:=1;
  2070. p^.location.loc:=LOC_FPU;
  2071. end;
  2072. end;
  2073. procedure first_real_real(var p : ptree);
  2074. begin
  2075. if p^.registersfpu<1 then
  2076. p^.registersfpu:=1;
  2077. p^.location.loc:=LOC_FPU;
  2078. end;
  2079. procedure first_pointer_to_array(var p : ptree);
  2080. begin
  2081. if p^.registers32<1 then
  2082. p^.registers32:=1;
  2083. p^.location.loc:=LOC_REFERENCE;
  2084. end;
  2085. procedure first_chararray_string(var p : ptree);
  2086. begin
  2087. { the only important information is the location of the }
  2088. { result }
  2089. { other stuff is done by firsttypeconv }
  2090. p^.location.loc:=LOC_MEM;
  2091. end;
  2092. procedure first_cchar_charpointer(var p : ptree);
  2093. begin
  2094. p^.left:=gentypeconvnode(p^.left,cstringdef);
  2095. { convert constant char to constant string }
  2096. firstpass(p^.left);
  2097. { evalute tree }
  2098. firstpass(p);
  2099. end;
  2100. procedure first_locmem(var p : ptree);
  2101. begin
  2102. p^.location.loc:=LOC_MEM;
  2103. end;
  2104. procedure first_bool_int(var p : ptree);
  2105. begin
  2106. p^.location.loc:=LOC_REGISTER;
  2107. { Florian I think this is overestimated
  2108. but I still do not really understand how to get this right (PM) }
  2109. { Hmmm, I think we need only one reg to return the result of }
  2110. { this node => so }
  2111. if p^.registers32<1 then
  2112. p^.registers32:=1;
  2113. { should work (FK)
  2114. p^.registers32:=p^.left^.registers32+1;}
  2115. end;
  2116. procedure first_int_bool(var p : ptree);
  2117. begin
  2118. p^.location.loc:=LOC_REGISTER;
  2119. { Florian I think this is overestimated
  2120. but I still do not really understand how to get this right (PM) }
  2121. { Hmmm, I think we need only one reg to return the result of }
  2122. { this node => so }
  2123. p^.left:=gentypeconvnode(p^.left,s32bitdef);
  2124. firstpass(p^.left);
  2125. if p^.registers32<1 then
  2126. p^.registers32:=1;
  2127. { p^.resulttype:=booldef; }
  2128. { should work (FK)
  2129. p^.registers32:=p^.left^.registers32+1;}
  2130. end;
  2131. procedure first_proc_to_procvar(var p : ptree);
  2132. begin
  2133. firstpass(p^.left);
  2134. if codegenerror then
  2135. exit;
  2136. if (p^.left^.location.loc<>LOC_REFERENCE) then
  2137. Message(cg_e_illegal_expression);
  2138. p^.registers32:=p^.left^.registers32;
  2139. if p^.registers32<1 then
  2140. p^.registers32:=1;
  2141. p^.location.loc:=LOC_REGISTER;
  2142. end;
  2143. function is_procsym_load(p:Ptree):boolean;
  2144. begin
  2145. is_procsym_load:=((p^.treetype=loadn) and (p^.symtableentry^.typ=procsym)) or
  2146. ((p^.treetype=addrn) and (p^.left^.treetype=loadn)
  2147. and (p^.left^.symtableentry^.typ=procsym)) ;
  2148. end;
  2149. { change a proc call to a procload for assignment to a procvar }
  2150. { this can only happen for proc/function without arguments }
  2151. function is_procsym_call(p:Ptree):boolean;
  2152. begin
  2153. is_procsym_call:=(p^.treetype=calln) and (p^.left=nil) and
  2154. (((p^.symtableprocentry^.typ=procsym) and (p^.right=nil)) or
  2155. ((p^.right<>nil) and (p^.right^.symtableprocentry^.typ=varsym)));
  2156. end;
  2157. {***}
  2158. function is_assignment_overloaded(from_def,to_def : pdef) : boolean;
  2159. var
  2160. passproc : pprocdef;
  2161. convtyp : tconverttype;
  2162. begin
  2163. is_assignment_overloaded:=false;
  2164. if assigned(overloaded_operators[assignment]) then
  2165. passproc:=overloaded_operators[assignment]^.definition
  2166. else
  2167. exit;
  2168. while passproc<>nil do
  2169. begin
  2170. if is_equal(passproc^.retdef,to_def) and
  2171. isconvertable(from_def,passproc^.para1^.data,convtyp,
  2172. ordconstn { nur Dummy},false ) then
  2173. begin
  2174. is_assignment_overloaded:=true;
  2175. break;
  2176. end;
  2177. passproc:=passproc^.nextoverloaded;
  2178. end;
  2179. end;
  2180. { Attention: do *** no *** recursive call of firstpass }
  2181. { because the child tree is always passed }
  2182. procedure firsttypeconv(var p : ptree);
  2183. var
  2184. hp : ptree;
  2185. aprocdef : pprocdef;
  2186. proctype : tdeftype;
  2187. const
  2188. firstconvert : array[tc_u8bit_2_s32bit..tc_cchar_charpointer] of
  2189. tfirstconvproc = (first_bigger_smaller,first_nothing,first_bigger_smaller,
  2190. first_bigger_smaller,first_bigger_smaller,
  2191. first_bigger_smaller,first_bigger_smaller,
  2192. first_bigger_smaller,first_string_string,
  2193. first_cstring_charpointer,first_string_chararray,
  2194. first_array_to_pointer,first_pointer_to_array,
  2195. first_char_to_string,first_bigger_smaller,
  2196. first_bigger_smaller,first_bigger_smaller,
  2197. first_bigger_smaller,first_bigger_smaller,
  2198. first_bigger_smaller,first_bigger_smaller,
  2199. first_bigger_smaller,first_bigger_smaller,
  2200. first_bigger_smaller,first_bigger_smaller,
  2201. first_bigger_smaller,first_bigger_smaller,
  2202. first_bigger_smaller,first_bigger_smaller,
  2203. first_bigger_smaller,first_bigger_smaller,
  2204. first_bigger_smaller,first_bigger_smaller,
  2205. first_bool_int,first_int_bool,
  2206. first_int_real,first_real_fix,
  2207. first_fix_real,first_int_fix,first_real_real,
  2208. first_locmem,first_proc_to_procvar,
  2209. first_cchar_charpointer);
  2210. begin
  2211. aprocdef:=nil;
  2212. { if explicite type conversation, then run firstpass }
  2213. if p^.explizit then
  2214. firstpass(p^.left);
  2215. if codegenerror then
  2216. begin
  2217. p^.resulttype:=generrordef;
  2218. exit;
  2219. end;
  2220. if not assigned(p^.left^.resulttype) then
  2221. begin
  2222. codegenerror:=true;
  2223. internalerror(52349);
  2224. exit;
  2225. end;
  2226. { remove obsolete type conversions }
  2227. if is_equal(p^.left^.resulttype,p^.resulttype) then
  2228. begin
  2229. hp:=p;
  2230. p:=p^.left;
  2231. p^.resulttype:=hp^.resulttype;
  2232. putnode(hp);
  2233. exit;
  2234. end;
  2235. p^.registers32:=p^.left^.registers32;
  2236. p^.registersfpu:=p^.left^.registersfpu;
  2237. {$ifdef SUPPORT_MMX}
  2238. p^.registersmmx:=p^.left^.registersmmx;
  2239. {$endif}
  2240. set_location(p^.location,p^.left^.location);
  2241. if is_assignment_overloaded(p^.left^.resulttype,p^.resulttype) then
  2242. begin
  2243. procinfo.flags:=procinfo.flags or pi_do_call;
  2244. hp:=gencallnode(overloaded_operators[assignment],nil);
  2245. hp^.left:=gencallparanode(p^.left,nil);
  2246. putnode(p);
  2247. p:=hp;
  2248. firstpass(p);
  2249. exit;
  2250. end;
  2251. if (not(isconvertable(p^.left^.resulttype,p^.resulttype,
  2252. p^.convtyp,p^.left^.treetype,p^.explizit))) then
  2253. begin
  2254. {Procedures have a resulttype of voiddef and functions of their
  2255. own resulttype. They will therefore always be incompatible with
  2256. a procvar. Because isconvertable cannot check for procedures we
  2257. use an extra check for them.}
  2258. if (cs_tp_compatible in aktswitches) and
  2259. ((is_procsym_load(p^.left) or is_procsym_call(p^.left)) and
  2260. (p^.resulttype^.deftype=procvardef)) then
  2261. begin
  2262. { just a test: p^.explizit:=false; }
  2263. if is_procsym_call(p^.left) then
  2264. begin
  2265. if p^.left^.right=nil then
  2266. begin
  2267. p^.left^.treetype:=loadn;
  2268. { are at same offset so this could be spared, but
  2269. it more secure to do it anyway }
  2270. p^.left^.symtableentry:=p^.left^.symtableprocentry;
  2271. p^.left^.resulttype:=pprocsym(p^.left^.symtableentry)^.definition;
  2272. aprocdef:=pprocdef(p^.left^.resulttype);
  2273. end
  2274. else
  2275. begin
  2276. p^.left^.right^.treetype:=loadn;
  2277. p^.left^.right^.symtableentry:=p^.left^.right^.symtableentry;
  2278. P^.left^.right^.resulttype:=pvarsym(p^.left^.symtableentry)^.definition;
  2279. hp:=p^.left^.right;
  2280. putnode(p^.left);
  2281. p^.left:=hp;
  2282. { should we do that ? }
  2283. firstpass(p^.left);
  2284. if not is_equal(p^.left^.resulttype,p^.resulttype) then
  2285. begin
  2286. Message(sym_e_type_mismatch);
  2287. exit;
  2288. end
  2289. else
  2290. begin
  2291. hp:=p;
  2292. p:=p^.left;
  2293. p^.resulttype:=hp^.resulttype;
  2294. putnode(hp);
  2295. exit;
  2296. end;
  2297. end;
  2298. end
  2299. else
  2300. begin
  2301. if p^.left^.treetype=addrn then
  2302. begin
  2303. hp:=p^.left;
  2304. p^.left:=p^.left^.left;
  2305. putnode(p^.left);
  2306. end
  2307. else
  2308. aprocdef:=pprocsym(p^.left^.symtableentry)^.definition;
  2309. end;
  2310. p^.convtyp:=tc_proc2procvar;
  2311. { Now check if the procedure we are going to assign to
  2312. the procvar, is compatible with the procvar's type.
  2313. Did the original procvar support do such a check?
  2314. I can't find any.}
  2315. { answer : is_equal works for procvardefs !! }
  2316. { but both must be procvardefs, so we cheet little }
  2317. if assigned(aprocdef) then
  2318. begin
  2319. proctype:=aprocdef^.deftype;
  2320. aprocdef^.deftype:=procvardef;
  2321. if not is_equal(aprocdef,p^.resulttype) then
  2322. begin
  2323. aprocdef^.deftype:=proctype;
  2324. Message(sym_e_type_mismatch);
  2325. end;
  2326. aprocdef^.deftype:=proctype;
  2327. firstconvert[p^.convtyp](p);
  2328. end
  2329. else
  2330. Message(sym_e_type_mismatch);
  2331. exit;
  2332. end
  2333. else
  2334. begin
  2335. if p^.explizit then
  2336. begin
  2337. { boolean to byte are special because the
  2338. location can be different }
  2339. if (p^.resulttype^.deftype=orddef) and
  2340. (porddef(p^.resulttype)^.typ=u8bit) and
  2341. (p^.left^.resulttype^.deftype=orddef) and
  2342. (porddef(p^.left^.resulttype)^.typ=bool8bit) then
  2343. begin
  2344. p^.convtyp:=tc_bool_2_int;
  2345. firstconvert[p^.convtyp](p);
  2346. exit;
  2347. end;
  2348. { normal tc_equal-Konvertierung durchf�hren }
  2349. p^.convtyp:=tc_equal;
  2350. { wenn Aufz„hltyp nach Ordinal konvertiert werden soll }
  2351. { dann Aufz„hltyp=s32bit }
  2352. if (p^.left^.resulttype^.deftype=enumdef) and
  2353. is_ordinal(p^.resulttype) then
  2354. begin
  2355. if p^.left^.treetype=ordconstn then
  2356. begin
  2357. hp:=genordinalconstnode(p^.left^.value,p^.resulttype);
  2358. disposetree(p);
  2359. firstpass(hp);
  2360. p:=hp;
  2361. exit;
  2362. end
  2363. else
  2364. begin
  2365. if not isconvertable(s32bitdef,p^.resulttype,p^.convtyp,
  2366. ordconstn { nur Dummy},false ) then
  2367. Message(cg_e_illegal_type_conversion);
  2368. end;
  2369. end
  2370. { ordinal to enumeration }
  2371. else
  2372. if (p^.resulttype^.deftype=enumdef) and
  2373. is_ordinal(p^.left^.resulttype) then
  2374. begin
  2375. if p^.left^.treetype=ordconstn then
  2376. begin
  2377. hp:=genordinalconstnode(p^.left^.value,p^.resulttype);
  2378. disposetree(p);
  2379. firstpass(hp);
  2380. p:=hp;
  2381. exit;
  2382. end
  2383. else
  2384. begin
  2385. if not isconvertable(p^.left^.resulttype,s32bitdef,p^.convtyp,
  2386. ordconstn { nur Dummy},false ) then
  2387. Message(cg_e_illegal_type_conversion);
  2388. end;
  2389. end
  2390. {Are we typecasting an ordconst to a char?}
  2391. else
  2392. if is_equal(p^.resulttype,cchardef) and
  2393. is_ordinal(p^.left^.resulttype) then
  2394. begin
  2395. if p^.left^.treetype=ordconstn then
  2396. begin
  2397. hp:=genordinalconstnode(p^.left^.value,p^.resulttype);
  2398. firstpass(hp);
  2399. disposetree(p);
  2400. p:=hp;
  2401. exit;
  2402. end
  2403. else
  2404. begin
  2405. { this is wrong because it converts to a 4 byte long var !!
  2406. if not isconvertable(p^.left^.resulttype,s32bitdef,p^.convtyp,ordconstn nur Dummy ) then }
  2407. if not isconvertable(p^.left^.resulttype,u8bitdef,
  2408. p^.convtyp,ordconstn { nur Dummy},false ) then
  2409. Message(cg_e_illegal_type_conversion);
  2410. end;
  2411. end
  2412. { only if the same size or formal def }
  2413. { why do we allow typecasting of voiddef ?? (PM) }
  2414. else
  2415. if not(
  2416. (p^.left^.resulttype^.deftype=formaldef) or
  2417. (p^.left^.resulttype^.size=p^.resulttype^.size) or
  2418. (is_equal(p^.left^.resulttype,voiddef) and
  2419. (p^.left^.treetype=derefn))
  2420. ) then
  2421. Message(cg_e_illegal_type_conversion);
  2422. { the conversion into a strutured type is only }
  2423. { possible, if the source is no register }
  2424. if (p^.resulttype^.deftype in [recorddef,stringdef,arraydef,objectdef]) and
  2425. (p^.left^.location.loc in [LOC_REGISTER,LOC_CREGISTER]) and
  2426. {it also works if the assignment is overloaded }
  2427. not is_assignment_overloaded(p^.left^.resulttype,p^.resulttype) then
  2428. Message(cg_e_illegal_type_conversion);
  2429. end
  2430. else
  2431. Message(sym_e_type_mismatch);
  2432. end
  2433. end
  2434. else
  2435. begin
  2436. { just a test: p^.explizit:=false; }
  2437. { ordinale contants are direct converted }
  2438. if (p^.left^.treetype=ordconstn) and is_ordinal(p^.resulttype) then
  2439. begin
  2440. { perform range checking }
  2441. if not(p^.explizit and (cs_tp_compatible in aktswitches)) then
  2442. testrange(p^.resulttype,p^.left^.value);
  2443. hp:=genordinalconstnode(p^.left^.value,p^.resulttype);
  2444. disposetree(p);
  2445. firstpass(hp);
  2446. p:=hp;
  2447. exit;
  2448. end;
  2449. if p^.convtyp<>tc_equal then
  2450. firstconvert[p^.convtyp](p);
  2451. end;
  2452. end;
  2453. { *************** subroutine handling **************** }
  2454. { protected field handling
  2455. protected field can not appear in
  2456. var parameters of function !!
  2457. this can only be done after we have determined the
  2458. overloaded function
  2459. this is the reason why it is not in the parser
  2460. PM }
  2461. procedure test_protected_sym(sym : psym);
  2462. begin
  2463. if ((sym^.properties and sp_protected)<>0) and
  2464. ((sym^.owner^.symtabletype=unitsymtable) or
  2465. ((sym^.owner^.symtabletype=objectsymtable) and
  2466. (pobjectdef(sym^.owner^.defowner)^.owner^.symtabletype=unitsymtable))) then
  2467. Message(parser_e_cant_access_protected_member);
  2468. end;
  2469. procedure test_protected(p : ptree);
  2470. begin
  2471. if p^.treetype=loadn then
  2472. begin
  2473. test_protected_sym(p^.symtableentry);
  2474. end
  2475. else if p^.treetype=typeconvn then
  2476. begin
  2477. test_protected(p^.left);
  2478. end
  2479. else if p^.treetype=derefn then
  2480. begin
  2481. test_protected(p^.left);
  2482. end
  2483. else if p^.treetype=subscriptn then
  2484. begin
  2485. { test_protected(p^.left);
  2486. Is a field of a protected var
  2487. also protected ??? PM }
  2488. test_protected_sym(p^.vs);
  2489. end;
  2490. end;
  2491. procedure firstcallparan(var p : ptree;defcoll : pdefcoll);
  2492. var store_valid : boolean;
  2493. convtyp : tconverttype;
  2494. begin
  2495. inc(parsing_para_level);
  2496. if assigned(p^.right) then
  2497. begin
  2498. if defcoll=nil then
  2499. firstcallparan(p^.right,nil)
  2500. else
  2501. firstcallparan(p^.right,defcoll^.next);
  2502. p^.registers32:=p^.right^.registers32;
  2503. p^.registersfpu:=p^.right^.registersfpu;
  2504. {$ifdef SUPPORT_MMX}
  2505. p^.registersmmx:=p^.right^.registersmmx;
  2506. {$endif}
  2507. end;
  2508. if defcoll=nil then
  2509. begin
  2510. { this breaks typeconversions in write !!! (PM) }
  2511. {if not(assigned(p^.resulttype)) then }
  2512. if not(assigned(p^.resulttype)) or
  2513. (p^.left^.treetype=typeconvn) then
  2514. firstpass(p^.left);
  2515. {else
  2516. exit; this broke the
  2517. value of registers32 !! }
  2518. if codegenerror then
  2519. begin
  2520. dec(parsing_para_level);
  2521. exit;
  2522. end;
  2523. p^.resulttype:=p^.left^.resulttype;
  2524. end
  2525. { if we know the routine which is called, then the type }
  2526. { conversions are inserted }
  2527. else
  2528. begin
  2529. if count_ref then
  2530. begin
  2531. store_valid:=must_be_valid;
  2532. if (defcoll^.paratyp=vs_var) then
  2533. test_protected(p^.left);
  2534. if (defcoll^.paratyp<>vs_var) then
  2535. must_be_valid:=true
  2536. else
  2537. must_be_valid:=false;
  2538. { here we must add something for the implicit type }
  2539. { conversion from array of char to pchar }
  2540. if isconvertable(p^.left^.resulttype,defcoll^.data,convtyp,
  2541. p^.left^.treetype,false) then
  2542. if convtyp=tc_array_to_pointer then
  2543. must_be_valid:=false;
  2544. firstpass(p^.left);
  2545. must_be_valid:=store_valid;
  2546. end;
  2547. if not(is_shortstring(p^.left^.resulttype) and
  2548. is_shortstring(defcoll^.data)) and
  2549. (defcoll^.data^.deftype<>formaldef) then
  2550. begin
  2551. if (defcoll^.paratyp=vs_var) and
  2552. { allows conversion from word to integer and
  2553. byte to shortint }
  2554. (not(
  2555. (p^.left^.resulttype^.deftype=orddef) and
  2556. (defcoll^.data^.deftype=orddef) and
  2557. (p^.left^.resulttype^.size=defcoll^.data^.size)
  2558. ) and
  2559. { an implicit pointer conversion is allowed }
  2560. not(
  2561. (p^.left^.resulttype^.deftype=pointerdef) and
  2562. (defcoll^.data^.deftype=pointerdef)
  2563. ) and
  2564. { child classes can be also passed }
  2565. not(
  2566. (p^.left^.resulttype^.deftype=objectdef) and
  2567. (defcoll^.data^.deftype=objectdef) and
  2568. pobjectdef(p^.left^.resulttype)^.isrelated(pobjectdef(defcoll^.data))
  2569. ) and
  2570. { an implicit file conversion is also allowed }
  2571. { from a typed file to an untyped one }
  2572. not(
  2573. (p^.left^.resulttype^.deftype=filedef) and
  2574. (defcoll^.data^.deftype=filedef) and
  2575. (pfiledef(defcoll^.data)^.filetype = ft_untyped) and
  2576. (pfiledef(p^.left^.resulttype)^.filetype = ft_typed)
  2577. ) and
  2578. not(is_equal(p^.left^.resulttype,defcoll^.data))) then
  2579. Message(parser_e_call_by_ref_without_typeconv);
  2580. { don't generate an type conversion for open arrays }
  2581. { else we loss the ranges }
  2582. if not(is_open_array(defcoll^.data)) then
  2583. begin
  2584. p^.left:=gentypeconvnode(p^.left,defcoll^.data);
  2585. firstpass(p^.left);
  2586. end;
  2587. if codegenerror then
  2588. begin
  2589. dec(parsing_para_level);
  2590. exit;
  2591. end;
  2592. end;
  2593. { check var strings }
  2594. if (cs_strict_var_strings in aktswitches) and
  2595. is_shortstring(p^.left^.resulttype) and
  2596. is_shortstring(defcoll^.data) and
  2597. (defcoll^.paratyp=vs_var) and
  2598. not(is_equal(p^.left^.resulttype,defcoll^.data)) then
  2599. Message(parser_e_strict_var_string_violation);
  2600. { Variablen, die call by reference �bergeben werden, }
  2601. { k”nnen nicht in ein Register kopiert werden }
  2602. { is this usefull here ? }
  2603. { this was missing in formal parameter list }
  2604. if defcoll^.paratyp=vs_var then
  2605. make_not_regable(p^.left);
  2606. p^.resulttype:=defcoll^.data;
  2607. end;
  2608. if p^.left^.registers32>p^.registers32 then
  2609. p^.registers32:=p^.left^.registers32;
  2610. if p^.left^.registersfpu>p^.registersfpu then
  2611. p^.registersfpu:=p^.left^.registersfpu;
  2612. {$ifdef SUPPORT_MMX}
  2613. if p^.left^.registersmmx>p^.registersmmx then
  2614. p^.registersmmx:=p^.left^.registersmmx;
  2615. {$endif SUPPORT_MMX}
  2616. dec(parsing_para_level);
  2617. end;
  2618. procedure firstcalln(var p : ptree);
  2619. type
  2620. pprocdefcoll = ^tprocdefcoll;
  2621. tprocdefcoll = record
  2622. data : pprocdef;
  2623. nextpara : pdefcoll;
  2624. firstpara : pdefcoll;
  2625. next : pprocdefcoll;
  2626. end;
  2627. var
  2628. hp,procs,hp2 : pprocdefcoll;
  2629. pd : pprocdef;
  2630. actprocsym : pprocsym;
  2631. def_from,def_to,conv_to : pdef;
  2632. pt,inlinecode : ptree;
  2633. exactmatch,inlined : boolean;
  2634. paralength,l : longint;
  2635. pdc : pdefcoll;
  2636. {$ifdef UseBrowser}
  2637. curtokenpos : tfileposinfo;
  2638. {$endif UseBrowser}
  2639. { only Dummy }
  2640. hcvt : tconverttype;
  2641. regi : tregister;
  2642. store_valid, old_count_ref : boolean;
  2643. { types.is_equal can't handle a formaldef ! }
  2644. function is_equal(def1,def2 : pdef) : boolean;
  2645. begin
  2646. { all types can be passed to a formaldef }
  2647. is_equal:=(def1^.deftype=formaldef) or
  2648. (assigned(def2) and types.is_equal(def1,def2))
  2649. { to support ansi/long/wide strings in a proper way }
  2650. { string and string[10] are assumed as equal }
  2651. { when searching the correct overloaded procedure }
  2652. or
  2653. (assigned(def1) and assigned(def2) and
  2654. (def1^.deftype=stringdef) and (def2^.deftype=stringdef) and
  2655. (pstringdef(def1)^.string_typ=pstringdef(def2)^.string_typ)
  2656. )
  2657. ;
  2658. end;
  2659. function is_in_limit(def_from,def_to : pdef) : boolean;
  2660. begin
  2661. is_in_limit:=(def_from^.deftype = orddef) and
  2662. (def_to^.deftype = orddef) and
  2663. (porddef(def_from)^.low>porddef(def_to)^.low) and
  2664. (porddef(def_from)^.high<porddef(def_to)^.high);
  2665. end;
  2666. begin
  2667. { release registers! }
  2668. { if procdefinition<>nil then we called firstpass already }
  2669. { it seems to be bad because of the registers }
  2670. { at least we can avoid the overloaded search !! }
  2671. procs:=nil;
  2672. { made this global for disposing !! }
  2673. store_valid:=must_be_valid;
  2674. must_be_valid:=false;
  2675. inlined:=false;
  2676. if assigned(p^.procdefinition) and
  2677. ((p^.procdefinition^.options and poinline)<>0) then
  2678. begin
  2679. inlinecode:=p^.right;
  2680. if assigned(inlinecode) then
  2681. begin
  2682. inlined:=true;
  2683. p^.procdefinition^.options:=p^.procdefinition^.options and (not poinline);
  2684. end;
  2685. p^.right:=nil;
  2686. end;
  2687. { procedure variable ? }
  2688. if assigned(p^.right) then
  2689. begin
  2690. { procedure does a call }
  2691. procinfo.flags:=procinfo.flags or pi_do_call;
  2692. { calc the correture value for the register }
  2693. {$ifdef i386}
  2694. for regi:=R_EAX to R_EDI do
  2695. inc(reg_pushes[regi],t_times*2);
  2696. {$endif}
  2697. {$ifdef m68k}
  2698. for regi:=R_D0 to R_A6 do
  2699. inc(reg_pushes[regi],t_times*2);
  2700. {$endif}
  2701. { calculate the type of the parameters }
  2702. if assigned(p^.left) then
  2703. begin
  2704. old_count_ref:=count_ref;
  2705. count_ref:=false;
  2706. firstcallparan(p^.left,nil);
  2707. count_ref:=old_count_ref;
  2708. if codegenerror then
  2709. exit;
  2710. end;
  2711. firstpass(p^.right);
  2712. { check the parameters }
  2713. pdc:=pprocvardef(p^.right^.resulttype)^.para1;
  2714. pt:=p^.left;
  2715. while assigned(pdc) and assigned(pt) do
  2716. begin
  2717. pt:=pt^.right;
  2718. pdc:=pdc^.next;
  2719. end;
  2720. if assigned(pt) or assigned(pdc) then
  2721. Message(parser_e_illegal_parameter_list);
  2722. { insert type conversions }
  2723. if assigned(p^.left) then
  2724. begin
  2725. old_count_ref:=count_ref;
  2726. count_ref:=true;
  2727. firstcallparan(p^.left,pprocvardef(p^.right^.resulttype)^.para1);
  2728. count_ref:=old_count_ref;
  2729. if codegenerror then
  2730. exit;
  2731. end;
  2732. p^.resulttype:=pprocvardef(p^.right^.resulttype)^.retdef;
  2733. { this was missing, leads to a bug below if
  2734. the procvar is a function }
  2735. p^.procdefinition:=pprocdef(p^.right^.resulttype);
  2736. end
  2737. else
  2738. { not a procedure variable }
  2739. begin
  2740. { determine the type of the parameters }
  2741. if assigned(p^.left) then
  2742. begin
  2743. old_count_ref:=count_ref;
  2744. count_ref:=false;
  2745. store_valid:=must_be_valid;
  2746. must_be_valid:=false;
  2747. firstcallparan(p^.left,nil);
  2748. count_ref:=old_count_ref;
  2749. must_be_valid:=store_valid;
  2750. if codegenerror then
  2751. exit;
  2752. end;
  2753. { do we know the procedure to call ? }
  2754. if not(assigned(p^.procdefinition)) then
  2755. begin
  2756. actprocsym:=p^.symtableprocentry;
  2757. { determine length of parameter list }
  2758. pt:=p^.left;
  2759. paralength:=0;
  2760. while assigned(pt) do
  2761. begin
  2762. inc(paralength);
  2763. pt:=pt^.right;
  2764. end;
  2765. { alle in Frage kommenden Prozeduren in eine }
  2766. { verkettete Liste einf�gen }
  2767. pd:=actprocsym^.definition;
  2768. while assigned(pd) do
  2769. begin
  2770. { we should also check that the overloaded function
  2771. has been declared in a unit that is in the uses !! }
  2772. { pd^.owner should be in the symtablestack !! }
  2773. { Laenge der deklarierten Parameterliste feststellen: }
  2774. { not necessary why nextprocsym field }
  2775. {st:=symtablestack;
  2776. if (pd^.owner^.symtabletype<>objectsymtable) then
  2777. while assigned(st) do
  2778. begin
  2779. if (st=pd^.owner) then break;
  2780. st:=st^.next;
  2781. end;
  2782. if assigned(st) then }
  2783. begin
  2784. pdc:=pd^.para1;
  2785. l:=0;
  2786. while assigned(pdc) do
  2787. begin
  2788. inc(l);
  2789. pdc:=pdc^.next;
  2790. end;
  2791. { nur wenn die Parameterl„nge paát, dann Einf�gen }
  2792. if l=paralength then
  2793. begin
  2794. new(hp);
  2795. hp^.data:=pd;
  2796. hp^.next:=procs;
  2797. hp^.nextpara:=pd^.para1;
  2798. hp^.firstpara:=pd^.para1;
  2799. procs:=hp;
  2800. end;
  2801. end;
  2802. pd:=pd^.nextoverloaded;
  2803. {$ifdef CHAINPROCSYMS}
  2804. if (pd=nil) and not (p^.unit_specific) then
  2805. begin
  2806. actprocsym:=actprocsym^.nextprocsym;
  2807. if assigned(actprocsym) then
  2808. pd:=actprocsym^.definition;
  2809. end;
  2810. {$endif CHAINPROCSYMS}
  2811. end;
  2812. { nun alle Parameter nacheinander vergleichen }
  2813. pt:=p^.left;
  2814. while assigned(pt) do
  2815. begin
  2816. { matches a parameter of one procedure exact ? }
  2817. exactmatch:=false;
  2818. hp:=procs;
  2819. while assigned(hp) do
  2820. begin
  2821. if is_equal(hp^.nextpara^.data,pt^.resulttype) then
  2822. begin
  2823. if hp^.nextpara^.data=pt^.resulttype then
  2824. begin
  2825. pt^.exact_match_found:=true;
  2826. hp^.nextpara^.argconvtyp:=act_exact;
  2827. end
  2828. else
  2829. hp^.nextpara^.argconvtyp:=act_equal;
  2830. exactmatch:=true;
  2831. end
  2832. else
  2833. hp^.nextpara^.argconvtyp:=act_convertable;
  2834. hp:=hp^.next;
  2835. end;
  2836. { .... if yes, del all the other procedures }
  2837. if exactmatch then
  2838. begin
  2839. { the first .... }
  2840. while (assigned(procs)) and not(is_equal(procs^.nextpara^.data,pt^.resulttype)) do
  2841. begin
  2842. hp:=procs^.next;
  2843. dispose(procs);
  2844. procs:=hp;
  2845. end;
  2846. { and the others }
  2847. hp:=procs;
  2848. while (assigned(hp)) and assigned(hp^.next) do
  2849. begin
  2850. if not(is_equal(hp^.next^.nextpara^.data,pt^.resulttype)) then
  2851. begin
  2852. hp2:=hp^.next^.next;
  2853. dispose(hp^.next);
  2854. hp^.next:=hp2;
  2855. end
  2856. else
  2857. hp:=hp^.next;
  2858. end;
  2859. end
  2860. { sollte nirgendwo ein Parameter exakt passen, }
  2861. { so alle Prozeduren entfernen, bei denen }
  2862. { der Parameter auch nach einer impliziten }
  2863. { Typkonvertierung nicht passt }
  2864. else
  2865. begin
  2866. { erst am Anfang }
  2867. while (assigned(procs)) and
  2868. not(isconvertable(pt^.resulttype,procs^.nextpara^.data,
  2869. hcvt,pt^.left^.treetype,false)) do
  2870. begin
  2871. hp:=procs^.next;
  2872. dispose(procs);
  2873. procs:=hp;
  2874. end;
  2875. { und jetzt aus der Mitte }
  2876. hp:=procs;
  2877. while (assigned(hp)) and assigned(hp^.next) do
  2878. begin
  2879. if not(isconvertable(pt^.resulttype,hp^.next^.nextpara^.data,
  2880. hcvt,pt^.left^.treetype,false)) then
  2881. begin
  2882. hp2:=hp^.next^.next;
  2883. dispose(hp^.next);
  2884. hp^.next:=hp2;
  2885. end
  2886. else
  2887. hp:=hp^.next;
  2888. end;
  2889. end;
  2890. { nun bei denn Prozeduren den nextpara-Zeiger auf den }
  2891. { naechsten Parameter setzen }
  2892. hp:=procs;
  2893. while assigned(hp) do
  2894. begin
  2895. hp^.nextpara:=hp^.nextpara^.next;
  2896. hp:=hp^.next;
  2897. end;
  2898. pt:=pt^.right;
  2899. end;
  2900. if procs=nil then
  2901. if (parsing_para_level=0) or (p^.left<>nil) then
  2902. begin
  2903. Message(parser_e_illegal_parameter_list);
  2904. exit;
  2905. end
  2906. else
  2907. begin
  2908. { try to convert to procvar }
  2909. p^.treetype:=loadn;
  2910. p^.resulttype:=pprocsym(p^.symtableprocentry)^.definition;
  2911. p^.symtableentry:=p^.symtableprocentry;
  2912. p^.is_first:=false;
  2913. p^.disposetyp:=dt_nothing;
  2914. firstpass(p);
  2915. exit;
  2916. end;
  2917. { if there are several choices left then for orddef }
  2918. { if a type is totally included in the other }
  2919. { we don't fear an overflow , }
  2920. { so we can do as if it is an exact match }
  2921. { this will convert integer to longint }
  2922. { rather than to words }
  2923. { conversion of byte to integer or longint }
  2924. {would still not be solved }
  2925. if assigned(procs^.next) then
  2926. begin
  2927. hp:=procs;
  2928. while assigned(hp) do
  2929. begin
  2930. hp^.nextpara:=hp^.firstpara;
  2931. hp:=hp^.next;
  2932. end;
  2933. pt:=p^.left;
  2934. while assigned(pt) do
  2935. begin
  2936. { matches a parameter of one procedure exact ? }
  2937. exactmatch:=false;
  2938. def_from:=pt^.resulttype;
  2939. hp:=procs;
  2940. while assigned(hp) do
  2941. begin
  2942. if not is_equal(hp^.nextpara^.data,pt^.resulttype) then
  2943. begin
  2944. def_to:=hp^.nextpara^.data;
  2945. if ((def_from^.deftype=orddef) and (def_to^.deftype=orddef)) and
  2946. (is_in_limit(def_from,def_to) or
  2947. ((hp^.nextpara^.paratyp=vs_var) and
  2948. (def_from^.size=def_to^.size))) then
  2949. begin
  2950. exactmatch:=true;
  2951. conv_to:=def_to;
  2952. end;
  2953. end;
  2954. hp:=hp^.next;
  2955. end;
  2956. { .... if yes, del all the other procedures }
  2957. if exactmatch then
  2958. begin
  2959. { the first .... }
  2960. while (assigned(procs)) and not(is_in_limit(def_from,procs^.nextpara^.data)) do
  2961. begin
  2962. hp:=procs^.next;
  2963. dispose(procs);
  2964. procs:=hp;
  2965. end;
  2966. { and the others }
  2967. hp:=procs;
  2968. while (assigned(hp)) and assigned(hp^.next) do
  2969. begin
  2970. if not(is_in_limit(def_from,hp^.next^.nextpara^.data)) then
  2971. begin
  2972. hp2:=hp^.next^.next;
  2973. dispose(hp^.next);
  2974. hp^.next:=hp2;
  2975. end
  2976. else
  2977. begin
  2978. def_to:=hp^.next^.nextpara^.data;
  2979. if (conv_to^.size>def_to^.size) or
  2980. ((porddef(conv_to)^.low<porddef(def_to)^.low) and
  2981. (porddef(conv_to)^.high>porddef(def_to)^.high)) then
  2982. begin
  2983. hp2:=procs;
  2984. procs:=hp;
  2985. conv_to:=def_to;
  2986. dispose(hp2);
  2987. end
  2988. else
  2989. hp:=hp^.next;
  2990. end;
  2991. end;
  2992. end;
  2993. { nun bei denn Prozeduren den nextpara-Zeiger auf den }
  2994. { naechsten Parameter setzen }
  2995. hp:=procs;
  2996. while assigned(hp) do
  2997. begin
  2998. hp^.nextpara:=hp^.nextpara^.next;
  2999. hp:=hp^.next;
  3000. end;
  3001. pt:=pt^.right;
  3002. end;
  3003. end;
  3004. { let's try to eliminate equal is exact is there }
  3005. {if assigned(procs^.next) then
  3006. begin
  3007. pt:=p^.left;
  3008. while assigned(pt) do
  3009. begin
  3010. if pt^.exact_match_found then
  3011. begin
  3012. hp:=procs;
  3013. while (assigned(procs)) and (procs^.nextpara^.data<>pt^.resulttype) do
  3014. begin
  3015. hp:=procs^.next;
  3016. dispose(procs);
  3017. procs:=hp;
  3018. end;
  3019. end;
  3020. pt:=pt^.right;
  3021. end;
  3022. end; }
  3023. {$ifndef CHAINPROCSYMS}
  3024. if assigned(procs^.next) then
  3025. Message(cg_e_cant_choose_overload_function);
  3026. {$else CHAINPROCSYMS}
  3027. if assigned(procs^.next) then
  3028. { if the last retained is the only one }
  3029. { from a unit it is OK PM }
  3030. { the last is the one coming from the first symtable }
  3031. { as the diff defcoll are inserted in front }
  3032. begin
  3033. hp2:=procs;
  3034. while assigned(hp2^.next) and assigned(hp2^.next^.next) do
  3035. hp2:=hp2^.next;
  3036. if (hp2^.data^.owner<>hp2^.next^.data^.owner) then
  3037. begin
  3038. hp:=procs^.next;
  3039. {hp2 is the correct one }
  3040. hp2:=hp2^.next;
  3041. while hp<>hp2 do
  3042. begin
  3043. dispose(procs);
  3044. procs:=hp;
  3045. hp:=procs^.next;
  3046. end;
  3047. procs:=hp2;
  3048. end
  3049. else
  3050. Message(cg_e_cant_choose_overload_function);
  3051. error(too_much_matches);
  3052. end;
  3053. {$endif CHAINPROCSYMS}
  3054. {$ifdef UseBrowser}
  3055. if make_ref then
  3056. begin
  3057. get_cur_file_pos(curtokenpos);
  3058. procs^.data^.lastref:=new(pref,init(procs^.data^.lastref,@curtokenpos));
  3059. end;
  3060. {$endif UseBrowser}
  3061. p^.procdefinition:=procs^.data;
  3062. p^.resulttype:=procs^.data^.retdef;
  3063. { big error for with statements
  3064. p^.symtableproc:=p^.procdefinition^.owner; }
  3065. p^.location.loc:=LOC_MEM;
  3066. {$ifdef CHAINPROCSYMS}
  3067. { object with method read;
  3068. call to read(x) will be a usual procedure call }
  3069. if assigned(p^.methodpointer) and
  3070. (p^.procdefinition^._class=nil) then
  3071. begin
  3072. { not ok for extended }
  3073. case p^.methodpointer^.treetype of
  3074. typen,hnewn : fatalerror(no_para_match);
  3075. end;
  3076. disposetree(p^.methodpointer);
  3077. p^.methodpointer:=nil;
  3078. end;
  3079. {$endif CHAINPROCSYMS}
  3080. end;{ end of procedure to call determination }
  3081. { handle predefined procedures }
  3082. if (p^.procdefinition^.options and pointernproc)<>0 then
  3083. begin
  3084. { settextbuf needs two args }
  3085. if assigned(p^.left^.right) then
  3086. pt:=geninlinenode(pprocdef(p^.procdefinition)^.extnumber,p^.left)
  3087. else
  3088. begin
  3089. pt:=geninlinenode(pprocdef(p^.procdefinition)^.extnumber,p^.left^.left);
  3090. putnode(p^.left);
  3091. end;
  3092. putnode(p);
  3093. firstpass(pt);
  3094. { was placed after the exit }
  3095. { caused GPF }
  3096. { error caused and corrected by (PM) }
  3097. p:=pt;
  3098. must_be_valid:=store_valid;
  3099. if codegenerror then
  3100. exit;
  3101. dispose(procs);
  3102. exit;
  3103. end
  3104. else
  3105. { no intern procedure => we do a call }
  3106. { calc the correture value for the register }
  3107. { handle predefined procedures }
  3108. if (p^.procdefinition^.options and poinline)<>0 then
  3109. begin
  3110. if assigned(p^.methodpointer) then
  3111. Message(cg_e_unable_inline_object_methods);
  3112. if assigned(p^.right) and (p^.right^.treetype<>procinlinen) then
  3113. Message(cg_e_unable_inline_procvar);
  3114. { p^.treetype:=procinlinen; }
  3115. if not assigned(p^.right) then
  3116. begin
  3117. if assigned(p^.procdefinition^.code) then
  3118. inlinecode:=genprocinlinenode(p,ptree(p^.procdefinition^.code))
  3119. else
  3120. Message(cg_e_no_code_for_inline_stored);
  3121. if assigned(inlinecode) then
  3122. begin
  3123. { consider it has not inlined if called
  3124. again inside the args }
  3125. p^.procdefinition^.options:=p^.procdefinition^.options and (not poinline);
  3126. firstpass(inlinecode);
  3127. inlined:=true;
  3128. end;
  3129. end;
  3130. end
  3131. else
  3132. procinfo.flags:=procinfo.flags or pi_do_call;
  3133. { work trough all parameters to insert the type conversions }
  3134. { !!! done now after internproc !! (PM) }
  3135. if assigned(p^.left) then
  3136. begin
  3137. old_count_ref:=count_ref;
  3138. count_ref:=true;
  3139. firstcallparan(p^.left,p^.procdefinition^.para1);
  3140. count_ref:=old_count_ref;
  3141. end;
  3142. {$ifdef i386}
  3143. for regi:=R_EAX to R_EDI do
  3144. begin
  3145. if (p^.procdefinition^.usedregisters and ($80 shr word(regi)))<>0 then
  3146. inc(reg_pushes[regi],t_times*2);
  3147. end;
  3148. {$endif}
  3149. {$ifdef m68k}
  3150. for regi:=R_D0 to R_A6 do
  3151. begin
  3152. if (p^.procdefinition^.usedregisters and ($800 shr word(regi)))<>0 then
  3153. inc(reg_pushes[regi],t_times*2);
  3154. end;
  3155. {$endif}
  3156. end;
  3157. { ensure that the result type is set }
  3158. p^.resulttype:=p^.procdefinition^.retdef;
  3159. { get a register for the return value }
  3160. if (p^.resulttype<>pdef(voiddef)) then
  3161. begin
  3162. if (p^.procdefinition^.options and poconstructor)<>0 then
  3163. begin
  3164. { extra handling of classes }
  3165. { p^.methodpointer should be assigned! }
  3166. if assigned(p^.methodpointer) and assigned(p^.methodpointer^.resulttype) and
  3167. (p^.methodpointer^.resulttype^.deftype=classrefdef) then
  3168. begin
  3169. p^.location.loc:=LOC_REGISTER;
  3170. p^.registers32:=1;
  3171. { the result type depends on the classref }
  3172. p^.resulttype:=pclassrefdef(p^.methodpointer^.resulttype)^.definition;
  3173. end
  3174. { a object constructor returns the result with the flags }
  3175. else
  3176. p^.location.loc:=LOC_FLAGS;
  3177. end
  3178. else
  3179. begin
  3180. {$ifdef SUPPORT_MMX}
  3181. if (cs_mmx in aktswitches) and
  3182. is_mmx_able_array(p^.resulttype) then
  3183. begin
  3184. p^.location.loc:=LOC_MMXREGISTER;
  3185. p^.registersmmx:=1;
  3186. end
  3187. else
  3188. {$endif SUPPORT_MMX}
  3189. if ret_in_acc(p^.resulttype) then
  3190. begin
  3191. p^.location.loc:=LOC_REGISTER;
  3192. p^.registers32:=1;
  3193. end
  3194. else if (p^.resulttype^.deftype=floatdef) then
  3195. begin
  3196. p^.location.loc:=LOC_FPU;
  3197. p^.registersfpu:=1;
  3198. end
  3199. end;
  3200. end;
  3201. {$ifdef StoreFPULevel}
  3202. { a fpu can be used in any procedure !! }
  3203. p^.registersfpu:=p^.procdefinition^.fpu_used;
  3204. {$endif StoreFPULevel}
  3205. { if this is a call to a method calc the registers }
  3206. if (p^.methodpointer<>nil) then
  3207. begin
  3208. case p^.methodpointer^.treetype of
  3209. { but only, if this is not a supporting node }
  3210. typen,hnewn : ;
  3211. else
  3212. begin
  3213. { R.Assign is not a constructor !!! }
  3214. { but for R^.Assign, R must be valid !! }
  3215. if ((p^.procdefinition^.options and poconstructor) <> 0) or
  3216. ((p^.methodpointer^.treetype=loadn) and
  3217. ((pobjectdef(p^.methodpointer^.resulttype)^.options and oo_hasvirtual) = 0)) then
  3218. must_be_valid:=false
  3219. else
  3220. must_be_valid:=true;
  3221. firstpass(p^.methodpointer);
  3222. p^.registersfpu:=max(p^.methodpointer^.registersfpu,p^.registersfpu);
  3223. p^.registers32:=max(p^.methodpointer^.registers32,p^.registers32);
  3224. {$ifdef SUPPORT_MMX}
  3225. p^.registersmmx:=max(p^.methodpointer^.registersmmx,p^.registersmmx);
  3226. {$endif SUPPORT_MMX}
  3227. end;
  3228. end;
  3229. end;
  3230. if inlined then
  3231. begin
  3232. p^.right:=inlinecode;
  3233. p^.procdefinition^.options:=p^.procdefinition^.options or poinline;
  3234. end;
  3235. { determine the registers of the procedure variable }
  3236. { is this OK for inlined procs also ?? (PM) }
  3237. if assigned(p^.right) then
  3238. begin
  3239. p^.registersfpu:=max(p^.right^.registersfpu,p^.registersfpu);
  3240. p^.registers32:=max(p^.right^.registers32,p^.registers32);
  3241. {$ifdef SUPPORT_MMX}
  3242. p^.registersmmx:=max(p^.right^.registersmmx,p^.registersmmx);
  3243. {$endif SUPPORT_MMX}
  3244. end;
  3245. { determine the registers of the procedure }
  3246. if assigned(p^.left) then
  3247. begin
  3248. p^.registersfpu:=max(p^.left^.registersfpu,p^.registersfpu);
  3249. p^.registers32:=max(p^.left^.registers32,p^.registers32);
  3250. {$ifdef SUPPORT_MMX}
  3251. p^.registersmmx:=max(p^.left^.registersmmx,p^.registersmmx);
  3252. {$endif SUPPORT_MMX}
  3253. end;
  3254. if assigned(procs) then
  3255. dispose(procs);
  3256. must_be_valid:=store_valid;
  3257. end;
  3258. procedure firstfuncret(var p : ptree);
  3259. begin
  3260. {$ifdef TEST_FUNCRET}
  3261. p^.resulttype:=p^.retdef;
  3262. p^.location.loc:=LOC_REFERENCE;
  3263. if ret_in_param(p^.retdef) or
  3264. (@procinfo<>pprocinfo(p^.funcretprocinfo)) then
  3265. p^.registers32:=1;
  3266. { no claim if setting higher return values }
  3267. if must_be_valid and
  3268. (@procinfo=pprocinfo(p^.funcretprocinfo)) and
  3269. not procinfo.funcret_is_valid then
  3270. note(uninitialized_function_return);
  3271. if count_ref then pprocinfo(p^.funcretprocinfo)^.funcret_is_valid:=true;
  3272. {$else TEST_FUNCRET}
  3273. p^.resulttype:=procinfo.retdef;
  3274. p^.location.loc:=LOC_REFERENCE;
  3275. if ret_in_param(procinfo.retdef) then
  3276. p^.registers32:=1;
  3277. if must_be_valid and
  3278. not(procinfo.funcret_is_valid) {and
  3279. ((procinfo.flags and pi_uses_asm)=0)} then
  3280. Message(sym_w_function_result_not_set);
  3281. if count_ref then procinfo.funcret_is_valid:=true;
  3282. {$endif TEST_FUNCRET}
  3283. end;
  3284. { intern inline suborutines }
  3285. procedure firstinline(var p : ptree);
  3286. var
  3287. hp,hpp : ptree;
  3288. store_count_ref,isreal,store_valid,file_is_typed : boolean;
  3289. procedure do_lowhigh(adef : pdef);
  3290. var
  3291. v : longint;
  3292. enum : penumsym;
  3293. begin
  3294. case Adef^.deftype of
  3295. orddef:
  3296. begin
  3297. if p^.inlinenumber=in_low_x then
  3298. v:=porddef(Adef)^.low
  3299. else
  3300. v:=porddef(Adef)^.high;
  3301. hp:=genordinalconstnode(v,adef);
  3302. firstpass(hp);
  3303. disposetree(p);
  3304. p:=hp;
  3305. end;
  3306. enumdef:
  3307. begin
  3308. enum:=Penumdef(Adef)^.first;
  3309. if p^.inlinenumber=in_high_x then
  3310. while enum^.next<>nil do
  3311. enum:=enum^.next;
  3312. hp:=genenumnode(enum);
  3313. disposetree(p);
  3314. p:=hp;
  3315. end
  3316. end;
  3317. end;
  3318. begin
  3319. store_valid:=must_be_valid;
  3320. store_count_ref:=count_ref;
  3321. count_ref:=false;
  3322. if not (p^.inlinenumber in [in_read_x,in_readln_x,in_sizeof_x,
  3323. in_typeof_x,in_ord_x,in_str_x_string,
  3324. in_reset_typedfile,in_rewrite_typedfile]) then
  3325. must_be_valid:=true
  3326. else
  3327. must_be_valid:=false;
  3328. { if we handle writeln; p^.left contains no valid address }
  3329. if assigned(p^.left) then
  3330. begin
  3331. if p^.left^.treetype=callparan then
  3332. firstcallparan(p^.left,nil)
  3333. else
  3334. firstpass(p^.left);
  3335. p^.registers32:=p^.left^.registers32;
  3336. p^.registersfpu:=p^.left^.registersfpu;
  3337. {$ifdef SUPPORT_MMX}
  3338. p^.registersmmx:=p^.left^.registersmmx;
  3339. {$endif SUPPORT_MMX}
  3340. set_location(p^.location,p^.left^.location);
  3341. end;
  3342. case p^.inlinenumber of
  3343. in_lo_word,in_hi_word:
  3344. begin
  3345. if p^.registers32<1 then
  3346. p^.registers32:=1;
  3347. p^.resulttype:=u8bitdef;
  3348. p^.location.loc:=LOC_REGISTER;
  3349. end;
  3350. in_lo_long,in_hi_long:
  3351. begin
  3352. if p^.registers32<1 then
  3353. p^.registers32:=1;
  3354. p^.resulttype:=u16bitdef;
  3355. p^.location.loc:=LOC_REGISTER;
  3356. end;
  3357. in_sizeof_x:
  3358. begin
  3359. if p^.registers32<1 then
  3360. p^.registers32:=1;
  3361. p^.resulttype:=s32bitdef;
  3362. p^.location.loc:=LOC_REGISTER;
  3363. end;
  3364. in_typeof_x:
  3365. begin
  3366. if p^.registers32<1 then
  3367. p^.registers32:=1;
  3368. p^.location.loc:=LOC_REGISTER;
  3369. p^.resulttype:=voidpointerdef;
  3370. end;
  3371. in_ord_x:
  3372. begin
  3373. if (p^.left^.treetype=ordconstn) then
  3374. begin
  3375. hp:=genordinalconstnode(p^.left^.value,s32bitdef);
  3376. disposetree(p);
  3377. p:=hp;
  3378. firstpass(p);
  3379. end
  3380. else
  3381. begin
  3382. if (p^.left^.resulttype^.deftype=orddef) then
  3383. if (porddef(p^.left^.resulttype)^.typ in [uchar,bool8bit]) then
  3384. begin
  3385. if porddef(p^.left^.resulttype)^.typ=bool8bit then
  3386. begin
  3387. hp:=gentypeconvnode(p^.left,u8bitdef);
  3388. putnode(p);
  3389. p:=hp;
  3390. p^.convtyp:=tc_bool_2_int;
  3391. p^.explizit:=true;
  3392. firstpass(p);
  3393. end
  3394. else
  3395. begin
  3396. hp:=gentypeconvnode(p^.left,u8bitdef);
  3397. putnode(p);
  3398. p:=hp;
  3399. p^.explizit:=true;
  3400. firstpass(p);
  3401. end;
  3402. end
  3403. { can this happen ? }
  3404. else if (porddef(p^.left^.resulttype)^.typ=uvoid) then
  3405. Message(sym_e_type_mismatch)
  3406. else
  3407. { all other orddef need no transformation }
  3408. begin
  3409. hp:=p^.left;
  3410. putnode(p);
  3411. p:=hp;
  3412. end
  3413. else if (p^.left^.resulttype^.deftype=enumdef) then
  3414. begin
  3415. hp:=gentypeconvnode(p^.left,s32bitdef);
  3416. putnode(p);
  3417. p:=hp;
  3418. p^.explizit:=true;
  3419. firstpass(p);
  3420. end
  3421. else
  3422. begin
  3423. { can anything else be ord() ?}
  3424. Message(sym_e_type_mismatch);
  3425. end;
  3426. end;
  3427. end;
  3428. in_chr_byte:
  3429. begin
  3430. hp:=gentypeconvnode(p^.left,cchardef);
  3431. putnode(p);
  3432. p:=hp;
  3433. p^.explizit:=true;
  3434. firstpass(p);
  3435. end;
  3436. in_length_string:
  3437. begin
  3438. {$ifdef UseAnsiString}
  3439. if is_ansistring(p^.left^.resulttype) then
  3440. p^.resulttype:=s32bitdef
  3441. else
  3442. {$endif UseAnsiString}
  3443. p^.resulttype:=u8bitdef;
  3444. { wer don't need string conversations here }
  3445. if (p^.left^.treetype=typeconvn) and
  3446. (p^.left^.left^.resulttype^.deftype=stringdef) then
  3447. begin
  3448. hp:=p^.left^.left;
  3449. putnode(p^.left);
  3450. p^.left:=hp;
  3451. end;
  3452. { evalutes length of constant strings direct }
  3453. if (p^.left^.treetype=stringconstn) then
  3454. begin
  3455. {$ifdef UseAnsiString}
  3456. hp:=genordinalconstnode(p^.left^.length,s32bitdef);
  3457. {$else UseAnsiString}
  3458. hp:=genordinalconstnode(length(p^.left^.values^),s32bitdef);
  3459. {$endif UseAnsiString}
  3460. disposetree(p);
  3461. firstpass(hp);
  3462. p:=hp;
  3463. end;
  3464. end;
  3465. in_assigned_x:
  3466. begin
  3467. p^.resulttype:=booldef;
  3468. p^.location.loc:=LOC_FLAGS;
  3469. end;
  3470. in_pred_x,
  3471. in_succ_x:
  3472. begin
  3473. p^.resulttype:=p^.left^.resulttype;
  3474. p^.location.loc:=LOC_REGISTER;
  3475. if not is_ordinal(p^.resulttype) then
  3476. Message(sym_e_type_mismatch)
  3477. else
  3478. begin
  3479. if (p^.resulttype^.deftype=enumdef) and
  3480. (penumdef(p^.resulttype)^.has_jumps) then
  3481. begin
  3482. Message(parser_e_succ_and_pred_enums_with_assign_not_possible);
  3483. end
  3484. else if p^.left^.treetype=ordconstn then
  3485. begin
  3486. if p^.inlinenumber=in_pred_x then
  3487. hp:=genordinalconstnode(p^.left^.value+1,
  3488. p^.left^.resulttype)
  3489. else
  3490. hp:=genordinalconstnode(p^.left^.value-1,
  3491. p^.left^.resulttype);
  3492. disposetree(p);
  3493. firstpass(hp);
  3494. p:=hp;
  3495. end;
  3496. end;
  3497. end;
  3498. in_dec_dword,
  3499. in_dec_word,
  3500. in_dec_byte,
  3501. in_inc_dword,
  3502. in_inc_word,
  3503. in_inc_byte :
  3504. begin
  3505. p^.resulttype:=voiddef;
  3506. if p^.left^.location.loc<>LOC_REFERENCE then
  3507. Message(cg_e_illegal_expression);
  3508. end;
  3509. in_inc_x,
  3510. in_dec_x:
  3511. begin
  3512. p^.resulttype:=voiddef;
  3513. if assigned(p^.left) then
  3514. begin
  3515. firstcallparan(p^.left,nil);
  3516. { first param must be var }
  3517. if not (p^.left^.left^.location.loc in [LOC_MEM,LOC_REFERENCE]) then
  3518. Message(cg_e_illegal_expression);
  3519. { check type }
  3520. if (p^.left^.resulttype^.deftype in [enumdef,pointerdef]) or
  3521. ((p^.left^.resulttype^.deftype=orddef) and
  3522. (porddef(p^.left^.resulttype)^.typ in [uchar,bool8bit,u8bit,s8bit,
  3523. bool16bit,u16bit,s16bit,bool32bit,u32bit,s32bit])) then
  3524. begin
  3525. { two paras ? }
  3526. if assigned(p^.left^.right) then
  3527. begin
  3528. { insert a type conversion }
  3529. { the second param is always longint }
  3530. p^.left^.right^.left:=gentypeconvnode(p^.left^.right^.left,s32bitdef);
  3531. { check the type conversion }
  3532. firstpass(p^.left^.right^.left);
  3533. if assigned(p^.left^.right^.right) then
  3534. Message(cg_e_illegal_expression);
  3535. end;
  3536. end
  3537. else
  3538. Message(sym_e_type_mismatch);
  3539. end
  3540. else
  3541. Message(sym_e_type_mismatch);
  3542. end;
  3543. in_read_x,
  3544. in_readln_x,
  3545. in_write_x,
  3546. in_writeln_x :
  3547. begin
  3548. { needs a call }
  3549. procinfo.flags:=procinfo.flags or pi_do_call;
  3550. p^.resulttype:=voiddef;
  3551. { we must know if it is a typed file or not }
  3552. { but we must first do the firstpass for it }
  3553. file_is_typed:=false;
  3554. if assigned(p^.left) then
  3555. begin
  3556. firstcallparan(p^.left,nil);
  3557. { now we can check }
  3558. hp:=p^.left;
  3559. while assigned(hp^.right) do
  3560. hp:=hp^.right;
  3561. { if resulttype is not assigned, then automatically }
  3562. { file is not typed. }
  3563. if assigned(hp) and assigned(hp^.resulttype) then
  3564. Begin
  3565. if (hp^.resulttype^.deftype=filedef) and
  3566. (pfiledef(hp^.resulttype)^.filetype=ft_typed) then
  3567. begin
  3568. file_is_typed:=true;
  3569. { test the type here
  3570. so we can use a trick in cgi386 (PM) }
  3571. hpp:=p^.left;
  3572. while (hpp<>hp) do
  3573. begin
  3574. { should we allow type conversion ? (PM)
  3575. if not isconvertable(hpp^.resulttype,
  3576. pfiledef(hp^.resulttype)^.typed_as,convtyp,hpp^.treetype) then
  3577. Message(sym_e_type_mismatch);
  3578. if not(is_equal(hpp^.resulttype,pfiledef(hp^.resulttype)^.typed_as)) then
  3579. begin
  3580. hpp^.left:=gentypeconvnode(hpp^.left,pfiledef(hp^.resulttype)^.typed_as);
  3581. end; }
  3582. if not is_equal(hpp^.resulttype,pfiledef(hp^.resulttype)^.typed_as) then
  3583. Message(sym_e_type_mismatch);
  3584. hpp:=hpp^.right;
  3585. end;
  3586. { once again for typeconversions }
  3587. firstcallparan(p^.left,nil);
  3588. end;
  3589. end; { endif assigned(hp) }
  3590. { insert type conversions for write(ln) }
  3591. if (not file_is_typed) and
  3592. ((p^.inlinenumber=in_write_x) or (p^.inlinenumber=in_writeln_x)) then
  3593. begin
  3594. hp:=p^.left;
  3595. while assigned(hp) do
  3596. begin
  3597. if assigned(hp^.left^.resulttype) then
  3598. begin
  3599. if hp^.left^.resulttype^.deftype=floatdef then
  3600. begin
  3601. isreal:=true;
  3602. end
  3603. else if hp^.left^.resulttype^.deftype=orddef then
  3604. case porddef(hp^.left^.resulttype)^.typ of
  3605. u8bit,s8bit,
  3606. u16bit,s16bit : hp^.left:=gentypeconvnode(hp^.left,s32bitdef);
  3607. bool16bit,bool32bit : hp^.left:=gentypeconvnode(hp^.left,booldef);
  3608. end
  3609. { but we convert only if the first index<>0, because in this case }
  3610. { we have a ASCIIZ string }
  3611. else if (hp^.left^.resulttype^.deftype=arraydef) and
  3612. (parraydef(hp^.left^.resulttype)^.lowrange<>0) and
  3613. (parraydef(hp^.left^.resulttype)^.definition^.deftype=orddef) and
  3614. (porddef(parraydef(hp^.left^.resulttype)^.definition)^.typ=uchar) then
  3615. hp^.left:=gentypeconvnode(hp^.left,cstringdef);
  3616. end;
  3617. hp:=hp^.right;
  3618. end;
  3619. end;
  3620. { pass all parameters again }
  3621. firstcallparan(p^.left,nil);
  3622. end;
  3623. end;
  3624. in_settextbuf_file_x :
  3625. begin
  3626. { warning here p^.left is the callparannode
  3627. not the argument directly }
  3628. { p^.left^.left is text var }
  3629. { p^.left^.right^.left is the buffer var }
  3630. { firstcallparan(p^.left,nil);
  3631. already done in firstcalln }
  3632. { now we know the type of buffer }
  3633. getsymonlyin(systemunit,'SETTEXTBUF');
  3634. hp:=gencallnode(pprocsym(srsym),systemunit);
  3635. hp^.left:=gencallparanode(
  3636. genordinalconstnode(p^.left^.left^.resulttype^.size,s32bitdef),p^.left);
  3637. putnode(p);
  3638. p:=hp;
  3639. firstpass(p);
  3640. end;
  3641. { the firstpass of the arg has been done in firstcalln ? }
  3642. in_reset_typedfile,in_rewrite_typedfile :
  3643. begin
  3644. procinfo.flags:=procinfo.flags or pi_do_call;
  3645. { to be sure the right definition is loaded }
  3646. p^.left^.resulttype:=nil;
  3647. firstload(p^.left);
  3648. p^.resulttype:=voiddef;
  3649. end;
  3650. in_str_x_string :
  3651. begin
  3652. procinfo.flags:=procinfo.flags or pi_do_call;
  3653. p^.resulttype:=voiddef;
  3654. if assigned(p^.left) then
  3655. begin
  3656. hp:=p^.left^.right;
  3657. { first pass just the string for first local use }
  3658. must_be_valid:=false;
  3659. count_ref:=true;
  3660. p^.left^.right:=nil;
  3661. firstcallparan(p^.left,nil);
  3662. must_be_valid:=true;
  3663. p^.left^.right:=hp;
  3664. firstcallparan(p^.left^.right,nil);
  3665. hp:=p^.left;
  3666. isreal:=false;
  3667. { valid string ? }
  3668. if not assigned(hp) or
  3669. (hp^.left^.resulttype^.deftype<>stringdef) or
  3670. (hp^.right=nil) or
  3671. (hp^.left^.location.loc<>LOC_REFERENCE) then
  3672. Message(cg_e_illegal_expression);
  3673. { !!!! check length of string }
  3674. while assigned(hp^.right) do hp:=hp^.right;
  3675. { check and convert the first param }
  3676. if hp^.is_colon_para then
  3677. Message(cg_e_illegal_expression)
  3678. else if hp^.resulttype^.deftype=orddef then
  3679. case porddef(hp^.left^.resulttype)^.typ of
  3680. u8bit,s8bit,
  3681. u16bit,s16bit :
  3682. hp^.left:=gentypeconvnode(hp^.left,s32bitdef);
  3683. end
  3684. else if hp^.resulttype^.deftype=floatdef then
  3685. begin
  3686. isreal:=true;
  3687. end
  3688. else Message(cg_e_illegal_expression);
  3689. { some format options ? }
  3690. hp:=p^.left^.right;
  3691. if assigned(hp) and hp^.is_colon_para then
  3692. begin
  3693. hp^.left:=gentypeconvnode(hp^.left,s32bitdef);
  3694. hp:=hp^.right;
  3695. end;
  3696. if assigned(hp) and hp^.is_colon_para then
  3697. begin
  3698. if isreal then
  3699. hp^.left:=gentypeconvnode(hp^.left,s32bitdef)
  3700. else
  3701. Message(parser_e_illegal_colon_qualifier);
  3702. hp:=hp^.right;
  3703. end;
  3704. { for first local use }
  3705. must_be_valid:=false;
  3706. count_ref:=true;
  3707. if assigned(hp) then
  3708. firstcallparan(hp,nil);
  3709. end
  3710. else
  3711. Message(parser_e_illegal_parameter_list);
  3712. { check params once more }
  3713. if codegenerror then
  3714. exit;
  3715. must_be_valid:=true;
  3716. firstcallparan(p^.left,nil);
  3717. end;
  3718. in_include_x_y,
  3719. in_exclude_x_y:
  3720. begin
  3721. p^.resulttype:=voiddef;
  3722. if assigned(p^.left) then
  3723. begin
  3724. firstcallparan(p^.left,nil);
  3725. p^.registers32:=p^.left^.registers32;
  3726. p^.registersfpu:=p^.left^.registersfpu;
  3727. {$ifdef SUPPORT_MMX}
  3728. p^.registersmmx:=p^.left^.registersmmx;
  3729. {$endif SUPPORT_MMX}
  3730. { first param must be var }
  3731. if (p^.left^.left^.location.loc<>LOC_REFERENCE) and
  3732. (p^.left^.left^.location.loc<>LOC_CREGISTER) then
  3733. Message(cg_e_illegal_expression);
  3734. { check type }
  3735. if (p^.left^.resulttype^.deftype=setdef) then
  3736. begin
  3737. { two paras ? }
  3738. if assigned(p^.left^.right) then
  3739. begin
  3740. { insert a type conversion }
  3741. { to the type of the set elements }
  3742. p^.left^.right^.left:=gentypeconvnode(
  3743. p^.left^.right^.left,
  3744. psetdef(p^.left^.resulttype)^.setof);
  3745. { check the type conversion }
  3746. firstpass(p^.left^.right^.left);
  3747. { only three parameters are allowed }
  3748. if assigned(p^.left^.right^.right) then
  3749. Message(cg_e_illegal_expression);
  3750. end;
  3751. end
  3752. else
  3753. Message(sym_e_type_mismatch);
  3754. end
  3755. else
  3756. Message(sym_e_type_mismatch);
  3757. end;
  3758. in_low_x,in_high_x:
  3759. begin
  3760. if p^.left^.treetype in [typen,loadn] then
  3761. begin
  3762. case p^.left^.resulttype^.deftype of
  3763. orddef,enumdef:
  3764. begin
  3765. do_lowhigh(p^.left^.resulttype);
  3766. firstpass(p);
  3767. end;
  3768. setdef:
  3769. begin
  3770. do_lowhigh(Psetdef(p^.left^.resulttype)^.setof);
  3771. firstpass(p);
  3772. end;
  3773. arraydef:
  3774. begin
  3775. if is_open_array(p^.left^.resulttype) then
  3776. begin
  3777. if p^.inlinenumber=in_low_x then
  3778. begin
  3779. hp:=genordinalconstnode(Parraydef(p^.left^.resulttype)^.lowrange,s32bitdef);
  3780. disposetree(p);
  3781. p:=hp;
  3782. firstpass(p);
  3783. end
  3784. else
  3785. begin
  3786. p^.resulttype:=s32bitdef;
  3787. p^.registers32:=max(1,
  3788. p^.registers32);
  3789. p^.location.loc:=LOC_REGISTER;
  3790. end;
  3791. end
  3792. else
  3793. begin
  3794. if p^.inlinenumber=in_low_x then
  3795. hp:=genordinalconstnode(Parraydef(p^.left^.resulttype)^.lowrange,s32bitdef)
  3796. else
  3797. hp:=genordinalconstnode(Parraydef(p^.left^.resulttype)^.highrange,s32bitdef);
  3798. disposetree(p);
  3799. p:=hp;
  3800. firstpass(p);
  3801. end;
  3802. end;
  3803. stringdef:
  3804. begin
  3805. if p^.inlinenumber=in_low_x then
  3806. hp:=genordinalconstnode(0,u8bitdef)
  3807. else
  3808. hp:=genordinalconstnode(Pstringdef(p^.left^.resulttype)^.len,u8bitdef);
  3809. disposetree(p);
  3810. p:=hp;
  3811. firstpass(p);
  3812. end;
  3813. else
  3814. Message(sym_e_type_mismatch);
  3815. end;
  3816. end
  3817. else
  3818. Message(parser_e_varid_or_typeid_expected);
  3819. end
  3820. else internalerror(8);
  3821. end;
  3822. must_be_valid:=store_valid;
  3823. count_ref:=store_count_ref;
  3824. end;
  3825. procedure firstsubscriptn(var p : ptree);
  3826. begin
  3827. firstpass(p^.left);
  3828. if codegenerror then
  3829. begin
  3830. p^.resulttype:=generrordef;
  3831. exit;
  3832. end;
  3833. p^.resulttype:=p^.vs^.definition;
  3834. { this must be done in the parser
  3835. if count_ref and not must_be_valid then
  3836. if (p^.vs^.properties and sp_protected)<>0 then
  3837. Message(parser_e_cant_write_protected_member);
  3838. }
  3839. p^.registers32:=p^.left^.registers32;
  3840. p^.registersfpu:=p^.left^.registersfpu;
  3841. {$ifdef SUPPORT_MMX}
  3842. p^.registersmmx:=p^.left^.registersmmx;
  3843. {$endif SUPPORT_MMX}
  3844. { classes must be dereferenced implicit }
  3845. if (p^.left^.resulttype^.deftype=objectdef) and
  3846. pobjectdef(p^.left^.resulttype)^.isclass then
  3847. begin
  3848. if p^.registers32=0 then
  3849. p^.registers32:=1;
  3850. p^.location.loc:=LOC_REFERENCE;
  3851. end
  3852. else
  3853. begin
  3854. if (p^.left^.location.loc<>LOC_MEM) and
  3855. (p^.left^.location.loc<>LOC_REFERENCE) then
  3856. Message(cg_e_illegal_expression);
  3857. set_location(p^.location,p^.left^.location);
  3858. end;
  3859. end;
  3860. procedure firstselfn(var p : ptree);
  3861. begin
  3862. if (p^.resulttype^.deftype=classrefdef) or
  3863. ((p^.resulttype^.deftype=objectdef)
  3864. and pobjectdef(p^.resulttype)^.isclass
  3865. ) then
  3866. p^.location.loc:=LOC_REGISTER
  3867. else
  3868. p^.location.loc:=LOC_REFERENCE;
  3869. end;
  3870. procedure firsttypen(var p : ptree);
  3871. begin
  3872. { DM: Why not allowed? For example: low(word) results in a type
  3873. id of word.
  3874. error(typeid_here_not_allowed);}
  3875. end;
  3876. procedure firsthnewn(var p : ptree);
  3877. begin
  3878. end;
  3879. procedure firsthdisposen(var p : ptree);
  3880. begin
  3881. firstpass(p^.left);
  3882. if codegenerror then
  3883. exit;
  3884. p^.registers32:=p^.left^.registers32;
  3885. p^.registersfpu:=p^.left^.registersfpu;
  3886. {$ifdef SUPPORT_MMX}
  3887. p^.registersmmx:=p^.left^.registersmmx;
  3888. {$endif SUPPORT_MMX}
  3889. if p^.registers32<1 then
  3890. p^.registers32:=1;
  3891. {
  3892. if p^.left^.location.loc<>LOC_REFERENCE then
  3893. Message(cg_e_illegal_expression);
  3894. }
  3895. p^.location.loc:=LOC_REFERENCE;
  3896. p^.resulttype:=ppointerdef(p^.left^.resulttype)^.definition;
  3897. end;
  3898. procedure firstnewn(var p : ptree);
  3899. begin
  3900. { Standardeinleitung }
  3901. firstpass(p^.left);
  3902. if codegenerror then
  3903. exit;
  3904. p^.registers32:=p^.left^.registers32;
  3905. p^.registersfpu:=p^.left^.registersfpu;
  3906. {$ifdef SUPPORT_MMX}
  3907. p^.registersmmx:=p^.left^.registersmmx;
  3908. {$endif SUPPORT_MMX}
  3909. { result type is already set }
  3910. procinfo.flags:=procinfo.flags or pi_do_call;
  3911. p^.location.loc:=LOC_REGISTER;
  3912. end;
  3913. procedure firstsimplenewdispose(var p : ptree);
  3914. begin
  3915. { this cannot be in a register !! }
  3916. make_not_regable(p^.left);
  3917. firstpass(p^.left);
  3918. { check the type }
  3919. if (p^.left^.resulttype=nil) or (p^.left^.resulttype^.deftype<>pointerdef) then
  3920. Message(parser_e_pointer_type_expected);
  3921. if (p^.left^.location.loc<>LOC_REFERENCE) {and
  3922. (p^.left^.location.loc<>LOC_CREGISTER)} then
  3923. Message(cg_e_illegal_expression);
  3924. p^.registers32:=p^.left^.registers32;
  3925. p^.registersfpu:=p^.left^.registersfpu;
  3926. {$ifdef SUPPORT_MMX}
  3927. p^.registersmmx:=p^.left^.registersmmx;
  3928. {$endif SUPPORT_MMX}
  3929. p^.resulttype:=voiddef;
  3930. procinfo.flags:=procinfo.flags or pi_do_call;
  3931. end;
  3932. procedure firstsetcons(var p : ptree);
  3933. var
  3934. hp : ptree;
  3935. begin
  3936. p^.location.loc:=LOC_MEM;
  3937. hp:=p^.left;
  3938. { is done by getnode*
  3939. p^.registers32:=0;
  3940. p^.registersfpu:=0;
  3941. }
  3942. while assigned(hp) do
  3943. begin
  3944. firstpass(hp^.left);
  3945. if codegenerror then
  3946. exit;
  3947. p^.registers32:=max(p^.registers32,hp^.left^.registers32);
  3948. p^.registersfpu:=max(p^.registersfpu,hp^.left^.registersfpu);;
  3949. {$ifdef SUPPORT_MMX}
  3950. p^.registersmmx:=max(p^.registersmmx,hp^.left^.registersmmx);
  3951. {$endif SUPPORT_MMX}
  3952. hp:=hp^.right;
  3953. end;
  3954. { result type is already set }
  3955. end;
  3956. procedure firstin(var p : ptree);
  3957. begin
  3958. p^.location.loc:=LOC_FLAGS;
  3959. p^.resulttype:=booldef;
  3960. firstpass(p^.right);
  3961. if codegenerror then
  3962. exit;
  3963. if p^.right^.resulttype^.deftype<>setdef then
  3964. Message(sym_e_set_expected);
  3965. firstpass(p^.left);
  3966. if codegenerror then
  3967. exit;
  3968. p^.left:=gentypeconvnode(p^.left,psetdef(p^.right^.resulttype)^.setof);
  3969. firstpass(p^.left);
  3970. if codegenerror then
  3971. exit;
  3972. left_right_max(p);
  3973. { this is not allways true due to optimization }
  3974. { but if we don't set this we get problems with optimizing self code }
  3975. if psetdef(p^.right^.resulttype)^.settype<>smallset then
  3976. procinfo.flags:=procinfo.flags or pi_do_call;
  3977. end;
  3978. procedure firststatement(var p : ptree);
  3979. begin
  3980. { left is the next statement in the list }
  3981. p^.resulttype:=voiddef;
  3982. { no temps over several statements }
  3983. cleartempgen;
  3984. { right is the statement itself calln assignn or a complex one }
  3985. firstpass(p^.right);
  3986. if (not (cs_extsyntax in aktswitches)) and
  3987. assigned(p^.right^.resulttype) and
  3988. (p^.right^.resulttype<>pdef(voiddef)) then
  3989. Message(cg_e_illegal_expression);
  3990. if codegenerror then
  3991. exit;
  3992. p^.registers32:=p^.right^.registers32;
  3993. p^.registersfpu:=p^.right^.registersfpu;
  3994. {$ifdef SUPPORT_MMX}
  3995. p^.registersmmx:=p^.right^.registersmmx;
  3996. {$endif SUPPORT_MMX}
  3997. { left is the next in the list }
  3998. firstpass(p^.left);
  3999. if codegenerror then
  4000. exit;
  4001. if p^.right^.registers32>p^.registers32 then
  4002. p^.registers32:=p^.right^.registers32;
  4003. if p^.right^.registersfpu>p^.registersfpu then
  4004. p^.registersfpu:=p^.right^.registersfpu;
  4005. {$ifdef SUPPORT_MMX}
  4006. if p^.right^.registersmmx>p^.registersmmx then
  4007. p^.registersmmx:=p^.right^.registersmmx;
  4008. {$endif}
  4009. end;
  4010. procedure firstblock(var p : ptree);
  4011. var
  4012. hp : ptree;
  4013. count : longint;
  4014. begin
  4015. count:=0;
  4016. hp:=p^.left;
  4017. while assigned(hp) do
  4018. begin
  4019. if cs_maxoptimieren in aktswitches then
  4020. begin
  4021. { Codeumstellungen }
  4022. { Funktionsresultate an exit anh„ngen }
  4023. { this is wrong for string or other complex
  4024. result types !!! }
  4025. if ret_in_acc(procinfo.retdef) and
  4026. assigned(hp^.left) and
  4027. (hp^.left^.right^.treetype=exitn) and
  4028. (hp^.right^.treetype=assignn) and
  4029. (hp^.right^.left^.treetype=funcretn) then
  4030. begin
  4031. if assigned(hp^.left^.right^.left) then
  4032. Message(cg_n_inefficient_code)
  4033. else
  4034. begin
  4035. hp^.left^.right^.left:=getcopy(hp^.right^.right);
  4036. disposetree(hp^.right);
  4037. hp^.right:=nil;
  4038. end;
  4039. end
  4040. { warning if unreachable code occurs and elimate this }
  4041. else if (hp^.right^.treetype in
  4042. [exitn,breakn,continuen,goton]) and
  4043. assigned(hp^.left) and
  4044. (hp^.left^.treetype<>labeln) then
  4045. begin
  4046. { use correct line number }
  4047. aktfilepos:=hp^.left^.fileinfo;
  4048. disposetree(hp^.left);
  4049. hp^.left:=nil;
  4050. Message(cg_w_unreachable_code);
  4051. { old lines }
  4052. aktfilepos:=hp^.right^.fileinfo;
  4053. end;
  4054. end;
  4055. if assigned(hp^.right) then
  4056. begin
  4057. cleartempgen;
  4058. firstpass(hp^.right);
  4059. if (not (cs_extsyntax in aktswitches)) and
  4060. assigned(hp^.right^.resulttype) and
  4061. (hp^.right^.resulttype<>pdef(voiddef)) then
  4062. Message(cg_e_illegal_expression);
  4063. if codegenerror then
  4064. exit;
  4065. hp^.registers32:=hp^.right^.registers32;
  4066. hp^.registersfpu:=hp^.right^.registersfpu;
  4067. {$ifdef SUPPORT_MMX}
  4068. hp^.registersmmx:=hp^.right^.registersmmx;
  4069. {$endif SUPPORT_MMX}
  4070. end
  4071. else
  4072. hp^.registers32:=0;
  4073. if hp^.registers32>p^.registers32 then
  4074. p^.registers32:=hp^.registers32;
  4075. if hp^.registersfpu>p^.registersfpu then
  4076. p^.registersfpu:=hp^.registersfpu;
  4077. {$ifdef SUPPORT_MMX}
  4078. if hp^.registersmmx>p^.registersmmx then
  4079. p^.registersmmx:=hp^.registersmmx;
  4080. {$endif}
  4081. inc(count);
  4082. hp:=hp^.left;
  4083. end;
  4084. { p^.registers32:=round(p^.registers32/count); }
  4085. end;
  4086. procedure first_while_repeat(var p : ptree);
  4087. var
  4088. old_t_times : longint;
  4089. begin
  4090. old_t_times:=t_times;
  4091. { Registergewichtung bestimmen }
  4092. if not(cs_littlesize in aktswitches ) then
  4093. t_times:=t_times*8;
  4094. cleartempgen;
  4095. must_be_valid:=true;
  4096. firstpass(p^.left);
  4097. if codegenerror then
  4098. exit;
  4099. if not((p^.left^.resulttype^.deftype=orddef) and
  4100. (porddef(p^.left^.resulttype)^.typ=bool8bit)) then
  4101. begin
  4102. Message(sym_e_type_mismatch);
  4103. exit;
  4104. end;
  4105. p^.registers32:=p^.left^.registers32;
  4106. p^.registersfpu:=p^.left^.registersfpu;
  4107. {$ifdef SUPPORT_MMX}
  4108. p^.registersmmx:=p^.left^.registersmmx;
  4109. {$endif SUPPORT_MMX}
  4110. { loop instruction }
  4111. if assigned(p^.right) then
  4112. begin
  4113. cleartempgen;
  4114. firstpass(p^.right);
  4115. if codegenerror then
  4116. exit;
  4117. if p^.registers32<p^.right^.registers32 then
  4118. p^.registers32:=p^.right^.registers32;
  4119. if p^.registersfpu<p^.right^.registersfpu then
  4120. p^.registersfpu:=p^.right^.registersfpu;
  4121. {$ifdef SUPPORT_MMX}
  4122. if p^.registersmmx<p^.right^.registersmmx then
  4123. p^.registersmmx:=p^.right^.registersmmx;
  4124. {$endif SUPPORT_MMX}
  4125. end;
  4126. t_times:=old_t_times;
  4127. end;
  4128. procedure firstif(var p : ptree);
  4129. var
  4130. old_t_times : longint;
  4131. hp : ptree;
  4132. begin
  4133. old_t_times:=t_times;
  4134. cleartempgen;
  4135. must_be_valid:=true;
  4136. firstpass(p^.left);
  4137. if codegenerror then
  4138. exit;
  4139. if not((p^.left^.resulttype^.deftype=orddef) and
  4140. (porddef(p^.left^.resulttype)^.typ in [bool8bit,bool16bit,bool32bit])) then
  4141. begin
  4142. Message(sym_e_type_mismatch);
  4143. exit;
  4144. end;
  4145. p^.registers32:=p^.left^.registers32;
  4146. p^.registersfpu:=p^.left^.registersfpu;
  4147. {$ifdef SUPPORT_MMX}
  4148. p^.registersmmx:=p^.left^.registersmmx;
  4149. {$endif SUPPORT_MMX}
  4150. { determines registers weigths }
  4151. if not(cs_littlesize in aktswitches ) then
  4152. t_times:=t_times div 2;
  4153. if t_times=0 then
  4154. t_times:=1;
  4155. { if path }
  4156. if assigned(p^.right) then
  4157. begin
  4158. cleartempgen;
  4159. firstpass(p^.right);
  4160. if codegenerror then
  4161. exit;
  4162. if p^.registers32<p^.right^.registers32 then
  4163. p^.registers32:=p^.right^.registers32;
  4164. if p^.registersfpu<p^.right^.registersfpu then
  4165. p^.registersfpu:=p^.right^.registersfpu;
  4166. {$ifdef SUPPORT_MMX}
  4167. if p^.registersmmx<p^.right^.registersmmx then
  4168. p^.registersmmx:=p^.right^.registersmmx;
  4169. {$endif SUPPORT_MMX}
  4170. end;
  4171. { else path }
  4172. if assigned(p^.t1) then
  4173. begin
  4174. cleartempgen;
  4175. firstpass(p^.t1);
  4176. if codegenerror then
  4177. exit;
  4178. if p^.registers32<p^.t1^.registers32 then
  4179. p^.registers32:=p^.t1^.registers32;
  4180. if p^.registersfpu<p^.t1^.registersfpu then
  4181. p^.registersfpu:=p^.t1^.registersfpu;
  4182. {$ifdef SUPPORT_MMX}
  4183. if p^.registersmmx<p^.t1^.registersmmx then
  4184. p^.registersmmx:=p^.t1^.registersmmx;
  4185. {$endif SUPPORT_MMX}
  4186. end;
  4187. if p^.left^.treetype=ordconstn then
  4188. begin
  4189. { optimize }
  4190. if p^.left^.value=1 then
  4191. begin
  4192. disposetree(p^.left);
  4193. hp:=p^.right;
  4194. disposetree(p^.t1);
  4195. { we cannot set p to nil !!! }
  4196. if assigned(hp) then
  4197. begin
  4198. putnode(p);
  4199. p:=hp;
  4200. end
  4201. else
  4202. begin
  4203. p^.left:=nil;
  4204. p^.t1:=nil;
  4205. p^.treetype:=nothingn;
  4206. end;
  4207. end
  4208. else
  4209. begin
  4210. disposetree(p^.left);
  4211. hp:=p^.t1;
  4212. disposetree(p^.right);
  4213. { we cannot set p to nil !!! }
  4214. if assigned(hp) then
  4215. begin
  4216. putnode(p);
  4217. p:=hp;
  4218. end
  4219. else
  4220. begin
  4221. p^.left:=nil;
  4222. p^.right:=nil;
  4223. p^.treetype:=nothingn;
  4224. end;
  4225. end;
  4226. end;
  4227. t_times:=old_t_times;
  4228. end;
  4229. procedure firstexitn(var p : ptree);
  4230. begin
  4231. if assigned(p^.left) then
  4232. begin
  4233. firstpass(p^.left);
  4234. p^.registers32:=p^.left^.registers32;
  4235. p^.registersfpu:=p^.left^.registersfpu;
  4236. {$ifdef SUPPORT_MMX}
  4237. p^.registersmmx:=p^.left^.registersmmx;
  4238. {$endif SUPPORT_MMX}
  4239. end;
  4240. end;
  4241. procedure firstfor(var p : ptree);
  4242. var
  4243. old_t_times : longint;
  4244. begin
  4245. { Registergewichtung bestimmen
  4246. (nicht genau), }
  4247. old_t_times:=t_times;
  4248. if not(cs_littlesize in aktswitches ) then
  4249. t_times:=t_times*8;
  4250. cleartempgen;
  4251. if p^.t1<>nil then
  4252. firstpass(p^.t1);
  4253. p^.registers32:=p^.t1^.registers32;
  4254. p^.registersfpu:=p^.t1^.registersfpu;
  4255. {$ifdef SUPPORT_MMX}
  4256. p^.registersmmx:=p^.left^.registersmmx;
  4257. {$endif SUPPORT_MMX}
  4258. if p^.left^.treetype<>assignn then
  4259. Message(cg_e_illegal_expression);
  4260. { Laufvariable retten }
  4261. p^.t2:=getcopy(p^.left^.left);
  4262. { Check count var }
  4263. if (p^.t2^.treetype<>loadn) then
  4264. Message(cg_e_illegal_count_var);
  4265. if (not(is_ordinal(p^.t2^.resulttype))) then
  4266. Message(parser_e_ordinal_expected);
  4267. cleartempgen;
  4268. must_be_valid:=false;
  4269. firstpass(p^.left);
  4270. must_be_valid:=true;
  4271. if p^.left^.registers32>p^.registers32 then
  4272. p^.registers32:=p^.left^.registers32;
  4273. if p^.left^.registersfpu>p^.registersfpu then
  4274. p^.registersfpu:=p^.left^.registersfpu;
  4275. {$ifdef SUPPORT_MMX}
  4276. if p^.left^.registersmmx>p^.registersmmx then
  4277. p^.registersmmx:=p^.left^.registersmmx;
  4278. {$endif SUPPORT_MMX}
  4279. cleartempgen;
  4280. firstpass(p^.t2);
  4281. if p^.t2^.registers32>p^.registers32 then
  4282. p^.registers32:=p^.t2^.registers32;
  4283. if p^.t2^.registersfpu>p^.registersfpu then
  4284. p^.registersfpu:=p^.t2^.registersfpu;
  4285. {$ifdef SUPPORT_MMX}
  4286. if p^.t2^.registersmmx>p^.registersmmx then
  4287. p^.registersmmx:=p^.t2^.registersmmx;
  4288. {$endif SUPPORT_MMX}
  4289. cleartempgen;
  4290. firstpass(p^.right);
  4291. if p^.right^.treetype<>ordconstn then
  4292. begin
  4293. p^.right:=gentypeconvnode(p^.right,p^.t2^.resulttype);
  4294. cleartempgen;
  4295. firstpass(p^.right);
  4296. end;
  4297. if p^.right^.registers32>p^.registers32 then
  4298. p^.registers32:=p^.right^.registers32;
  4299. if p^.right^.registersfpu>p^.registersfpu then
  4300. p^.registersfpu:=p^.right^.registersfpu;
  4301. {$ifdef SUPPORT_MMX}
  4302. if p^.right^.registersmmx>p^.registersmmx then
  4303. p^.registersmmx:=p^.right^.registersmmx;
  4304. {$endif SUPPORT_MMX}
  4305. t_times:=old_t_times;
  4306. end;
  4307. procedure firstasm(var p : ptree);
  4308. begin
  4309. { it's a f... to determine the used registers }
  4310. { should be done by getnode
  4311. I think also, that all values should be set to their maximum (FK)
  4312. p^.registers32:=0;
  4313. p^.registersfpu:=0;
  4314. p^.registersmmx:=0;
  4315. }
  4316. procinfo.flags:=procinfo.flags or pi_uses_asm;
  4317. end;
  4318. procedure firstgoto(var p : ptree);
  4319. begin
  4320. {
  4321. p^.registers32:=0;
  4322. p^.registersfpu:=0;
  4323. }
  4324. p^.resulttype:=voiddef;
  4325. end;
  4326. procedure firstlabel(var p : ptree);
  4327. begin
  4328. cleartempgen;
  4329. firstpass(p^.left);
  4330. p^.registers32:=p^.left^.registers32;
  4331. p^.registersfpu:=p^.left^.registersfpu;
  4332. {$ifdef SUPPORT_MMX}
  4333. p^.registersmmx:=p^.left^.registersmmx;
  4334. {$endif SUPPORT_MMX}
  4335. p^.resulttype:=voiddef;
  4336. end;
  4337. procedure firstcase(var p : ptree);
  4338. var
  4339. old_t_times : longint;
  4340. hp : ptree;
  4341. begin
  4342. { evalutes the case expression }
  4343. cleartempgen;
  4344. must_be_valid:=true;
  4345. firstpass(p^.left);
  4346. if codegenerror then
  4347. exit;
  4348. p^.registers32:=p^.left^.registers32;
  4349. p^.registersfpu:=p^.left^.registersfpu;
  4350. {$ifdef SUPPORT_MMX}
  4351. p^.registersmmx:=p^.left^.registersmmx;
  4352. {$endif SUPPORT_MMX}
  4353. { walk through all instructions }
  4354. { estimates the repeat of each instruction }
  4355. old_t_times:=t_times;
  4356. if not(cs_littlesize in aktswitches ) then
  4357. begin
  4358. t_times:=t_times div case_count_labels(p^.nodes);
  4359. if t_times<1 then
  4360. t_times:=1;
  4361. end;
  4362. { first case }
  4363. hp:=p^.right;
  4364. while assigned(hp) do
  4365. begin
  4366. cleartempgen;
  4367. firstpass(hp^.right);
  4368. { searchs max registers }
  4369. if hp^.right^.registers32>p^.registers32 then
  4370. p^.registers32:=hp^.right^.registers32;
  4371. if hp^.right^.registersfpu>p^.registersfpu then
  4372. p^.registersfpu:=hp^.right^.registersfpu;
  4373. {$ifdef SUPPORT_MMX}
  4374. if hp^.right^.registersmmx>p^.registersmmx then
  4375. p^.registersmmx:=hp^.right^.registersmmx;
  4376. {$endif SUPPORT_MMX}
  4377. hp:=hp^.left;
  4378. end;
  4379. { may be handle else tree }
  4380. if assigned(p^.elseblock) then
  4381. begin
  4382. cleartempgen;
  4383. firstpass(p^.elseblock);
  4384. if codegenerror then
  4385. exit;
  4386. if p^.registers32<p^.elseblock^.registers32 then
  4387. p^.registers32:=p^.elseblock^.registers32;
  4388. if p^.registersfpu<p^.elseblock^.registersfpu then
  4389. p^.registersfpu:=p^.elseblock^.registersfpu;
  4390. {$ifdef SUPPORT_MMX}
  4391. if p^.registersmmx<p^.elseblock^.registersmmx then
  4392. p^.registersmmx:=p^.elseblock^.registersmmx;
  4393. {$endif SUPPORT_MMX}
  4394. end;
  4395. t_times:=old_t_times;
  4396. { there is one register required for the case expression }
  4397. if p^.registers32<1 then p^.registers32:=1;
  4398. end;
  4399. procedure firsttryexcept(var p : ptree);
  4400. begin
  4401. end;
  4402. procedure firsttryfinally(var p : ptree);
  4403. begin
  4404. cleartempgen;
  4405. must_be_valid:=true;
  4406. firstpass(p^.left);
  4407. cleartempgen;
  4408. must_be_valid:=true;
  4409. firstpass(p^.right);
  4410. if codegenerror then
  4411. exit;
  4412. p^.registers32:=max(p^.left^.registers32,p^.right^.registers32);
  4413. p^.registersfpu:=max(p^.left^.registersfpu,p^.right^.registersfpu);
  4414. {$ifdef SUPPORT_MMX}
  4415. p^.registersmmx:=max(p^.left^.registersmmx,p^.right^.registersmmx);
  4416. {$endif SUPPORT_MMX}
  4417. end;
  4418. procedure firstis(var p : ptree);
  4419. begin
  4420. firstpass(p^.left);
  4421. firstpass(p^.right);
  4422. if (p^.right^.resulttype^.deftype<>classrefdef) then
  4423. Message(sym_e_type_mismatch);
  4424. if codegenerror then
  4425. exit;
  4426. left_right_max(p);
  4427. { left must be a class }
  4428. if (p^.left^.resulttype^.deftype<>objectdef) or
  4429. not(pobjectdef(p^.left^.resulttype)^.isclass) then
  4430. Message(sym_e_type_mismatch);
  4431. { the operands must be related }
  4432. if (not(pobjectdef(p^.left^.resulttype)^.isrelated(
  4433. pobjectdef(pclassrefdef(p^.right^.resulttype)^.definition)))) and
  4434. (not(pobjectdef(pclassrefdef(p^.right^.resulttype)^.definition)^.isrelated(
  4435. pobjectdef(p^.left^.resulttype)))) then
  4436. Message(sym_e_type_mismatch);
  4437. p^.location.loc:=LOC_FLAGS;
  4438. p^.resulttype:=booldef;
  4439. end;
  4440. procedure firstas(var p : ptree);
  4441. begin
  4442. firstpass(p^.right);
  4443. firstpass(p^.left);
  4444. if (p^.right^.resulttype^.deftype<>classrefdef) then
  4445. Message(sym_e_type_mismatch);
  4446. if codegenerror then
  4447. exit;
  4448. left_right_max(p);
  4449. (* this was wrong,no ??
  4450. p^.registersfpu:=max(p^.left^.registersfpu,p^.left^.registersfpu);
  4451. p^.registers32:=max(p^.left^.registers32,p^.right^.registers32);
  4452. {$ifdef SUPPORT_MMX}
  4453. p^.registersmmx:=max(p^.left^.registersmmx,p^.right^.registersmmx);
  4454. {$endif SUPPORT_MMX} *)
  4455. { left must be a class }
  4456. if (p^.left^.resulttype^.deftype<>objectdef) or
  4457. not(pobjectdef(p^.left^.resulttype)^.isclass) then
  4458. Message(sym_e_type_mismatch);
  4459. { the operands must be related }
  4460. if (not(pobjectdef(p^.left^.resulttype)^.isrelated(
  4461. pobjectdef(pclassrefdef(p^.right^.resulttype)^.definition)))) and
  4462. (not(pobjectdef(pclassrefdef(p^.right^.resulttype)^.definition)^.isrelated(
  4463. pobjectdef(p^.left^.resulttype)))) then
  4464. Message(sym_e_type_mismatch);
  4465. p^.location:=p^.left^.location;
  4466. p^.resulttype:=pclassrefdef(p^.right^.resulttype)^.definition;
  4467. end;
  4468. procedure firstloadvmt(var p : ptree);
  4469. begin
  4470. { resulttype must be set !
  4471. p^.registersfpu:=0;
  4472. }
  4473. p^.registers32:=1;
  4474. p^.location.loc:=LOC_REGISTER;
  4475. end;
  4476. procedure firstraise(var p : ptree);
  4477. begin
  4478. p^.resulttype:=voiddef;
  4479. {
  4480. p^.registersfpu:=0;
  4481. p^.registers32:=0;
  4482. }
  4483. if assigned(p^.left) then
  4484. begin
  4485. firstpass(p^.left);
  4486. { this must be a _class_ }
  4487. if (p^.left^.resulttype^.deftype<>objectdef) or
  4488. ((pobjectdef(p^.left^.resulttype)^.options and oois_class)=0) then
  4489. Message(sym_e_type_mismatch);
  4490. p^.registersfpu:=p^.left^.registersfpu;
  4491. p^.registers32:=p^.left^.registers32;
  4492. {$ifdef SUPPORT_MMX}
  4493. p^.registersmmx:=p^.left^.registersmmx;
  4494. {$endif SUPPORT_MMX}
  4495. if assigned(p^.right) then
  4496. begin
  4497. firstpass(p^.right);
  4498. p^.right:=gentypeconvnode(p^.right,s32bitdef);
  4499. firstpass(p^.right);
  4500. left_right_max(p);
  4501. end;
  4502. end;
  4503. end;
  4504. procedure firstwith(var p : ptree);
  4505. begin
  4506. if assigned(p^.left) and assigned(p^.right) then
  4507. begin
  4508. firstpass(p^.left);
  4509. if codegenerror then
  4510. exit;
  4511. firstpass(p^.right);
  4512. if codegenerror then
  4513. exit;
  4514. left_right_max(p);
  4515. p^.resulttype:=voiddef;
  4516. end
  4517. else
  4518. begin
  4519. { optimization }
  4520. disposetree(p);
  4521. p:=nil;
  4522. end;
  4523. end;
  4524. procedure firstprocinline(var p : ptree);
  4525. begin
  4526. {left contains the code in tree form }
  4527. { but it has already been firstpassed }
  4528. { so firstpass(p^.left); does not seem required }
  4529. { might be required later if we change the arg handling !! }
  4530. end;
  4531. type
  4532. firstpassproc = procedure(var p : ptree);
  4533. procedure firstpass(var p : ptree);
  4534. (* ttreetyp = (addn, {Represents the + operator.}
  4535. muln, {Represents the * operator.}
  4536. subn, {Represents the - operator.}
  4537. divn, {Represents the div operator.}
  4538. symdifn, {Represents the >< operator.}
  4539. modn, {Represents the mod operator.}
  4540. assignn, {Represents an assignment.}
  4541. loadn, {Represents the use of a variabele.}
  4542. rangen, {Represents a range (i.e. 0..9).}
  4543. ltn, {Represents the < operator.}
  4544. lten, {Represents the <= operator.}
  4545. gtn, {Represents the > operator.}
  4546. gten, {Represents the >= operator.}
  4547. equaln, {Represents the = operator.}
  4548. unequaln, {Represents the <> operator.}
  4549. inn, {Represents the in operator.}
  4550. orn, {Represents the or operator.}
  4551. xorn, {Represents the xor operator.}
  4552. shrn, {Represents the shr operator.}
  4553. shln, {Represents the shl operator.}
  4554. slashn, {Represents the / operator.}
  4555. andn, {Represents the and operator.}
  4556. subscriptn, {??? Field in a record/object?}
  4557. derefn, {Dereferences a pointer.}
  4558. addrn, {Represents the @ operator.}
  4559. doubleaddrn, {Represents the @@ operator.}
  4560. ordconstn, {Represents an ordinal value.}
  4561. typeconvn, {Represents type-conversion/typecast.}
  4562. calln, {Represents a call node.}
  4563. callparan, {Represents a parameter.}
  4564. realconstn, {Represents a real value.}
  4565. fixconstn, {Represents a fixed value.}
  4566. umminusn, {Represents a sign change (i.e. -2).}
  4567. asmn, {Represents an assembler node }
  4568. vecn, {Represents array indexing.}
  4569. stringconstn, {Represents a string constant.}
  4570. funcretn, {Represents the function result var.}
  4571. selfn, {Represents the self parameter.}
  4572. notn, {Represents the not operator.}
  4573. inlinen, {Internal procedures (i.e. writeln).}
  4574. niln, {Represents the nil pointer.}
  4575. errorn, {This part of the tree could not be
  4576. parsed because of a compiler error.}
  4577. typen, {A type name. Used for i.e. typeof(obj).}
  4578. hnewn, {The new operation, constructor call.}
  4579. hdisposen, {The dispose operation with destructor call.}
  4580. newn, {The new operation, constructor call.}
  4581. simpledisposen, {The dispose operation.}
  4582. setelen, {A set element (i.e. [a,b]).}
  4583. setconstrn, {A set constant (i.e. [1,2]).}
  4584. blockn, {A block of statements.}
  4585. statementn, {One statement in list of nodes.}
  4586. loopn, { used in genloopnode, must be converted }
  4587. ifn, {An if statement.}
  4588. breakn, {A break statement.}
  4589. continuen, {A continue statement.}
  4590. repeatn, {A repeat until block.}
  4591. whilen, {A while do statement.}
  4592. forn, {A for loop.}
  4593. exitn, {An exit statement.}
  4594. withn, {A with statement.}
  4595. casen, {A case statement.}
  4596. labeln, {A label.}
  4597. goton, {A goto statement.}
  4598. simplenewn, {The new operation.}
  4599. tryexceptn, {A try except block.}
  4600. raisen, {A raise statement.}
  4601. switchesn, {??? Currently unused...}
  4602. tryfinallyn, {A try finally statement.}
  4603. isn, {Represents the is operator.}
  4604. asn, {Represents the as typecast.}
  4605. caretn, {Represents the ^ operator.}
  4606. failn, {Represents the fail statement.}
  4607. starstarn, {Represents the ** operator exponentiation }
  4608. procinlinen, {Procedures that can be inlined }
  4609. { added for optimizations where we cannot suppress }
  4610. nothingn,
  4611. loadvmtn); {???.} *)
  4612. const
  4613. procedures : array[ttreetyp] of firstpassproc =
  4614. (firstadd,firstadd,firstadd,firstmoddiv,firstadd,
  4615. firstmoddiv,firstassignment,firstload,firstrange,
  4616. firstadd,firstadd,firstadd,firstadd,
  4617. firstadd,firstadd,firstin,firstadd,
  4618. firstadd,firstshlshr,firstshlshr,firstadd,
  4619. firstadd,firstsubscriptn,firstderef,firstaddr,firstdoubleaddr,
  4620. firstordconst,firsttypeconv,firstcalln,firstnothing,
  4621. firstrealconst,firstfixconst,firstumminus,firstasm,firstvecn,
  4622. firststringconst,firstfuncret,firstselfn,
  4623. firstnot,firstinline,firstniln,firsterror,
  4624. firsttypen,firsthnewn,firsthdisposen,firstnewn,
  4625. firstsimplenewdispose,firstnothing,firstsetcons,firstblock,
  4626. firststatement,firstnothing,firstif,firstnothing,
  4627. firstnothing,first_while_repeat,first_while_repeat,firstfor,
  4628. firstexitn,firstwith,firstcase,firstlabel,
  4629. firstgoto,firstsimplenewdispose,firsttryexcept,firstraise,
  4630. firstnothing,firsttryfinally,firstis,firstas,firstadd,
  4631. firstnothing,firstadd,firstprocinline,firstnothing,firstloadvmt);
  4632. var
  4633. oldcodegenerror : boolean;
  4634. oldswitches : Tcswitches;
  4635. oldpos : tfileposinfo;
  4636. {$ifdef extdebug}
  4637. str1,str2 : string;
  4638. oldp : ptree;
  4639. not_first : boolean;
  4640. {$endif extdebug}
  4641. begin
  4642. {$ifdef extdebug}
  4643. if (p^.firstpasscount>0) and only_one_pass then
  4644. exit;
  4645. {$endif extdebug}
  4646. oldcodegenerror:=codegenerror;
  4647. oldpos:=aktfilepos;
  4648. oldswitches:=aktswitches;
  4649. {$ifdef extdebug}
  4650. if p^.firstpasscount>0 then
  4651. begin
  4652. move(p^,str1[1],sizeof(ttree));
  4653. str1[0]:=char(sizeof(ttree));
  4654. new(oldp);
  4655. oldp^:=p^;
  4656. not_first:=true;
  4657. end
  4658. else
  4659. not_first:=false;
  4660. {$endif extdebug}
  4661. aktfilepos:=p^.fileinfo;
  4662. aktswitches:=p^.pragmas;
  4663. if not p^.error then
  4664. begin
  4665. codegenerror:=false;
  4666. procedures[p^.treetype](p);
  4667. p^.error:=codegenerror;
  4668. codegenerror:=codegenerror or oldcodegenerror;
  4669. end
  4670. else
  4671. codegenerror:=true;
  4672. {$ifdef extdebug}
  4673. if not_first then
  4674. begin
  4675. { dirty trick to compare two ttree's (PM) }
  4676. move(p^,str2[1],sizeof(ttree));
  4677. str2[0]:=char(sizeof(ttree));
  4678. if str1<>str2 then
  4679. begin
  4680. comment(v_debug,'tree changed after first counting pass '
  4681. +tostr(longint(p^.treetype)));
  4682. compare_trees(oldp,p);
  4683. end;
  4684. dispose(oldp);
  4685. end;
  4686. if count_ref then
  4687. inc(p^.firstpasscount);
  4688. {$endif extdebug}
  4689. aktswitches:=oldswitches;
  4690. aktfilepos:=oldpos;
  4691. end;
  4692. function do_firstpass(var p : ptree) : boolean;
  4693. begin
  4694. codegenerror:=false;
  4695. firstpass(p);
  4696. do_firstpass:=codegenerror;
  4697. end;
  4698. { to be called only for a whole function }
  4699. { to insert code at entry and exit }
  4700. function function_firstpass(var p : ptree) : boolean;
  4701. begin
  4702. codegenerror:=false;
  4703. firstpass(p);
  4704. function_firstpass:=codegenerror;
  4705. end;
  4706. end.
  4707. {
  4708. $Log$
  4709. Revision 1.46 1998-07-28 21:52:52 florian
  4710. + implementation of raise and try..finally
  4711. + some misc. exception stuff
  4712. Revision 1.45 1998/07/26 21:58:59 florian
  4713. + better support for switch $H
  4714. + index access to ansi strings added
  4715. + assigment of data (records/arrays) containing ansi strings
  4716. Revision 1.44 1998/07/24 22:16:59 florian
  4717. * internal error 10 together with array access fixed. I hope
  4718. that's the final fix.
  4719. Revision 1.43 1998/07/20 18:40:14 florian
  4720. * handling of ansi string constants should now work
  4721. Revision 1.42 1998/07/20 10:23:01 florian
  4722. * better ansi string assignement
  4723. Revision 1.41 1998/07/18 22:54:27 florian
  4724. * some ansi/wide/longstring support fixed:
  4725. o parameter passing
  4726. o returning as result from functions
  4727. Revision 1.40 1998/07/18 17:11:09 florian
  4728. + ansi string constants fixed
  4729. + switch $H partial implemented
  4730. Revision 1.39 1998/07/14 21:46:47 peter
  4731. * updated messages file
  4732. Revision 1.38 1998/07/14 14:46:50 peter
  4733. * released NEWINPUT
  4734. Revision 1.37 1998/07/07 12:31:44 peter
  4735. * fixed string:= which allowed almost any type
  4736. Revision 1.36 1998/07/07 11:20:00 peter
  4737. + NEWINPUT for a better inputfile and scanner object
  4738. Revision 1.35 1998/06/25 14:04:19 peter
  4739. + internal inc/dec
  4740. Revision 1.34 1998/06/25 08:48:14 florian
  4741. * first version of rtti support
  4742. Revision 1.33 1998/06/16 08:56:24 peter
  4743. + targetcpu
  4744. * cleaner pmodules for newppu
  4745. Revision 1.32 1998/06/14 18:23:57 peter
  4746. * fixed xor bug (from mailinglist)
  4747. Revision 1.31 1998/06/13 00:10:09 peter
  4748. * working browser and newppu
  4749. * some small fixes against crashes which occured in bp7 (but not in
  4750. fpc?!)
  4751. Revision 1.30 1998/06/12 10:32:28 pierre
  4752. * column problem hopefully solved
  4753. + C vars declaration changed
  4754. Revision 1.29 1998/06/09 16:01:44 pierre
  4755. + added procedure directive parsing for procvars
  4756. (accepted are popstack cdecl and pascal)
  4757. + added C vars with the following syntax
  4758. var C calias 'true_c_name';(can be followed by external)
  4759. reason is that you must add the Cprefix
  4760. which is target dependent
  4761. Revision 1.28 1998/06/05 14:37:29 pierre
  4762. * fixes for inline for operators
  4763. * inline procedure more correctly restricted
  4764. Revision 1.27 1998/06/05 00:01:06 florian
  4765. * bugs with assigning related objects and passing objects by reference
  4766. to a procedure
  4767. Revision 1.26 1998/06/04 09:55:39 pierre
  4768. * demangled name of procsym reworked to become independant
  4769. of the mangling scheme
  4770. Revision 1.25 1998/06/03 22:48:57 peter
  4771. + wordbool,longbool
  4772. * rename bis,von -> high,low
  4773. * moved some systemunit loading/creating to psystem.pas
  4774. Revision 1.24 1998/06/02 17:03:01 pierre
  4775. * with node corrected for objects
  4776. * small bugs for SUPPORT_MMX fixed
  4777. Revision 1.23 1998/06/01 16:50:20 peter
  4778. + boolean -> ord conversion
  4779. * fixed ord -> boolean conversion
  4780. Revision 1.22 1998/05/28 17:26:49 peter
  4781. * fixed -R switch, it didn't work after my previous akt/init patch
  4782. * fixed bugs 110,130,136
  4783. Revision 1.21 1998/05/25 17:11:41 pierre
  4784. * firstpasscount bug fixed
  4785. now all is already set correctly the first time
  4786. under EXTDEBUG try -gp to skip all other firstpasses
  4787. it works !!
  4788. * small bug fixes
  4789. - for smallsets with -dTESTSMALLSET
  4790. - some warnings removed (by correcting code !)
  4791. Revision 1.20 1998/05/23 01:21:17 peter
  4792. + aktasmmode, aktoptprocessor, aktoutputformat
  4793. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  4794. + $LIBNAME to set the library name where the unit will be put in
  4795. * splitted cgi386 a bit (codeseg to large for bp7)
  4796. * nasm, tasm works again. nasm moved to ag386nsm.pas
  4797. Revision 1.19 1998/05/20 09:42:34 pierre
  4798. + UseTokenInfo now default
  4799. * unit in interface uses and implementation uses gives error now
  4800. * only one error for unknown symbol (uses lastsymknown boolean)
  4801. the problem came from the label code !
  4802. + first inlined procedures and function work
  4803. (warning there might be allowed cases were the result is still wrong !!)
  4804. * UseBrower updated gives a global list of all position of all used symbols
  4805. with switch -gb
  4806. Revision 1.18 1998/05/11 13:07:55 peter
  4807. + $ifdef NEWPPU for the new ppuformat
  4808. + $define GDB not longer required
  4809. * removed all warnings and stripped some log comments
  4810. * no findfirst/findnext anymore to remove smartlink *.o files
  4811. Revision 1.17 1998/05/06 08:38:43 pierre
  4812. * better position info with UseTokenInfo
  4813. UseTokenInfo greatly simplified
  4814. + added check for changed tree after first time firstpass
  4815. (if we could remove all the cases were it happen
  4816. we could skip all firstpass if firstpasscount > 1)
  4817. Only with ExtDebug
  4818. Revision 1.16 1998/05/01 16:38:45 florian
  4819. * handling of private and protected fixed
  4820. + change_keywords_to_tp implemented to remove
  4821. keywords which aren't supported by tp
  4822. * break and continue are now symbols of the system unit
  4823. + widestring, longstring and ansistring type released
  4824. Revision 1.15 1998/05/01 09:01:23 florian
  4825. + correct semantics of private and protected
  4826. * small fix in variable scope:
  4827. a id can be used in a parameter list of a method, even it is used in
  4828. an anchestor class as field id
  4829. Revision 1.14 1998/04/30 15:59:41 pierre
  4830. * GDB works again better :
  4831. correct type info in one pass
  4832. + UseTokenInfo for better source position
  4833. * fixed one remaining bug in scanner for line counts
  4834. * several little fixes
  4835. Revision 1.13 1998/04/29 10:33:56 pierre
  4836. + added some code for ansistring (not complete nor working yet)
  4837. * corrected operator overloading
  4838. * corrected nasm output
  4839. + started inline procedures
  4840. + added starstarn : use ** for exponentiation (^ gave problems)
  4841. + started UseTokenInfo cond to get accurate positions
  4842. Revision 1.12 1998/04/22 21:06:50 florian
  4843. * last fixes before the release:
  4844. - veryyyy slow firstcall fixed
  4845. Revision 1.11 1998/04/21 10:16:48 peter
  4846. * patches from strasbourg
  4847. * objects is not used anymore in the fpc compiled version
  4848. Revision 1.10 1998/04/14 23:27:03 florian
  4849. + exclude/include with constant second parameter added
  4850. Revision 1.9 1998/04/13 21:15:42 florian
  4851. * error handling of pass_1 and cgi386 fixed
  4852. * the following bugs fixed: 0117, 0118, 0119 and 0129, 0122 was already
  4853. fixed, verified
  4854. Revision 1.8 1998/04/13 08:42:52 florian
  4855. * call by reference and call by value open arrays fixed
  4856. Revision 1.7 1998/04/12 22:39:44 florian
  4857. * problem with read access to properties solved
  4858. * correct handling of hidding methods via virtual (COM)
  4859. * correct result type of constructor calls (COM), the resulttype
  4860. depends now on the type of the class reference
  4861. Revision 1.6 1998/04/09 22:16:34 florian
  4862. * problem with previous REGALLOC solved
  4863. * improved property support
  4864. Revision 1.5 1998/04/08 16:58:04 pierre
  4865. * several bugfixes
  4866. ADD ADC and AND are also sign extended
  4867. nasm output OK (program still crashes at end
  4868. and creates wrong assembler files !!)
  4869. procsym types sym in tdef removed !!
  4870. Revision 1.4 1998/04/07 22:45:04 florian
  4871. * bug0092, bug0115 and bug0121 fixed
  4872. + packed object/class/array
  4873. }