pass_1.pas 194 KB

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