pass_1.pas 195 KB

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