pass_1.pas 218 KB

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