pass_1.pas 217 KB

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