pass_1.pas 193 KB

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