pass_1.pas 195 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237
  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 (p^.left^.resulttype^.deftype=stringdef) 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 equalt 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. procedure firstvecn(var p : ptree);
  1816. var
  1817. harr : pdef;
  1818. ct : tconverttype;
  1819. begin
  1820. firstpass(p^.left);
  1821. firstpass(p^.right);
  1822. if codegenerror then
  1823. exit;
  1824. { range check only for arrays }
  1825. if (p^.left^.resulttype^.deftype=arraydef) then
  1826. begin
  1827. if not(isconvertable(p^.right^.resulttype,
  1828. parraydef(p^.left^.resulttype)^.rangedef,
  1829. ct,ordconstn,false)) and
  1830. not(is_equal(p^.right^.resulttype,
  1831. parraydef(p^.left^.resulttype)^.rangedef)) then
  1832. Message(sym_e_type_mismatch);
  1833. end;
  1834. { Never convert a boolean or a char !}
  1835. { maybe type conversion }
  1836. if (p^.right^.resulttype^.deftype<>enumdef) and
  1837. not ((p^.right^.resulttype^.deftype=orddef) and
  1838. (Porddef(p^.right^.resulttype)^.typ in [bool8bit,bool16bit,bool32bit,uchar])) then
  1839. begin
  1840. p^.right:=gentypeconvnode(p^.right,s32bitdef);
  1841. { once more firstpass }
  1842. {?? It's better to only firstpass when the tree has
  1843. changed, isn't it ?}
  1844. firstpass(p^.right);
  1845. end;
  1846. if codegenerror then
  1847. exit;
  1848. { determine return type }
  1849. if not assigned(p^.resulttype) then
  1850. if p^.left^.resulttype^.deftype=arraydef then
  1851. p^.resulttype:=parraydef(p^.left^.resulttype)^.definition
  1852. else if (p^.left^.resulttype^.deftype=pointerdef) then
  1853. begin
  1854. { convert pointer to array }
  1855. harr:=new(parraydef,init(0,$7fffffff,s32bitdef));
  1856. parraydef(harr)^.definition:=ppointerdef(p^.left^.resulttype)^.definition;
  1857. p^.left:=gentypeconvnode(p^.left,harr);
  1858. firstpass(p^.left);
  1859. if codegenerror then
  1860. exit;
  1861. p^.resulttype:=parraydef(harr)^.definition
  1862. end
  1863. else
  1864. { indexed access to arrays }
  1865. p^.resulttype:=cchardef;
  1866. { the register calculation is easy if a const index is used }
  1867. if p^.right^.treetype=ordconstn then
  1868. p^.registers32:=p^.left^.registers32
  1869. else
  1870. begin
  1871. p^.registers32:=max(p^.left^.registers32,p^.right^.registers32);
  1872. { not correct, but what works better ? }
  1873. if p^.left^.registers32>0 then
  1874. p^.registers32:=max(p^.registers32,2)
  1875. else
  1876. { min. one register }
  1877. p^.registers32:=max(p^.registers32,1);
  1878. end;
  1879. p^.registersfpu:=max(p^.left^.registersfpu,p^.right^.registersfpu);
  1880. {$ifdef SUPPORT_MMX}
  1881. p^.registersmmx:=max(p^.left^.registersmmx,p^.right^.registersmmx);
  1882. {$endif SUPPORT_MMX}
  1883. p^.location.loc:=p^.left^.location.loc;
  1884. end;
  1885. type
  1886. tfirstconvproc = procedure(var p : ptree);
  1887. procedure first_bigger_smaller(var p : ptree);
  1888. begin
  1889. if (p^.left^.location.loc<>LOC_REGISTER) and (p^.registers32=0) then
  1890. p^.registers32:=1;
  1891. p^.location.loc:=LOC_REGISTER;
  1892. end;
  1893. procedure first_cstring_charpointer(var p : ptree);
  1894. begin
  1895. p^.registers32:=1;
  1896. p^.location.loc:=LOC_REGISTER;
  1897. end;
  1898. procedure first_string_chararray(var p : ptree);
  1899. begin
  1900. p^.registers32:=1;
  1901. p^.location.loc:=LOC_REGISTER;
  1902. end;
  1903. procedure first_string_string(var p : ptree);
  1904. begin
  1905. if pstringdef(p^.resulttype)^.string_typ<>
  1906. pstringdef(p^.left^.resulttype)^.string_typ then
  1907. begin
  1908. { trick: secondstringconstn generates a const entry depending on }
  1909. { the result type }
  1910. if p^.left^.treetype=stringconstn then
  1911. p^.left^.resulttype:=p^.resulttype
  1912. else
  1913. procinfo.flags:=procinfo.flags or pi_do_call;
  1914. end;
  1915. { for simplicity lets first keep all ansistrings
  1916. as LOC_MEM, could also become LOC_REGISTER }
  1917. p^.location.loc:=LOC_MEM;
  1918. end;
  1919. procedure first_char_to_string(var p : ptree);
  1920. var
  1921. hp : ptree;
  1922. begin
  1923. if p^.left^.treetype=ordconstn then
  1924. begin
  1925. hp:=genstringconstnode(chr(p^.left^.value));
  1926. firstpass(hp);
  1927. disposetree(p);
  1928. p:=hp;
  1929. end
  1930. else
  1931. p^.location.loc:=LOC_MEM;
  1932. end;
  1933. procedure first_nothing(var p : ptree);
  1934. begin
  1935. p^.location.loc:=LOC_MEM;
  1936. end;
  1937. procedure first_array_to_pointer(var p : ptree);
  1938. begin
  1939. if p^.registers32<1 then
  1940. p^.registers32:=1;
  1941. p^.location.loc:=LOC_REGISTER;
  1942. end;
  1943. procedure first_int_real(var p : ptree);
  1944. var t : ptree;
  1945. begin
  1946. if p^.left^.treetype=ordconstn then
  1947. begin
  1948. { convert constants direct }
  1949. { not because of type conversion }
  1950. t:=genrealconstnode(p^.left^.value);
  1951. { do a first pass here
  1952. because firstpass of typeconv does
  1953. not redo it for left field !! }
  1954. firstpass(t);
  1955. { the type can be something else than s64real !!}
  1956. t:=gentypeconvnode(t,p^.resulttype);
  1957. firstpass(t);
  1958. disposetree(p);
  1959. p:=t;
  1960. exit;
  1961. end
  1962. else
  1963. begin
  1964. if p^.registersfpu<1 then
  1965. p^.registersfpu:=1;
  1966. p^.location.loc:=LOC_FPU;
  1967. end;
  1968. end;
  1969. procedure first_int_fix(var p : ptree);
  1970. begin
  1971. if p^.left^.treetype=ordconstn then
  1972. begin
  1973. { convert constants direct }
  1974. p^.treetype:=fixconstn;
  1975. p^.valuef:=p^.left^.value shl 16;
  1976. p^.disposetyp:=dt_nothing;
  1977. disposetree(p^.left);
  1978. p^.location.loc:=LOC_MEM;
  1979. end
  1980. else
  1981. begin
  1982. if p^.registers32<1 then
  1983. p^.registers32:=1;
  1984. p^.location.loc:=LOC_REGISTER;
  1985. end;
  1986. end;
  1987. procedure first_real_fix(var p : ptree);
  1988. begin
  1989. if p^.left^.treetype=realconstn then
  1990. begin
  1991. { convert constants direct }
  1992. p^.treetype:=fixconstn;
  1993. p^.valuef:=round(p^.left^.valued*65536);
  1994. p^.disposetyp:=dt_nothing;
  1995. disposetree(p^.left);
  1996. p^.location.loc:=LOC_MEM;
  1997. end
  1998. else
  1999. begin
  2000. { at least one fpu and int register needed }
  2001. if p^.registers32<1 then
  2002. p^.registers32:=1;
  2003. if p^.registersfpu<1 then
  2004. p^.registersfpu:=1;
  2005. p^.location.loc:=LOC_REGISTER;
  2006. end;
  2007. end;
  2008. procedure first_fix_real(var p : ptree);
  2009. begin
  2010. if p^.left^.treetype=fixconstn then
  2011. begin
  2012. { convert constants direct }
  2013. p^.treetype:=realconstn;
  2014. p^.valued:=round(p^.left^.valuef/65536.0);
  2015. p^.disposetyp:=dt_nothing;
  2016. disposetree(p^.left);
  2017. p^.location.loc:=LOC_MEM;
  2018. end
  2019. else
  2020. begin
  2021. if p^.registersfpu<1 then
  2022. p^.registersfpu:=1;
  2023. p^.location.loc:=LOC_FPU;
  2024. end;
  2025. end;
  2026. procedure first_real_real(var p : ptree);
  2027. begin
  2028. if p^.registersfpu<1 then
  2029. p^.registersfpu:=1;
  2030. p^.location.loc:=LOC_FPU;
  2031. end;
  2032. procedure first_pointer_to_array(var p : ptree);
  2033. begin
  2034. if p^.registers32<1 then
  2035. p^.registers32:=1;
  2036. p^.location.loc:=LOC_REFERENCE;
  2037. end;
  2038. procedure first_chararray_string(var p : ptree);
  2039. begin
  2040. { the only important information is the location of the }
  2041. { result }
  2042. { other stuff is done by firsttypeconv }
  2043. p^.location.loc:=LOC_MEM;
  2044. end;
  2045. procedure first_cchar_charpointer(var p : ptree);
  2046. begin
  2047. p^.left:=gentypeconvnode(p^.left,cstringdef);
  2048. { convert constant char to constant string }
  2049. firstpass(p^.left);
  2050. { evalute tree }
  2051. firstpass(p);
  2052. end;
  2053. procedure first_locmem(var p : ptree);
  2054. begin
  2055. p^.location.loc:=LOC_MEM;
  2056. end;
  2057. procedure first_bool_int(var p : ptree);
  2058. begin
  2059. p^.location.loc:=LOC_REGISTER;
  2060. { Florian I think this is overestimated
  2061. but I still do not really understand how to get this right (PM) }
  2062. { Hmmm, I think we need only one reg to return the result of }
  2063. { this node => so }
  2064. if p^.registers32<1 then
  2065. p^.registers32:=1;
  2066. { should work (FK)
  2067. p^.registers32:=p^.left^.registers32+1;}
  2068. end;
  2069. procedure first_int_bool(var p : ptree);
  2070. begin
  2071. p^.location.loc:=LOC_REGISTER;
  2072. { Florian I think this is overestimated
  2073. but I still do not really understand how to get this right (PM) }
  2074. { Hmmm, I think we need only one reg to return the result of }
  2075. { this node => so }
  2076. p^.left:=gentypeconvnode(p^.left,s32bitdef);
  2077. firstpass(p^.left);
  2078. if p^.registers32<1 then
  2079. p^.registers32:=1;
  2080. { p^.resulttype:=booldef; }
  2081. { should work (FK)
  2082. p^.registers32:=p^.left^.registers32+1;}
  2083. end;
  2084. procedure first_proc_to_procvar(var p : ptree);
  2085. begin
  2086. firstpass(p^.left);
  2087. if codegenerror then
  2088. exit;
  2089. if (p^.left^.location.loc<>LOC_REFERENCE) then
  2090. Message(cg_e_illegal_expression);
  2091. p^.registers32:=p^.left^.registers32;
  2092. if p^.registers32<1 then
  2093. p^.registers32:=1;
  2094. p^.location.loc:=LOC_REGISTER;
  2095. end;
  2096. function is_procsym_load(p:Ptree):boolean;
  2097. begin
  2098. is_procsym_load:=((p^.treetype=loadn) and (p^.symtableentry^.typ=procsym)) or
  2099. ((p^.treetype=addrn) and (p^.left^.treetype=loadn)
  2100. and (p^.left^.symtableentry^.typ=procsym)) ;
  2101. end;
  2102. { change a proc call to a procload for assignment to a procvar }
  2103. { this can only happen for proc/function without arguments }
  2104. function is_procsym_call(p:Ptree):boolean;
  2105. begin
  2106. is_procsym_call:=(p^.treetype=calln) and (p^.left=nil) and
  2107. (((p^.symtableprocentry^.typ=procsym) and (p^.right=nil)) or
  2108. ((p^.right<>nil) and (p^.right^.symtableprocentry^.typ=varsym)));
  2109. end;
  2110. {***}
  2111. function is_assignment_overloaded(from_def,to_def : pdef) : boolean;
  2112. var
  2113. passproc : pprocdef;
  2114. convtyp : tconverttype;
  2115. begin
  2116. is_assignment_overloaded:=false;
  2117. if assigned(overloaded_operators[assignment]) then
  2118. passproc:=overloaded_operators[assignment]^.definition
  2119. else
  2120. exit;
  2121. while passproc<>nil do
  2122. begin
  2123. if is_equal(passproc^.retdef,to_def) and
  2124. isconvertable(from_def,passproc^.para1^.data,convtyp,
  2125. ordconstn { nur Dummy},false ) then
  2126. begin
  2127. is_assignment_overloaded:=true;
  2128. break;
  2129. end;
  2130. passproc:=passproc^.nextoverloaded;
  2131. end;
  2132. end;
  2133. { Attention: do *** no *** recursive call of firstpass }
  2134. { because the child tree is always passed }
  2135. procedure firsttypeconv(var p : ptree);
  2136. var
  2137. hp : ptree;
  2138. aprocdef : pprocdef;
  2139. proctype : tdeftype;
  2140. const
  2141. firstconvert : array[tc_u8bit_2_s32bit..tc_cchar_charpointer] of
  2142. tfirstconvproc = (first_bigger_smaller,first_nothing,first_bigger_smaller,
  2143. first_bigger_smaller,first_bigger_smaller,
  2144. first_bigger_smaller,first_bigger_smaller,
  2145. first_bigger_smaller,first_string_string,
  2146. first_cstring_charpointer,first_string_chararray,
  2147. first_array_to_pointer,first_pointer_to_array,
  2148. first_char_to_string,first_bigger_smaller,
  2149. first_bigger_smaller,first_bigger_smaller,
  2150. first_bigger_smaller,first_bigger_smaller,
  2151. first_bigger_smaller,first_bigger_smaller,
  2152. first_bigger_smaller,first_bigger_smaller,
  2153. first_bigger_smaller,first_bigger_smaller,
  2154. first_bigger_smaller,first_bigger_smaller,
  2155. first_bigger_smaller,first_bigger_smaller,
  2156. first_bigger_smaller,first_bigger_smaller,
  2157. first_bigger_smaller,first_bigger_smaller,
  2158. first_bool_int,first_int_bool,
  2159. first_int_real,first_real_fix,
  2160. first_fix_real,first_int_fix,first_real_real,
  2161. first_locmem,first_proc_to_procvar,
  2162. first_cchar_charpointer);
  2163. begin
  2164. aprocdef:=nil;
  2165. { if explicite type conversation, then run firstpass }
  2166. if p^.explizit then
  2167. firstpass(p^.left);
  2168. if codegenerror then
  2169. begin
  2170. p^.resulttype:=generrordef;
  2171. exit;
  2172. end;
  2173. if not assigned(p^.left^.resulttype) then
  2174. begin
  2175. codegenerror:=true;
  2176. internalerror(52349);
  2177. exit;
  2178. end;
  2179. { remove obsolete type conversions }
  2180. if is_equal(p^.left^.resulttype,p^.resulttype) then
  2181. begin
  2182. hp:=p;
  2183. p:=p^.left;
  2184. p^.resulttype:=hp^.resulttype;
  2185. putnode(hp);
  2186. exit;
  2187. end;
  2188. p^.registers32:=p^.left^.registers32;
  2189. p^.registersfpu:=p^.left^.registersfpu;
  2190. {$ifdef SUPPORT_MMX}
  2191. p^.registersmmx:=p^.left^.registersmmx;
  2192. {$endif}
  2193. set_location(p^.location,p^.left^.location);
  2194. if is_assignment_overloaded(p^.left^.resulttype,p^.resulttype) then
  2195. begin
  2196. procinfo.flags:=procinfo.flags or pi_do_call;
  2197. hp:=gencallnode(overloaded_operators[assignment],nil);
  2198. hp^.left:=gencallparanode(p^.left,nil);
  2199. putnode(p);
  2200. p:=hp;
  2201. firstpass(p);
  2202. exit;
  2203. end;
  2204. if (not(isconvertable(p^.left^.resulttype,p^.resulttype,
  2205. p^.convtyp,p^.left^.treetype,p^.explizit))) then
  2206. begin
  2207. {Procedures have a resulttype of voiddef and functions of their
  2208. own resulttype. They will therefore always be incompatible with
  2209. a procvar. Because isconvertable cannot check for procedures we
  2210. use an extra check for them.}
  2211. if (cs_tp_compatible in aktswitches) and
  2212. ((is_procsym_load(p^.left) or is_procsym_call(p^.left)) and
  2213. (p^.resulttype^.deftype=procvardef)) then
  2214. begin
  2215. { just a test: p^.explizit:=false; }
  2216. if is_procsym_call(p^.left) then
  2217. begin
  2218. if p^.left^.right=nil then
  2219. begin
  2220. p^.left^.treetype:=loadn;
  2221. { are at same offset so this could be spared, but
  2222. it more secure to do it anyway }
  2223. p^.left^.symtableentry:=p^.left^.symtableprocentry;
  2224. p^.left^.resulttype:=pprocsym(p^.left^.symtableentry)^.definition;
  2225. aprocdef:=pprocdef(p^.left^.resulttype);
  2226. end
  2227. else
  2228. begin
  2229. p^.left^.right^.treetype:=loadn;
  2230. p^.left^.right^.symtableentry:=p^.left^.right^.symtableentry;
  2231. P^.left^.right^.resulttype:=pvarsym(p^.left^.symtableentry)^.definition;
  2232. hp:=p^.left^.right;
  2233. putnode(p^.left);
  2234. p^.left:=hp;
  2235. { should we do that ? }
  2236. firstpass(p^.left);
  2237. if not is_equal(p^.left^.resulttype,p^.resulttype) then
  2238. begin
  2239. Message(sym_e_type_mismatch);
  2240. exit;
  2241. end
  2242. else
  2243. begin
  2244. hp:=p;
  2245. p:=p^.left;
  2246. p^.resulttype:=hp^.resulttype;
  2247. putnode(hp);
  2248. exit;
  2249. end;
  2250. end;
  2251. end
  2252. else
  2253. begin
  2254. if p^.left^.treetype=addrn then
  2255. begin
  2256. hp:=p^.left;
  2257. p^.left:=p^.left^.left;
  2258. putnode(p^.left);
  2259. end
  2260. else
  2261. aprocdef:=pprocsym(p^.left^.symtableentry)^.definition;
  2262. end;
  2263. p^.convtyp:=tc_proc2procvar;
  2264. { Now check if the procedure we are going to assign to
  2265. the procvar, is compatible with the procvar's type.
  2266. Did the original procvar support do such a check?
  2267. I can't find any.}
  2268. { answer : is_equal works for procvardefs !! }
  2269. { but both must be procvardefs, so we cheet little }
  2270. if assigned(aprocdef) then
  2271. begin
  2272. proctype:=aprocdef^.deftype;
  2273. aprocdef^.deftype:=procvardef;
  2274. if not is_equal(aprocdef,p^.resulttype) then
  2275. begin
  2276. aprocdef^.deftype:=proctype;
  2277. Message(sym_e_type_mismatch);
  2278. end;
  2279. aprocdef^.deftype:=proctype;
  2280. firstconvert[p^.convtyp](p);
  2281. end
  2282. else
  2283. Message(sym_e_type_mismatch);
  2284. exit;
  2285. end
  2286. else
  2287. begin
  2288. if p^.explizit then
  2289. begin
  2290. { boolean to byte are special because the
  2291. location can be different }
  2292. if (p^.resulttype^.deftype=orddef) and
  2293. (porddef(p^.resulttype)^.typ=u8bit) and
  2294. (p^.left^.resulttype^.deftype=orddef) and
  2295. (porddef(p^.left^.resulttype)^.typ=bool8bit) then
  2296. begin
  2297. p^.convtyp:=tc_bool_2_int;
  2298. firstconvert[p^.convtyp](p);
  2299. exit;
  2300. end;
  2301. { normal tc_equal-Konvertierung durchf�hren }
  2302. p^.convtyp:=tc_equal;
  2303. { wenn Aufz„hltyp nach Ordinal konvertiert werden soll }
  2304. { dann Aufz„hltyp=s32bit }
  2305. if (p^.left^.resulttype^.deftype=enumdef) and
  2306. is_ordinal(p^.resulttype) then
  2307. begin
  2308. if p^.left^.treetype=ordconstn then
  2309. begin
  2310. hp:=genordinalconstnode(p^.left^.value,p^.resulttype);
  2311. disposetree(p);
  2312. firstpass(hp);
  2313. p:=hp;
  2314. exit;
  2315. end
  2316. else
  2317. begin
  2318. if not isconvertable(s32bitdef,p^.resulttype,p^.convtyp,
  2319. ordconstn { nur Dummy},false ) then
  2320. Message(cg_e_illegal_type_conversion);
  2321. end;
  2322. end
  2323. { ordinal to enumeration }
  2324. else
  2325. if (p^.resulttype^.deftype=enumdef) and
  2326. is_ordinal(p^.left^.resulttype) then
  2327. begin
  2328. if p^.left^.treetype=ordconstn then
  2329. begin
  2330. hp:=genordinalconstnode(p^.left^.value,p^.resulttype);
  2331. disposetree(p);
  2332. firstpass(hp);
  2333. p:=hp;
  2334. exit;
  2335. end
  2336. else
  2337. begin
  2338. if not isconvertable(p^.left^.resulttype,s32bitdef,p^.convtyp,
  2339. ordconstn { nur Dummy},false ) then
  2340. Message(cg_e_illegal_type_conversion);
  2341. end;
  2342. end
  2343. {Are we typecasting an ordconst to a char?}
  2344. else
  2345. if is_equal(p^.resulttype,cchardef) and
  2346. is_ordinal(p^.left^.resulttype) then
  2347. begin
  2348. if p^.left^.treetype=ordconstn then
  2349. begin
  2350. hp:=genordinalconstnode(p^.left^.value,p^.resulttype);
  2351. firstpass(hp);
  2352. disposetree(p);
  2353. p:=hp;
  2354. exit;
  2355. end
  2356. else
  2357. begin
  2358. { this is wrong because it converts to a 4 byte long var !!
  2359. if not isconvertable(p^.left^.resulttype,s32bitdef,p^.convtyp,ordconstn nur Dummy ) then }
  2360. if not isconvertable(p^.left^.resulttype,u8bitdef,
  2361. p^.convtyp,ordconstn { nur Dummy},false ) then
  2362. Message(cg_e_illegal_type_conversion);
  2363. end;
  2364. end
  2365. { only if the same size or formal def }
  2366. { why do we allow typecasting of voiddef ?? (PM) }
  2367. else
  2368. if not(
  2369. (p^.left^.resulttype^.deftype=formaldef) or
  2370. (p^.left^.resulttype^.size=p^.resulttype^.size) or
  2371. (is_equal(p^.left^.resulttype,voiddef) and
  2372. (p^.left^.treetype=derefn))
  2373. ) then
  2374. Message(cg_e_illegal_type_conversion);
  2375. { the conversion into a strutured type is only }
  2376. { possible, if the source is no register }
  2377. if (p^.resulttype^.deftype in [recorddef,stringdef,arraydef,objectdef]) and
  2378. (p^.left^.location.loc in [LOC_REGISTER,LOC_CREGISTER]) and
  2379. {it also works if the assignment is overloaded }
  2380. not is_assignment_overloaded(p^.left^.resulttype,p^.resulttype) then
  2381. Message(cg_e_illegal_type_conversion);
  2382. end
  2383. else
  2384. Message(sym_e_type_mismatch);
  2385. end
  2386. end
  2387. else
  2388. begin
  2389. { just a test: p^.explizit:=false; }
  2390. { ordinale contants are direct converted }
  2391. if (p^.left^.treetype=ordconstn) and is_ordinal(p^.resulttype) then
  2392. begin
  2393. { perform range checking }
  2394. if not(p^.explizit and (cs_tp_compatible in aktswitches)) then
  2395. testrange(p^.resulttype,p^.left^.value);
  2396. hp:=genordinalconstnode(p^.left^.value,p^.resulttype);
  2397. disposetree(p);
  2398. firstpass(hp);
  2399. p:=hp;
  2400. exit;
  2401. end;
  2402. if p^.convtyp<>tc_equal then
  2403. firstconvert[p^.convtyp](p);
  2404. end;
  2405. end;
  2406. { *************** subroutine handling **************** }
  2407. { protected field handling
  2408. protected field can not appear in
  2409. var parameters of function !!
  2410. this can only be done after we have determined the
  2411. overloaded function
  2412. this is the reason why it is not in the parser
  2413. PM }
  2414. procedure test_protected_sym(sym : psym);
  2415. begin
  2416. if ((sym^.properties and sp_protected)<>0) and
  2417. ((sym^.owner^.symtabletype=unitsymtable) or
  2418. ((sym^.owner^.symtabletype=objectsymtable) and
  2419. (pobjectdef(sym^.owner^.defowner)^.owner^.symtabletype=unitsymtable))) then
  2420. Message(parser_e_cant_access_protected_member);
  2421. end;
  2422. procedure test_protected(p : ptree);
  2423. begin
  2424. if p^.treetype=loadn then
  2425. begin
  2426. test_protected_sym(p^.symtableentry);
  2427. end
  2428. else if p^.treetype=typeconvn then
  2429. begin
  2430. test_protected(p^.left);
  2431. end
  2432. else if p^.treetype=derefn then
  2433. begin
  2434. test_protected(p^.left);
  2435. end
  2436. else if p^.treetype=subscriptn then
  2437. begin
  2438. { test_protected(p^.left);
  2439. Is a field of a protected var
  2440. also protected ??? PM }
  2441. test_protected_sym(p^.vs);
  2442. end;
  2443. end;
  2444. procedure firstcallparan(var p : ptree;defcoll : pdefcoll);
  2445. var store_valid : boolean;
  2446. convtyp : tconverttype;
  2447. begin
  2448. inc(parsing_para_level);
  2449. if assigned(p^.right) then
  2450. begin
  2451. if defcoll=nil then
  2452. firstcallparan(p^.right,nil)
  2453. else
  2454. firstcallparan(p^.right,defcoll^.next);
  2455. p^.registers32:=p^.right^.registers32;
  2456. p^.registersfpu:=p^.right^.registersfpu;
  2457. {$ifdef SUPPORT_MMX}
  2458. p^.registersmmx:=p^.right^.registersmmx;
  2459. {$endif}
  2460. end;
  2461. if defcoll=nil then
  2462. begin
  2463. { this breaks typeconversions in write !!! (PM) }
  2464. {if not(assigned(p^.resulttype)) then }
  2465. if not(assigned(p^.resulttype)) or
  2466. (p^.left^.treetype=typeconvn) then
  2467. firstpass(p^.left);
  2468. {else
  2469. exit; this broke the
  2470. value of registers32 !! }
  2471. if codegenerror then
  2472. begin
  2473. dec(parsing_para_level);
  2474. exit;
  2475. end;
  2476. p^.resulttype:=p^.left^.resulttype;
  2477. end
  2478. { if we know the routine which is called, then the type }
  2479. { conversions are inserted }
  2480. else
  2481. begin
  2482. if count_ref then
  2483. begin
  2484. store_valid:=must_be_valid;
  2485. if (defcoll^.paratyp=vs_var) then
  2486. test_protected(p^.left);
  2487. if (defcoll^.paratyp<>vs_var) then
  2488. must_be_valid:=true
  2489. else
  2490. must_be_valid:=false;
  2491. { here we must add something for the implicit type }
  2492. { conversion from array of char to pchar }
  2493. if isconvertable(p^.left^.resulttype,defcoll^.data,convtyp,
  2494. p^.left^.treetype,false) then
  2495. if convtyp=tc_array_to_pointer then
  2496. must_be_valid:=false;
  2497. firstpass(p^.left);
  2498. must_be_valid:=store_valid;
  2499. end;
  2500. if not(is_shortstring(p^.left^.resulttype) and
  2501. is_shortstring(defcoll^.data)) and
  2502. (defcoll^.data^.deftype<>formaldef) then
  2503. begin
  2504. if (defcoll^.paratyp=vs_var) and
  2505. { allows conversion from word to integer and
  2506. byte to shortint }
  2507. (not(
  2508. (p^.left^.resulttype^.deftype=orddef) and
  2509. (defcoll^.data^.deftype=orddef) and
  2510. (p^.left^.resulttype^.size=defcoll^.data^.size)
  2511. ) and
  2512. { an implicit pointer conversion is allowed }
  2513. not(
  2514. (p^.left^.resulttype^.deftype=pointerdef) and
  2515. (defcoll^.data^.deftype=pointerdef)
  2516. ) and
  2517. { child classes can be also passed }
  2518. not(
  2519. (p^.left^.resulttype^.deftype=objectdef) and
  2520. (defcoll^.data^.deftype=objectdef) and
  2521. pobjectdef(p^.left^.resulttype)^.isrelated(pobjectdef(defcoll^.data))
  2522. ) and
  2523. { an implicit file conversion is also allowed }
  2524. { from a typed file to an untyped one }
  2525. not(
  2526. (p^.left^.resulttype^.deftype=filedef) and
  2527. (defcoll^.data^.deftype=filedef) and
  2528. (pfiledef(defcoll^.data)^.filetype = ft_untyped) and
  2529. (pfiledef(p^.left^.resulttype)^.filetype = ft_typed)
  2530. ) and
  2531. not(is_equal(p^.left^.resulttype,defcoll^.data))) then
  2532. Message(parser_e_call_by_ref_without_typeconv);
  2533. { don't generate an type conversion for open arrays }
  2534. { else we loss the ranges }
  2535. if not(is_open_array(defcoll^.data)) then
  2536. begin
  2537. p^.left:=gentypeconvnode(p^.left,defcoll^.data);
  2538. firstpass(p^.left);
  2539. end;
  2540. if codegenerror then
  2541. begin
  2542. dec(parsing_para_level);
  2543. exit;
  2544. end;
  2545. end;
  2546. { check var strings }
  2547. if (cs_strict_var_strings in aktswitches) and
  2548. is_shortstring(p^.left^.resulttype) and
  2549. is_shortstring(defcoll^.data) and
  2550. (defcoll^.paratyp=vs_var) and
  2551. not(is_equal(p^.left^.resulttype,defcoll^.data)) then
  2552. Message(parser_e_strict_var_string_violation);
  2553. { Variablen, die call by reference �bergeben werden, }
  2554. { k”nnen nicht in ein Register kopiert werden }
  2555. { is this usefull here ? }
  2556. { this was missing in formal parameter list }
  2557. if defcoll^.paratyp=vs_var then
  2558. make_not_regable(p^.left);
  2559. p^.resulttype:=defcoll^.data;
  2560. end;
  2561. if p^.left^.registers32>p^.registers32 then
  2562. p^.registers32:=p^.left^.registers32;
  2563. if p^.left^.registersfpu>p^.registersfpu then
  2564. p^.registersfpu:=p^.left^.registersfpu;
  2565. {$ifdef SUPPORT_MMX}
  2566. if p^.left^.registersmmx>p^.registersmmx then
  2567. p^.registersmmx:=p^.left^.registersmmx;
  2568. {$endif SUPPORT_MMX}
  2569. dec(parsing_para_level);
  2570. end;
  2571. procedure firstcalln(var p : ptree);
  2572. type
  2573. pprocdefcoll = ^tprocdefcoll;
  2574. tprocdefcoll = record
  2575. data : pprocdef;
  2576. nextpara : pdefcoll;
  2577. firstpara : pdefcoll;
  2578. next : pprocdefcoll;
  2579. end;
  2580. var
  2581. hp,procs,hp2 : pprocdefcoll;
  2582. pd : pprocdef;
  2583. actprocsym : pprocsym;
  2584. def_from,def_to,conv_to : pdef;
  2585. pt,inlinecode : ptree;
  2586. exactmatch,inlined : boolean;
  2587. paralength,l : longint;
  2588. pdc : pdefcoll;
  2589. {$ifdef UseBrowser}
  2590. curtokenpos : tfileposinfo;
  2591. {$endif UseBrowser}
  2592. { only Dummy }
  2593. hcvt : tconverttype;
  2594. regi : tregister;
  2595. store_valid, old_count_ref : boolean;
  2596. { types.is_equal can't handle a formaldef ! }
  2597. function is_equal(def1,def2 : pdef) : boolean;
  2598. begin
  2599. { all types can be passed to a formaldef }
  2600. is_equal:=(def1^.deftype=formaldef) or
  2601. (assigned(def2) and types.is_equal(def1,def2))
  2602. { to support ansi/long/wide strings in a proper way }
  2603. { string and string[10] are assumed as equal }
  2604. { when searching the correct overloaded procedure }
  2605. or
  2606. (assigned(def1) and assigned(def2) and
  2607. (def1^.deftype=stringdef) and (def2^.deftype=stringdef) and
  2608. (pstringdef(def1)^.string_typ=pstringdef(def2)^.string_typ)
  2609. )
  2610. ;
  2611. end;
  2612. function is_in_limit(def_from,def_to : pdef) : boolean;
  2613. begin
  2614. is_in_limit:=(def_from^.deftype = orddef) and
  2615. (def_to^.deftype = orddef) and
  2616. (porddef(def_from)^.low>porddef(def_to)^.low) and
  2617. (porddef(def_from)^.high<porddef(def_to)^.high);
  2618. end;
  2619. begin
  2620. { release registers! }
  2621. { if procdefinition<>nil then we called firstpass already }
  2622. { it seems to be bad because of the registers }
  2623. { at least we can avoid the overloaded search !! }
  2624. procs:=nil;
  2625. { made this global for disposing !! }
  2626. store_valid:=must_be_valid;
  2627. must_be_valid:=false;
  2628. inlined:=false;
  2629. if assigned(p^.procdefinition) and
  2630. ((p^.procdefinition^.options and poinline)<>0) then
  2631. begin
  2632. inlinecode:=p^.right;
  2633. if assigned(inlinecode) then
  2634. begin
  2635. inlined:=true;
  2636. p^.procdefinition^.options:=p^.procdefinition^.options and (not poinline);
  2637. end;
  2638. p^.right:=nil;
  2639. end;
  2640. { procedure variable ? }
  2641. if assigned(p^.right) then
  2642. begin
  2643. { procedure does a call }
  2644. procinfo.flags:=procinfo.flags or pi_do_call;
  2645. { calc the correture value for the register }
  2646. {$ifdef i386}
  2647. for regi:=R_EAX to R_EDI do
  2648. inc(reg_pushes[regi],t_times*2);
  2649. {$endif}
  2650. {$ifdef m68k}
  2651. for regi:=R_D0 to R_A6 do
  2652. inc(reg_pushes[regi],t_times*2);
  2653. {$endif}
  2654. { calculate the type of the parameters }
  2655. if assigned(p^.left) then
  2656. begin
  2657. old_count_ref:=count_ref;
  2658. count_ref:=false;
  2659. firstcallparan(p^.left,nil);
  2660. count_ref:=old_count_ref;
  2661. if codegenerror then
  2662. exit;
  2663. end;
  2664. firstpass(p^.right);
  2665. { check the parameters }
  2666. pdc:=pprocvardef(p^.right^.resulttype)^.para1;
  2667. pt:=p^.left;
  2668. while assigned(pdc) and assigned(pt) do
  2669. begin
  2670. pt:=pt^.right;
  2671. pdc:=pdc^.next;
  2672. end;
  2673. if assigned(pt) or assigned(pdc) then
  2674. Message(parser_e_illegal_parameter_list);
  2675. { insert type conversions }
  2676. if assigned(p^.left) then
  2677. begin
  2678. old_count_ref:=count_ref;
  2679. count_ref:=true;
  2680. firstcallparan(p^.left,pprocvardef(p^.right^.resulttype)^.para1);
  2681. count_ref:=old_count_ref;
  2682. if codegenerror then
  2683. exit;
  2684. end;
  2685. p^.resulttype:=pprocvardef(p^.right^.resulttype)^.retdef;
  2686. { this was missing, leads to a bug below if
  2687. the procvar is a function }
  2688. p^.procdefinition:=pprocdef(p^.right^.resulttype);
  2689. end
  2690. else
  2691. { not a procedure variable }
  2692. begin
  2693. { determine the type of the parameters }
  2694. if assigned(p^.left) then
  2695. begin
  2696. old_count_ref:=count_ref;
  2697. count_ref:=false;
  2698. store_valid:=must_be_valid;
  2699. must_be_valid:=false;
  2700. firstcallparan(p^.left,nil);
  2701. count_ref:=old_count_ref;
  2702. must_be_valid:=store_valid;
  2703. if codegenerror then
  2704. exit;
  2705. end;
  2706. { do we know the procedure to call ? }
  2707. if not(assigned(p^.procdefinition)) then
  2708. begin
  2709. actprocsym:=p^.symtableprocentry;
  2710. { determine length of parameter list }
  2711. pt:=p^.left;
  2712. paralength:=0;
  2713. while assigned(pt) do
  2714. begin
  2715. inc(paralength);
  2716. pt:=pt^.right;
  2717. end;
  2718. { alle in Frage kommenden Prozeduren in eine }
  2719. { verkettete Liste einf�gen }
  2720. pd:=actprocsym^.definition;
  2721. while assigned(pd) do
  2722. begin
  2723. { we should also check that the overloaded function
  2724. has been declared in a unit that is in the uses !! }
  2725. { pd^.owner should be in the symtablestack !! }
  2726. { Laenge der deklarierten Parameterliste feststellen: }
  2727. { not necessary why nextprocsym field }
  2728. {st:=symtablestack;
  2729. if (pd^.owner^.symtabletype<>objectsymtable) then
  2730. while assigned(st) do
  2731. begin
  2732. if (st=pd^.owner) then break;
  2733. st:=st^.next;
  2734. end;
  2735. if assigned(st) then }
  2736. begin
  2737. pdc:=pd^.para1;
  2738. l:=0;
  2739. while assigned(pdc) do
  2740. begin
  2741. inc(l);
  2742. pdc:=pdc^.next;
  2743. end;
  2744. { nur wenn die Parameterl„nge paát, dann Einf�gen }
  2745. if l=paralength then
  2746. begin
  2747. new(hp);
  2748. hp^.data:=pd;
  2749. hp^.next:=procs;
  2750. hp^.nextpara:=pd^.para1;
  2751. hp^.firstpara:=pd^.para1;
  2752. procs:=hp;
  2753. end;
  2754. end;
  2755. pd:=pd^.nextoverloaded;
  2756. {$ifdef CHAINPROCSYMS}
  2757. if (pd=nil) and not (p^.unit_specific) then
  2758. begin
  2759. actprocsym:=actprocsym^.nextprocsym;
  2760. if assigned(actprocsym) then
  2761. pd:=actprocsym^.definition;
  2762. end;
  2763. {$endif CHAINPROCSYMS}
  2764. end;
  2765. { nun alle Parameter nacheinander vergleichen }
  2766. pt:=p^.left;
  2767. while assigned(pt) do
  2768. begin
  2769. { matches a parameter of one procedure exact ? }
  2770. exactmatch:=false;
  2771. hp:=procs;
  2772. while assigned(hp) do
  2773. begin
  2774. if is_equal(hp^.nextpara^.data,pt^.resulttype) then
  2775. begin
  2776. if hp^.nextpara^.data=pt^.resulttype then
  2777. begin
  2778. pt^.exact_match_found:=true;
  2779. hp^.nextpara^.argconvtyp:=act_exact;
  2780. end
  2781. else
  2782. hp^.nextpara^.argconvtyp:=act_equal;
  2783. exactmatch:=true;
  2784. end
  2785. else
  2786. hp^.nextpara^.argconvtyp:=act_convertable;
  2787. hp:=hp^.next;
  2788. end;
  2789. { .... if yes, del all the other procedures }
  2790. if exactmatch then
  2791. begin
  2792. { the first .... }
  2793. while (assigned(procs)) and not(is_equal(procs^.nextpara^.data,pt^.resulttype)) do
  2794. begin
  2795. hp:=procs^.next;
  2796. dispose(procs);
  2797. procs:=hp;
  2798. end;
  2799. { and the others }
  2800. hp:=procs;
  2801. while (assigned(hp)) and assigned(hp^.next) do
  2802. begin
  2803. if not(is_equal(hp^.next^.nextpara^.data,pt^.resulttype)) then
  2804. begin
  2805. hp2:=hp^.next^.next;
  2806. dispose(hp^.next);
  2807. hp^.next:=hp2;
  2808. end
  2809. else
  2810. hp:=hp^.next;
  2811. end;
  2812. end
  2813. { sollte nirgendwo ein Parameter exakt passen, }
  2814. { so alle Prozeduren entfernen, bei denen }
  2815. { der Parameter auch nach einer impliziten }
  2816. { Typkonvertierung nicht passt }
  2817. else
  2818. begin
  2819. { erst am Anfang }
  2820. while (assigned(procs)) and
  2821. not(isconvertable(pt^.resulttype,procs^.nextpara^.data,
  2822. hcvt,pt^.left^.treetype,false)) do
  2823. begin
  2824. hp:=procs^.next;
  2825. dispose(procs);
  2826. procs:=hp;
  2827. end;
  2828. { und jetzt aus der Mitte }
  2829. hp:=procs;
  2830. while (assigned(hp)) and assigned(hp^.next) do
  2831. begin
  2832. if not(isconvertable(pt^.resulttype,hp^.next^.nextpara^.data,
  2833. hcvt,pt^.left^.treetype,false)) then
  2834. begin
  2835. hp2:=hp^.next^.next;
  2836. dispose(hp^.next);
  2837. hp^.next:=hp2;
  2838. end
  2839. else
  2840. hp:=hp^.next;
  2841. end;
  2842. end;
  2843. { nun bei denn Prozeduren den nextpara-Zeiger auf den }
  2844. { naechsten Parameter setzen }
  2845. hp:=procs;
  2846. while assigned(hp) do
  2847. begin
  2848. hp^.nextpara:=hp^.nextpara^.next;
  2849. hp:=hp^.next;
  2850. end;
  2851. pt:=pt^.right;
  2852. end;
  2853. if procs=nil then
  2854. if (parsing_para_level=0) or (p^.left<>nil) then
  2855. begin
  2856. Message(parser_e_illegal_parameter_list);
  2857. exit;
  2858. end
  2859. else
  2860. begin
  2861. { try to convert to procvar }
  2862. p^.treetype:=loadn;
  2863. p^.resulttype:=pprocsym(p^.symtableprocentry)^.definition;
  2864. p^.symtableentry:=p^.symtableprocentry;
  2865. p^.is_first:=false;
  2866. p^.disposetyp:=dt_nothing;
  2867. firstpass(p);
  2868. exit;
  2869. end;
  2870. { if there are several choices left then for orddef }
  2871. { if a type is totally included in the other }
  2872. { we don't fear an overflow , }
  2873. { so we can do as if it is an exact match }
  2874. { this will convert integer to longint }
  2875. { rather than to words }
  2876. { conversion of byte to integer or longint }
  2877. {would still not be solved }
  2878. if assigned(procs^.next) then
  2879. begin
  2880. hp:=procs;
  2881. while assigned(hp) do
  2882. begin
  2883. hp^.nextpara:=hp^.firstpara;
  2884. hp:=hp^.next;
  2885. end;
  2886. pt:=p^.left;
  2887. while assigned(pt) do
  2888. begin
  2889. { matches a parameter of one procedure exact ? }
  2890. exactmatch:=false;
  2891. def_from:=pt^.resulttype;
  2892. hp:=procs;
  2893. while assigned(hp) do
  2894. begin
  2895. if not is_equal(hp^.nextpara^.data,pt^.resulttype) then
  2896. begin
  2897. def_to:=hp^.nextpara^.data;
  2898. if ((def_from^.deftype=orddef) and (def_to^.deftype=orddef)) and
  2899. (is_in_limit(def_from,def_to) or
  2900. ((hp^.nextpara^.paratyp=vs_var) and
  2901. (def_from^.size=def_to^.size))) then
  2902. begin
  2903. exactmatch:=true;
  2904. conv_to:=def_to;
  2905. end;
  2906. end;
  2907. hp:=hp^.next;
  2908. end;
  2909. { .... if yes, del all the other procedures }
  2910. if exactmatch then
  2911. begin
  2912. { the first .... }
  2913. while (assigned(procs)) and not(is_in_limit(def_from,procs^.nextpara^.data)) do
  2914. begin
  2915. hp:=procs^.next;
  2916. dispose(procs);
  2917. procs:=hp;
  2918. end;
  2919. { and the others }
  2920. hp:=procs;
  2921. while (assigned(hp)) and assigned(hp^.next) do
  2922. begin
  2923. if not(is_in_limit(def_from,hp^.next^.nextpara^.data)) then
  2924. begin
  2925. hp2:=hp^.next^.next;
  2926. dispose(hp^.next);
  2927. hp^.next:=hp2;
  2928. end
  2929. else
  2930. begin
  2931. def_to:=hp^.next^.nextpara^.data;
  2932. if (conv_to^.size>def_to^.size) or
  2933. ((porddef(conv_to)^.low<porddef(def_to)^.low) and
  2934. (porddef(conv_to)^.high>porddef(def_to)^.high)) then
  2935. begin
  2936. hp2:=procs;
  2937. procs:=hp;
  2938. conv_to:=def_to;
  2939. dispose(hp2);
  2940. end
  2941. else
  2942. hp:=hp^.next;
  2943. end;
  2944. end;
  2945. end;
  2946. { nun bei denn Prozeduren den nextpara-Zeiger auf den }
  2947. { naechsten Parameter setzen }
  2948. hp:=procs;
  2949. while assigned(hp) do
  2950. begin
  2951. hp^.nextpara:=hp^.nextpara^.next;
  2952. hp:=hp^.next;
  2953. end;
  2954. pt:=pt^.right;
  2955. end;
  2956. end;
  2957. { let's try to eliminate equal is exact is there }
  2958. {if assigned(procs^.next) then
  2959. begin
  2960. pt:=p^.left;
  2961. while assigned(pt) do
  2962. begin
  2963. if pt^.exact_match_found then
  2964. begin
  2965. hp:=procs;
  2966. while (assigned(procs)) and (procs^.nextpara^.data<>pt^.resulttype) do
  2967. begin
  2968. hp:=procs^.next;
  2969. dispose(procs);
  2970. procs:=hp;
  2971. end;
  2972. end;
  2973. pt:=pt^.right;
  2974. end;
  2975. end; }
  2976. {$ifndef CHAINPROCSYMS}
  2977. if assigned(procs^.next) then
  2978. Message(cg_e_cant_choose_overload_function);
  2979. {$else CHAINPROCSYMS}
  2980. if assigned(procs^.next) then
  2981. { if the last retained is the only one }
  2982. { from a unit it is OK PM }
  2983. { the last is the one coming from the first symtable }
  2984. { as the diff defcoll are inserted in front }
  2985. begin
  2986. hp2:=procs;
  2987. while assigned(hp2^.next) and assigned(hp2^.next^.next) do
  2988. hp2:=hp2^.next;
  2989. if (hp2^.data^.owner<>hp2^.next^.data^.owner) then
  2990. begin
  2991. hp:=procs^.next;
  2992. {hp2 is the correct one }
  2993. hp2:=hp2^.next;
  2994. while hp<>hp2 do
  2995. begin
  2996. dispose(procs);
  2997. procs:=hp;
  2998. hp:=procs^.next;
  2999. end;
  3000. procs:=hp2;
  3001. end
  3002. else
  3003. Message(cg_e_cant_choose_overload_function);
  3004. error(too_much_matches);
  3005. end;
  3006. {$endif CHAINPROCSYMS}
  3007. {$ifdef UseBrowser}
  3008. if make_ref then
  3009. begin
  3010. get_cur_file_pos(curtokenpos);
  3011. procs^.data^.lastref:=new(pref,init(procs^.data^.lastref,@curtokenpos));
  3012. end;
  3013. {$endif UseBrowser}
  3014. p^.procdefinition:=procs^.data;
  3015. p^.resulttype:=procs^.data^.retdef;
  3016. { big error for with statements
  3017. p^.symtableproc:=p^.procdefinition^.owner; }
  3018. p^.location.loc:=LOC_MEM;
  3019. {$ifdef CHAINPROCSYMS}
  3020. { object with method read;
  3021. call to read(x) will be a usual procedure call }
  3022. if assigned(p^.methodpointer) and
  3023. (p^.procdefinition^._class=nil) then
  3024. begin
  3025. { not ok for extended }
  3026. case p^.methodpointer^.treetype of
  3027. typen,hnewn : fatalerror(no_para_match);
  3028. end;
  3029. disposetree(p^.methodpointer);
  3030. p^.methodpointer:=nil;
  3031. end;
  3032. {$endif CHAINPROCSYMS}
  3033. end;{ end of procedure to call determination }
  3034. { handle predefined procedures }
  3035. if (p^.procdefinition^.options and pointernproc)<>0 then
  3036. begin
  3037. { settextbuf needs two args }
  3038. if assigned(p^.left^.right) then
  3039. pt:=geninlinenode(pprocdef(p^.procdefinition)^.extnumber,p^.left)
  3040. else
  3041. begin
  3042. pt:=geninlinenode(pprocdef(p^.procdefinition)^.extnumber,p^.left^.left);
  3043. putnode(p^.left);
  3044. end;
  3045. putnode(p);
  3046. firstpass(pt);
  3047. { was placed after the exit }
  3048. { caused GPF }
  3049. { error caused and corrected by (PM) }
  3050. p:=pt;
  3051. must_be_valid:=store_valid;
  3052. if codegenerror then
  3053. exit;
  3054. dispose(procs);
  3055. exit;
  3056. end
  3057. else
  3058. { no intern procedure => we do a call }
  3059. { calc the correture value for the register }
  3060. { handle predefined procedures }
  3061. if (p^.procdefinition^.options and poinline)<>0 then
  3062. begin
  3063. if assigned(p^.methodpointer) then
  3064. Message(cg_e_unable_inline_object_methods);
  3065. if assigned(p^.right) and (p^.right^.treetype<>procinlinen) then
  3066. Message(cg_e_unable_inline_procvar);
  3067. { p^.treetype:=procinlinen; }
  3068. if not assigned(p^.right) then
  3069. begin
  3070. if assigned(p^.procdefinition^.code) then
  3071. inlinecode:=genprocinlinenode(p,ptree(p^.procdefinition^.code))
  3072. else
  3073. Message(cg_e_no_code_for_inline_stored);
  3074. if assigned(inlinecode) then
  3075. begin
  3076. { consider it has not inlined if called
  3077. again inside the args }
  3078. p^.procdefinition^.options:=p^.procdefinition^.options and (not poinline);
  3079. firstpass(inlinecode);
  3080. inlined:=true;
  3081. end;
  3082. end;
  3083. end
  3084. else
  3085. procinfo.flags:=procinfo.flags or pi_do_call;
  3086. { work trough all parameters to insert the type conversions }
  3087. { !!! done now after internproc !! (PM) }
  3088. if assigned(p^.left) then
  3089. begin
  3090. old_count_ref:=count_ref;
  3091. count_ref:=true;
  3092. firstcallparan(p^.left,p^.procdefinition^.para1);
  3093. count_ref:=old_count_ref;
  3094. end;
  3095. {$ifdef i386}
  3096. for regi:=R_EAX to R_EDI do
  3097. begin
  3098. if (p^.procdefinition^.usedregisters and ($80 shr word(regi)))<>0 then
  3099. inc(reg_pushes[regi],t_times*2);
  3100. end;
  3101. {$endif}
  3102. {$ifdef m68k}
  3103. for regi:=R_D0 to R_A6 do
  3104. begin
  3105. if (p^.procdefinition^.usedregisters and ($800 shr word(regi)))<>0 then
  3106. inc(reg_pushes[regi],t_times*2);
  3107. end;
  3108. {$endif}
  3109. end;
  3110. { ensure that the result type is set }
  3111. p^.resulttype:=p^.procdefinition^.retdef;
  3112. { get a register for the return value }
  3113. if (p^.resulttype<>pdef(voiddef)) then
  3114. begin
  3115. if (p^.procdefinition^.options and poconstructor)<>0 then
  3116. begin
  3117. { extra handling of classes }
  3118. { p^.methodpointer should be assigned! }
  3119. if assigned(p^.methodpointer) and assigned(p^.methodpointer^.resulttype) and
  3120. (p^.methodpointer^.resulttype^.deftype=classrefdef) then
  3121. begin
  3122. p^.location.loc:=LOC_REGISTER;
  3123. p^.registers32:=1;
  3124. { the result type depends on the classref }
  3125. p^.resulttype:=pclassrefdef(p^.methodpointer^.resulttype)^.definition;
  3126. end
  3127. { a object constructor returns the result with the flags }
  3128. else
  3129. p^.location.loc:=LOC_FLAGS;
  3130. end
  3131. else
  3132. begin
  3133. {$ifdef SUPPORT_MMX}
  3134. if (cs_mmx in aktswitches) and
  3135. is_mmx_able_array(p^.resulttype) then
  3136. begin
  3137. p^.location.loc:=LOC_MMXREGISTER;
  3138. p^.registersmmx:=1;
  3139. end
  3140. else
  3141. {$endif SUPPORT_MMX}
  3142. if ret_in_acc(p^.resulttype) then
  3143. begin
  3144. p^.location.loc:=LOC_REGISTER;
  3145. p^.registers32:=1;
  3146. end
  3147. else if (p^.resulttype^.deftype=floatdef) then
  3148. begin
  3149. p^.location.loc:=LOC_FPU;
  3150. p^.registersfpu:=1;
  3151. end
  3152. end;
  3153. end;
  3154. {$ifdef StoreFPULevel}
  3155. { a fpu can be used in any procedure !! }
  3156. p^.registersfpu:=p^.procdefinition^.fpu_used;
  3157. {$endif StoreFPULevel}
  3158. { if this is a call to a method calc the registers }
  3159. if (p^.methodpointer<>nil) then
  3160. begin
  3161. case p^.methodpointer^.treetype of
  3162. { but only, if this is not a supporting node }
  3163. typen,hnewn : ;
  3164. else
  3165. begin
  3166. { R.Assign is not a constructor !!! }
  3167. { but for R^.Assign, R must be valid !! }
  3168. if ((p^.procdefinition^.options and poconstructor) <> 0) or
  3169. ((p^.methodpointer^.treetype=loadn) and
  3170. ((pobjectdef(p^.methodpointer^.resulttype)^.options and oo_hasvirtual) = 0)) then
  3171. must_be_valid:=false
  3172. else
  3173. must_be_valid:=true;
  3174. firstpass(p^.methodpointer);
  3175. p^.registersfpu:=max(p^.methodpointer^.registersfpu,p^.registersfpu);
  3176. p^.registers32:=max(p^.methodpointer^.registers32,p^.registers32);
  3177. {$ifdef SUPPORT_MMX}
  3178. p^.registersmmx:=max(p^.methodpointer^.registersmmx,p^.registersmmx);
  3179. {$endif SUPPORT_MMX}
  3180. end;
  3181. end;
  3182. end;
  3183. if inlined then
  3184. begin
  3185. p^.right:=inlinecode;
  3186. p^.procdefinition^.options:=p^.procdefinition^.options or poinline;
  3187. end;
  3188. { determine the registers of the procedure variable }
  3189. { is this OK for inlined procs also ?? (PM) }
  3190. if assigned(p^.right) then
  3191. begin
  3192. p^.registersfpu:=max(p^.right^.registersfpu,p^.registersfpu);
  3193. p^.registers32:=max(p^.right^.registers32,p^.registers32);
  3194. {$ifdef SUPPORT_MMX}
  3195. p^.registersmmx:=max(p^.right^.registersmmx,p^.registersmmx);
  3196. {$endif SUPPORT_MMX}
  3197. end;
  3198. { determine the registers of the procedure }
  3199. if assigned(p^.left) then
  3200. begin
  3201. p^.registersfpu:=max(p^.left^.registersfpu,p^.registersfpu);
  3202. p^.registers32:=max(p^.left^.registers32,p^.registers32);
  3203. {$ifdef SUPPORT_MMX}
  3204. p^.registersmmx:=max(p^.left^.registersmmx,p^.registersmmx);
  3205. {$endif SUPPORT_MMX}
  3206. end;
  3207. if assigned(procs) then
  3208. dispose(procs);
  3209. must_be_valid:=store_valid;
  3210. end;
  3211. procedure firstfuncret(var p : ptree);
  3212. begin
  3213. {$ifdef TEST_FUNCRET}
  3214. p^.resulttype:=p^.retdef;
  3215. p^.location.loc:=LOC_REFERENCE;
  3216. if ret_in_param(p^.retdef) or
  3217. (@procinfo<>pprocinfo(p^.funcretprocinfo)) then
  3218. p^.registers32:=1;
  3219. { no claim if setting higher return values }
  3220. if must_be_valid and
  3221. (@procinfo=pprocinfo(p^.funcretprocinfo)) and
  3222. not procinfo.funcret_is_valid then
  3223. note(uninitialized_function_return);
  3224. if count_ref then pprocinfo(p^.funcretprocinfo)^.funcret_is_valid:=true;
  3225. {$else TEST_FUNCRET}
  3226. p^.resulttype:=procinfo.retdef;
  3227. p^.location.loc:=LOC_REFERENCE;
  3228. if ret_in_param(procinfo.retdef) then
  3229. p^.registers32:=1;
  3230. if must_be_valid and
  3231. not(procinfo.funcret_is_valid) {and
  3232. ((procinfo.flags and pi_uses_asm)=0)} then
  3233. Message(sym_w_function_result_not_set);
  3234. if count_ref then procinfo.funcret_is_valid:=true;
  3235. {$endif TEST_FUNCRET}
  3236. end;
  3237. { intern inline suborutines }
  3238. procedure firstinline(var p : ptree);
  3239. var
  3240. hp,hpp : ptree;
  3241. store_count_ref,isreal,store_valid,file_is_typed : boolean;
  3242. procedure do_lowhigh(adef : pdef);
  3243. var
  3244. v : longint;
  3245. enum : penumsym;
  3246. begin
  3247. case Adef^.deftype of
  3248. orddef:
  3249. begin
  3250. if p^.inlinenumber=in_low_x then
  3251. v:=porddef(Adef)^.low
  3252. else
  3253. v:=porddef(Adef)^.high;
  3254. hp:=genordinalconstnode(v,adef);
  3255. firstpass(hp);
  3256. disposetree(p);
  3257. p:=hp;
  3258. end;
  3259. enumdef:
  3260. begin
  3261. enum:=Penumdef(Adef)^.first;
  3262. if p^.inlinenumber=in_high_x then
  3263. while enum^.next<>nil do
  3264. enum:=enum^.next;
  3265. hp:=genenumnode(enum);
  3266. disposetree(p);
  3267. p:=hp;
  3268. end
  3269. end;
  3270. end;
  3271. begin
  3272. store_valid:=must_be_valid;
  3273. store_count_ref:=count_ref;
  3274. count_ref:=false;
  3275. if not (p^.inlinenumber in [in_read_x,in_readln_x,in_sizeof_x,
  3276. in_typeof_x,in_ord_x,in_str_x_string,
  3277. in_reset_typedfile,in_rewrite_typedfile]) then
  3278. must_be_valid:=true
  3279. else
  3280. must_be_valid:=false;
  3281. { if we handle writeln; p^.left contains no valid address }
  3282. if assigned(p^.left) then
  3283. begin
  3284. if p^.left^.treetype=callparan then
  3285. firstcallparan(p^.left,nil)
  3286. else
  3287. firstpass(p^.left);
  3288. p^.registers32:=p^.left^.registers32;
  3289. p^.registersfpu:=p^.left^.registersfpu;
  3290. {$ifdef SUPPORT_MMX}
  3291. p^.registersmmx:=p^.left^.registersmmx;
  3292. {$endif SUPPORT_MMX}
  3293. set_location(p^.location,p^.left^.location);
  3294. end;
  3295. case p^.inlinenumber of
  3296. in_lo_word,in_hi_word:
  3297. begin
  3298. if p^.registers32<1 then
  3299. p^.registers32:=1;
  3300. p^.resulttype:=u8bitdef;
  3301. p^.location.loc:=LOC_REGISTER;
  3302. end;
  3303. in_lo_long,in_hi_long:
  3304. begin
  3305. if p^.registers32<1 then
  3306. p^.registers32:=1;
  3307. p^.resulttype:=u16bitdef;
  3308. p^.location.loc:=LOC_REGISTER;
  3309. end;
  3310. in_sizeof_x:
  3311. begin
  3312. if p^.registers32<1 then
  3313. p^.registers32:=1;
  3314. p^.resulttype:=s32bitdef;
  3315. p^.location.loc:=LOC_REGISTER;
  3316. end;
  3317. in_typeof_x:
  3318. begin
  3319. if p^.registers32<1 then
  3320. p^.registers32:=1;
  3321. p^.location.loc:=LOC_REGISTER;
  3322. p^.resulttype:=voidpointerdef;
  3323. end;
  3324. in_ord_x:
  3325. begin
  3326. if (p^.left^.treetype=ordconstn) then
  3327. begin
  3328. hp:=genordinalconstnode(p^.left^.value,s32bitdef);
  3329. disposetree(p);
  3330. p:=hp;
  3331. firstpass(p);
  3332. end
  3333. else
  3334. begin
  3335. if (p^.left^.resulttype^.deftype=orddef) then
  3336. if (porddef(p^.left^.resulttype)^.typ in [uchar,bool8bit]) then
  3337. begin
  3338. if porddef(p^.left^.resulttype)^.typ=bool8bit then
  3339. begin
  3340. hp:=gentypeconvnode(p^.left,u8bitdef);
  3341. putnode(p);
  3342. p:=hp;
  3343. p^.convtyp:=tc_bool_2_int;
  3344. p^.explizit:=true;
  3345. firstpass(p);
  3346. end
  3347. else
  3348. begin
  3349. hp:=gentypeconvnode(p^.left,u8bitdef);
  3350. putnode(p);
  3351. p:=hp;
  3352. p^.explizit:=true;
  3353. firstpass(p);
  3354. end;
  3355. end
  3356. { can this happen ? }
  3357. else if (porddef(p^.left^.resulttype)^.typ=uvoid) then
  3358. Message(sym_e_type_mismatch)
  3359. else
  3360. { all other orddef need no transformation }
  3361. begin
  3362. hp:=p^.left;
  3363. putnode(p);
  3364. p:=hp;
  3365. end
  3366. else if (p^.left^.resulttype^.deftype=enumdef) then
  3367. begin
  3368. hp:=gentypeconvnode(p^.left,s32bitdef);
  3369. putnode(p);
  3370. p:=hp;
  3371. p^.explizit:=true;
  3372. firstpass(p);
  3373. end
  3374. else
  3375. begin
  3376. { can anything else be ord() ?}
  3377. Message(sym_e_type_mismatch);
  3378. end;
  3379. end;
  3380. end;
  3381. in_chr_byte:
  3382. begin
  3383. hp:=gentypeconvnode(p^.left,cchardef);
  3384. putnode(p);
  3385. p:=hp;
  3386. p^.explizit:=true;
  3387. firstpass(p);
  3388. end;
  3389. in_length_string:
  3390. begin
  3391. {$ifdef UseAnsiString}
  3392. if is_ansistring(p^.left^.resulttype) then
  3393. p^.resulttype:=s32bitdef
  3394. else
  3395. {$endif UseAnsiString}
  3396. p^.resulttype:=u8bitdef;
  3397. { wer don't need string conversations here }
  3398. if (p^.left^.treetype=typeconvn) and
  3399. (p^.left^.left^.resulttype^.deftype=stringdef) then
  3400. begin
  3401. hp:=p^.left^.left;
  3402. putnode(p^.left);
  3403. p^.left:=hp;
  3404. end;
  3405. { evalutes length of constant strings direct }
  3406. if (p^.left^.treetype=stringconstn) then
  3407. begin
  3408. hp:=genordinalconstnode(length(p^.left^.values^),s32bitdef);
  3409. disposetree(p);
  3410. firstpass(hp);
  3411. p:=hp;
  3412. end;
  3413. end;
  3414. in_assigned_x:
  3415. begin
  3416. p^.resulttype:=booldef;
  3417. p^.location.loc:=LOC_FLAGS;
  3418. end;
  3419. in_pred_x,
  3420. in_succ_x:
  3421. begin
  3422. p^.resulttype:=p^.left^.resulttype;
  3423. p^.location.loc:=LOC_REGISTER;
  3424. if not is_ordinal(p^.resulttype) then
  3425. Message(sym_e_type_mismatch)
  3426. else
  3427. begin
  3428. if (p^.resulttype^.deftype=enumdef) and
  3429. (penumdef(p^.resulttype)^.has_jumps) then
  3430. begin
  3431. Message(parser_e_succ_and_pred_enums_with_assign_not_possible);
  3432. end
  3433. else if p^.left^.treetype=ordconstn then
  3434. begin
  3435. if p^.inlinenumber=in_pred_x then
  3436. hp:=genordinalconstnode(p^.left^.value+1,
  3437. p^.left^.resulttype)
  3438. else
  3439. hp:=genordinalconstnode(p^.left^.value-1,
  3440. p^.left^.resulttype);
  3441. disposetree(p);
  3442. firstpass(hp);
  3443. p:=hp;
  3444. end;
  3445. end;
  3446. end;
  3447. in_dec_dword,
  3448. in_dec_word,
  3449. in_dec_byte,
  3450. in_inc_dword,
  3451. in_inc_word,
  3452. in_inc_byte :
  3453. begin
  3454. p^.resulttype:=voiddef;
  3455. if p^.left^.location.loc<>LOC_REFERENCE then
  3456. Message(cg_e_illegal_expression);
  3457. end;
  3458. in_inc_x,
  3459. in_dec_x:
  3460. begin
  3461. p^.resulttype:=voiddef;
  3462. if assigned(p^.left) then
  3463. begin
  3464. firstcallparan(p^.left,nil);
  3465. { first param must be var }
  3466. if not (p^.left^.left^.location.loc in [LOC_MEM,LOC_REFERENCE]) then
  3467. Message(cg_e_illegal_expression);
  3468. { check type }
  3469. if (p^.left^.resulttype^.deftype in [enumdef,pointerdef]) or
  3470. ((p^.left^.resulttype^.deftype=orddef) and
  3471. (porddef(p^.left^.resulttype)^.typ in [uchar,bool8bit,u8bit,s8bit,
  3472. bool16bit,u16bit,s16bit,bool32bit,u32bit,s32bit])) then
  3473. begin
  3474. { two paras ? }
  3475. if assigned(p^.left^.right) then
  3476. begin
  3477. { insert a type conversion }
  3478. { the second param is always longint }
  3479. p^.left^.right^.left:=gentypeconvnode(p^.left^.right^.left,s32bitdef);
  3480. { check the type conversion }
  3481. firstpass(p^.left^.right^.left);
  3482. if assigned(p^.left^.right^.right) then
  3483. Message(cg_e_illegal_expression);
  3484. end;
  3485. end
  3486. else
  3487. Message(sym_e_type_mismatch);
  3488. end
  3489. else
  3490. Message(sym_e_type_mismatch);
  3491. end;
  3492. in_read_x,
  3493. in_readln_x,
  3494. in_write_x,
  3495. in_writeln_x :
  3496. begin
  3497. { needs a call }
  3498. procinfo.flags:=procinfo.flags or pi_do_call;
  3499. p^.resulttype:=voiddef;
  3500. { we must know if it is a typed file or not }
  3501. { but we must first do the firstpass for it }
  3502. file_is_typed:=false;
  3503. if assigned(p^.left) then
  3504. begin
  3505. firstcallparan(p^.left,nil);
  3506. { now we can check }
  3507. hp:=p^.left;
  3508. while assigned(hp^.right) do
  3509. hp:=hp^.right;
  3510. { if resulttype is not assigned, then automatically }
  3511. { file is not typed. }
  3512. if assigned(hp) and assigned(hp^.resulttype) then
  3513. Begin
  3514. if (hp^.resulttype^.deftype=filedef) and
  3515. (pfiledef(hp^.resulttype)^.filetype=ft_typed) then
  3516. begin
  3517. file_is_typed:=true;
  3518. { test the type here
  3519. so we can use a trick in cgi386 (PM) }
  3520. hpp:=p^.left;
  3521. while (hpp<>hp) do
  3522. begin
  3523. { should we allow type conversion ? (PM)
  3524. if not isconvertable(hpp^.resulttype,
  3525. pfiledef(hp^.resulttype)^.typed_as,convtyp,hpp^.treetype) then
  3526. Message(sym_e_type_mismatch);
  3527. if not(is_equal(hpp^.resulttype,pfiledef(hp^.resulttype)^.typed_as)) then
  3528. begin
  3529. hpp^.left:=gentypeconvnode(hpp^.left,pfiledef(hp^.resulttype)^.typed_as);
  3530. end; }
  3531. if not is_equal(hpp^.resulttype,pfiledef(hp^.resulttype)^.typed_as) then
  3532. Message(sym_e_type_mismatch);
  3533. hpp:=hpp^.right;
  3534. end;
  3535. { once again for typeconversions }
  3536. firstcallparan(p^.left,nil);
  3537. end;
  3538. end; { endif assigned(hp) }
  3539. { insert type conversions for write(ln) }
  3540. if (not file_is_typed) and
  3541. ((p^.inlinenumber=in_write_x) or (p^.inlinenumber=in_writeln_x)) then
  3542. begin
  3543. hp:=p^.left;
  3544. while assigned(hp) do
  3545. begin
  3546. if assigned(hp^.left^.resulttype) then
  3547. begin
  3548. if hp^.left^.resulttype^.deftype=floatdef then
  3549. begin
  3550. isreal:=true;
  3551. end
  3552. else if hp^.left^.resulttype^.deftype=orddef then
  3553. case porddef(hp^.left^.resulttype)^.typ of
  3554. u8bit,s8bit,
  3555. u16bit,s16bit : hp^.left:=gentypeconvnode(hp^.left,s32bitdef);
  3556. bool16bit,bool32bit : hp^.left:=gentypeconvnode(hp^.left,booldef);
  3557. end
  3558. { but we convert only if the first index<>0, because in this case }
  3559. { we have a ASCIIZ string }
  3560. else if (hp^.left^.resulttype^.deftype=arraydef) and
  3561. (parraydef(hp^.left^.resulttype)^.lowrange<>0) and
  3562. (parraydef(hp^.left^.resulttype)^.definition^.deftype=orddef) and
  3563. (porddef(parraydef(hp^.left^.resulttype)^.definition)^.typ=uchar) then
  3564. hp^.left:=gentypeconvnode(hp^.left,cstringdef);
  3565. end;
  3566. hp:=hp^.right;
  3567. end;
  3568. end;
  3569. { pass all parameters again }
  3570. firstcallparan(p^.left,nil);
  3571. end;
  3572. end;
  3573. in_settextbuf_file_x :
  3574. begin
  3575. { warning here p^.left is the callparannode
  3576. not the argument directly }
  3577. { p^.left^.left is text var }
  3578. { p^.left^.right^.left is the buffer var }
  3579. { firstcallparan(p^.left,nil);
  3580. already done in firstcalln }
  3581. { now we know the type of buffer }
  3582. getsymonlyin(systemunit,'SETTEXTBUF');
  3583. hp:=gencallnode(pprocsym(srsym),systemunit);
  3584. hp^.left:=gencallparanode(
  3585. genordinalconstnode(p^.left^.left^.resulttype^.size,s32bitdef),p^.left);
  3586. putnode(p);
  3587. p:=hp;
  3588. firstpass(p);
  3589. end;
  3590. { the firstpass of the arg has been done in firstcalln ? }
  3591. in_reset_typedfile,in_rewrite_typedfile :
  3592. begin
  3593. procinfo.flags:=procinfo.flags or pi_do_call;
  3594. { to be sure the right definition is loaded }
  3595. p^.left^.resulttype:=nil;
  3596. firstload(p^.left);
  3597. p^.resulttype:=voiddef;
  3598. end;
  3599. in_str_x_string :
  3600. begin
  3601. procinfo.flags:=procinfo.flags or pi_do_call;
  3602. p^.resulttype:=voiddef;
  3603. if assigned(p^.left) then
  3604. begin
  3605. hp:=p^.left^.right;
  3606. { first pass just the string for first local use }
  3607. must_be_valid:=false;
  3608. count_ref:=true;
  3609. p^.left^.right:=nil;
  3610. firstcallparan(p^.left,nil);
  3611. must_be_valid:=true;
  3612. p^.left^.right:=hp;
  3613. firstcallparan(p^.left^.right,nil);
  3614. hp:=p^.left;
  3615. isreal:=false;
  3616. { valid string ? }
  3617. if not assigned(hp) or
  3618. (hp^.left^.resulttype^.deftype<>stringdef) or
  3619. (hp^.right=nil) or
  3620. (hp^.left^.location.loc<>LOC_REFERENCE) then
  3621. Message(cg_e_illegal_expression);
  3622. { !!!! check length of string }
  3623. while assigned(hp^.right) do hp:=hp^.right;
  3624. { check and convert the first param }
  3625. if hp^.is_colon_para then
  3626. Message(cg_e_illegal_expression)
  3627. else if hp^.resulttype^.deftype=orddef then
  3628. case porddef(hp^.left^.resulttype)^.typ of
  3629. u8bit,s8bit,
  3630. u16bit,s16bit :
  3631. hp^.left:=gentypeconvnode(hp^.left,s32bitdef);
  3632. end
  3633. else if hp^.resulttype^.deftype=floatdef then
  3634. begin
  3635. isreal:=true;
  3636. end
  3637. else Message(cg_e_illegal_expression);
  3638. { some format options ? }
  3639. hp:=p^.left^.right;
  3640. if assigned(hp) and hp^.is_colon_para then
  3641. begin
  3642. hp^.left:=gentypeconvnode(hp^.left,s32bitdef);
  3643. hp:=hp^.right;
  3644. end;
  3645. if assigned(hp) and hp^.is_colon_para then
  3646. begin
  3647. if isreal then
  3648. hp^.left:=gentypeconvnode(hp^.left,s32bitdef)
  3649. else
  3650. Message(parser_e_illegal_colon_qualifier);
  3651. hp:=hp^.right;
  3652. end;
  3653. { for first local use }
  3654. must_be_valid:=false;
  3655. count_ref:=true;
  3656. if assigned(hp) then
  3657. firstcallparan(hp,nil);
  3658. end
  3659. else
  3660. Message(parser_e_illegal_parameter_list);
  3661. { check params once more }
  3662. if codegenerror then
  3663. exit;
  3664. must_be_valid:=true;
  3665. firstcallparan(p^.left,nil);
  3666. end;
  3667. in_include_x_y,
  3668. in_exclude_x_y:
  3669. begin
  3670. p^.resulttype:=voiddef;
  3671. if assigned(p^.left) then
  3672. begin
  3673. firstcallparan(p^.left,nil);
  3674. p^.registers32:=p^.left^.registers32;
  3675. p^.registersfpu:=p^.left^.registersfpu;
  3676. {$ifdef SUPPORT_MMX}
  3677. p^.registersmmx:=p^.left^.registersmmx;
  3678. {$endif SUPPORT_MMX}
  3679. { first param must be var }
  3680. if (p^.left^.left^.location.loc<>LOC_REFERENCE) and
  3681. (p^.left^.left^.location.loc<>LOC_CREGISTER) then
  3682. Message(cg_e_illegal_expression);
  3683. { check type }
  3684. if (p^.left^.resulttype^.deftype=setdef) then
  3685. begin
  3686. { two paras ? }
  3687. if assigned(p^.left^.right) then
  3688. begin
  3689. { insert a type conversion }
  3690. { to the type of the set elements }
  3691. p^.left^.right^.left:=gentypeconvnode(
  3692. p^.left^.right^.left,
  3693. psetdef(p^.left^.resulttype)^.setof);
  3694. { check the type conversion }
  3695. firstpass(p^.left^.right^.left);
  3696. { only three parameters are allowed }
  3697. if assigned(p^.left^.right^.right) then
  3698. Message(cg_e_illegal_expression);
  3699. end;
  3700. end
  3701. else
  3702. Message(sym_e_type_mismatch);
  3703. end
  3704. else
  3705. Message(sym_e_type_mismatch);
  3706. end;
  3707. in_low_x,in_high_x:
  3708. begin
  3709. if p^.left^.treetype in [typen,loadn] then
  3710. begin
  3711. case p^.left^.resulttype^.deftype of
  3712. orddef,enumdef:
  3713. begin
  3714. do_lowhigh(p^.left^.resulttype);
  3715. firstpass(p);
  3716. end;
  3717. setdef:
  3718. begin
  3719. do_lowhigh(Psetdef(p^.left^.resulttype)^.setof);
  3720. firstpass(p);
  3721. end;
  3722. arraydef:
  3723. begin
  3724. if is_open_array(p^.left^.resulttype) then
  3725. begin
  3726. if p^.inlinenumber=in_low_x then
  3727. begin
  3728. hp:=genordinalconstnode(Parraydef(p^.left^.resulttype)^.lowrange,s32bitdef);
  3729. disposetree(p);
  3730. p:=hp;
  3731. firstpass(p);
  3732. end
  3733. else
  3734. begin
  3735. p^.resulttype:=s32bitdef;
  3736. p^.registers32:=max(1,
  3737. p^.registers32);
  3738. p^.location.loc:=LOC_REGISTER;
  3739. end;
  3740. end
  3741. else
  3742. begin
  3743. if p^.inlinenumber=in_low_x then
  3744. hp:=genordinalconstnode(Parraydef(p^.left^.resulttype)^.lowrange,s32bitdef)
  3745. else
  3746. hp:=genordinalconstnode(Parraydef(p^.left^.resulttype)^.highrange,s32bitdef);
  3747. disposetree(p);
  3748. p:=hp;
  3749. firstpass(p);
  3750. end;
  3751. end;
  3752. stringdef:
  3753. begin
  3754. if p^.inlinenumber=in_low_x then
  3755. hp:=genordinalconstnode(0,u8bitdef)
  3756. else
  3757. hp:=genordinalconstnode(Pstringdef(p^.left^.resulttype)^.len,u8bitdef);
  3758. disposetree(p);
  3759. p:=hp;
  3760. firstpass(p);
  3761. end;
  3762. else
  3763. Message(sym_e_type_mismatch);
  3764. end;
  3765. end
  3766. else
  3767. Message(parser_e_varid_or_typeid_expected);
  3768. end
  3769. else internalerror(8);
  3770. end;
  3771. must_be_valid:=store_valid;
  3772. count_ref:=store_count_ref;
  3773. end;
  3774. procedure firstsubscriptn(var p : ptree);
  3775. begin
  3776. firstpass(p^.left);
  3777. if codegenerror then
  3778. begin
  3779. p^.resulttype:=generrordef;
  3780. exit;
  3781. end;
  3782. p^.resulttype:=p^.vs^.definition;
  3783. { this must be done in the parser
  3784. if count_ref and not must_be_valid then
  3785. if (p^.vs^.properties and sp_protected)<>0 then
  3786. Message(parser_e_cant_write_protected_member);
  3787. }
  3788. p^.registers32:=p^.left^.registers32;
  3789. p^.registersfpu:=p^.left^.registersfpu;
  3790. {$ifdef SUPPORT_MMX}
  3791. p^.registersmmx:=p^.left^.registersmmx;
  3792. {$endif SUPPORT_MMX}
  3793. { classes must be dereferenced implicit }
  3794. if (p^.left^.resulttype^.deftype=objectdef) and
  3795. pobjectdef(p^.left^.resulttype)^.isclass then
  3796. begin
  3797. if p^.registers32=0 then
  3798. p^.registers32:=1;
  3799. p^.location.loc:=LOC_REFERENCE;
  3800. end
  3801. else
  3802. begin
  3803. if (p^.left^.location.loc<>LOC_MEM) and
  3804. (p^.left^.location.loc<>LOC_REFERENCE) then
  3805. Message(cg_e_illegal_expression);
  3806. set_location(p^.location,p^.left^.location);
  3807. end;
  3808. end;
  3809. procedure firstselfn(var p : ptree);
  3810. begin
  3811. if (p^.resulttype^.deftype=classrefdef) or
  3812. ((p^.resulttype^.deftype=objectdef)
  3813. and pobjectdef(p^.resulttype)^.isclass
  3814. ) then
  3815. p^.location.loc:=LOC_REGISTER
  3816. else
  3817. p^.location.loc:=LOC_REFERENCE;
  3818. end;
  3819. procedure firsttypen(var p : ptree);
  3820. begin
  3821. { DM: Why not allowed? For example: low(word) results in a type
  3822. id of word.
  3823. error(typeid_here_not_allowed);}
  3824. end;
  3825. procedure firsthnewn(var p : ptree);
  3826. begin
  3827. end;
  3828. procedure firsthdisposen(var p : ptree);
  3829. begin
  3830. firstpass(p^.left);
  3831. if codegenerror then
  3832. exit;
  3833. p^.registers32:=p^.left^.registers32;
  3834. p^.registersfpu:=p^.left^.registersfpu;
  3835. {$ifdef SUPPORT_MMX}
  3836. p^.registersmmx:=p^.left^.registersmmx;
  3837. {$endif SUPPORT_MMX}
  3838. if p^.registers32<1 then
  3839. p^.registers32:=1;
  3840. {
  3841. if p^.left^.location.loc<>LOC_REFERENCE then
  3842. Message(cg_e_illegal_expression);
  3843. }
  3844. p^.location.loc:=LOC_REFERENCE;
  3845. p^.resulttype:=ppointerdef(p^.left^.resulttype)^.definition;
  3846. end;
  3847. procedure firstnewn(var p : ptree);
  3848. begin
  3849. { Standardeinleitung }
  3850. firstpass(p^.left);
  3851. if codegenerror then
  3852. exit;
  3853. p^.registers32:=p^.left^.registers32;
  3854. p^.registersfpu:=p^.left^.registersfpu;
  3855. {$ifdef SUPPORT_MMX}
  3856. p^.registersmmx:=p^.left^.registersmmx;
  3857. {$endif SUPPORT_MMX}
  3858. { result type is already set }
  3859. procinfo.flags:=procinfo.flags or pi_do_call;
  3860. p^.location.loc:=LOC_REGISTER;
  3861. end;
  3862. procedure firstsimplenewdispose(var p : ptree);
  3863. begin
  3864. { this cannot be in a register !! }
  3865. make_not_regable(p^.left);
  3866. firstpass(p^.left);
  3867. { check the type }
  3868. if (p^.left^.resulttype=nil) or (p^.left^.resulttype^.deftype<>pointerdef) then
  3869. Message(parser_e_pointer_type_expected);
  3870. if (p^.left^.location.loc<>LOC_REFERENCE) {and
  3871. (p^.left^.location.loc<>LOC_CREGISTER)} then
  3872. Message(cg_e_illegal_expression);
  3873. p^.registers32:=p^.left^.registers32;
  3874. p^.registersfpu:=p^.left^.registersfpu;
  3875. {$ifdef SUPPORT_MMX}
  3876. p^.registersmmx:=p^.left^.registersmmx;
  3877. {$endif SUPPORT_MMX}
  3878. p^.resulttype:=voiddef;
  3879. procinfo.flags:=procinfo.flags or pi_do_call;
  3880. end;
  3881. procedure firstsetcons(var p : ptree);
  3882. var
  3883. hp : ptree;
  3884. begin
  3885. p^.location.loc:=LOC_MEM;
  3886. hp:=p^.left;
  3887. { is done by getnode*
  3888. p^.registers32:=0;
  3889. p^.registersfpu:=0;
  3890. }
  3891. while assigned(hp) do
  3892. begin
  3893. firstpass(hp^.left);
  3894. if codegenerror then
  3895. exit;
  3896. p^.registers32:=max(p^.registers32,hp^.left^.registers32);
  3897. p^.registersfpu:=max(p^.registersfpu,hp^.left^.registersfpu);;
  3898. {$ifdef SUPPORT_MMX}
  3899. p^.registersmmx:=max(p^.registersmmx,hp^.left^.registersmmx);
  3900. {$endif SUPPORT_MMX}
  3901. hp:=hp^.right;
  3902. end;
  3903. { result type is already set }
  3904. end;
  3905. procedure firstin(var p : ptree);
  3906. begin
  3907. p^.location.loc:=LOC_FLAGS;
  3908. p^.resulttype:=booldef;
  3909. firstpass(p^.right);
  3910. if codegenerror then
  3911. exit;
  3912. if p^.right^.resulttype^.deftype<>setdef then
  3913. Message(sym_e_set_expected);
  3914. firstpass(p^.left);
  3915. if codegenerror then
  3916. exit;
  3917. p^.left:=gentypeconvnode(p^.left,psetdef(p^.right^.resulttype)^.setof);
  3918. firstpass(p^.left);
  3919. if codegenerror then
  3920. exit;
  3921. left_right_max(p);
  3922. { this is not allways true due to optimization }
  3923. { but if we don't set this we get problems with optimizing self code }
  3924. if psetdef(p^.right^.resulttype)^.settype<>smallset then
  3925. procinfo.flags:=procinfo.flags or pi_do_call;
  3926. end;
  3927. procedure firststatement(var p : ptree);
  3928. begin
  3929. { left is the next statement in the list }
  3930. p^.resulttype:=voiddef;
  3931. { no temps over several statements }
  3932. cleartempgen;
  3933. { right is the statement itself calln assignn or a complex one }
  3934. firstpass(p^.right);
  3935. if (not (cs_extsyntax in aktswitches)) and
  3936. assigned(p^.right^.resulttype) and
  3937. (p^.right^.resulttype<>pdef(voiddef)) then
  3938. Message(cg_e_illegal_expression);
  3939. if codegenerror then
  3940. exit;
  3941. p^.registers32:=p^.right^.registers32;
  3942. p^.registersfpu:=p^.right^.registersfpu;
  3943. {$ifdef SUPPORT_MMX}
  3944. p^.registersmmx:=p^.right^.registersmmx;
  3945. {$endif SUPPORT_MMX}
  3946. { left is the next in the list }
  3947. firstpass(p^.left);
  3948. if codegenerror then
  3949. exit;
  3950. if p^.right^.registers32>p^.registers32 then
  3951. p^.registers32:=p^.right^.registers32;
  3952. if p^.right^.registersfpu>p^.registersfpu then
  3953. p^.registersfpu:=p^.right^.registersfpu;
  3954. {$ifdef SUPPORT_MMX}
  3955. if p^.right^.registersmmx>p^.registersmmx then
  3956. p^.registersmmx:=p^.right^.registersmmx;
  3957. {$endif}
  3958. end;
  3959. procedure firstblock(var p : ptree);
  3960. var
  3961. hp : ptree;
  3962. count : longint;
  3963. begin
  3964. count:=0;
  3965. hp:=p^.left;
  3966. while assigned(hp) do
  3967. begin
  3968. if cs_maxoptimieren in aktswitches then
  3969. begin
  3970. { Codeumstellungen }
  3971. { Funktionsresultate an exit anh„ngen }
  3972. { this is wrong for string or other complex
  3973. result types !!! }
  3974. if ret_in_acc(procinfo.retdef) and
  3975. assigned(hp^.left) and
  3976. (hp^.left^.right^.treetype=exitn) and
  3977. (hp^.right^.treetype=assignn) and
  3978. (hp^.right^.left^.treetype=funcretn) then
  3979. begin
  3980. if assigned(hp^.left^.right^.left) then
  3981. Message(cg_n_inefficient_code)
  3982. else
  3983. begin
  3984. hp^.left^.right^.left:=getcopy(hp^.right^.right);
  3985. disposetree(hp^.right);
  3986. hp^.right:=nil;
  3987. end;
  3988. end
  3989. { warning if unreachable code occurs and elimate this }
  3990. else if (hp^.right^.treetype in
  3991. [exitn,breakn,continuen,goton]) and
  3992. assigned(hp^.left) and
  3993. (hp^.left^.treetype<>labeln) then
  3994. begin
  3995. { use correct line number }
  3996. aktfilepos:=hp^.left^.fileinfo;
  3997. disposetree(hp^.left);
  3998. hp^.left:=nil;
  3999. Message(cg_w_unreachable_code);
  4000. { old lines }
  4001. aktfilepos:=hp^.right^.fileinfo;
  4002. end;
  4003. end;
  4004. if assigned(hp^.right) then
  4005. begin
  4006. cleartempgen;
  4007. firstpass(hp^.right);
  4008. if (not (cs_extsyntax in aktswitches)) and
  4009. assigned(hp^.right^.resulttype) and
  4010. (hp^.right^.resulttype<>pdef(voiddef)) then
  4011. Message(cg_e_illegal_expression);
  4012. if codegenerror then
  4013. exit;
  4014. hp^.registers32:=hp^.right^.registers32;
  4015. hp^.registersfpu:=hp^.right^.registersfpu;
  4016. {$ifdef SUPPORT_MMX}
  4017. hp^.registersmmx:=hp^.right^.registersmmx;
  4018. {$endif SUPPORT_MMX}
  4019. end
  4020. else
  4021. hp^.registers32:=0;
  4022. if hp^.registers32>p^.registers32 then
  4023. p^.registers32:=hp^.registers32;
  4024. if hp^.registersfpu>p^.registersfpu then
  4025. p^.registersfpu:=hp^.registersfpu;
  4026. {$ifdef SUPPORT_MMX}
  4027. if hp^.registersmmx>p^.registersmmx then
  4028. p^.registersmmx:=hp^.registersmmx;
  4029. {$endif}
  4030. inc(count);
  4031. hp:=hp^.left;
  4032. end;
  4033. { p^.registers32:=round(p^.registers32/count); }
  4034. end;
  4035. procedure first_while_repeat(var p : ptree);
  4036. var
  4037. old_t_times : longint;
  4038. begin
  4039. old_t_times:=t_times;
  4040. { Registergewichtung bestimmen }
  4041. if not(cs_littlesize in aktswitches ) then
  4042. t_times:=t_times*8;
  4043. cleartempgen;
  4044. must_be_valid:=true;
  4045. firstpass(p^.left);
  4046. if codegenerror then
  4047. exit;
  4048. if not((p^.left^.resulttype^.deftype=orddef) and
  4049. (porddef(p^.left^.resulttype)^.typ=bool8bit)) then
  4050. begin
  4051. Message(sym_e_type_mismatch);
  4052. exit;
  4053. end;
  4054. p^.registers32:=p^.left^.registers32;
  4055. p^.registersfpu:=p^.left^.registersfpu;
  4056. {$ifdef SUPPORT_MMX}
  4057. p^.registersmmx:=p^.left^.registersmmx;
  4058. {$endif SUPPORT_MMX}
  4059. { loop instruction }
  4060. if assigned(p^.right) then
  4061. begin
  4062. cleartempgen;
  4063. firstpass(p^.right);
  4064. if codegenerror then
  4065. exit;
  4066. if p^.registers32<p^.right^.registers32 then
  4067. p^.registers32:=p^.right^.registers32;
  4068. if p^.registersfpu<p^.right^.registersfpu then
  4069. p^.registersfpu:=p^.right^.registersfpu;
  4070. {$ifdef SUPPORT_MMX}
  4071. if p^.registersmmx<p^.right^.registersmmx then
  4072. p^.registersmmx:=p^.right^.registersmmx;
  4073. {$endif SUPPORT_MMX}
  4074. end;
  4075. t_times:=old_t_times;
  4076. end;
  4077. procedure firstif(var p : ptree);
  4078. var
  4079. old_t_times : longint;
  4080. hp : ptree;
  4081. begin
  4082. old_t_times:=t_times;
  4083. cleartempgen;
  4084. must_be_valid:=true;
  4085. firstpass(p^.left);
  4086. if codegenerror then
  4087. exit;
  4088. if not((p^.left^.resulttype^.deftype=orddef) and
  4089. (porddef(p^.left^.resulttype)^.typ in [bool8bit,bool16bit,bool32bit])) then
  4090. begin
  4091. Message(sym_e_type_mismatch);
  4092. exit;
  4093. end;
  4094. p^.registers32:=p^.left^.registers32;
  4095. p^.registersfpu:=p^.left^.registersfpu;
  4096. {$ifdef SUPPORT_MMX}
  4097. p^.registersmmx:=p^.left^.registersmmx;
  4098. {$endif SUPPORT_MMX}
  4099. { determines registers weigths }
  4100. if not(cs_littlesize in aktswitches ) then
  4101. t_times:=t_times div 2;
  4102. if t_times=0 then
  4103. t_times:=1;
  4104. { if path }
  4105. if assigned(p^.right) then
  4106. begin
  4107. cleartempgen;
  4108. firstpass(p^.right);
  4109. if codegenerror then
  4110. exit;
  4111. if p^.registers32<p^.right^.registers32 then
  4112. p^.registers32:=p^.right^.registers32;
  4113. if p^.registersfpu<p^.right^.registersfpu then
  4114. p^.registersfpu:=p^.right^.registersfpu;
  4115. {$ifdef SUPPORT_MMX}
  4116. if p^.registersmmx<p^.right^.registersmmx then
  4117. p^.registersmmx:=p^.right^.registersmmx;
  4118. {$endif SUPPORT_MMX}
  4119. end;
  4120. { else path }
  4121. if assigned(p^.t1) then
  4122. begin
  4123. cleartempgen;
  4124. firstpass(p^.t1);
  4125. if codegenerror then
  4126. exit;
  4127. if p^.registers32<p^.t1^.registers32 then
  4128. p^.registers32:=p^.t1^.registers32;
  4129. if p^.registersfpu<p^.t1^.registersfpu then
  4130. p^.registersfpu:=p^.t1^.registersfpu;
  4131. {$ifdef SUPPORT_MMX}
  4132. if p^.registersmmx<p^.t1^.registersmmx then
  4133. p^.registersmmx:=p^.t1^.registersmmx;
  4134. {$endif SUPPORT_MMX}
  4135. end;
  4136. if p^.left^.treetype=ordconstn then
  4137. begin
  4138. { optimize }
  4139. if p^.left^.value=1 then
  4140. begin
  4141. disposetree(p^.left);
  4142. hp:=p^.right;
  4143. disposetree(p^.t1);
  4144. { we cannot set p to nil !!! }
  4145. if assigned(hp) then
  4146. begin
  4147. putnode(p);
  4148. p:=hp;
  4149. end
  4150. else
  4151. begin
  4152. p^.left:=nil;
  4153. p^.t1:=nil;
  4154. p^.treetype:=nothingn;
  4155. end;
  4156. end
  4157. else
  4158. begin
  4159. disposetree(p^.left);
  4160. hp:=p^.t1;
  4161. disposetree(p^.right);
  4162. { we cannot set p to nil !!! }
  4163. if assigned(hp) then
  4164. begin
  4165. putnode(p);
  4166. p:=hp;
  4167. end
  4168. else
  4169. begin
  4170. p^.left:=nil;
  4171. p^.right:=nil;
  4172. p^.treetype:=nothingn;
  4173. end;
  4174. end;
  4175. end;
  4176. t_times:=old_t_times;
  4177. end;
  4178. procedure firstexitn(var p : ptree);
  4179. begin
  4180. if assigned(p^.left) then
  4181. begin
  4182. firstpass(p^.left);
  4183. p^.registers32:=p^.left^.registers32;
  4184. p^.registersfpu:=p^.left^.registersfpu;
  4185. {$ifdef SUPPORT_MMX}
  4186. p^.registersmmx:=p^.left^.registersmmx;
  4187. {$endif SUPPORT_MMX}
  4188. end;
  4189. end;
  4190. procedure firstfor(var p : ptree);
  4191. var
  4192. old_t_times : longint;
  4193. begin
  4194. { Registergewichtung bestimmen
  4195. (nicht genau), }
  4196. old_t_times:=t_times;
  4197. if not(cs_littlesize in aktswitches ) then
  4198. t_times:=t_times*8;
  4199. cleartempgen;
  4200. if p^.t1<>nil then
  4201. firstpass(p^.t1);
  4202. p^.registers32:=p^.t1^.registers32;
  4203. p^.registersfpu:=p^.t1^.registersfpu;
  4204. {$ifdef SUPPORT_MMX}
  4205. p^.registersmmx:=p^.left^.registersmmx;
  4206. {$endif SUPPORT_MMX}
  4207. if p^.left^.treetype<>assignn then
  4208. Message(cg_e_illegal_expression);
  4209. { Laufvariable retten }
  4210. p^.t2:=getcopy(p^.left^.left);
  4211. { Check count var }
  4212. if (p^.t2^.treetype<>loadn) then
  4213. Message(cg_e_illegal_count_var);
  4214. if (not(is_ordinal(p^.t2^.resulttype))) then
  4215. Message(parser_e_ordinal_expected);
  4216. cleartempgen;
  4217. must_be_valid:=false;
  4218. firstpass(p^.left);
  4219. must_be_valid:=true;
  4220. if p^.left^.registers32>p^.registers32 then
  4221. p^.registers32:=p^.left^.registers32;
  4222. if p^.left^.registersfpu>p^.registersfpu then
  4223. p^.registersfpu:=p^.left^.registersfpu;
  4224. {$ifdef SUPPORT_MMX}
  4225. if p^.left^.registersmmx>p^.registersmmx then
  4226. p^.registersmmx:=p^.left^.registersmmx;
  4227. {$endif SUPPORT_MMX}
  4228. cleartempgen;
  4229. firstpass(p^.t2);
  4230. if p^.t2^.registers32>p^.registers32 then
  4231. p^.registers32:=p^.t2^.registers32;
  4232. if p^.t2^.registersfpu>p^.registersfpu then
  4233. p^.registersfpu:=p^.t2^.registersfpu;
  4234. {$ifdef SUPPORT_MMX}
  4235. if p^.t2^.registersmmx>p^.registersmmx then
  4236. p^.registersmmx:=p^.t2^.registersmmx;
  4237. {$endif SUPPORT_MMX}
  4238. cleartempgen;
  4239. firstpass(p^.right);
  4240. if p^.right^.treetype<>ordconstn then
  4241. begin
  4242. p^.right:=gentypeconvnode(p^.right,p^.t2^.resulttype);
  4243. cleartempgen;
  4244. firstpass(p^.right);
  4245. end;
  4246. if p^.right^.registers32>p^.registers32 then
  4247. p^.registers32:=p^.right^.registers32;
  4248. if p^.right^.registersfpu>p^.registersfpu then
  4249. p^.registersfpu:=p^.right^.registersfpu;
  4250. {$ifdef SUPPORT_MMX}
  4251. if p^.right^.registersmmx>p^.registersmmx then
  4252. p^.registersmmx:=p^.right^.registersmmx;
  4253. {$endif SUPPORT_MMX}
  4254. t_times:=old_t_times;
  4255. end;
  4256. procedure firstasm(var p : ptree);
  4257. begin
  4258. { it's a f... to determine the used registers }
  4259. { should be done by getnode
  4260. I think also, that all values should be set to their maximum (FK)
  4261. p^.registers32:=0;
  4262. p^.registersfpu:=0;
  4263. p^.registersmmx:=0;
  4264. }
  4265. procinfo.flags:=procinfo.flags or pi_uses_asm;
  4266. end;
  4267. procedure firstgoto(var p : ptree);
  4268. begin
  4269. {
  4270. p^.registers32:=0;
  4271. p^.registersfpu:=0;
  4272. }
  4273. p^.resulttype:=voiddef;
  4274. end;
  4275. procedure firstlabel(var p : ptree);
  4276. begin
  4277. cleartempgen;
  4278. firstpass(p^.left);
  4279. p^.registers32:=p^.left^.registers32;
  4280. p^.registersfpu:=p^.left^.registersfpu;
  4281. {$ifdef SUPPORT_MMX}
  4282. p^.registersmmx:=p^.left^.registersmmx;
  4283. {$endif SUPPORT_MMX}
  4284. p^.resulttype:=voiddef;
  4285. end;
  4286. procedure firstcase(var p : ptree);
  4287. var
  4288. old_t_times : longint;
  4289. hp : ptree;
  4290. begin
  4291. { evalutes the case expression }
  4292. cleartempgen;
  4293. must_be_valid:=true;
  4294. firstpass(p^.left);
  4295. if codegenerror then
  4296. exit;
  4297. p^.registers32:=p^.left^.registers32;
  4298. p^.registersfpu:=p^.left^.registersfpu;
  4299. {$ifdef SUPPORT_MMX}
  4300. p^.registersmmx:=p^.left^.registersmmx;
  4301. {$endif SUPPORT_MMX}
  4302. { walk through all instructions }
  4303. { estimates the repeat of each instruction }
  4304. old_t_times:=t_times;
  4305. if not(cs_littlesize in aktswitches ) then
  4306. begin
  4307. t_times:=t_times div case_count_labels(p^.nodes);
  4308. if t_times<1 then
  4309. t_times:=1;
  4310. end;
  4311. { first case }
  4312. hp:=p^.right;
  4313. while assigned(hp) do
  4314. begin
  4315. cleartempgen;
  4316. firstpass(hp^.right);
  4317. { searchs max registers }
  4318. if hp^.right^.registers32>p^.registers32 then
  4319. p^.registers32:=hp^.right^.registers32;
  4320. if hp^.right^.registersfpu>p^.registersfpu then
  4321. p^.registersfpu:=hp^.right^.registersfpu;
  4322. {$ifdef SUPPORT_MMX}
  4323. if hp^.right^.registersmmx>p^.registersmmx then
  4324. p^.registersmmx:=hp^.right^.registersmmx;
  4325. {$endif SUPPORT_MMX}
  4326. hp:=hp^.left;
  4327. end;
  4328. { may be handle else tree }
  4329. if assigned(p^.elseblock) then
  4330. begin
  4331. cleartempgen;
  4332. firstpass(p^.elseblock);
  4333. if codegenerror then
  4334. exit;
  4335. if p^.registers32<p^.elseblock^.registers32 then
  4336. p^.registers32:=p^.elseblock^.registers32;
  4337. if p^.registersfpu<p^.elseblock^.registersfpu then
  4338. p^.registersfpu:=p^.elseblock^.registersfpu;
  4339. {$ifdef SUPPORT_MMX}
  4340. if p^.registersmmx<p^.elseblock^.registersmmx then
  4341. p^.registersmmx:=p^.elseblock^.registersmmx;
  4342. {$endif SUPPORT_MMX}
  4343. end;
  4344. t_times:=old_t_times;
  4345. { there is one register required for the case expression }
  4346. if p^.registers32<1 then p^.registers32:=1;
  4347. end;
  4348. procedure firsttryexcept(var p : ptree);
  4349. begin
  4350. end;
  4351. procedure firsttryfinally(var p : ptree);
  4352. begin
  4353. end;
  4354. procedure firstis(var p : ptree);
  4355. begin
  4356. firstpass(p^.left);
  4357. firstpass(p^.right);
  4358. if (p^.right^.resulttype^.deftype<>classrefdef) then
  4359. Message(sym_e_type_mismatch);
  4360. if codegenerror then
  4361. exit;
  4362. left_right_max(p);
  4363. { left must be a class }
  4364. if (p^.left^.resulttype^.deftype<>objectdef) or
  4365. not(pobjectdef(p^.left^.resulttype)^.isclass) then
  4366. Message(sym_e_type_mismatch);
  4367. { the operands must be related }
  4368. if (not(pobjectdef(p^.left^.resulttype)^.isrelated(
  4369. pobjectdef(pclassrefdef(p^.right^.resulttype)^.definition)))) and
  4370. (not(pobjectdef(pclassrefdef(p^.right^.resulttype)^.definition)^.isrelated(
  4371. pobjectdef(p^.left^.resulttype)))) then
  4372. Message(sym_e_type_mismatch);
  4373. p^.location.loc:=LOC_FLAGS;
  4374. p^.resulttype:=booldef;
  4375. end;
  4376. procedure firstas(var p : ptree);
  4377. begin
  4378. firstpass(p^.right);
  4379. firstpass(p^.left);
  4380. if (p^.right^.resulttype^.deftype<>classrefdef) then
  4381. Message(sym_e_type_mismatch);
  4382. if codegenerror then
  4383. exit;
  4384. left_right_max(p);
  4385. (* this was wrong,no ??
  4386. p^.registersfpu:=max(p^.left^.registersfpu,p^.left^.registersfpu);
  4387. p^.registers32:=max(p^.left^.registers32,p^.right^.registers32);
  4388. {$ifdef SUPPORT_MMX}
  4389. p^.registersmmx:=max(p^.left^.registersmmx,p^.right^.registersmmx);
  4390. {$endif SUPPORT_MMX} *)
  4391. { left must be a class }
  4392. if (p^.left^.resulttype^.deftype<>objectdef) or
  4393. not(pobjectdef(p^.left^.resulttype)^.isclass) then
  4394. Message(sym_e_type_mismatch);
  4395. { the operands must be related }
  4396. if (not(pobjectdef(p^.left^.resulttype)^.isrelated(
  4397. pobjectdef(pclassrefdef(p^.right^.resulttype)^.definition)))) and
  4398. (not(pobjectdef(pclassrefdef(p^.right^.resulttype)^.definition)^.isrelated(
  4399. pobjectdef(p^.left^.resulttype)))) then
  4400. Message(sym_e_type_mismatch);
  4401. p^.location:=p^.left^.location;
  4402. p^.resulttype:=pclassrefdef(p^.right^.resulttype)^.definition;
  4403. end;
  4404. procedure firstloadvmt(var p : ptree);
  4405. begin
  4406. { resulttype must be set !
  4407. p^.registersfpu:=0;
  4408. }
  4409. p^.registers32:=1;
  4410. p^.location.loc:=LOC_REGISTER;
  4411. end;
  4412. procedure firstraise(var p : ptree);
  4413. begin
  4414. p^.resulttype:=voiddef;
  4415. {
  4416. p^.registersfpu:=0;
  4417. p^.registers32:=0;
  4418. }
  4419. if assigned(p^.left) then
  4420. begin
  4421. firstpass(p^.left);
  4422. { this must be a _class_ }
  4423. if (p^.left^.resulttype^.deftype<>objectdef) or
  4424. ((pobjectdef(p^.left^.resulttype)^.options and oois_class)=0) then
  4425. Message(sym_e_type_mismatch);
  4426. p^.registersfpu:=p^.left^.registersfpu;
  4427. p^.registers32:=p^.left^.registers32;
  4428. {$ifdef SUPPORT_MMX}
  4429. p^.registersmmx:=p^.left^.registersmmx;
  4430. {$endif SUPPORT_MMX}
  4431. if assigned(p^.right) then
  4432. begin
  4433. firstpass(p^.right);
  4434. p^.right:=gentypeconvnode(p^.right,s32bitdef);
  4435. firstpass(p^.right);
  4436. left_right_max(p);
  4437. end;
  4438. end;
  4439. end;
  4440. procedure firstwith(var p : ptree);
  4441. begin
  4442. if assigned(p^.left) and assigned(p^.right) then
  4443. begin
  4444. firstpass(p^.left);
  4445. if codegenerror then
  4446. exit;
  4447. firstpass(p^.right);
  4448. if codegenerror then
  4449. exit;
  4450. left_right_max(p);
  4451. p^.resulttype:=voiddef;
  4452. end
  4453. else
  4454. begin
  4455. { optimization }
  4456. disposetree(p);
  4457. p:=nil;
  4458. end;
  4459. end;
  4460. procedure firstprocinline(var p : ptree);
  4461. begin
  4462. {left contains the code in tree form }
  4463. { but it has already been firstpassed }
  4464. { so firstpass(p^.left); does not seem required }
  4465. { might be required later if we change the arg handling !! }
  4466. end;
  4467. type
  4468. firstpassproc = procedure(var p : ptree);
  4469. procedure firstpass(var p : ptree);
  4470. (* ttreetyp = (addn, {Represents the + operator.}
  4471. muln, {Represents the * operator.}
  4472. subn, {Represents the - operator.}
  4473. divn, {Represents the div operator.}
  4474. symdifn, {Represents the >< operator.}
  4475. modn, {Represents the mod operator.}
  4476. assignn, {Represents an assignment.}
  4477. loadn, {Represents the use of a variabele.}
  4478. rangen, {Represents a range (i.e. 0..9).}
  4479. ltn, {Represents the < operator.}
  4480. lten, {Represents the <= operator.}
  4481. gtn, {Represents the > operator.}
  4482. gten, {Represents the >= operator.}
  4483. equaln, {Represents the = operator.}
  4484. unequaln, {Represents the <> operator.}
  4485. inn, {Represents the in operator.}
  4486. orn, {Represents the or operator.}
  4487. xorn, {Represents the xor operator.}
  4488. shrn, {Represents the shr operator.}
  4489. shln, {Represents the shl operator.}
  4490. slashn, {Represents the / operator.}
  4491. andn, {Represents the and operator.}
  4492. subscriptn, {??? Field in a record/object?}
  4493. derefn, {Dereferences a pointer.}
  4494. addrn, {Represents the @ operator.}
  4495. doubleaddrn, {Represents the @@ operator.}
  4496. ordconstn, {Represents an ordinal value.}
  4497. typeconvn, {Represents type-conversion/typecast.}
  4498. calln, {Represents a call node.}
  4499. callparan, {Represents a parameter.}
  4500. realconstn, {Represents a real value.}
  4501. fixconstn, {Represents a fixed value.}
  4502. umminusn, {Represents a sign change (i.e. -2).}
  4503. asmn, {Represents an assembler node }
  4504. vecn, {Represents array indexing.}
  4505. stringconstn, {Represents a string constant.}
  4506. funcretn, {Represents the function result var.}
  4507. selfn, {Represents the self parameter.}
  4508. notn, {Represents the not operator.}
  4509. inlinen, {Internal procedures (i.e. writeln).}
  4510. niln, {Represents the nil pointer.}
  4511. errorn, {This part of the tree could not be
  4512. parsed because of a compiler error.}
  4513. typen, {A type name. Used for i.e. typeof(obj).}
  4514. hnewn, {The new operation, constructor call.}
  4515. hdisposen, {The dispose operation with destructor call.}
  4516. newn, {The new operation, constructor call.}
  4517. simpledisposen, {The dispose operation.}
  4518. setelen, {A set element (i.e. [a,b]).}
  4519. setconstrn, {A set constant (i.e. [1,2]).}
  4520. blockn, {A block of statements.}
  4521. statementn, {One statement in list of nodes.}
  4522. loopn, { used in genloopnode, must be converted }
  4523. ifn, {An if statement.}
  4524. breakn, {A break statement.}
  4525. continuen, {A continue statement.}
  4526. repeatn, {A repeat until block.}
  4527. whilen, {A while do statement.}
  4528. forn, {A for loop.}
  4529. exitn, {An exit statement.}
  4530. withn, {A with statement.}
  4531. casen, {A case statement.}
  4532. labeln, {A label.}
  4533. goton, {A goto statement.}
  4534. simplenewn, {The new operation.}
  4535. tryexceptn, {A try except block.}
  4536. raisen, {A raise statement.}
  4537. switchesn, {??? Currently unused...}
  4538. tryfinallyn, {A try finally statement.}
  4539. isn, {Represents the is operator.}
  4540. asn, {Represents the as typecast.}
  4541. caretn, {Represents the ^ operator.}
  4542. failn, {Represents the fail statement.}
  4543. starstarn, {Represents the ** operator exponentiation }
  4544. procinlinen, {Procedures that can be inlined }
  4545. { added for optimizations where we cannot suppress }
  4546. nothingn,
  4547. loadvmtn); {???.} *)
  4548. const
  4549. procedures : array[ttreetyp] of firstpassproc =
  4550. (firstadd,firstadd,firstadd,firstmoddiv,firstadd,
  4551. firstmoddiv,firstassignment,firstload,firstrange,
  4552. firstadd,firstadd,firstadd,firstadd,
  4553. firstadd,firstadd,firstin,firstadd,
  4554. firstadd,firstshlshr,firstshlshr,firstadd,
  4555. firstadd,firstsubscriptn,firstderef,firstaddr,firstdoubleaddr,
  4556. firstordconst,firsttypeconv,firstcalln,firstnothing,
  4557. firstrealconst,firstfixconst,firstumminus,firstasm,firstvecn,
  4558. firststringconst,firstfuncret,firstselfn,
  4559. firstnot,firstinline,firstniln,firsterror,
  4560. firsttypen,firsthnewn,firsthdisposen,firstnewn,
  4561. firstsimplenewdispose,firstnothing,firstsetcons,firstblock,
  4562. firststatement,firstnothing,firstif,firstnothing,
  4563. firstnothing,first_while_repeat,first_while_repeat,firstfor,
  4564. firstexitn,firstwith,firstcase,firstlabel,
  4565. firstgoto,firstsimplenewdispose,firsttryexcept,firstraise,
  4566. firstnothing,firsttryfinally,firstis,firstas,firstadd,
  4567. firstnothing,firstadd,firstprocinline,firstnothing,firstloadvmt);
  4568. var
  4569. oldcodegenerror : boolean;
  4570. oldswitches : Tcswitches;
  4571. oldpos : tfileposinfo;
  4572. {$ifdef extdebug}
  4573. str1,str2 : string;
  4574. oldp : ptree;
  4575. not_first : boolean;
  4576. {$endif extdebug}
  4577. begin
  4578. {$ifdef extdebug}
  4579. if (p^.firstpasscount>0) and only_one_pass then
  4580. exit;
  4581. {$endif extdebug}
  4582. oldcodegenerror:=codegenerror;
  4583. oldpos:=aktfilepos;
  4584. oldswitches:=aktswitches;
  4585. {$ifdef extdebug}
  4586. if p^.firstpasscount>0 then
  4587. begin
  4588. move(p^,str1[1],sizeof(ttree));
  4589. str1[0]:=char(sizeof(ttree));
  4590. new(oldp);
  4591. oldp^:=p^;
  4592. not_first:=true;
  4593. end
  4594. else
  4595. not_first:=false;
  4596. {$endif extdebug}
  4597. aktfilepos:=p^.fileinfo;
  4598. aktswitches:=p^.pragmas;
  4599. if not p^.error then
  4600. begin
  4601. codegenerror:=false;
  4602. procedures[p^.treetype](p);
  4603. p^.error:=codegenerror;
  4604. codegenerror:=codegenerror or oldcodegenerror;
  4605. end
  4606. else
  4607. codegenerror:=true;
  4608. {$ifdef extdebug}
  4609. if not_first then
  4610. begin
  4611. { dirty trick to compare two ttree's (PM) }
  4612. move(p^,str2[1],sizeof(ttree));
  4613. str2[0]:=char(sizeof(ttree));
  4614. if str1<>str2 then
  4615. begin
  4616. comment(v_debug,'tree changed after first counting pass '
  4617. +tostr(longint(p^.treetype)));
  4618. compare_trees(oldp,p);
  4619. end;
  4620. dispose(oldp);
  4621. end;
  4622. if count_ref then
  4623. inc(p^.firstpasscount);
  4624. {$endif extdebug}
  4625. aktswitches:=oldswitches;
  4626. aktfilepos:=oldpos;
  4627. end;
  4628. function do_firstpass(var p : ptree) : boolean;
  4629. begin
  4630. codegenerror:=false;
  4631. firstpass(p);
  4632. do_firstpass:=codegenerror;
  4633. end;
  4634. { to be called only for a whole function }
  4635. { to insert code at entry and exit }
  4636. function function_firstpass(var p : ptree) : boolean;
  4637. begin
  4638. codegenerror:=false;
  4639. firstpass(p);
  4640. function_firstpass:=codegenerror;
  4641. end;
  4642. end.
  4643. {
  4644. $Log$
  4645. Revision 1.41 1998-07-18 22:54:27 florian
  4646. * some ansi/wide/longstring support fixed:
  4647. o parameter passing
  4648. o returning as result from functions
  4649. Revision 1.40 1998/07/18 17:11:09 florian
  4650. + ansi string constants fixed
  4651. + switch $H partial implemented
  4652. Revision 1.39 1998/07/14 21:46:47 peter
  4653. * updated messages file
  4654. Revision 1.38 1998/07/14 14:46:50 peter
  4655. * released NEWINPUT
  4656. Revision 1.37 1998/07/07 12:31:44 peter
  4657. * fixed string:= which allowed almost any type
  4658. Revision 1.36 1998/07/07 11:20:00 peter
  4659. + NEWINPUT for a better inputfile and scanner object
  4660. Revision 1.35 1998/06/25 14:04:19 peter
  4661. + internal inc/dec
  4662. Revision 1.34 1998/06/25 08:48:14 florian
  4663. * first version of rtti support
  4664. Revision 1.33 1998/06/16 08:56:24 peter
  4665. + targetcpu
  4666. * cleaner pmodules for newppu
  4667. Revision 1.32 1998/06/14 18:23:57 peter
  4668. * fixed xor bug (from mailinglist)
  4669. Revision 1.31 1998/06/13 00:10:09 peter
  4670. * working browser and newppu
  4671. * some small fixes against crashes which occured in bp7 (but not in
  4672. fpc?!)
  4673. Revision 1.30 1998/06/12 10:32:28 pierre
  4674. * column problem hopefully solved
  4675. + C vars declaration changed
  4676. Revision 1.29 1998/06/09 16:01:44 pierre
  4677. + added procedure directive parsing for procvars
  4678. (accepted are popstack cdecl and pascal)
  4679. + added C vars with the following syntax
  4680. var C calias 'true_c_name';(can be followed by external)
  4681. reason is that you must add the Cprefix
  4682. which is target dependent
  4683. Revision 1.28 1998/06/05 14:37:29 pierre
  4684. * fixes for inline for operators
  4685. * inline procedure more correctly restricted
  4686. Revision 1.27 1998/06/05 00:01:06 florian
  4687. * bugs with assigning related objects and passing objects by reference
  4688. to a procedure
  4689. Revision 1.26 1998/06/04 09:55:39 pierre
  4690. * demangled name of procsym reworked to become independant
  4691. of the mangling scheme
  4692. Revision 1.25 1998/06/03 22:48:57 peter
  4693. + wordbool,longbool
  4694. * rename bis,von -> high,low
  4695. * moved some systemunit loading/creating to psystem.pas
  4696. Revision 1.24 1998/06/02 17:03:01 pierre
  4697. * with node corrected for objects
  4698. * small bugs for SUPPORT_MMX fixed
  4699. Revision 1.23 1998/06/01 16:50:20 peter
  4700. + boolean -> ord conversion
  4701. * fixed ord -> boolean conversion
  4702. Revision 1.22 1998/05/28 17:26:49 peter
  4703. * fixed -R switch, it didn't work after my previous akt/init patch
  4704. * fixed bugs 110,130,136
  4705. Revision 1.21 1998/05/25 17:11:41 pierre
  4706. * firstpasscount bug fixed
  4707. now all is already set correctly the first time
  4708. under EXTDEBUG try -gp to skip all other firstpasses
  4709. it works !!
  4710. * small bug fixes
  4711. - for smallsets with -dTESTSMALLSET
  4712. - some warnings removed (by correcting code !)
  4713. Revision 1.20 1998/05/23 01:21:17 peter
  4714. + aktasmmode, aktoptprocessor, aktoutputformat
  4715. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  4716. + $LIBNAME to set the library name where the unit will be put in
  4717. * splitted cgi386 a bit (codeseg to large for bp7)
  4718. * nasm, tasm works again. nasm moved to ag386nsm.pas
  4719. Revision 1.19 1998/05/20 09:42:34 pierre
  4720. + UseTokenInfo now default
  4721. * unit in interface uses and implementation uses gives error now
  4722. * only one error for unknown symbol (uses lastsymknown boolean)
  4723. the problem came from the label code !
  4724. + first inlined procedures and function work
  4725. (warning there might be allowed cases were the result is still wrong !!)
  4726. * UseBrower updated gives a global list of all position of all used symbols
  4727. with switch -gb
  4728. Revision 1.18 1998/05/11 13:07:55 peter
  4729. + $ifdef NEWPPU for the new ppuformat
  4730. + $define GDB not longer required
  4731. * removed all warnings and stripped some log comments
  4732. * no findfirst/findnext anymore to remove smartlink *.o files
  4733. Revision 1.17 1998/05/06 08:38:43 pierre
  4734. * better position info with UseTokenInfo
  4735. UseTokenInfo greatly simplified
  4736. + added check for changed tree after first time firstpass
  4737. (if we could remove all the cases were it happen
  4738. we could skip all firstpass if firstpasscount > 1)
  4739. Only with ExtDebug
  4740. Revision 1.16 1998/05/01 16:38:45 florian
  4741. * handling of private and protected fixed
  4742. + change_keywords_to_tp implemented to remove
  4743. keywords which aren't supported by tp
  4744. * break and continue are now symbols of the system unit
  4745. + widestring, longstring and ansistring type released
  4746. Revision 1.15 1998/05/01 09:01:23 florian
  4747. + correct semantics of private and protected
  4748. * small fix in variable scope:
  4749. a id can be used in a parameter list of a method, even it is used in
  4750. an anchestor class as field id
  4751. Revision 1.14 1998/04/30 15:59:41 pierre
  4752. * GDB works again better :
  4753. correct type info in one pass
  4754. + UseTokenInfo for better source position
  4755. * fixed one remaining bug in scanner for line counts
  4756. * several little fixes
  4757. Revision 1.13 1998/04/29 10:33:56 pierre
  4758. + added some code for ansistring (not complete nor working yet)
  4759. * corrected operator overloading
  4760. * corrected nasm output
  4761. + started inline procedures
  4762. + added starstarn : use ** for exponentiation (^ gave problems)
  4763. + started UseTokenInfo cond to get accurate positions
  4764. Revision 1.12 1998/04/22 21:06:50 florian
  4765. * last fixes before the release:
  4766. - veryyyy slow firstcall fixed
  4767. Revision 1.11 1998/04/21 10:16:48 peter
  4768. * patches from strasbourg
  4769. * objects is not used anymore in the fpc compiled version
  4770. Revision 1.10 1998/04/14 23:27:03 florian
  4771. + exclude/include with constant second parameter added
  4772. Revision 1.9 1998/04/13 21:15:42 florian
  4773. * error handling of pass_1 and cgi386 fixed
  4774. * the following bugs fixed: 0117, 0118, 0119 and 0129, 0122 was already
  4775. fixed, verified
  4776. Revision 1.8 1998/04/13 08:42:52 florian
  4777. * call by reference and call by value open arrays fixed
  4778. Revision 1.7 1998/04/12 22:39:44 florian
  4779. * problem with read access to properties solved
  4780. * correct handling of hidding methods via virtual (COM)
  4781. * correct result type of constructor calls (COM), the resulttype
  4782. depends now on the type of the class reference
  4783. Revision 1.6 1998/04/09 22:16:34 florian
  4784. * problem with previous REGALLOC solved
  4785. * improved property support
  4786. Revision 1.5 1998/04/08 16:58:04 pierre
  4787. * several bugfixes
  4788. ADD ADC and AND are also sign extended
  4789. nasm output OK (program still crashes at end
  4790. and creates wrong assembler files !!)
  4791. procsym types sym in tdef removed !!
  4792. Revision 1.4 1998/04/07 22:45:04 florian
  4793. * bug0092, bug0115 and bug0121 fixed
  4794. + packed object/class/array
  4795. }