cgi386.pas 224 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 by Florian Klaempfl
  4. This unit generates i386 (or better) assembler from the parse tree
  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. {$E+,F+,N+,D+,L+,Y+}
  20. {$endif}
  21. unit cgi386;
  22. interface
  23. uses
  24. tree;
  25. { produces assembler for the expression in variable p }
  26. { and produces an assembler node at the end }
  27. procedure generatecode(var p : ptree);
  28. { produces the actual code }
  29. function do_secondpass(var p : ptree) : boolean;
  30. procedure secondpass(var p : ptree);
  31. {$ifdef test_dest_loc}
  32. const
  33. { used to avoid temporary assignments }
  34. dest_loc_known : boolean = false;
  35. in_dest_loc : boolean = false;
  36. dest_loc_tree : ptree = nil;
  37. var
  38. dest_loc : tlocation;
  39. procedure mov_reg_to_dest(p : ptree; s : topsize; reg : tregister);
  40. {$endif test_dest_loc}
  41. implementation
  42. uses
  43. verbose,cobjects,systems,globals,files,
  44. symtable,types,aasm,i386,
  45. pass_1,hcodegen,tgeni386,cgai386
  46. {$ifdef GDB}
  47. ,gdb
  48. {$endif}
  49. {$ifdef TP}
  50. ,cgi3862
  51. {$endif}
  52. ,cg386con,cg386mat,cg386cnv
  53. ;
  54. const
  55. never_copy_const_param : boolean = false;
  56. {$ifdef test_dest_loc}
  57. procedure mov_reg_to_dest(p : ptree; s : topsize; reg : tregister);
  58. begin
  59. if (dest_loc.loc=LOC_CREGISTER) or (dest_loc.loc=LOC_REGISTER) then
  60. begin
  61. emit_reg_reg(A_MOV,s,reg,dest_loc.register);
  62. p^.location:=dest_loc;
  63. in_dest_loc:=true;
  64. end
  65. else
  66. if (dest_loc.loc=LOC_REFERENCE) or (dest_loc.loc=LOC_MEM) then
  67. begin
  68. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,s,reg,newreference(dest_loc.reference))));
  69. p^.location:=dest_loc;
  70. in_dest_loc:=true;
  71. end
  72. else
  73. internalerror(20080);
  74. end;
  75. {$endif test_dest_loc}
  76. const
  77. bytes2Sxx:array[1..4] of Topsize=(S_B,S_W,S_NO,S_L);
  78. procedure message(const t : tmsgconst);
  79. var
  80. olderrorcount : longint;
  81. begin
  82. if not(codegenerror) then
  83. begin
  84. olderrorcount:=status.errorcount;
  85. verbose.Message(t);
  86. codegenerror:=olderrorcount<>status.errorcount;
  87. end;
  88. end;
  89. procedure message1(const t : tmsgconst;const s : string);
  90. var
  91. olderrorcount : longint;
  92. begin
  93. if not(codegenerror) then
  94. begin
  95. olderrorcount:=status.errorcount;
  96. verbose.Message1(t,s);
  97. codegenerror:=olderrorcount<>status.errorcount;
  98. end;
  99. end;
  100. procedure message2(const t : tmsgconst;const s1,s2 : string);
  101. var
  102. olderrorcount : longint;
  103. begin
  104. if not(codegenerror) then
  105. begin
  106. olderrorcount:=status.errorcount;
  107. verbose.Message2(t,s1,s2);
  108. codegenerror:=olderrorcount<>status.errorcount;
  109. end;
  110. end;
  111. procedure message3(const t : tmsgconst;const s1,s2,s3 : string);
  112. var
  113. olderrorcount : longint;
  114. begin
  115. if not(codegenerror) then
  116. begin
  117. olderrorcount:=status.errorcount;
  118. verbose.Message3(t,s1,s2,s3);
  119. codegenerror:=olderrorcount<>status.errorcount;
  120. end;
  121. end;
  122. type
  123. secondpassproc = procedure(var p : ptree);
  124. procedure seconderror(var p : ptree);
  125. begin
  126. p^.error:=true;
  127. codegenerror:=true;
  128. end;
  129. var
  130. { this is for open arrays and strings }
  131. { but be careful, this data is in the }
  132. { generated code destroyed quick, and also }
  133. { the next call of secondload destroys this }
  134. { data }
  135. { So be careful using the informations }
  136. { provided by this variables }
  137. highframepointer : tregister;
  138. highoffset : longint;
  139. {$ifndef TP}
  140. {$I cgi386ad.inc}
  141. {$endif TP}
  142. procedure secondload(var p : ptree);
  143. var
  144. hregister : tregister;
  145. symtabletype : tsymtabletype;
  146. i : longint;
  147. hp : preference;
  148. begin
  149. simple_loadn:=true;
  150. reset_reference(p^.location.reference);
  151. case p^.symtableentry^.typ of
  152. { this is only for toasm and toaddr }
  153. absolutesym :
  154. begin
  155. stringdispose(p^.location.reference.symbol);
  156. if (pabsolutesym(p^.symtableentry)^.abstyp=toaddr) then
  157. begin
  158. if pabsolutesym(p^.symtableentry)^.absseg then
  159. p^.location.reference.segment:=R_FS;
  160. p^.location.reference.offset:=pabsolutesym(p^.symtableentry)^.address;
  161. end
  162. else
  163. p^.location.reference.symbol:=stringdup(p^.symtableentry^.mangledname);
  164. maybe_concat_external(p^.symtableentry^.owner,p^.symtableentry^.mangledname);
  165. end;
  166. varsym :
  167. begin
  168. hregister:=R_NO;
  169. symtabletype:=p^.symtable^.symtabletype;
  170. { in case it is a register variable: }
  171. if pvarsym(p^.symtableentry)^.reg<>R_NO then
  172. begin
  173. p^.location.loc:=LOC_CREGISTER;
  174. p^.location.register:=pvarsym(p^.symtableentry)^.reg;
  175. unused:=unused-[pvarsym(p^.symtableentry)^.reg];
  176. end
  177. else
  178. begin
  179. { first handle local and temporary variables }
  180. if (symtabletype in [parasymtable,inlinelocalsymtable,
  181. inlineparasymtable,localsymtable]) then
  182. begin
  183. p^.location.reference.base:=procinfo.framepointer;
  184. p^.location.reference.offset:=pvarsym(p^.symtableentry)^.address;
  185. if (symtabletype=localsymtable) or (symtabletype=inlinelocalsymtable) then
  186. p^.location.reference.offset:=-p^.location.reference.offset;
  187. if (symtabletype=parasymtable) or (symtabletype=inlineparasymtable) then
  188. inc(p^.location.reference.offset,p^.symtable^.call_offset);
  189. if (lexlevel>(p^.symtable^.symtablelevel)) then
  190. begin
  191. hregister:=getregister32;
  192. { make a reference }
  193. hp:=new_reference(procinfo.framepointer,
  194. procinfo.framepointer_offset);
  195. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,hp,hregister)));
  196. simple_loadn:=false;
  197. i:=lexlevel-1;
  198. while i>(p^.symtable^.symtablelevel) do
  199. begin
  200. { make a reference }
  201. hp:=new_reference(hregister,8);
  202. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,hp,hregister)));
  203. dec(i);
  204. end;
  205. p^.location.reference.base:=hregister;
  206. end;
  207. end
  208. else
  209. case symtabletype of
  210. unitsymtable,globalsymtable,
  211. staticsymtable : begin
  212. stringdispose(p^.location.reference.symbol);
  213. p^.location.reference.symbol:=stringdup(p^.symtableentry^.mangledname);
  214. if symtabletype=unitsymtable then
  215. concat_external(p^.symtableentry^.mangledname,EXT_NEAR);
  216. end;
  217. objectsymtable : begin
  218. if (pvarsym(p^.symtableentry)^.properties and sp_static)<>0 then
  219. begin
  220. stringdispose(p^.location.reference.symbol);
  221. p^.location.reference.symbol:=stringdup(p^.symtableentry^.mangledname);
  222. if p^.symtable^.defowner^.owner^.symtabletype=unitsymtable then
  223. concat_external(p^.symtableentry^.mangledname,EXT_NEAR);
  224. end
  225. else
  226. begin
  227. p^.location.reference.base:=R_ESI;
  228. p^.location.reference.offset:=pvarsym(p^.symtableentry)^.address;
  229. end;
  230. end;
  231. withsymtable:
  232. begin
  233. hregister:=getregister32;
  234. p^.location.reference.base:=hregister;
  235. { make a reference }
  236. { symtable datasize field
  237. contains the offset of the temp
  238. stored }
  239. hp:=new_reference(procinfo.framepointer,
  240. p^.symtable^.datasize);
  241. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,hp,hregister)));
  242. p^.location.reference.offset:=
  243. pvarsym(p^.symtableentry)^.address;
  244. end;
  245. end;
  246. { in case call by reference, then calculate: }
  247. if (pvarsym(p^.symtableentry)^.varspez=vs_var) or
  248. ((pvarsym(p^.symtableentry)^.varspez=vs_const) and
  249. dont_copy_const_param(pvarsym(p^.symtableentry)^.definition)) or
  250. { call by value open arrays are also indirect addressed }
  251. is_open_array(pvarsym(p^.symtableentry)^.definition) then
  252. begin
  253. simple_loadn:=false;
  254. if hregister=R_NO then
  255. hregister:=getregister32;
  256. if (p^.location.reference.base=procinfo.framepointer) then
  257. begin
  258. highframepointer:=p^.location.reference.base;
  259. highoffset:=p^.location.reference.offset;
  260. end
  261. else
  262. begin
  263. highframepointer:=R_EDI;
  264. highoffset:=p^.location.reference.offset;
  265. exprasmlist^.concat(new(pai386,op_reg_reg(A_MOV,S_L,
  266. p^.location.reference.base,R_EDI)));
  267. end;
  268. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,newreference(p^.location.reference),
  269. hregister)));
  270. clear_reference(p^.location.reference);
  271. p^.location.reference.base:=hregister;
  272. end;
  273. {
  274. if (pvarsym(p^.symtableentry)^.definition^.deftype=objectdef) and
  275. ((pobjectdef(pvarsym(p^.symtableentry)^.definition)^.options and oois_class)<>0) then
  276. begin
  277. simple_loadn:=false;
  278. if hregister=R_NO then
  279. hregister:=getregister32;
  280. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,newreference(p^.location.reference),
  281. hregister)));
  282. clear_reference(p^.location.reference);
  283. p^.location.reference.base:=hregister;
  284. end;
  285. }
  286. end;
  287. end;
  288. procsym:
  289. begin
  290. {!!!!! Be aware, work on virtual methods too }
  291. stringdispose(p^.location.reference.symbol);
  292. p^.location.reference.symbol:=
  293. stringdup(pprocsym(p^.symtableentry)^.definition^.mangledname);
  294. maybe_concat_external(p^.symtable,p^.symtableentry^.mangledname);
  295. end;
  296. typedconstsym :
  297. begin
  298. stringdispose(p^.location.reference.symbol);
  299. p^.location.reference.symbol:=stringdup(p^.symtableentry^.mangledname);
  300. maybe_concat_external(p^.symtable,p^.symtableentry^.mangledname);
  301. end;
  302. else internalerror(4);
  303. end;
  304. end;
  305. procedure secondassignment(var p : ptree);
  306. var
  307. opsize : topsize;
  308. otlabel,hlabel,oflabel : plabel;
  309. hregister : tregister;
  310. loc : tloc;
  311. begin
  312. otlabel:=truelabel;
  313. oflabel:=falselabel;
  314. getlabel(truelabel);
  315. getlabel(falselabel);
  316. { calculate left sides }
  317. if not(p^.concat_string) then
  318. secondpass(p^.left);
  319. if codegenerror then
  320. exit;
  321. case p^.left^.location.loc of
  322. LOC_REFERENCE : begin
  323. { in case left operator uses to register }
  324. { but to few are free then LEA }
  325. if (p^.left^.location.reference.base<>R_NO) and
  326. (p^.left^.location.reference.index<>R_NO) and
  327. (usablereg32<p^.right^.registers32) then
  328. begin
  329. del_reference(p^.left^.location.reference);
  330. hregister:=getregister32;
  331. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,newreference(
  332. p^.left^.location.reference),
  333. hregister)));
  334. clear_reference(p^.left^.location.reference);
  335. p^.left^.location.reference.base:=hregister;
  336. p^.left^.location.reference.index:=R_NO;
  337. end;
  338. loc:=LOC_REFERENCE;
  339. end;
  340. LOC_CREGISTER:
  341. loc:=LOC_CREGISTER;
  342. LOC_MMXREGISTER:
  343. loc:=LOC_MMXREGISTER;
  344. LOC_CMMXREGISTER:
  345. loc:=LOC_CMMXREGISTER;
  346. else
  347. begin
  348. Message(cg_e_illegal_expression);
  349. exit;
  350. end;
  351. end;
  352. { lets try to optimize this (PM) }
  353. { define a dest_loc that is the location }
  354. { and a ptree to verify that it is the right }
  355. { place to insert it }
  356. {$ifdef test_dest_loc}
  357. if (aktexprlevel<4) then
  358. begin
  359. dest_loc_known:=true;
  360. dest_loc:=p^.left^.location;
  361. dest_loc_tree:=p^.right;
  362. end;
  363. {$endif test_dest_loc}
  364. if (p^.right^.treetype=realconstn) then
  365. begin
  366. if p^.left^.resulttype^.deftype=floatdef then
  367. begin
  368. case pfloatdef(p^.left^.resulttype)^.typ of
  369. s32real : p^.right^.realtyp:=ait_real_32bit;
  370. s64real : p^.right^.realtyp:=ait_real_64bit;
  371. s80real : p^.right^.realtyp:=ait_real_extended;
  372. { what about f32bit and s64bit }
  373. end;
  374. end;
  375. end;
  376. secondpass(p^.right);
  377. if codegenerror then
  378. exit;
  379. {$ifdef test_dest_loc}
  380. dest_loc_known:=false;
  381. if in_dest_loc then
  382. begin
  383. truelabel:=otlabel;
  384. falselabel:=oflabel;
  385. in_dest_loc:=false;
  386. exit;
  387. end;
  388. {$endif test_dest_loc}
  389. if p^.left^.resulttype^.deftype=stringdef then
  390. begin
  391. {$ifdef UseAnsiString}
  392. if is_ansistring(p^.left^.resulttype) then
  393. begin
  394. { the source and destinations are released
  395. in loadansistring, because an ansi string can
  396. also be in a register
  397. }
  398. loadansistring(p);
  399. end
  400. else
  401. {$endif UseAnsiString}
  402. if not (p^.concat_string) then
  403. begin
  404. { we do not need destination anymore }
  405. del_reference(p^.left^.location.reference);
  406. del_reference(p^.right^.location.reference);
  407. loadstring(p);
  408. ungetiftemp(p^.right^.location.reference);
  409. end
  410. else
  411. begin
  412. { its the only thing we have to do }
  413. del_reference(p^.right^.location.reference);
  414. end
  415. end
  416. else case p^.right^.location.loc of
  417. LOC_REFERENCE,
  418. LOC_MEM : begin
  419. { handle ordinal constants trimmed }
  420. if (p^.right^.treetype in [ordconstn,fixconstn]) or
  421. (loc=LOC_CREGISTER) then
  422. begin
  423. case p^.left^.resulttype^.size of
  424. 1 : opsize:=S_B;
  425. 2 : opsize:=S_W;
  426. 4 : opsize:=S_L;
  427. end;
  428. if loc=LOC_CREGISTER then
  429. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,opsize,
  430. newreference(p^.right^.location.reference),
  431. p^.left^.location.register)))
  432. else
  433. exprasmlist^.concat(new(pai386,op_const_ref(A_MOV,opsize,
  434. p^.right^.location.reference.offset,
  435. newreference(p^.left^.location.reference))));
  436. {exprasmlist^.concat(new(pai386,op_const_loc(A_MOV,opsize,
  437. p^.right^.location.reference.offset,
  438. p^.left^.location)));}
  439. end
  440. else
  441. begin
  442. concatcopy(p^.right^.location.reference,
  443. p^.left^.location.reference,p^.left^.resulttype^.size,false);
  444. ungetiftemp(p^.right^.location.reference);
  445. end;
  446. end;
  447. {$ifdef SUPPORT_MMX}
  448. LOC_CMMXREGISTER,
  449. LOC_MMXREGISTER:
  450. begin
  451. if loc=LOC_CMMXREGISTER then
  452. exprasmlist^.concat(new(pai386,op_reg_reg(A_MOVQ,S_NO,
  453. p^.right^.location.register,p^.left^.location.register)))
  454. else
  455. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOVQ,S_NO,
  456. p^.right^.location.register,newreference(p^.left^.location.reference))));
  457. end;
  458. {$endif SUPPORT_MMX}
  459. LOC_REGISTER,
  460. LOC_CREGISTER : begin
  461. case p^.right^.resulttype^.size of
  462. 1 : opsize:=S_B;
  463. 2 : opsize:=S_W;
  464. 4 : opsize:=S_L;
  465. end;
  466. { simplified with op_reg_loc }
  467. if loc=LOC_CREGISTER then
  468. exprasmlist^.concat(new(pai386,op_reg_reg(A_MOV,opsize,
  469. p^.right^.location.register,
  470. p^.left^.location.register)))
  471. else
  472. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,opsize,
  473. p^.right^.location.register,
  474. newreference(p^.left^.location.reference))));
  475. {exprasmlist^.concat(new(pai386,op_reg_loc(A_MOV,opsize,
  476. p^.right^.location.register,
  477. p^.left^.location))); }
  478. end;
  479. LOC_FPU : begin
  480. if loc<>LOC_REFERENCE then
  481. internalerror(10010)
  482. else
  483. floatstore(pfloatdef(p^.left^.resulttype)^.typ,
  484. p^.left^.location.reference);
  485. end;
  486. LOC_JUMP : begin
  487. getlabel(hlabel);
  488. emitl(A_LABEL,truelabel);
  489. if loc=LOC_CREGISTER then
  490. exprasmlist^.concat(new(pai386,op_const_reg(A_MOV,S_B,
  491. 1,p^.left^.location.register)))
  492. else
  493. exprasmlist^.concat(new(pai386,op_const_ref(A_MOV,S_B,
  494. 1,newreference(p^.left^.location.reference))));
  495. {exprasmlist^.concat(new(pai386,op_const_loc(A_MOV,S_B,
  496. 1,p^.left^.location)));}
  497. emitl(A_JMP,hlabel);
  498. emitl(A_LABEL,falselabel);
  499. if loc=LOC_CREGISTER then
  500. exprasmlist^.concat(new(pai386,op_reg_reg(A_XOR,S_B,
  501. p^.left^.location.register,
  502. p^.left^.location.register)))
  503. else
  504. exprasmlist^.concat(new(pai386,op_const_ref(A_MOV,S_B,
  505. 0,newreference(p^.left^.location.reference))));
  506. emitl(A_LABEL,hlabel);
  507. end;
  508. LOC_FLAGS : begin
  509. if loc=LOC_CREGISTER then
  510. exprasmlist^.concat(new(pai386,op_reg(flag_2_set[p^.right^.location.resflags],S_B,
  511. p^.left^.location.register)))
  512. else
  513. exprasmlist^.concat(new(pai386,op_ref(flag_2_set[p^.right^.location.resflags],S_B,
  514. newreference(p^.left^.location.reference))));
  515. end;
  516. end;
  517. truelabel:=otlabel;
  518. falselabel:=oflabel;
  519. end;
  520. procedure secondaddr(var p : ptree);
  521. begin
  522. secondpass(p^.left);
  523. p^.location.loc:=LOC_REGISTER;
  524. del_reference(p^.left^.location.reference);
  525. p^.location.register:=getregister32;
  526. {@ on a procvar means returning an address to the procedure that
  527. is stored in it.}
  528. { yes but p^.left^.symtableentry can be nil
  529. for example on @self !! }
  530. { symtableentry can be also invalid, if left is no tree node }
  531. if (p^.left^.treetype=loadn) and
  532. assigned(p^.left^.symtableentry) and
  533. (p^.left^.symtableentry^.typ=varsym) and
  534. (pvarsym(p^.left^.symtableentry)^.definition^.deftype=procvardef) then
  535. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  536. newreference(p^.left^.location.reference),
  537. p^.location.register)))
  538. else
  539. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,
  540. newreference(p^.left^.location.reference),
  541. p^.location.register)));
  542. { for use of other segments }
  543. if p^.left^.location.reference.segment<>R_DEFAULT_SEG then
  544. p^.location.segment:=p^.left^.location.reference.segment;
  545. end;
  546. procedure seconddoubleaddr(var p : ptree);
  547. begin
  548. secondpass(p^.left);
  549. p^.location.loc:=LOC_REGISTER;
  550. del_reference(p^.left^.location.reference);
  551. p^.location.register:=getregister32;
  552. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,
  553. newreference(p^.left^.location.reference),
  554. p^.location.register)));
  555. end;
  556. procedure secondnot(var p : ptree);
  557. const
  558. flagsinvers : array[F_E..F_BE] of tresflags =
  559. (F_NE,F_E,F_LE,F_GE,F_L,F_G,F_NC,F_C,
  560. F_A,F_AE,F_B,F_BE);
  561. var
  562. hl : plabel;
  563. opsize : topsize;
  564. begin
  565. if (p^.resulttype^.deftype=orddef) and
  566. (porddef(p^.resulttype)^.typ in [bool8bit,bool16bit,bool32bit]) then
  567. begin
  568. case porddef(p^.resulttype)^.typ of
  569. bool8bit : opsize:=S_B;
  570. bool16bit : opsize:=S_W;
  571. bool32bit : opsize:=S_L;
  572. end;
  573. case p^.location.loc of
  574. LOC_JUMP : begin
  575. hl:=truelabel;
  576. truelabel:=falselabel;
  577. falselabel:=hl;
  578. secondpass(p^.left);
  579. maketojumpbool(p^.left);
  580. hl:=truelabel;
  581. truelabel:=falselabel;
  582. falselabel:=hl;
  583. end;
  584. LOC_FLAGS : begin
  585. secondpass(p^.left);
  586. p^.location.resflags:=flagsinvers[p^.left^.location.resflags];
  587. end;
  588. LOC_REGISTER : begin
  589. secondpass(p^.left);
  590. p^.location.register:=p^.left^.location.register;
  591. exprasmlist^.concat(new(pai386,op_const_reg(A_XOR,opsize,1,p^.location.register)));
  592. end;
  593. LOC_CREGISTER : begin
  594. secondpass(p^.left);
  595. p^.location.loc:=LOC_REGISTER;
  596. case porddef(p^.resulttype)^.typ of
  597. bool8bit : p^.location.register:=reg32toreg8(getregister32);
  598. bool16bit : p^.location.register:=reg32toreg16(getregister32);
  599. bool32bit : p^.location.register:=getregister32;
  600. end;
  601. emit_reg_reg(A_MOV,opsize,p^.left^.location.register,p^.location.register);
  602. exprasmlist^.concat(new(pai386,op_const_reg(A_XOR,opsize,1,p^.location.register)));
  603. end;
  604. LOC_REFERENCE,
  605. LOC_MEM : begin
  606. secondpass(p^.left);
  607. del_reference(p^.left^.location.reference);
  608. p^.location.loc:=LOC_REGISTER;
  609. case porddef(p^.resulttype)^.typ of
  610. bool8bit : p^.location.register:=reg32toreg8(getregister32);
  611. bool16bit : p^.location.register:=reg32toreg16(getregister32);
  612. bool32bit : p^.location.register:=getregister32;
  613. end;
  614. if p^.left^.location.loc=LOC_CREGISTER then
  615. emit_reg_reg(A_MOV,opsize,p^.left^.location.register,p^.location.register)
  616. else
  617. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,opsize,
  618. newreference(p^.left^.location.reference),p^.location.register)));
  619. exprasmlist^.concat(new(pai386,op_const_reg(A_XOR,opsize,1,p^.location.register)));
  620. end;
  621. end;
  622. end
  623. {$ifdef SUPPORT_MMX}
  624. else if (cs_mmx in aktswitches) and is_mmx_able_array(p^.left^.resulttype) then
  625. begin
  626. secondpass(p^.left);
  627. p^.location.loc:=LOC_MMXREGISTER;
  628. { prepare EDI }
  629. exprasmlist^.concat(new(pai386,op_const_reg(A_MOV,S_L,$ffffffff,R_EDI)));
  630. { load operand }
  631. case p^.left^.location.loc of
  632. LOC_MMXREGISTER:
  633. p^.location:=p^.left^.location;
  634. LOC_CMMXREGISTER:
  635. begin
  636. p^.location.register:=getregistermmx;
  637. emit_reg_reg(A_MOVQ,S_NO,p^.left^.location.register,
  638. p^.location.register);
  639. end;
  640. LOC_REFERENCE,LOC_MEM:
  641. begin
  642. del_reference(p^.left^.location.reference);
  643. p^.location.register:=getregistermmx;
  644. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOVQ,S_NO,
  645. newreference(p^.left^.location.reference),
  646. p^.location.register)));
  647. end;
  648. end;
  649. { load mask }
  650. emit_reg_reg(A_MOV,S_D,R_EDI,R_MM7);
  651. { lower 32 bit }
  652. emit_reg_reg(A_PXOR,S_D,R_MM7,p^.location.register);
  653. { shift mask }
  654. exprasmlist^.concat(new(pai386,op_const_reg(A_PSLLQ,S_NO,
  655. 32,R_MM7)));
  656. { higher 32 bit }
  657. emit_reg_reg(A_PXOR,S_D,R_MM7,p^.location.register);
  658. end
  659. {$endif SUPPORT_MMX}
  660. else
  661. begin
  662. secondpass(p^.left);
  663. p^.location.loc:=LOC_REGISTER;
  664. case p^.left^.location.loc of
  665. LOC_REGISTER : begin
  666. p^.location.register:=p^.left^.location.register;
  667. exprasmlist^.concat(new(pai386,op_reg(A_NOT,S_L,p^.location.register)));
  668. end;
  669. LOC_CREGISTER : begin
  670. p^.location.register:=getregister32;
  671. emit_reg_reg(A_MOV,S_L,p^.left^.location.register,
  672. p^.location.register);
  673. exprasmlist^.concat(new(pai386,op_reg(A_NOT,S_L,p^.location.register)));
  674. end;
  675. LOC_REFERENCE,LOC_MEM :
  676. begin
  677. del_reference(p^.left^.location.reference);
  678. p^.location.register:=getregister32;
  679. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  680. newreference(p^.left^.location.reference),
  681. p^.location.register)));
  682. exprasmlist^.concat(new(pai386,op_reg(A_NOT,S_L,p^.location.register)));
  683. end;
  684. end;
  685. {if p^.left^.location.loc=loc_register then
  686. p^.location.register:=p^.left^.location.register
  687. else
  688. begin
  689. del_locref(p^.left^.location);
  690. p^.location.register:=getregister32;
  691. exprasmlist^.concat(new(pai386,op_loc_reg(A_MOV,S_L,
  692. p^.left^.location,
  693. p^.location.register)));
  694. end;
  695. exprasmlist^.concat(new(pai386,op_reg(A_NOT,S_L,p^.location.register)));}
  696. end;
  697. end;
  698. procedure secondnothing(var p : ptree);
  699. begin
  700. end;
  701. procedure secondderef(var p : ptree);
  702. var
  703. hr : tregister;
  704. begin
  705. secondpass(p^.left);
  706. clear_reference(p^.location.reference);
  707. case p^.left^.location.loc of
  708. LOC_REGISTER:
  709. p^.location.reference.base:=p^.left^.location.register;
  710. LOC_CREGISTER:
  711. begin
  712. { ... and reserve one for the pointer }
  713. hr:=getregister32;
  714. emit_reg_reg(A_MOV,S_L,p^.left^.location.register,hr);
  715. p^.location.reference.base:=hr;
  716. end;
  717. else
  718. begin
  719. { free register }
  720. del_reference(p^.left^.location.reference);
  721. { ...and reserve one for the pointer }
  722. hr:=getregister32;
  723. exprasmlist^.concat(new(pai386,op_ref_reg(
  724. A_MOV,S_L,newreference(p^.left^.location.reference),
  725. hr)));
  726. p^.location.reference.base:=hr;
  727. end;
  728. end;
  729. end;
  730. procedure secondvecn(var p : ptree);
  731. var
  732. pushed : boolean;
  733. ind,hr : tregister;
  734. _p : ptree;
  735. function get_mul_size:longint;
  736. begin
  737. if p^.memindex then
  738. get_mul_size:=1
  739. else
  740. get_mul_size:=p^.resulttype^.size;
  741. end;
  742. procedure calc_emit_mul;
  743. var
  744. l1,l2 : longint;
  745. begin
  746. l1:=get_mul_size;
  747. case l1 of
  748. 1,2,4,8 : p^.location.reference.scalefactor:=l1;
  749. else
  750. begin
  751. if ispowerof2(l1,l2) then
  752. exprasmlist^.concat(new(pai386,op_const_reg(A_SHL,S_L,l2,ind)))
  753. else
  754. exprasmlist^.concat(new(pai386,op_const_reg(A_IMUL,S_L,l1,ind)));
  755. end;
  756. end;
  757. end;
  758. var
  759. extraoffset : longint;
  760. t : ptree;
  761. hp : preference;
  762. tai:Pai386;
  763. begin
  764. secondpass(p^.left);
  765. set_location(p^.location,p^.left^.location);
  766. { in ansistrings S[1] is pchar(S)[0] !! }
  767. if is_ansistring(p^.left^.resulttype) then
  768. dec(p^.location.reference.offset);
  769. { offset can only differ from 0 if arraydef }
  770. if p^.left^.resulttype^.deftype=arraydef then
  771. dec(p^.location.reference.offset,
  772. get_mul_size*parraydef(p^.left^.resulttype)^.lowrange);
  773. if p^.right^.treetype=ordconstn then
  774. begin
  775. { offset can only differ from 0 if arraydef }
  776. if (p^.left^.resulttype^.deftype=arraydef) then
  777. begin
  778. if not(is_open_array(p^.left^.resulttype)) then
  779. begin
  780. if (p^.right^.value>parraydef(p^.left^.resulttype)^.highrange) or
  781. (p^.right^.value<parraydef(p^.left^.resulttype)^.lowrange) then
  782. Message(parser_e_range_check_error);
  783. dec(p^.left^.location.reference.offset,
  784. get_mul_size*parraydef(p^.left^.resulttype)^.lowrange);
  785. end
  786. else
  787. begin
  788. { range checking for open arrays }
  789. end;
  790. end;
  791. inc(p^.left^.location.reference.offset,
  792. get_mul_size*p^.right^.value);
  793. if p^.memseg then
  794. p^.left^.location.reference.segment:=R_FS;
  795. p^.left^.resulttype:=p^.resulttype;
  796. disposetree(p^.right);
  797. _p:=p^.left;
  798. putnode(p);
  799. p:=_p;
  800. end
  801. else
  802. begin
  803. { quick hack, to overcome Delphi 2 }
  804. if (cs_maxoptimieren in aktswitches) and
  805. (p^.left^.resulttype^.deftype=arraydef) then
  806. begin
  807. extraoffset:=0;
  808. if (p^.right^.treetype=addn) then
  809. begin
  810. if p^.right^.right^.treetype=ordconstn then
  811. begin
  812. extraoffset:=p^.right^.right^.value;
  813. t:=p^.right^.left;
  814. putnode(p^.right);
  815. putnode(p^.right^.right);
  816. p^.right:=t
  817. end
  818. else if p^.right^.left^.treetype=ordconstn then
  819. begin
  820. extraoffset:=p^.right^.left^.value;
  821. t:=p^.right^.right;
  822. putnode(p^.right);
  823. putnode(p^.right^.left);
  824. p^.right:=t
  825. end;
  826. end
  827. else if (p^.right^.treetype=subn) then
  828. begin
  829. if p^.right^.right^.treetype=ordconstn then
  830. begin
  831. extraoffset:=p^.right^.right^.value;
  832. t:=p^.right^.left;
  833. putnode(p^.right);
  834. putnode(p^.right^.right);
  835. p^.right:=t
  836. end
  837. else if p^.right^.left^.treetype=ordconstn then
  838. begin
  839. extraoffset:=p^.right^.left^.value;
  840. t:=p^.right^.right;
  841. putnode(p^.right);
  842. putnode(p^.right^.left);
  843. p^.right:=t
  844. end;
  845. end;
  846. inc(p^.location.reference.offset,
  847. get_mul_size*extraoffset);
  848. end;
  849. { calculate from left to right }
  850. if (p^.location.loc<>LOC_REFERENCE) and
  851. (p^.location.loc<>LOC_MEM) then
  852. Message(cg_e_illegal_expression);
  853. pushed:=maybe_push(p^.right^.registers32,p);
  854. secondpass(p^.right);
  855. if pushed then restore(p);
  856. case p^.right^.location.loc of
  857. LOC_REGISTER:
  858. begin
  859. ind:=p^.right^.location.register;
  860. case p^.right^.resulttype^.size of
  861. 1:
  862. begin
  863. hr:=reg8toreg32(ind);
  864. emit_reg_reg(A_MOVZX,S_BL,ind,hr);
  865. ind:=hr;
  866. end;
  867. 2:
  868. begin
  869. hr:=reg16toreg32(ind);
  870. emit_reg_reg(A_MOVZX,S_WL,ind,hr);
  871. ind:=hr;
  872. end;
  873. end;
  874. end;
  875. LOC_CREGISTER:
  876. begin
  877. ind:=getregister32;
  878. case p^.right^.resulttype^.size of
  879. 1:
  880. emit_reg_reg(A_MOVZX,S_BL,p^.right^.location.register,ind);
  881. 2:
  882. emit_reg_reg(A_MOVZX,S_WL,p^.right^.location.register,ind);
  883. 4:
  884. emit_reg_reg(A_MOV,S_L,p^.right^.location.register,ind);
  885. end;
  886. end;
  887. LOC_FLAGS:
  888. begin
  889. ind:=getregister32;
  890. exprasmlist^.concat(new(pai386,op_reg(flag_2_set[p^.right^.location.resflags],S_B,reg32toreg8(ind))));
  891. emit_reg_reg(A_MOVZX,S_BL,reg32toreg8(ind),ind);
  892. end
  893. else
  894. begin
  895. del_reference(p^.right^.location.reference);
  896. ind:=getregister32;
  897. { Booleans are stored in an 8 bit memory location, so
  898. the use of MOVL is not correct }
  899. case p^.right^.resulttype^.size of
  900. 1:
  901. tai:=new(pai386,op_ref_reg(A_MOVZX,S_BL,newreference(p^.right^.location.reference),ind));
  902. 2:
  903. tai:=new(Pai386,op_ref_reg(A_MOVZX,S_WL,newreference(p^.right^.location.reference),ind));
  904. 4:
  905. tai:=new(Pai386,op_ref_reg(A_MOV,S_L,newreference(p^.right^.location.reference),ind));
  906. end;
  907. exprasmlist^.concat(tai);
  908. end;
  909. end;
  910. { produce possible range check code: }
  911. if cs_rangechecking in aktswitches then
  912. begin
  913. if p^.left^.resulttype^.deftype=arraydef then
  914. begin
  915. hp:=new_reference(R_NO,0);
  916. parraydef(p^.left^.resulttype)^.genrangecheck;
  917. hp^.symbol:=stringdup('R_'+tostr(parraydef(p^.left^.resulttype)^.rangenr));
  918. exprasmlist^.concat(new(pai386,op_reg_ref(A_BOUND,S_L,ind,hp)));
  919. end;
  920. end;
  921. if p^.location.reference.index=R_NO then
  922. begin
  923. p^.location.reference.index:=ind;
  924. calc_emit_mul;
  925. end
  926. else
  927. begin
  928. if p^.location.reference.base=R_NO then
  929. begin
  930. case p^.location.reference.scalefactor of
  931. 2 : exprasmlist^.concat(new(pai386,op_const_reg(A_SHL,S_L,1,p^.location.reference.index)));
  932. 4 : exprasmlist^.concat(new(pai386,op_const_reg(A_SHL,S_L,2,p^.location.reference.index)));
  933. 8 : exprasmlist^.concat(new(pai386,op_const_reg(A_SHL,S_L,3,p^.location.reference.index)));
  934. end;
  935. calc_emit_mul;
  936. p^.location.reference.base:=p^.location.reference.index;
  937. p^.location.reference.index:=ind;
  938. end
  939. else
  940. begin
  941. exprasmlist^.concat(new(pai386,op_ref_reg(
  942. A_LEA,S_L,newreference(p^.location.reference),
  943. p^.location.reference.index)));
  944. ungetregister32(p^.location.reference.base);
  945. { the symbol offset is loaded, }
  946. { so release the symbol name and set symbol }
  947. { to nil }
  948. stringdispose(p^.location.reference.symbol);
  949. p^.location.reference.offset:=0;
  950. calc_emit_mul;
  951. p^.location.reference.base:=p^.location.reference.index;
  952. p^.location.reference.index:=ind;
  953. end;
  954. end;
  955. if p^.memseg then
  956. p^.location.reference.segment:=R_FS;
  957. end;
  958. end;
  959. { save the size of pushed parameter }
  960. var
  961. pushedparasize : longint;
  962. procedure secondcallparan(var p : ptree;defcoll : pdefcoll;
  963. push_from_left_to_right : boolean
  964. ;inlined : boolean;
  965. para_offset : longint
  966. );
  967. procedure maybe_push_open_array_high;
  968. var
  969. r : preference;
  970. begin
  971. { open array ? }
  972. { defcoll^.data can be nil for read/write }
  973. if assigned(defcoll^.data) and
  974. is_open_array(defcoll^.data) then
  975. begin
  976. inc(pushedparasize,4);
  977. { push high }
  978. if is_open_array(p^.left^.resulttype) then
  979. begin
  980. r:=new_reference(highframepointer,highoffset+4);
  981. if inlined then
  982. begin
  983. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,r,R_EDI)));
  984. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  985. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,r)));
  986. end
  987. else
  988. exprasmlist^.concat(new(pai386,op_ref(A_PUSH,S_L,r)));
  989. end
  990. else
  991. if inlined then
  992. begin
  993. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  994. exprasmlist^.concat(new(pai386,op_const_ref(A_MOV,S_L,
  995. parraydef(p^.left^.resulttype)^.highrange-
  996. parraydef(p^.left^.resulttype)^.lowrange,r)));
  997. end
  998. else
  999. push_int(parraydef(p^.left^.resulttype)^.highrange-
  1000. parraydef(p^.left^.resulttype)^.lowrange);
  1001. end;
  1002. end;
  1003. var
  1004. size : longint;
  1005. stackref : treference;
  1006. otlabel,hlabel,oflabel : plabel;
  1007. { temporary variables: }
  1008. tempdeftype : tdeftype;
  1009. tempreference : treference;
  1010. r : preference;
  1011. s : topsize;
  1012. op : tasmop;
  1013. begin
  1014. { push from left to right if specified }
  1015. if push_from_left_to_right and assigned(p^.right) then
  1016. secondcallparan(p^.right,defcoll^.next,push_from_left_to_right
  1017. ,inlined,para_offset
  1018. );
  1019. otlabel:=truelabel;
  1020. oflabel:=falselabel;
  1021. getlabel(truelabel);
  1022. getlabel(falselabel);
  1023. secondpass(p^.left);
  1024. { in codegen.handleread.. defcoll^.data is set to nil }
  1025. if assigned(defcoll^.data) and
  1026. (defcoll^.data^.deftype=formaldef) then
  1027. begin
  1028. { allow @var }
  1029. inc(pushedparasize,4);
  1030. if p^.left^.treetype=addrn then
  1031. begin
  1032. { always a register }
  1033. if inlined then
  1034. begin
  1035. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1036. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
  1037. p^.left^.location.register,r)));
  1038. end
  1039. else
  1040. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,p^.left^.location.register)));
  1041. ungetregister32(p^.left^.location.register);
  1042. end
  1043. else
  1044. begin
  1045. if (p^.left^.location.loc<>LOC_REFERENCE) and
  1046. (p^.left^.location.loc<>LOC_MEM) then
  1047. Message(sym_e_type_mismatch)
  1048. else
  1049. begin
  1050. if inlined then
  1051. begin
  1052. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,
  1053. newreference(p^.left^.location.reference),R_EDI)));
  1054. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1055. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
  1056. R_EDI,r)));
  1057. end
  1058. else
  1059. emitpushreferenceaddr(p^.left^.location.reference);
  1060. del_reference(p^.left^.location.reference);
  1061. end;
  1062. end;
  1063. end
  1064. { handle call by reference parameter }
  1065. else if (defcoll^.paratyp=vs_var) then
  1066. begin
  1067. if (p^.left^.location.loc<>LOC_REFERENCE) then
  1068. Message(cg_e_var_must_be_reference);
  1069. maybe_push_open_array_high;
  1070. inc(pushedparasize,4);
  1071. if inlined then
  1072. begin
  1073. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,
  1074. newreference(p^.left^.location.reference),R_EDI)));
  1075. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1076. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
  1077. R_EDI,r)));
  1078. end
  1079. else
  1080. emitpushreferenceaddr(p^.left^.location.reference);
  1081. del_reference(p^.left^.location.reference);
  1082. end
  1083. else
  1084. begin
  1085. tempdeftype:=p^.resulttype^.deftype;
  1086. if tempdeftype=filedef then
  1087. Message(cg_e_file_must_call_by_reference);
  1088. if (defcoll^.paratyp=vs_const) and
  1089. dont_copy_const_param(p^.resulttype) then
  1090. begin
  1091. maybe_push_open_array_high;
  1092. inc(pushedparasize,4);
  1093. if inlined then
  1094. begin
  1095. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,
  1096. newreference(p^.left^.location.reference),R_EDI)));
  1097. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1098. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
  1099. R_EDI,r)));
  1100. end
  1101. else
  1102. emitpushreferenceaddr(p^.left^.location.reference);
  1103. del_reference(p^.left^.location.reference);
  1104. end
  1105. else
  1106. case p^.left^.location.loc of
  1107. LOC_REGISTER,
  1108. LOC_CREGISTER:
  1109. begin
  1110. case p^.left^.location.register of
  1111. R_EAX,R_EBX,R_ECX,R_EDX,R_ESI,
  1112. R_EDI,R_ESP,R_EBP :
  1113. begin
  1114. inc(pushedparasize,4);
  1115. if inlined then
  1116. begin
  1117. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1118. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
  1119. p^.left^.location.register,r)));
  1120. end
  1121. else
  1122. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,p^.left^.location.register)));
  1123. ungetregister32(p^.left^.location.register);
  1124. end;
  1125. R_AX,R_BX,R_CX,R_DX,R_SI,R_DI:
  1126. begin
  1127. inc(pushedparasize,2);
  1128. if inlined then
  1129. begin
  1130. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1131. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_W,
  1132. p^.left^.location.register,r)));
  1133. end
  1134. else
  1135. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_W,p^.left^.location.register)));
  1136. ungetregister32(reg16toreg32(p^.left^.location.register));
  1137. end;
  1138. R_AL,R_BL,R_CL,R_DL:
  1139. begin
  1140. inc(pushedparasize,2);
  1141. { we must push always 16 bit }
  1142. if inlined then
  1143. begin
  1144. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1145. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
  1146. reg8toreg16(p^.left^.location.register),r)));
  1147. end
  1148. else
  1149. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_W,
  1150. reg8toreg16(p^.left^.location.register))));
  1151. ungetregister32(reg8toreg32(p^.left^.location.register));
  1152. end;
  1153. end;
  1154. end;
  1155. LOC_FPU:
  1156. begin
  1157. size:=pfloatdef(p^.left^.resulttype)^.size;
  1158. inc(pushedparasize,size); { must be before for inlined }
  1159. if not inlined then
  1160. exprasmlist^.concat(new(pai386,op_const_reg(A_SUB,S_L,size,R_ESP)));
  1161. r:=new_reference(R_ESP,0);
  1162. floatstoreops(pfloatdef(p^.left^.resulttype)^.typ,op,s);
  1163. { this is the easiest case for inlined !! }
  1164. if inlined then
  1165. begin
  1166. r^.base:=procinfo.framepointer;
  1167. r^.offset:=para_offset-pushedparasize;
  1168. end;
  1169. exprasmlist^.concat(new(pai386,op_ref(op,s,r)));
  1170. end;
  1171. LOC_REFERENCE,LOC_MEM:
  1172. begin
  1173. tempreference:=p^.left^.location.reference;
  1174. del_reference(p^.left^.location.reference);
  1175. case p^.resulttype^.deftype of
  1176. orddef :
  1177. begin
  1178. case porddef(p^.resulttype)^.typ of
  1179. s32bit,u32bit,bool32bit :
  1180. begin
  1181. inc(pushedparasize,4);
  1182. if inlined then
  1183. begin
  1184. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  1185. newreference(tempreference),R_EDI)));
  1186. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1187. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
  1188. R_EDI,r)));
  1189. end
  1190. else
  1191. emit_push_mem(tempreference);
  1192. end;
  1193. s8bit,u8bit,uchar,bool8bit,bool16bit,s16bit,u16bit :
  1194. begin
  1195. inc(pushedparasize,2);
  1196. if inlined then
  1197. begin
  1198. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_W,
  1199. newreference(tempreference),R_DI)));
  1200. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1201. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_W,
  1202. R_DI,r)));
  1203. end
  1204. else
  1205. exprasmlist^.concat(new(pai386,op_ref(A_PUSH,S_W,
  1206. newreference(tempreference))));
  1207. end;
  1208. end;
  1209. end;
  1210. floatdef :
  1211. begin
  1212. case pfloatdef(p^.resulttype)^.typ of
  1213. f32bit,
  1214. s32real :
  1215. begin
  1216. inc(pushedparasize,4);
  1217. if inlined then
  1218. begin
  1219. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  1220. newreference(tempreference),R_EDI)));
  1221. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1222. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
  1223. R_EDI,r)));
  1224. end
  1225. else
  1226. emit_push_mem(tempreference);
  1227. end;
  1228. s64real,
  1229. s64bit : begin
  1230. inc(pushedparasize,4);
  1231. inc(tempreference.offset,4);
  1232. if inlined then
  1233. begin
  1234. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  1235. newreference(tempreference),R_EDI)));
  1236. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1237. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
  1238. R_EDI,r)));
  1239. end
  1240. else
  1241. emit_push_mem(tempreference);
  1242. inc(pushedparasize,4);
  1243. dec(tempreference.offset,4);
  1244. if inlined then
  1245. begin
  1246. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  1247. newreference(tempreference),R_EDI)));
  1248. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1249. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
  1250. R_EDI,r)));
  1251. end
  1252. else
  1253. emit_push_mem(tempreference);
  1254. end;
  1255. s80real : begin
  1256. inc(pushedparasize,4);
  1257. inc(tempreference.offset,6);
  1258. if inlined then
  1259. begin
  1260. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  1261. newreference(tempreference),R_EDI)));
  1262. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1263. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
  1264. R_EDI,r)));
  1265. end
  1266. else
  1267. emit_push_mem(tempreference);
  1268. dec(tempreference.offset,4);
  1269. inc(pushedparasize,4);
  1270. if inlined then
  1271. begin
  1272. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  1273. newreference(tempreference),R_EDI)));
  1274. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1275. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
  1276. R_EDI,r)));
  1277. end
  1278. else
  1279. emit_push_mem(tempreference);
  1280. dec(tempreference.offset,2);
  1281. inc(pushedparasize,2);
  1282. if inlined then
  1283. begin
  1284. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_W,
  1285. newreference(tempreference),R_DI)));
  1286. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1287. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_W,
  1288. R_DI,r)));
  1289. end
  1290. else
  1291. exprasmlist^.concat(new(pai386,op_ref(A_PUSH,S_W,
  1292. newreference(tempreference))));
  1293. end;
  1294. end;
  1295. end;
  1296. pointerdef,procvardef,
  1297. enumdef,classrefdef:
  1298. begin
  1299. inc(pushedparasize,4);
  1300. if inlined then
  1301. begin
  1302. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  1303. newreference(tempreference),R_EDI)));
  1304. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1305. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
  1306. R_EDI,r)));
  1307. end
  1308. else
  1309. emit_push_mem(tempreference);
  1310. end;
  1311. arraydef,recorddef,stringdef,setdef,objectdef :
  1312. begin
  1313. { small set ? }
  1314. if ((p^.resulttype^.deftype=setdef) and
  1315. (psetdef(p^.resulttype)^.settype=smallset)) then
  1316. begin
  1317. inc(pushedparasize,4);
  1318. if inlined then
  1319. begin
  1320. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1321. concatcopy(tempreference,r^,4,false);
  1322. end
  1323. else
  1324. emit_push_mem(tempreference);
  1325. end
  1326. { call by value open array ? }
  1327. else if (p^.resulttype^.deftype=arraydef) and
  1328. assigned(defcoll^.data) and
  1329. is_open_array(defcoll^.data) then
  1330. begin
  1331. { first, push high }
  1332. maybe_push_open_array_high;
  1333. inc(pushedparasize,4);
  1334. if inlined then
  1335. begin
  1336. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,
  1337. newreference(p^.left^.location.reference),R_EDI)));
  1338. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1339. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
  1340. R_EDI,r)));
  1341. end
  1342. else
  1343. emitpushreferenceaddr(p^.left^.location.reference);
  1344. end
  1345. else
  1346. begin
  1347. size:=p^.resulttype^.size;
  1348. { Alignment }
  1349. {
  1350. if (size>=4) and ((size and 3)<>0) then
  1351. inc(size,4-(size and 3))
  1352. else if (size>=2) and ((size and 1)<>0) then
  1353. inc(size,2-(size and 1))
  1354. else
  1355. if size=1 then size:=2;
  1356. }
  1357. { create stack space }
  1358. if not inlined then
  1359. exprasmlist^.concat(new(pai386,op_const_reg(A_SUB,S_L,size,R_ESP)));
  1360. inc(pushedparasize,size);
  1361. { create stack reference }
  1362. stackref.symbol := nil;
  1363. if not inlined then
  1364. begin
  1365. clear_reference(stackref);
  1366. stackref.base:=R_ESP;
  1367. end
  1368. else
  1369. begin
  1370. clear_reference(stackref);
  1371. stackref.base:=procinfo.framepointer;
  1372. stackref.offset:=para_offset-pushedparasize;
  1373. end;
  1374. { produce copy }
  1375. if p^.resulttype^.deftype=stringdef then
  1376. begin
  1377. copystring(stackref,p^.left^.location.reference,
  1378. pstringdef(p^.resulttype)^.len);
  1379. end
  1380. else
  1381. begin
  1382. concatcopy(p^.left^.location.reference,
  1383. stackref,p^.resulttype^.size,true);
  1384. end;
  1385. end;
  1386. end;
  1387. else Message(cg_e_illegal_expression);
  1388. end;
  1389. end;
  1390. LOC_JUMP:
  1391. begin
  1392. getlabel(hlabel);
  1393. inc(pushedparasize,2);
  1394. emitl(A_LABEL,truelabel);
  1395. if inlined then
  1396. begin
  1397. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1398. exprasmlist^.concat(new(pai386,op_const_ref(A_MOV,S_W,
  1399. 1,r)));
  1400. end
  1401. else
  1402. exprasmlist^.concat(new(pai386,op_const(A_PUSH,S_W,1)));
  1403. emitl(A_JMP,hlabel);
  1404. emitl(A_LABEL,falselabel);
  1405. if inlined then
  1406. begin
  1407. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1408. exprasmlist^.concat(new(pai386,op_const_ref(A_MOV,S_W,
  1409. 0,r)));
  1410. end
  1411. else
  1412. exprasmlist^.concat(new(pai386,op_const(A_PUSH,S_W,0)));
  1413. emitl(A_LABEL,hlabel);
  1414. end;
  1415. LOC_FLAGS:
  1416. begin
  1417. if not(R_EAX in unused) then
  1418. exprasmlist^.concat(new(pai386,op_reg_reg(A_MOV,S_L,R_EAX,R_EDI)));
  1419. { clear full EAX is faster }
  1420. { but dont you set the equal flag ? }
  1421. {exprasmlist^.concat(new(pai386,op_reg_reg(A_XOR,S_L,R_EAX,R_EAX)));}
  1422. exprasmlist^.concat(new(pai386,op_reg(flag_2_set[p^.left^.location.resflags],S_B,
  1423. R_AL)));
  1424. exprasmlist^.concat(new(pai386,op_reg_reg(A_MOVZX,S_BW,R_AL,R_AX)));
  1425. {exprasmlist^.concat(new(pai386,op_reg_reg(A_XOR,S_L,R_EAX,R_EAX)));}
  1426. inc(pushedparasize,2);
  1427. if inlined then
  1428. begin
  1429. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1430. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_W,
  1431. R_AX,r)));
  1432. end
  1433. else
  1434. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_W,R_AX)));
  1435. { this is also false !!!
  1436. if not(R_EAX in unused) then
  1437. exprasmlist^.concat(new(pai386,op_reg_reg(A_MOV,S_L,R_EAX,R_EDI)));}
  1438. if not(R_EAX in unused) then
  1439. exprasmlist^.concat(new(pai386,op_reg_reg(A_MOV,S_L,R_EDI,R_EAX)));
  1440. end;
  1441. {$ifdef SUPPORT_MMX}
  1442. LOC_MMXREGISTER,
  1443. LOC_CMMXREGISTER:
  1444. begin
  1445. inc(pushedparasize,8); { was missing !!! (PM) }
  1446. exprasmlist^.concat(new(pai386,op_const_reg(
  1447. A_SUB,S_L,8,R_ESP)));
  1448. if inlined then
  1449. begin
  1450. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1451. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOVQ,S_NO,
  1452. p^.left^.location.register,r)));
  1453. end
  1454. else
  1455. begin
  1456. r:=new_reference(R_ESP,0);
  1457. exprasmlist^.concat(new(pai386,op_reg_ref(
  1458. A_MOVQ,S_NO,p^.left^.location.register,r)));
  1459. end;
  1460. end;
  1461. {$endif SUPPORT_MMX}
  1462. end;
  1463. end;
  1464. truelabel:=otlabel;
  1465. falselabel:=oflabel;
  1466. { push from right to left }
  1467. if not push_from_left_to_right and assigned(p^.right) then
  1468. secondcallparan(p^.right,defcoll^.next,push_from_left_to_right
  1469. ,inlined,para_offset
  1470. );
  1471. end;
  1472. procedure secondcalln(var p : ptree);
  1473. var
  1474. unusedregisters : tregisterset;
  1475. pushed : tpushed;
  1476. funcretref : treference;
  1477. hregister : tregister;
  1478. oldpushedparasize : longint;
  1479. { true if ESI must be loaded again after the subroutine }
  1480. loadesi : boolean;
  1481. { true if a virtual method must be called directly }
  1482. no_virtual_call : boolean;
  1483. { true if we produce a con- or destrutor in a call }
  1484. is_con_or_destructor : boolean;
  1485. { true if a constructor is called again }
  1486. extended_new : boolean;
  1487. { adress returned from an I/O-error }
  1488. iolabel : plabel;
  1489. { lexlevel count }
  1490. i : longint;
  1491. { help reference pointer }
  1492. r : preference;
  1493. pp,params : ptree;
  1494. inlined : boolean;
  1495. inlinecode : ptree;
  1496. para_offset : longint;
  1497. { instruction for alignement correction }
  1498. corr : pai386;
  1499. { we must pop this size also after !! }
  1500. must_pop : boolean;
  1501. pop_size : longint;
  1502. label
  1503. dont_call;
  1504. begin
  1505. extended_new:=false;
  1506. iolabel:=nil;
  1507. inlinecode:=nil;
  1508. inlined:=false;
  1509. loadesi:=true;
  1510. no_virtual_call:=false;
  1511. unusedregisters:=unused;
  1512. if not assigned(p^.procdefinition) then
  1513. exit;
  1514. if (p^.procdefinition^.options and poinline)<>0 then
  1515. begin
  1516. inlined:=true;
  1517. inlinecode:=p^.right;
  1518. { set it to the same lexical level }
  1519. p^.procdefinition^.parast^.symtablelevel:=
  1520. aktprocsym^.definition^.parast^.symtablelevel;
  1521. if assigned(p^.left) then
  1522. inlinecode^.para_offset:=
  1523. gettempofsizepersistant(inlinecode^.para_size);
  1524. p^.procdefinition^.parast^.call_offset:=
  1525. inlinecode^.para_offset;
  1526. {$ifdef extdebug}
  1527. Comment(V_debug,
  1528. 'inlined parasymtable is at offset '
  1529. +tostr(p^.procdefinition^.parast^.call_offset));
  1530. exprasmlist^.concat(new(pai_asm_comment,init(
  1531. strpnew('inlined parasymtable is at offset '
  1532. +tostr(p^.procdefinition^.parast^.call_offset)))));
  1533. {$endif extdebug}
  1534. p^.right:=nil;
  1535. { disable further inlining of the same proc
  1536. in the args }
  1537. p^.procdefinition^.options:=p^.procdefinition^.options and (not poinline);
  1538. end;
  1539. { only if no proc var }
  1540. if not(assigned(p^.right)) then
  1541. is_con_or_destructor:=((p^.procdefinition^.options and poconstructor)<>0)
  1542. or ((p^.procdefinition^.options and podestructor)<>0);
  1543. { proc variables destroy all registers }
  1544. if (p^.right=nil) and
  1545. { virtual methods too }
  1546. ((p^.procdefinition^.options and povirtualmethod)=0) then
  1547. begin
  1548. if ((p^.procdefinition^.options and poiocheck)<>0)
  1549. and (cs_iocheck in aktswitches) then
  1550. begin
  1551. getlabel(iolabel);
  1552. emitl(A_LABEL,iolabel);
  1553. end
  1554. else iolabel:=nil;
  1555. { save all used registers }
  1556. pushusedregisters(pushed,p^.procdefinition^.usedregisters);
  1557. { give used registers through }
  1558. usedinproc:=usedinproc or p^.procdefinition^.usedregisters;
  1559. end
  1560. else
  1561. begin
  1562. pushusedregisters(pushed,$ff);
  1563. usedinproc:=$ff;
  1564. { no IO check for methods and procedure variables }
  1565. iolabel:=nil;
  1566. end;
  1567. { generate the code for the parameter and push them }
  1568. oldpushedparasize:=pushedparasize;
  1569. pushedparasize:=0;
  1570. corr:=new(pai386,op_const_reg(A_SUB,S_L,0,R_ESP));
  1571. exprasmlist^.concat(corr);
  1572. if (p^.resulttype<>pdef(voiddef)) and
  1573. ret_in_param(p^.resulttype) then
  1574. begin
  1575. funcretref.symbol:=nil;
  1576. {$ifdef test_dest_loc}
  1577. if dest_loc_known and (dest_loc_tree=p) and
  1578. (dest_loc.loc in [LOC_REFERENCE,LOC_MEM]) then
  1579. begin
  1580. funcretref:=dest_loc.reference;
  1581. if assigned(dest_loc.reference.symbol) then
  1582. funcretref.symbol:=stringdup(dest_loc.reference.symbol^);
  1583. in_dest_loc:=true;
  1584. end
  1585. else
  1586. {$endif test_dest_loc}
  1587. if inlined then
  1588. begin
  1589. reset_reference(funcretref);
  1590. funcretref.offset:=gettempofsizepersistant(p^.procdefinition^.retdef^.size);
  1591. funcretref.base:=procinfo.framepointer;
  1592. end
  1593. else
  1594. gettempofsizereference(p^.procdefinition^.retdef^.size,funcretref);
  1595. end;
  1596. if assigned(p^.left) then
  1597. begin
  1598. pushedparasize:=0;
  1599. { be found elsewhere }
  1600. if inlined then
  1601. para_offset:=p^.procdefinition^.parast^.call_offset+
  1602. p^.procdefinition^.parast^.datasize
  1603. else
  1604. para_offset:=0;
  1605. if assigned(p^.right) then
  1606. secondcallparan(p^.left,pprocvardef(p^.right^.resulttype)^.para1,
  1607. (p^.procdefinition^.options and poleftright)<>0
  1608. ,inlined,
  1609. para_offset
  1610. )
  1611. else
  1612. secondcallparan(p^.left,p^.procdefinition^.para1,
  1613. (p^.procdefinition^.options and poleftright)<>0
  1614. ,inlined,
  1615. para_offset
  1616. );
  1617. end;
  1618. params:=p^.left;
  1619. p^.left:=nil;
  1620. if inlined then
  1621. inlinecode^.retoffset:=gettempofsizepersistant(4);
  1622. if ret_in_param(p^.resulttype) then
  1623. begin
  1624. inc(pushedparasize,4);
  1625. if inlined then
  1626. begin
  1627. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,
  1628. newreference(funcretref),R_EDI)));
  1629. r:=new_reference(procinfo.framepointer,inlinecode^.retoffset);
  1630. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
  1631. R_EDI,r)));
  1632. end
  1633. else
  1634. emitpushreferenceaddr(funcretref);
  1635. end;
  1636. { procedure variable ? }
  1637. if (p^.right=nil) then
  1638. begin
  1639. { overloaded operator have no symtable }
  1640. { push self }
  1641. if assigned(p^.symtable) and
  1642. (p^.symtable^.symtabletype=withsymtable) then
  1643. begin
  1644. { dirty trick to avoid the secondcall below }
  1645. p^.methodpointer:=genzeronode(callparan);
  1646. p^.methodpointer^.location.loc:=LOC_REGISTER;
  1647. p^.methodpointer^.location.register:=R_ESI;
  1648. { make a reference }
  1649. new(r);
  1650. reset_reference(r^);
  1651. r^.offset:=p^.symtable^.datasize;
  1652. r^.base:=procinfo.framepointer;
  1653. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,r,R_ESI)));
  1654. end;
  1655. { push self }
  1656. if assigned(p^.symtable) and
  1657. ((p^.symtable^.symtabletype=objectsymtable) or
  1658. (p^.symtable^.symtabletype=withsymtable)) then
  1659. begin
  1660. if assigned(p^.methodpointer) then
  1661. begin
  1662. {
  1663. if p^.methodpointer^.resulttype=classrefdef then
  1664. begin
  1665. two possibilities:
  1666. 1. constructor
  1667. 2. class method
  1668. end
  1669. else }
  1670. begin
  1671. case p^.methodpointer^.treetype of
  1672. typen:
  1673. begin
  1674. { direct call to inherited method }
  1675. if (p^.procdefinition^.options and poabstractmethod)<>0 then
  1676. begin
  1677. Message(cg_e_cant_call_abstract_method);
  1678. goto dont_call;
  1679. end;
  1680. { generate no virtual call }
  1681. no_virtual_call:=true;
  1682. if (p^.symtableprocentry^.properties and sp_static)<>0 then
  1683. begin
  1684. { well lets put the VMT address directly into ESI }
  1685. { it is kind of dirty but that is the simplest }
  1686. { way to accept virtual static functions (PM) }
  1687. loadesi:=true;
  1688. exprasmlist^.concat(new(pai386,op_csymbol_reg(A_MOV,S_L,
  1689. newcsymbol(pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname,0),R_ESI)));
  1690. maybe_concat_external(pobjectdef(p^.methodpointer^.resulttype)^.owner,
  1691. pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname);
  1692. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_ESI)));
  1693. end
  1694. else
  1695. { this is a member call, so ESI isn't modfied }
  1696. loadesi:=false;
  1697. if not(is_con_or_destructor and
  1698. pobjectdef(p^.methodpointer^.resulttype)^.isclass and
  1699. assigned(aktprocsym) and
  1700. ((aktprocsym^.definition^.options and
  1701. (poconstructor or podestructor))<>0)) then
  1702. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_ESI)));
  1703. { if an inherited con- or destructor should be }
  1704. { called in a con- or destructor then a warning }
  1705. { will be made }
  1706. { con- and destructors need a pointer to the vmt }
  1707. if is_con_or_destructor and
  1708. not(pobjectdef(p^.methodpointer^.resulttype)^.isclass) and
  1709. assigned(aktprocsym) then
  1710. begin
  1711. if not ((aktprocsym^.definition^.options
  1712. and (poconstructor or podestructor))<>0) then
  1713. Message(cg_w_member_cd_call_from_method);
  1714. end;
  1715. if is_con_or_destructor then
  1716. push_int(0)
  1717. end;
  1718. hnewn:
  1719. begin
  1720. { extended syntax of new }
  1721. { ESI must be zero }
  1722. exprasmlist^.concat(new(pai386,op_reg_reg(A_XOR,S_L,R_ESI,R_ESI)));
  1723. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_ESI)));
  1724. { insert the vmt }
  1725. exprasmlist^.concat(new(pai386,op_csymbol(A_PUSH,S_L,
  1726. newcsymbol(pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname,0))));
  1727. maybe_concat_external(pobjectdef(p^.methodpointer^.resulttype)^.owner,
  1728. pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname);
  1729. extended_new:=true;
  1730. end;
  1731. hdisposen:
  1732. begin
  1733. secondpass(p^.methodpointer);
  1734. { destructor with extended syntax called from dispose }
  1735. { hdisposen always deliver LOC_REFERENCE }
  1736. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,
  1737. newreference(p^.methodpointer^.location.reference),R_ESI)));
  1738. del_reference(p^.methodpointer^.location.reference);
  1739. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_ESI)));
  1740. exprasmlist^.concat(new(pai386,op_csymbol(A_PUSH,S_L,
  1741. newcsymbol(pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname,0))));
  1742. maybe_concat_external(pobjectdef(p^.methodpointer^.resulttype)^.owner,
  1743. pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname);
  1744. end;
  1745. else
  1746. begin
  1747. { call to an instance member }
  1748. if (p^.symtable^.symtabletype<>withsymtable) then
  1749. begin
  1750. secondpass(p^.methodpointer);
  1751. case p^.methodpointer^.location.loc of
  1752. LOC_REGISTER:
  1753. begin
  1754. ungetregister32(p^.methodpointer^.location.register);
  1755. emit_reg_reg(A_MOV,S_L,p^.methodpointer^.location.register,R_ESI);
  1756. end;
  1757. else
  1758. begin
  1759. if (p^.methodpointer^.resulttype^.deftype=objectdef) and
  1760. pobjectdef(p^.methodpointer^.resulttype)^.isclass then
  1761. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  1762. newreference(p^.methodpointer^.location.reference),R_ESI)))
  1763. else
  1764. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,
  1765. newreference(p^.methodpointer^.location.reference),R_ESI)));
  1766. del_reference(p^.methodpointer^.location.reference);
  1767. end;
  1768. end;
  1769. end;
  1770. { when calling a class method, we have
  1771. to load ESI with the VMT !
  1772. But that's wrong, if we call a class method via self
  1773. }
  1774. if ((p^.procdefinition^.options and poclassmethod)<>0)
  1775. and not(p^.methodpointer^.treetype=selfn) then
  1776. begin
  1777. { class method needs current VMT }
  1778. new(r);
  1779. reset_reference(r^);
  1780. r^.base:=R_ESI;
  1781. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,r,R_ESI)));
  1782. end;
  1783. { direct call to class constructor, don't allocate memory }
  1784. if is_con_or_destructor and (p^.methodpointer^.resulttype^.deftype=objectdef) and
  1785. (pobjectdef(p^.methodpointer^.resulttype)^.isclass) then
  1786. exprasmlist^.concat(new(pai386,op_const(A_PUSH,S_L,0)))
  1787. else
  1788. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_ESI)));
  1789. if is_con_or_destructor then
  1790. begin
  1791. { classes don't get a VMT pointer pushed }
  1792. if (p^.methodpointer^.resulttype^.deftype=objectdef) and
  1793. not(pobjectdef(p^.methodpointer^.resulttype)^.isclass) then
  1794. begin
  1795. if ((p^.procdefinition^.options and poconstructor)<>0) then
  1796. begin
  1797. { it's no bad idea, to insert the VMT }
  1798. exprasmlist^.concat(new(pai386,op_csymbol(A_PUSH,S_L,
  1799. newcsymbol(pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname,
  1800. 0))));
  1801. maybe_concat_external(pobjectdef(p^.methodpointer^.resulttype)^.owner,
  1802. pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname);
  1803. end
  1804. { destructors haven't to dispose the instance, if this is }
  1805. { a direct call }
  1806. else
  1807. push_int(0);
  1808. end;
  1809. end;
  1810. end;
  1811. end;
  1812. end;
  1813. end
  1814. else
  1815. begin
  1816. if ((p^.procdefinition^.options and poclassmethod)<>0) and
  1817. not(
  1818. assigned(aktprocsym) and
  1819. ((aktprocsym^.definition^.options and poclassmethod)<>0)
  1820. ) then
  1821. begin
  1822. { class method needs current VMT }
  1823. new(r);
  1824. reset_reference(r^);
  1825. r^.base:=R_ESI;
  1826. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,r,R_ESI)));
  1827. end
  1828. else
  1829. begin
  1830. { member call, ESI isn't modified }
  1831. loadesi:=false;
  1832. end;
  1833. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_ESI)));
  1834. { but a con- or destructor here would probably almost }
  1835. { always be placed wrong }
  1836. if is_con_or_destructor then
  1837. begin
  1838. Message(cg_w_member_cd_call_from_method);
  1839. push_int(0);
  1840. end;
  1841. end;
  1842. end;
  1843. { push base pointer ?}
  1844. if (lexlevel>1) and assigned(pprocdef(p^.procdefinition)^.parast) and
  1845. ((p^.procdefinition^.parast^.symtablelevel)>2) then
  1846. begin
  1847. { if we call a nested function in a method, we must }
  1848. { push also SELF! }
  1849. { THAT'S NOT TRUE, we have to load ESI via frame pointer }
  1850. { access }
  1851. {
  1852. begin
  1853. loadesi:=false;
  1854. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_ESI)));
  1855. end;
  1856. }
  1857. if lexlevel=(p^.procdefinition^.parast^.symtablelevel) then
  1858. begin
  1859. new(r);
  1860. reset_reference(r^);
  1861. r^.offset:=procinfo.framepointer_offset;
  1862. r^.base:=procinfo.framepointer;
  1863. exprasmlist^.concat(new(pai386,op_ref(A_PUSH,S_L,r)))
  1864. end
  1865. { this is only true if the difference is one !!
  1866. but it cannot be more !! }
  1867. else if (lexlevel=p^.procdefinition^.parast^.symtablelevel-1) then
  1868. begin
  1869. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,procinfo.framepointer)))
  1870. end
  1871. else if (lexlevel>p^.procdefinition^.parast^.symtablelevel) then
  1872. begin
  1873. hregister:=getregister32;
  1874. new(r);
  1875. reset_reference(r^);
  1876. r^.offset:=procinfo.framepointer_offset;
  1877. r^.base:=procinfo.framepointer;
  1878. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,r,hregister)));
  1879. for i:=(p^.procdefinition^.parast^.symtablelevel) to lexlevel-1 do
  1880. begin
  1881. new(r);
  1882. reset_reference(r^);
  1883. {we should get the correct frame_pointer_offset at each level
  1884. how can we do this !!! }
  1885. r^.offset:=procinfo.framepointer_offset;
  1886. r^.base:=hregister;
  1887. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,r,hregister)));
  1888. end;
  1889. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,hregister)));
  1890. ungetregister32(hregister);
  1891. end
  1892. else
  1893. internalerror(25000);
  1894. end;
  1895. { exported methods should be never called direct.
  1896. Why? Bp7 Allows it (PFV)
  1897. if (p^.procdefinition^.options and poexports)<>0 then
  1898. Message(cg_e_dont_call_exported_direct); }
  1899. if (not inlined) and ((pushedparasize mod 4)<>0) then
  1900. begin
  1901. corr^.op1:=pointer(4-(pushedparasize mod 4));
  1902. must_pop:=true;
  1903. pop_size:=4-(pushedparasize mod 4);
  1904. end
  1905. else
  1906. begin
  1907. exprasmlist^.remove(corr);
  1908. must_pop:=false;
  1909. pop_size:=0;
  1910. end;
  1911. if ((p^.procdefinition^.options and povirtualmethod)<>0) and
  1912. not(no_virtual_call) then
  1913. begin
  1914. { static functions contain the vmt_address in ESI }
  1915. { also class methods }
  1916. if assigned(aktprocsym) then
  1917. begin
  1918. if ((aktprocsym^.properties and sp_static)<>0) or
  1919. ((aktprocsym^.definition^.options and poclassmethod)<>0) or
  1920. ((p^.procdefinition^.options and postaticmethod)<>0) or
  1921. ((p^.procdefinition^.options and poconstructor)<>0) or
  1922. { ESI is loaded earlier }
  1923. ((p^.procdefinition^.options and poclassmethod)<>0)then
  1924. begin
  1925. new(r);
  1926. reset_reference(r^);
  1927. r^.base:=R_ESI;
  1928. end
  1929. else
  1930. begin
  1931. new(r);
  1932. reset_reference(r^);
  1933. r^.base:=R_ESI;
  1934. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,r,R_EDI)));
  1935. new(r);
  1936. reset_reference(r^);
  1937. r^.base:=R_EDI;
  1938. end;
  1939. end
  1940. else
  1941. { aktprocsym should be assigned, also in main program }
  1942. internalerror(12345);
  1943. {
  1944. begin
  1945. new(r);
  1946. reset_reference(r^);
  1947. r^.base:=R_ESI;
  1948. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,r,R_EDI)));
  1949. new(r);
  1950. reset_reference(r^);
  1951. r^.base:=R_EDI;
  1952. end;
  1953. }
  1954. if p^.procdefinition^.extnumber=-1 then
  1955. internalerror($Da);
  1956. r^.offset:=p^.procdefinition^.extnumber*4+12;
  1957. if (cs_rangechecking in aktswitches) then
  1958. begin
  1959. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,r^.base)));
  1960. emitcall('CHECK_OBJECT',true);
  1961. end;
  1962. exprasmlist^.concat(new(pai386,op_ref(A_CALL,S_NO,r)));
  1963. end
  1964. else if not inlined then
  1965. emitcall(p^.procdefinition^.mangledname,
  1966. (p^.symtableproc^.symtabletype=unitsymtable) or
  1967. ((p^.symtableproc^.symtabletype=objectsymtable) and
  1968. (pobjectdef(p^.symtableproc^.defowner)^.owner^.symtabletype=unitsymtable)))
  1969. else { inlined proc }
  1970. { inlined code is in inlinecode }
  1971. begin
  1972. secondpass(inlinecode);
  1973. { set poinline again }
  1974. p^.procdefinition^.options:=p^.procdefinition^.options or poinline;
  1975. { free the args }
  1976. ungetpersistanttemp(p^.procdefinition^.parast^.call_offset,
  1977. p^.procdefinition^.parast^.datasize);
  1978. end;
  1979. if (not inlined) and ((p^.procdefinition^.options and poclearstack)<>0) then
  1980. begin
  1981. { consider the alignment with the rest (PM) }
  1982. pushedparasize:=pushedparasize+pop_size;
  1983. must_pop:=false;
  1984. if pushedparasize=4 then
  1985. { better than an add on all processors }
  1986. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,R_EDI)))
  1987. { the pentium has two pipes and pop reg is pairable }
  1988. { but the registers must be different! }
  1989. else if (pushedparasize=8) and
  1990. not(cs_littlesize in aktswitches) and
  1991. (aktoptprocessor=pentium) and
  1992. (procinfo._class=nil) then
  1993. begin
  1994. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,R_EDI)));
  1995. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,R_ESI)));
  1996. end
  1997. else exprasmlist^.concat(new(pai386,op_const_reg(A_ADD,S_L,pushedparasize,R_ESP)));
  1998. end;
  1999. end
  2000. else
  2001. begin
  2002. if (pushedparasize mod 4)<>0 then
  2003. begin
  2004. corr^.op1:=pointer(4-(pushedparasize mod 4));
  2005. must_pop:=true;
  2006. pop_size:=4-(pushedparasize mod 4);
  2007. end
  2008. else
  2009. begin
  2010. exprasmlist^.remove(corr);
  2011. must_pop:=false;
  2012. pop_size:=0;
  2013. end;
  2014. secondpass(p^.right);
  2015. { method pointer ? }
  2016. if (p^.procdefinition^.options and pomethodpointer)<>0 then
  2017. begin
  2018. { method pointer can't be in a register }
  2019. inc(p^.right^.location.reference.offset,4);
  2020. { push self pointer }
  2021. exprasmlist^.concat(new(pai386,op_ref(A_PUSH,S_L,newreference(p^.right^.location.reference))));
  2022. del_reference(p^.right^.location.reference);
  2023. dec(p^.right^.location.reference.offset,4);
  2024. end;
  2025. case p^.right^.location.loc of
  2026. LOC_REGISTER,LOC_CREGISTER:
  2027. begin
  2028. exprasmlist^.concat(new(pai386,op_reg(A_CALL,S_NO,p^.right^.location.register)));
  2029. ungetregister32(p^.right^.location.register);
  2030. end
  2031. else
  2032. exprasmlist^.concat(new(pai386,op_ref(A_CALL,S_NO,newreference(p^.right^.location.reference))));
  2033. del_reference(p^.right^.location.reference);
  2034. end;
  2035. end;
  2036. dont_call:
  2037. pushedparasize:=oldpushedparasize;
  2038. unused:=unusedregisters;
  2039. { handle function results }
  2040. { structured results are easy to handle.... }
  2041. { needed also when result_no_used !! }
  2042. if (p^.resulttype<>pdef(voiddef)) and ret_in_param(p^.resulttype) then
  2043. begin
  2044. p^.location.loc:=LOC_MEM;
  2045. stringdispose(p^.location.reference.symbol);
  2046. p^.location.reference:=funcretref;
  2047. end;
  2048. if (p^.resulttype<>pdef(voiddef)) and p^.return_value_used then
  2049. begin
  2050. { a contructor could be a function with boolean result }
  2051. if (p^.right=nil) and
  2052. ((p^.procdefinition^.options and poconstructor)<>0) and
  2053. { quick'n'dirty check if it is a class or an object }
  2054. (p^.resulttype^.deftype=orddef) then
  2055. begin
  2056. p^.location.loc:=LOC_FLAGS;
  2057. p^.location.resflags:=F_NE;
  2058. if extended_new then
  2059. begin
  2060. {$ifdef test_dest_loc}
  2061. if dest_loc_known and (dest_loc_tree=p) then
  2062. mov_reg_to_dest(p,S_L,R_EAX)
  2063. else
  2064. {$endif test_dest_loc}
  2065. begin
  2066. hregister:=getregister32;
  2067. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  2068. p^.location.register:=hregister;
  2069. end;
  2070. end;
  2071. end
  2072. { structed results are easy to handle.... }
  2073. else if ret_in_param(p^.resulttype) then
  2074. begin
  2075. {p^.location.loc:=LOC_MEM;
  2076. stringdispose(p^.location.reference.symbol);
  2077. p^.location.reference:=funcretref;
  2078. already done above (PM) }
  2079. end
  2080. else
  2081. begin
  2082. if (p^.resulttype^.deftype=orddef) then
  2083. begin
  2084. p^.location.loc:=LOC_REGISTER;
  2085. case porddef(p^.resulttype)^.typ of
  2086. s32bit,u32bit,bool32bit :
  2087. begin
  2088. {$ifdef test_dest_loc}
  2089. if dest_loc_known and (dest_loc_tree=p) then
  2090. mov_reg_to_dest(p,S_L,R_EAX)
  2091. else
  2092. {$endif test_dest_loc}
  2093. begin
  2094. hregister:=getregister32;
  2095. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  2096. p^.location.register:=hregister;
  2097. end;
  2098. end;
  2099. uchar,u8bit,bool8bit,s8bit :
  2100. begin
  2101. {$ifdef test_dest_loc}
  2102. if dest_loc_known and (dest_loc_tree=p) then
  2103. mov_reg_to_dest(p,S_B,R_AL)
  2104. else
  2105. {$endif test_dest_loc}
  2106. begin
  2107. hregister:=getregister32;
  2108. emit_reg_reg(A_MOV,S_B,R_AL,reg32toreg8(hregister));
  2109. p^.location.register:=reg32toreg8(hregister);
  2110. end;
  2111. end;
  2112. s16bit,u16bit,bool16bit :
  2113. begin
  2114. {$ifdef test_dest_loc}
  2115. if dest_loc_known and (dest_loc_tree=p) then
  2116. mov_reg_to_dest(p,S_W,R_AX)
  2117. else
  2118. {$endif test_dest_loc}
  2119. begin
  2120. hregister:=getregister32;
  2121. emit_reg_reg(A_MOV,S_W,R_AX,reg32toreg16(hregister));
  2122. p^.location.register:=reg32toreg16(hregister);
  2123. end;
  2124. end;
  2125. else internalerror(7);
  2126. end
  2127. end
  2128. else if (p^.resulttype^.deftype=floatdef) then
  2129. case pfloatdef(p^.resulttype)^.typ of
  2130. f32bit : begin
  2131. p^.location.loc:=LOC_REGISTER;
  2132. {$ifdef test_dest_loc}
  2133. if dest_loc_known and (dest_loc_tree=p) then
  2134. mov_reg_to_dest(p,S_L,R_EAX)
  2135. else
  2136. {$endif test_dest_loc}
  2137. begin
  2138. hregister:=getregister32;
  2139. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  2140. p^.location.register:=hregister;
  2141. end;
  2142. end;
  2143. else
  2144. p^.location.loc:=LOC_FPU;
  2145. end
  2146. else
  2147. begin
  2148. p^.location.loc:=LOC_REGISTER;
  2149. {$ifdef test_dest_loc}
  2150. if dest_loc_known and (dest_loc_tree=p) then
  2151. mov_reg_to_dest(p,S_L,R_EAX)
  2152. else
  2153. {$endif test_dest_loc}
  2154. begin
  2155. hregister:=getregister32;
  2156. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  2157. p^.location.register:=hregister;
  2158. end;
  2159. end;
  2160. end;
  2161. end;
  2162. { perhaps i/o check ? }
  2163. if iolabel<>nil then
  2164. begin
  2165. exprasmlist^.concat(new(pai386,op_csymbol(A_PUSH,S_L,newcsymbol(lab2str(iolabel),0))));
  2166. { this was wrong, probably an error due to diff3
  2167. emitcall(p^.procdefinition^.mangledname);}
  2168. emitcall('IOCHECK',true);
  2169. end;
  2170. { this should be optimized (PM) }
  2171. if must_pop then
  2172. exprasmlist^.concat(new(pai386,op_const_reg(A_ADD,S_L,pop_size,R_ESP)));
  2173. { restore registers }
  2174. popusedregisters(pushed);
  2175. { at last, restore instance pointer (SELF) }
  2176. if loadesi then
  2177. maybe_loadesi;
  2178. pp:=params;
  2179. while assigned(pp) do
  2180. begin
  2181. if assigned(pp^.left) then
  2182. if (pp^.left^.location.loc=LOC_REFERENCE) or
  2183. (pp^.left^.location.loc=LOC_MEM) then
  2184. ungetiftemp(pp^.left^.location.reference);
  2185. pp:=pp^.right;
  2186. end;
  2187. if inlined then
  2188. ungetpersistanttemp(inlinecode^.retoffset,4);
  2189. disposetree(params);
  2190. { from now on the result can be freed normally }
  2191. if inlined and ret_in_param(p^.resulttype) then
  2192. persistanttemptonormal(funcretref.offset);
  2193. { if return value is not used }
  2194. if (not p^.return_value_used) and (p^.resulttype<>pdef(voiddef)) then
  2195. begin
  2196. if p^.location.loc in [LOC_MEM,LOC_REFERENCE] then
  2197. { release unused temp }
  2198. ungetiftemp(p^.location.reference)
  2199. else if p^.location.loc=LOC_FPU then
  2200. { release FPU stack }
  2201. exprasmlist^.concat(new(pai386,op_none(A_FDECSTP,S_NO)));
  2202. end;
  2203. end;
  2204. { reverts the parameter list }
  2205. var nb_para : integer;
  2206. function reversparameter(p : ptree) : ptree;
  2207. var
  2208. hp1,hp2 : ptree;
  2209. begin
  2210. hp1:=nil;
  2211. nb_para := 0;
  2212. while assigned(p) do
  2213. begin
  2214. { pull out }
  2215. hp2:=p;
  2216. p:=p^.right;
  2217. inc(nb_para);
  2218. { pull in }
  2219. hp2^.right:=hp1;
  2220. hp1:=hp2;
  2221. end;
  2222. reversparameter:=hp1;
  2223. end;
  2224. procedure secondinline(var p : ptree);
  2225. const in2size:array[in_inc_byte..in_dec_dword] of Topsize=
  2226. (S_B,S_W,S_L,S_B,S_W,S_L);
  2227. in2instr:array[in_inc_byte..in_dec_dword] of Tasmop=
  2228. (A_INC,A_INC,A_INC,A_DEC,A_DEC,A_DEC);
  2229. ad2instr:array[in_inc_byte..in_dec_dword] of Tasmop=
  2230. (A_ADD,A_ADD,A_ADD,A_SUB,A_SUB,A_SUB);
  2231. { tfloattype = (f32bit,s32real,s64real,s80real,s64bit); }
  2232. float_name: array[tfloattype] of string[8]=
  2233. ('FIXED','SINGLE','REAL','EXTENDED','COMP','FIXED16');
  2234. var
  2235. aktfile : treference;
  2236. ft : tfiletype;
  2237. opsize : topsize;
  2238. asmop : tasmop;
  2239. pushed : tpushed;
  2240. { produces code for READ(LN) and WRITE(LN) }
  2241. procedure handlereadwrite(doread,callwriteln : boolean);
  2242. procedure loadstream;
  2243. const
  2244. io:array[0..1] of string[7]=('_OUTPUT','_INPUT');
  2245. var
  2246. r : preference;
  2247. begin
  2248. new(r);
  2249. reset_reference(r^);
  2250. r^.symbol:=stringdup('U_'+upper(target_info.system_unit)+io[byte(doread)]);
  2251. { if not (cs_compilesystem in aktswitches) then }
  2252. concat_external(r^.symbol^,EXT_NEAR);
  2253. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,r,R_EDI)))
  2254. end;
  2255. var
  2256. node,hp : ptree;
  2257. typedtyp,pararesult : pdef;
  2258. doflush,has_length : boolean;
  2259. dummycoll : tdefcoll;
  2260. iolabel : plabel;
  2261. npara : longint;
  2262. begin
  2263. { I/O check }
  2264. if cs_iocheck in aktswitches then
  2265. begin
  2266. getlabel(iolabel);
  2267. emitl(A_LABEL,iolabel);
  2268. end
  2269. else iolabel:=nil;
  2270. { no automatic call from flush }
  2271. doflush:=false;
  2272. { for write of real with the length specified }
  2273. has_length:=false;
  2274. hp:=nil;
  2275. { reserve temporary pointer to data variable }
  2276. aktfile.symbol:=nil;
  2277. gettempofsizereference(4,aktfile);
  2278. { first state text data }
  2279. ft:=ft_text;
  2280. { and state a parameter ? }
  2281. if p^.left=nil then
  2282. begin
  2283. { state screen address}
  2284. doflush:=true;
  2285. { the following instructions are for "writeln;" }
  2286. loadstream;
  2287. { save @Dateivarible in temporary variable }
  2288. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,newreference(aktfile))));
  2289. end
  2290. else
  2291. begin
  2292. { revers paramters }
  2293. node:=reversparameter(p^.left);
  2294. p^.left := node;
  2295. npara := nb_para;
  2296. { calculate data variable }
  2297. { is first parameter a file type ? }
  2298. if node^.left^.resulttype^.deftype=filedef then
  2299. begin
  2300. ft:=pfiledef(node^.left^.resulttype)^.filetype;
  2301. if ft=ft_typed then
  2302. typedtyp:=pfiledef(node^.left^.resulttype)^.typed_as;
  2303. secondpass(node^.left);
  2304. if codegenerror then
  2305. exit;
  2306. { save reference in temporary variables } { reference in tempor„re Variable retten }
  2307. if node^.left^.location.loc<>LOC_REFERENCE then
  2308. begin
  2309. Message(cg_e_illegal_expression);
  2310. exit;
  2311. end;
  2312. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,newreference(node^.left^.location.reference),R_EDI)));
  2313. { skip to the next parameter }
  2314. node:=node^.right;
  2315. end
  2316. else
  2317. begin
  2318. { if we write to stdout/in then flush after the write(ln) }
  2319. doflush:=true;
  2320. loadstream;
  2321. end;
  2322. { save @Dateivarible in temporary variable }
  2323. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,newreference(aktfile))));
  2324. if doread then
  2325. { parameter by READ gives call by reference }
  2326. dummycoll.paratyp:=vs_var
  2327. { an WRITE Call by "Const" }
  2328. else dummycoll.paratyp:=vs_const;
  2329. { because of secondcallparan, which otherwise attaches }
  2330. if ft=ft_typed then
  2331. begin
  2332. { this is to avoid copy of simple const parameters }
  2333. dummycoll.data:=new(pformaldef,init);
  2334. end
  2335. else
  2336. { I think, this isn't a good solution (FK) }
  2337. dummycoll.data:=nil;
  2338. while assigned(node) do
  2339. begin
  2340. pushusedregisters(pushed,$ff);
  2341. hp:=node;
  2342. node:=node^.right;
  2343. hp^.right:=nil;
  2344. if hp^.is_colon_para then
  2345. Message(parser_e_illegal_colon_qualifier);
  2346. if ft=ft_typed then
  2347. never_copy_const_param:=true;
  2348. secondcallparan(hp,@dummycoll,false
  2349. ,false,0
  2350. );
  2351. if ft=ft_typed then
  2352. never_copy_const_param:=false;
  2353. hp^.right:=node;
  2354. if codegenerror then
  2355. exit;
  2356. emit_push_mem(aktfile);
  2357. if (ft=ft_typed) then
  2358. begin
  2359. { OK let's try this }
  2360. { first we must only allow the right type }
  2361. { we have to call blockread or blockwrite }
  2362. { but the real problem is that }
  2363. { reset and rewrite should have set }
  2364. { the type size }
  2365. { as recordsize for that file !!!! }
  2366. { how can we make that }
  2367. { I think that is only possible by adding }
  2368. { reset and rewrite to the inline list a call }
  2369. { allways read only one record by element }
  2370. push_int(typedtyp^.size);
  2371. if doread then
  2372. emitcall('TYPED_READ',true)
  2373. else
  2374. emitcall('TYPED_WRITE',true)
  2375. {!!!!!!!}
  2376. end
  2377. else
  2378. begin
  2379. { save current position }
  2380. pararesult:=hp^.left^.resulttype;
  2381. { handle possible field width }
  2382. { of course only for write(ln) }
  2383. if not doread then
  2384. begin
  2385. { handle total width parameter }
  2386. if assigned(node) and node^.is_colon_para then
  2387. begin
  2388. hp:=node;
  2389. node:=node^.right;
  2390. hp^.right:=nil;
  2391. secondcallparan(hp,@dummycoll,false
  2392. ,false,0
  2393. );
  2394. hp^.right:=node;
  2395. if codegenerror then
  2396. exit;
  2397. has_length:=true;
  2398. end
  2399. else
  2400. if pararesult^.deftype<>floatdef then
  2401. push_int(0)
  2402. else
  2403. push_int(-32767);
  2404. { a second colon para for a float ? }
  2405. if assigned(node) and node^.is_colon_para then
  2406. begin
  2407. hp:=node;
  2408. node:=node^.right;
  2409. hp^.right:=nil;
  2410. secondcallparan(hp,@dummycoll,false
  2411. ,false,0
  2412. );
  2413. hp^.right:=node;
  2414. if pararesult^.deftype<>floatdef then
  2415. Message(parser_e_illegal_colon_qualifier);
  2416. if codegenerror then
  2417. exit;
  2418. end
  2419. else
  2420. begin
  2421. if pararesult^.deftype=floatdef then
  2422. push_int(-1);
  2423. end
  2424. end;
  2425. case pararesult^.deftype of
  2426. stringdef:
  2427. begin
  2428. if doread then
  2429. emitcall('READ_TEXT_STRING',true)
  2430. else
  2431. begin
  2432. emitcall('WRITE_TEXT_STRING',true);
  2433. {ungetiftemp(hp^.left^.location.reference);}
  2434. end;
  2435. end;
  2436. pointerdef : begin
  2437. if is_equal(ppointerdef(pararesult)^.definition,cchardef) then
  2438. begin
  2439. if doread then
  2440. emitcall('READ_TEXT_PCHAR_AS_POINTER',true)
  2441. else
  2442. emitcall('WRITE_TEXT_PCHAR_AS_POINTER',true);
  2443. end
  2444. else
  2445. Message(parser_e_illegal_parameter_list);
  2446. end;
  2447. arraydef : begin
  2448. if (parraydef(pararesult)^.lowrange=0)
  2449. and is_equal(parraydef(pararesult)^.definition,cchardef) then
  2450. begin
  2451. if doread then
  2452. emitcall('READ_TEXT_PCHAR_AS_ARRAY',true)
  2453. else
  2454. emitcall('WRITE_TEXT_PCHAR_AS_ARRAY',true);
  2455. end
  2456. else
  2457. Message(parser_e_illegal_parameter_list);
  2458. end;
  2459. floatdef:
  2460. begin
  2461. if doread then
  2462. emitcall('READ_TEXT_'+float_name[pfloatdef(pararesult)^.typ],true)
  2463. else
  2464. emitcall('WRITE_TEXT_'+float_name[pfloatdef(pararesult)^.typ],true);
  2465. end;
  2466. orddef : begin
  2467. case porddef(pararesult)^.typ of
  2468. u8bit : if doread then
  2469. emitcall('READ_TEXT_BYTE',true);
  2470. s8bit : if doread then
  2471. emitcall('READ_TEXT_SHORTINT',true);
  2472. u16bit : if doread then
  2473. emitcall('READ_TEXT_WORD',true);
  2474. s16bit : if doread then
  2475. emitcall('READ_TEXT_INTEGER',true);
  2476. s32bit : if doread then
  2477. emitcall('READ_TEXT_LONGINT',true)
  2478. else
  2479. emitcall('WRITE_TEXT_LONGINT',true);
  2480. u32bit : if doread then
  2481. emitcall('READ_TEXT_CARDINAL',true)
  2482. else
  2483. emitcall('WRITE_TEXT_CARDINAL',true);
  2484. uchar : if doread then
  2485. emitcall('READ_TEXT_CHAR',true)
  2486. else
  2487. emitcall('WRITE_TEXT_CHAR',true);
  2488. bool8bit,
  2489. bool16bit,
  2490. bool32bit : if doread then
  2491. { emitcall('READ_TEXT_BOOLEAN',true) }
  2492. Message(parser_e_illegal_parameter_list)
  2493. else
  2494. emitcall('WRITE_TEXT_BOOLEAN',true);
  2495. else Message(parser_e_illegal_parameter_list);
  2496. end;
  2497. end;
  2498. else Message(parser_e_illegal_parameter_list);
  2499. end;
  2500. end;
  2501. { load ESI in methods again }
  2502. popusedregisters(pushed);
  2503. maybe_loadesi;
  2504. end;
  2505. end;
  2506. if callwriteln then
  2507. begin
  2508. pushusedregisters(pushed,$ff);
  2509. emit_push_mem(aktfile);
  2510. { pushexceptlabel; }
  2511. if ft<>ft_text then
  2512. Message(parser_e_illegal_parameter_list) ;
  2513. emitcall('WRITELN_TEXT',true);
  2514. popusedregisters(pushed);
  2515. maybe_loadesi;
  2516. end;
  2517. if doflush and not(doread) then
  2518. begin
  2519. pushusedregisters(pushed,$ff);
  2520. { pushexceptlabel; }
  2521. emitcall('FLUSH_STDOUT',true);
  2522. popusedregisters(pushed);
  2523. maybe_loadesi;
  2524. end;
  2525. if iolabel<>nil then
  2526. begin
  2527. { registers are saved in the procedure }
  2528. exprasmlist^.concat(new(pai386,op_csymbol(A_PUSH,S_L,newcsymbol(lab2str(iolabel),0))));
  2529. emitcall('IOCHECK',true);
  2530. end;
  2531. ungetiftemp(aktfile);
  2532. if assigned(p^.left) then
  2533. begin
  2534. p^.left:=reversparameter(p^.left);
  2535. if npara<>nb_para then
  2536. Message(cg_f_internal_error_in_secondinline);
  2537. hp:=p^.left;
  2538. while assigned(hp) do
  2539. begin
  2540. if assigned(hp^.left) then
  2541. if (hp^.left^.location.loc=LOC_REFERENCE) or
  2542. (hp^.left^.location.loc=LOC_MEM) then
  2543. ungetiftemp(hp^.left^.location.reference);
  2544. hp:=hp^.right;
  2545. end;
  2546. end;
  2547. end;
  2548. procedure handle_str;
  2549. var
  2550. hp,node : ptree;
  2551. dummycoll : tdefcoll;
  2552. is_real,has_length : boolean;
  2553. begin
  2554. pushusedregisters(pushed,$ff);
  2555. node:=p^.left;
  2556. is_real:=false;
  2557. has_length:=false;
  2558. while assigned(node^.right) do node:=node^.right;
  2559. { if a real parameter somewhere then call REALSTR }
  2560. if (node^.left^.resulttype^.deftype=floatdef) then
  2561. is_real:=true;
  2562. node:=p^.left;
  2563. { we have at least two args }
  2564. { with at max 2 colon_para in between }
  2565. { first arg longint or float }
  2566. hp:=node;
  2567. node:=node^.right;
  2568. hp^.right:=nil;
  2569. dummycoll.data:=hp^.resulttype;
  2570. { string arg }
  2571. dummycoll.paratyp:=vs_var;
  2572. secondcallparan(hp,@dummycoll,false
  2573. ,false,0
  2574. );
  2575. if codegenerror then
  2576. exit;
  2577. dummycoll.paratyp:=vs_const;
  2578. { second arg }
  2579. hp:=node;
  2580. node:=node^.right;
  2581. hp^.right:=nil;
  2582. { frac para }
  2583. if hp^.is_colon_para and assigned(node) and
  2584. node^.is_colon_para then
  2585. begin
  2586. dummycoll.data:=hp^.resulttype;
  2587. secondcallparan(hp,@dummycoll,false
  2588. ,false,0
  2589. );
  2590. if codegenerror then
  2591. exit;
  2592. hp:=node;
  2593. node:=node^.right;
  2594. hp^.right:=nil;
  2595. has_length:=true;
  2596. end
  2597. else
  2598. if is_real then
  2599. push_int(-1);
  2600. { third arg, length only if is_real }
  2601. if hp^.is_colon_para then
  2602. begin
  2603. dummycoll.data:=hp^.resulttype;
  2604. secondcallparan(hp,@dummycoll,false
  2605. ,false,0
  2606. );
  2607. if codegenerror then
  2608. exit;
  2609. hp:=node;
  2610. node:=node^.right;
  2611. hp^.right:=nil;
  2612. end
  2613. else
  2614. if is_real then
  2615. push_int(-32767)
  2616. else
  2617. push_int(-1);
  2618. { last arg longint or real }
  2619. secondcallparan(hp,@dummycoll,false
  2620. ,false,0
  2621. );
  2622. if codegenerror then
  2623. exit;
  2624. if is_real then
  2625. emitcall('STR_'+float_name[pfloatdef(hp^.resulttype)^.typ],true)
  2626. else if porddef(hp^.resulttype)^.typ=u32bit then
  2627. emitcall('STR_CARDINAL',true)
  2628. else
  2629. emitcall('STR_LONGINT',true);
  2630. popusedregisters(pushed);
  2631. end;
  2632. var
  2633. r : preference;
  2634. l : longint;
  2635. ispushed : boolean;
  2636. hregister : tregister;
  2637. begin
  2638. case p^.inlinenumber of
  2639. in_lo_word,
  2640. in_hi_word :
  2641. begin
  2642. secondpass(p^.left);
  2643. p^.location.loc:=LOC_REGISTER;
  2644. if p^.left^.location.loc<>LOC_REGISTER then
  2645. begin
  2646. if p^.left^.location.loc=LOC_CREGISTER then
  2647. begin
  2648. p^.location.register:=reg32toreg16(getregister32);
  2649. emit_reg_reg(A_MOV,S_W,p^.left^.location.register,
  2650. p^.location.register);
  2651. end
  2652. else
  2653. begin
  2654. del_reference(p^.left^.location.reference);
  2655. p^.location.register:=reg32toreg16(getregister32);
  2656. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_W,newreference(p^.left^.location.reference),
  2657. p^.location.register)));
  2658. end;
  2659. end
  2660. else p^.location.register:=p^.left^.location.register;
  2661. if p^.inlinenumber=in_hi_word then
  2662. exprasmlist^.concat(new(pai386,op_const_reg(A_SHR,S_W,8,p^.location.register)));
  2663. p^.location.register:=reg16toreg8(p^.location.register);
  2664. end;
  2665. in_high_x :
  2666. begin
  2667. if is_open_array(p^.left^.resulttype) then
  2668. begin
  2669. secondpass(p^.left);
  2670. del_reference(p^.left^.location.reference);
  2671. p^.location.register:=getregister32;
  2672. new(r);
  2673. reset_reference(r^);
  2674. r^.base:=highframepointer;
  2675. r^.offset:=highoffset+4;
  2676. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  2677. r,p^.location.register)));
  2678. end
  2679. end;
  2680. in_sizeof_x,
  2681. in_typeof_x :
  2682. begin
  2683. { for both cases load vmt }
  2684. if p^.left^.treetype=typen then
  2685. begin
  2686. p^.location.register:=getregister32;
  2687. exprasmlist^.concat(new(pai386,op_csymbol_reg(A_MOV,
  2688. S_L,newcsymbol(pobjectdef(p^.left^.resulttype)^.vmt_mangledname,0),
  2689. p^.location.register)));
  2690. end
  2691. else
  2692. begin
  2693. secondpass(p^.left);
  2694. del_reference(p^.left^.location.reference);
  2695. p^.location.loc:=LOC_REGISTER;
  2696. p^.location.register:=getregister32;
  2697. { load VMT pointer }
  2698. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  2699. newreference(p^.left^.location.reference),
  2700. p^.location.register)));
  2701. end;
  2702. { in sizeof load size }
  2703. if p^.inlinenumber=in_sizeof_x then
  2704. begin
  2705. new(r);
  2706. reset_reference(r^);
  2707. r^.base:=p^.location.register;
  2708. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,r,
  2709. p^.location.register)));
  2710. end;
  2711. end;
  2712. in_lo_long,
  2713. in_hi_long :
  2714. begin
  2715. secondpass(p^.left);
  2716. p^.location.loc:=LOC_REGISTER;
  2717. if p^.left^.location.loc<>LOC_REGISTER then
  2718. begin
  2719. if p^.left^.location.loc=LOC_CREGISTER then
  2720. begin
  2721. p^.location.register:=getregister32;
  2722. emit_reg_reg(A_MOV,S_L,p^.left^.location.register,
  2723. p^.location.register);
  2724. end
  2725. else
  2726. begin
  2727. del_reference(p^.left^.location.reference);
  2728. p^.location.register:=getregister32;
  2729. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,newreference(p^.left^.location.reference),
  2730. p^.location.register)));
  2731. end;
  2732. end
  2733. else p^.location.register:=p^.left^.location.register;
  2734. if p^.inlinenumber=in_hi_long then
  2735. exprasmlist^.concat(new(pai386,op_const_reg(A_SHR,S_L,16,p^.location.register)));
  2736. p^.location.register:=reg32toreg16(p^.location.register);
  2737. end;
  2738. {***CHARBUG}
  2739. {We can now comment them out, as they are handled as typecast.
  2740. Saves an incredible amount of 8 bytes code.
  2741. I'am not lucky about this, because it's _not_ a type cast (FK) }
  2742. { in_ord_char,
  2743. in_chr_byte,}
  2744. {***}
  2745. in_length_string :
  2746. begin
  2747. secondpass(p^.left);
  2748. set_location(p^.location,p^.left^.location);
  2749. { length in ansi strings is at offset -8 }
  2750. {$ifdef UseAnsiString}
  2751. if is_ansistring(p^.left^.resulttype) then
  2752. dec(p^.location.reference.offset,8);
  2753. {$endif UseAnsiString}
  2754. end;
  2755. in_pred_x,
  2756. in_succ_x:
  2757. begin
  2758. secondpass(p^.left);
  2759. if p^.inlinenumber=in_pred_x then
  2760. asmop:=A_DEC
  2761. else
  2762. asmop:=A_INC;
  2763. case p^.resulttype^.size of
  2764. 4 : opsize:=S_L;
  2765. 2 : opsize:=S_W;
  2766. 1 : opsize:=S_B;
  2767. else
  2768. internalerror(10080);
  2769. end;
  2770. p^.location.loc:=LOC_REGISTER;
  2771. if p^.left^.location.loc<>LOC_REGISTER then
  2772. begin
  2773. p^.location.register:=getregister32;
  2774. if (p^.resulttype^.size=2) then
  2775. p^.location.register:=reg32toreg16(p^.location.register);
  2776. if (p^.resulttype^.size=1) then
  2777. p^.location.register:=reg32toreg8(p^.location.register);
  2778. if p^.left^.location.loc=LOC_CREGISTER then
  2779. emit_reg_reg(A_MOV,opsize,p^.left^.location.register,
  2780. p^.location.register)
  2781. else
  2782. if p^.left^.location.loc=LOC_FLAGS then
  2783. exprasmlist^.concat(new(pai386,op_reg(flag_2_set[p^.left^.location.resflags],S_B,
  2784. p^.location.register)))
  2785. else
  2786. begin
  2787. del_reference(p^.left^.location.reference);
  2788. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,opsize,newreference(p^.left^.location.reference),
  2789. p^.location.register)));
  2790. end;
  2791. end
  2792. else p^.location.register:=p^.left^.location.register;
  2793. exprasmlist^.concat(new(pai386,op_reg(asmop,opsize,
  2794. p^.location.register)))
  2795. { here we should insert bounds check ? }
  2796. { and direct call to bounds will crash the program }
  2797. { if we are at the limit }
  2798. { we could also simply say that pred(first)=first and succ(last)=last }
  2799. { could this be usefull I don't think so (PM)
  2800. emitoverflowcheck;}
  2801. end;
  2802. in_inc_byte..in_dec_dword:
  2803. begin
  2804. secondpass(p^.left);
  2805. if cs_check_overflow in aktswitches then
  2806. begin
  2807. { SINCE THE CARRY FLAG IS NEVER SET BY DEC/INC, we must use }
  2808. { ADD and SUB to check for overflow for unsigned operations. }
  2809. exprasmlist^.concat(new(pai386,op_const_ref(ad2instr[p^.inlinenumber],
  2810. in2size[p^.inlinenumber],1,newreference(p^.left^.location.reference))));
  2811. emitoverflowcheck(p^.left);
  2812. end
  2813. else
  2814. exprasmlist^.concat(new(pai386,op_ref(in2instr[p^.inlinenumber],
  2815. in2size[p^.inlinenumber],newreference(p^.left^.location.reference))));
  2816. end;
  2817. in_assigned_x :
  2818. begin
  2819. secondpass(p^.left^.left);
  2820. p^.location.loc:=LOC_FLAGS;
  2821. if (p^.left^.left^.location.loc=LOC_REGISTER) or
  2822. (p^.left^.left^.location.loc=LOC_CREGISTER) then
  2823. begin
  2824. exprasmlist^.concat(new(pai386,op_reg_reg(A_OR,S_L,
  2825. p^.left^.left^.location.register,
  2826. p^.left^.left^.location.register)));
  2827. ungetregister32(p^.left^.left^.location.register);
  2828. end
  2829. else
  2830. begin
  2831. exprasmlist^.concat(new(pai386,op_const_ref(A_CMP,S_L,0,
  2832. newreference(p^.left^.left^.location.reference))));
  2833. del_reference(p^.left^.left^.location.reference);
  2834. end;
  2835. p^.location.resflags:=F_NE;
  2836. end;
  2837. in_reset_typedfile,in_rewrite_typedfile :
  2838. begin
  2839. pushusedregisters(pushed,$ff);
  2840. exprasmlist^.concat(new(pai386,op_const(A_PUSH,S_L,pfiledef(p^.left^.resulttype)^.typed_as^.size)));
  2841. secondload(p^.left);
  2842. emitpushreferenceaddr(p^.left^.location.reference);
  2843. if p^.inlinenumber=in_reset_typedfile then
  2844. emitcall('RESET_TYPED',true)
  2845. else
  2846. emitcall('REWRITE_TYPED',true);
  2847. popusedregisters(pushed);
  2848. end;
  2849. in_write_x :
  2850. handlereadwrite(false,false);
  2851. in_writeln_x :
  2852. handlereadwrite(false,true);
  2853. in_read_x :
  2854. handlereadwrite(true,false);
  2855. in_readln_x :
  2856. begin
  2857. handlereadwrite(true,false);
  2858. pushusedregisters(pushed,$ff);
  2859. emit_push_mem(aktfile);
  2860. { pushexceptlabel; }
  2861. if ft<>ft_text then
  2862. Message(parser_e_illegal_parameter_list);
  2863. emitcall('READLN_TEXT',true);
  2864. popusedregisters(pushed);
  2865. maybe_loadesi;
  2866. end;
  2867. in_str_x_string :
  2868. begin
  2869. handle_str;
  2870. maybe_loadesi;
  2871. end;
  2872. in_include_x_y,
  2873. in_exclude_x_y:
  2874. begin
  2875. secondpass(p^.left^.left);
  2876. if p^.left^.right^.left^.treetype=ordconstn then
  2877. begin
  2878. { calculate bit position }
  2879. l:=1 shl (p^.left^.right^.left^.value mod 32);
  2880. { determine operator }
  2881. if p^.inlinenumber=in_include_x_y then
  2882. asmop:=A_OR
  2883. else
  2884. begin
  2885. asmop:=A_AND;
  2886. l:=not(l);
  2887. end;
  2888. if (p^.left^.left^.location.loc=LOC_REFERENCE) then
  2889. begin
  2890. inc(p^.left^.left^.location.reference.offset,(p^.left^.right^.left^.value div 32)*4);
  2891. exprasmlist^.concat(new(pai386,op_const_ref(asmop,S_L,
  2892. l,newreference(p^.left^.left^.location.reference))));
  2893. del_reference(p^.left^.left^.location.reference);
  2894. end
  2895. else
  2896. { LOC_CREGISTER }
  2897. exprasmlist^.concat(new(pai386,op_const_reg(asmop,S_L,
  2898. l,p^.left^.left^.location.register)));
  2899. end
  2900. else
  2901. begin
  2902. { generate code for the element to set }
  2903. ispushed:=maybe_push(p^.left^.right^.left^.registers32,p^.left^.left);
  2904. secondpass(p^.left^.right^.left);
  2905. if ispushed then
  2906. restore(p^.left^.left);
  2907. { determine asm operator }
  2908. if p^.inlinenumber=in_include_x_y then
  2909. asmop:=A_BTS
  2910. else
  2911. asmop:=A_BTR;
  2912. if psetdef(p^.left^.resulttype)^.settype=smallset then
  2913. begin
  2914. if p^.left^.right^.left^.location.loc in
  2915. [LOC_CREGISTER,LOC_REGISTER] then
  2916. hregister:=p^.left^.right^.left^.location.register
  2917. else
  2918. begin
  2919. hregister:=R_EDI;
  2920. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  2921. newreference(p^.left^.right^.left^.location.reference),
  2922. R_EDI)));
  2923. end;
  2924. if (p^.left^.left^.location.loc=LOC_REFERENCE) then
  2925. exprasmlist^.concat(new(pai386,op_reg_ref(asmop,S_L,R_EDI,
  2926. newreference(p^.left^.right^.left^.location.reference))))
  2927. else
  2928. exprasmlist^.concat(new(pai386,op_reg_reg(asmop,S_L,R_EDI,
  2929. p^.left^.right^.left^.location.register)));
  2930. end
  2931. else
  2932. begin
  2933. end;
  2934. end;
  2935. end;
  2936. else internalerror(9);
  2937. end;
  2938. end;
  2939. procedure secondsubscriptn(var p : ptree);
  2940. var
  2941. hr : tregister;
  2942. begin
  2943. secondpass(p^.left);
  2944. if codegenerror then
  2945. exit;
  2946. { classes must be dereferenced implicit }
  2947. if (p^.left^.resulttype^.deftype=objectdef) and
  2948. pobjectdef(p^.left^.resulttype)^.isclass then
  2949. begin
  2950. clear_reference(p^.location.reference);
  2951. case p^.left^.location.loc of
  2952. LOC_REGISTER:
  2953. p^.location.reference.base:=p^.left^.location.register;
  2954. LOC_CREGISTER:
  2955. begin
  2956. { ... and reserve one for the pointer }
  2957. hr:=getregister32;
  2958. emit_reg_reg(A_MOV,S_L,p^.left^.location.register,hr);
  2959. p^.location.reference.base:=hr;
  2960. end;
  2961. else
  2962. begin
  2963. { free register }
  2964. del_reference(p^.left^.location.reference);
  2965. { ... and reserve one for the pointer }
  2966. hr:=getregister32;
  2967. exprasmlist^.concat(new(pai386,op_ref_reg(
  2968. A_MOV,S_L,newreference(p^.left^.location.reference),
  2969. hr)));
  2970. p^.location.reference.base:=hr;
  2971. end;
  2972. end;
  2973. end
  2974. else
  2975. set_location(p^.location,p^.left^.location);
  2976. inc(p^.location.reference.offset,p^.vs^.address);
  2977. end;
  2978. procedure secondselfn(var p : ptree);
  2979. begin
  2980. clear_reference(p^.location.reference);
  2981. if (p^.resulttype^.deftype=classrefdef) or
  2982. ((p^.resulttype^.deftype=objectdef)
  2983. and pobjectdef(p^.resulttype)^.isclass
  2984. ) then
  2985. p^.location.register:=R_ESI
  2986. else
  2987. p^.location.reference.base:=R_ESI;
  2988. end;
  2989. procedure secondhdisposen(var p : ptree);
  2990. begin
  2991. secondpass(p^.left);
  2992. if codegenerror then
  2993. exit;
  2994. clear_reference(p^.location.reference);
  2995. case p^.left^.location.loc of
  2996. LOC_REGISTER,
  2997. LOC_CREGISTER:
  2998. begin
  2999. p^.location.reference.index:=getregister32;
  3000. exprasmlist^.concat(new(pai386,op_reg_reg(A_MOV,S_L,
  3001. p^.left^.location.register,
  3002. p^.location.reference.index)));
  3003. end;
  3004. LOC_MEM,LOC_REFERENCE :
  3005. begin
  3006. del_reference(p^.left^.location.reference);
  3007. p^.location.reference.index:=getregister32;
  3008. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,newreference(p^.left^.location.reference),
  3009. p^.location.reference.index)));
  3010. end;
  3011. end;
  3012. end;
  3013. procedure secondhnewn(var p : ptree);
  3014. begin
  3015. end;
  3016. procedure secondnewn(var p : ptree);
  3017. begin
  3018. secondpass(p^.left);
  3019. if codegenerror then
  3020. exit;
  3021. p^.location.register:=p^.left^.location.register;
  3022. end;
  3023. procedure secondsimplenewdispose(var p : ptree);
  3024. var
  3025. pushed : tpushed;
  3026. begin
  3027. secondpass(p^.left);
  3028. if codegenerror then
  3029. exit;
  3030. pushusedregisters(pushed,$ff);
  3031. { determines the size of the mem block }
  3032. push_int(ppointerdef(p^.left^.resulttype)^.definition^.size);
  3033. { push pointer adress }
  3034. case p^.left^.location.loc of
  3035. LOC_CREGISTER : exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,
  3036. p^.left^.location.register)));
  3037. LOC_REFERENCE : emitpushreferenceaddr(p^.left^.location.reference);
  3038. end;
  3039. { call the mem handling procedures }
  3040. case p^.treetype of
  3041. simpledisposen:
  3042. emitcall('FREEMEM',true);
  3043. simplenewn:
  3044. emitcall('GETMEM',true);
  3045. end;
  3046. popusedregisters(pushed);
  3047. { may be load ESI }
  3048. maybe_loadesi;
  3049. end;
  3050. { copies p a set element on the stack }
  3051. procedure pushsetelement(var p : ptree);
  3052. var
  3053. hr : tregister;
  3054. begin
  3055. { copy the element on the stack, slightly complicated }
  3056. case p^.location.loc of
  3057. LOC_REGISTER,
  3058. LOC_CREGISTER : begin
  3059. hr:=p^.location.register;
  3060. case hr of
  3061. R_EAX,R_EBX,R_ECX,R_EDX,R_EDI,R_ESI,R_ESP :
  3062. begin
  3063. ungetregister32(hr);
  3064. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_W,reg32toreg16(hr))));
  3065. end;
  3066. R_AX,R_BX,R_CX,R_DX,R_DI,R_SI,R_SP :
  3067. begin
  3068. ungetregister32(reg16toreg32(hr));
  3069. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_W,hr)));
  3070. end;
  3071. R_AL,R_BL,R_CL,R_DL :
  3072. begin
  3073. ungetregister32(reg8toreg32(hr));
  3074. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_W,reg8toreg16(hr))));
  3075. end;
  3076. end;
  3077. end;
  3078. else
  3079. begin
  3080. exprasmlist^.concat(new(pai386,op_ref(A_PUSH,S_W,newreference(p^.location.reference))));
  3081. del_reference(p^.location.reference);
  3082. end;
  3083. end;
  3084. end;
  3085. procedure secondsetcons(var p : ptree);
  3086. var
  3087. l : plabel;
  3088. i : longint;
  3089. hp : ptree;
  3090. href,sref : treference;
  3091. {$ifdef TestSmallSet}
  3092. smallsetvalue : longint;
  3093. hr,hr2 : tregister;
  3094. {$endif TestSmallSet}
  3095. begin
  3096. { this should be reimplemented for smallsets }
  3097. { differently (PM) }
  3098. { produce constant part }
  3099. {$ifdef TestSmallSet}
  3100. if psetdef(p^.resulttype)^.settype=smallset then
  3101. begin
  3102. smallsetvalue:=(p^.constset^[3]*256)+p^.constset^[2];
  3103. smallsetvalue:=(smallsetvalue*256+p^.constset^[1])*256+p^.constset^[0];
  3104. {consts^.concat(new(pai_const,init_32bit(smallsetvalue)));}
  3105. hr:=getregister32;
  3106. exprasmlist^.concat(new(pai386,op_const_reg(A_MOV,S_L,
  3107. smallsetvalue,hr)));
  3108. hp:=p^.left;
  3109. if assigned(hp) then
  3110. begin
  3111. while assigned(hp) do
  3112. begin
  3113. secondpass(hp^.left);
  3114. if codegenerror then
  3115. exit;
  3116. case hp^.left^.location.loc of
  3117. LOC_MEM,LOC_REFERENCE :
  3118. begin
  3119. hr2:=getregister32;
  3120. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  3121. newreference(hp^.left^.location.reference),hr2)));
  3122. exprasmlist^.concat(new(pai386,op_reg_reg(A_BTS,S_NO,
  3123. hr2,hr)));
  3124. ungetregister32(hr2);
  3125. end;
  3126. LOC_REGISTER,LOC_CREGISTER :
  3127. exprasmlist^.concat(new(pai386,op_reg_reg(A_BTS,S_NO,
  3128. hp^.left^.location.register,hr)));
  3129. else
  3130. internalerror(10567);
  3131. end;
  3132. hp:=hp^.right;
  3133. end;
  3134. end;
  3135. p^.location.loc:=LOC_REGISTER;
  3136. p^.location.register:=hr;
  3137. end
  3138. else
  3139. {$endif TestSmallSet}
  3140. begin
  3141. href.symbol := Nil;
  3142. clear_reference(href);
  3143. getlabel(l);
  3144. stringdispose(p^.location.reference.symbol);
  3145. href.symbol:=stringdup(constlabel2str(l,constseta));
  3146. concat_constlabel(l,constseta);
  3147. for i:=0 to 31 do
  3148. consts^.concat(new(pai_const,init_8bit(p^.constset^[i])));
  3149. hp:=p^.left;
  3150. if assigned(hp) then
  3151. begin
  3152. sref.symbol:=nil;
  3153. gettempofsizereference(32,sref);
  3154. concatcopy(href,sref,32,false);
  3155. while assigned(hp) do
  3156. begin
  3157. secondpass(hp^.left);
  3158. if codegenerror then
  3159. exit;
  3160. pushsetelement(hp^.left);
  3161. emitpushreferenceaddr(sref);
  3162. { register is save in subroutine }
  3163. emitcall('SET_SET_BYTE',true);
  3164. hp:=hp^.right;
  3165. end;
  3166. p^.location.reference:=sref;
  3167. end
  3168. else p^.location.reference:=href;
  3169. end;
  3170. end;
  3171. { could be built into secondadd but it }
  3172. { should be easy to read }
  3173. procedure secondin(var p : ptree);
  3174. type Tsetpart=record
  3175. range:boolean; {Part is a range.}
  3176. start,stop:byte; {Start/stop when range; Stop=element
  3177. when an element.}
  3178. end;
  3179. var
  3180. pushed,ranges : boolean;
  3181. hr,pleftreg : tregister;
  3182. opsize : topsize;
  3183. setparts:array[1..8] of Tsetpart;
  3184. i,numparts:byte;
  3185. href,href2:Treference;
  3186. l,l2 : plabel;
  3187. function analizeset(Aset:Pconstset):boolean;
  3188. var compares,maxcompares:word;
  3189. i:byte;
  3190. type byteset=set of byte;
  3191. begin
  3192. analizeset:=false;
  3193. ranges:=false;
  3194. numparts:=0;
  3195. compares:=0;
  3196. {Lots of comparisions take a lot of time, so do not allow
  3197. too much comparisions. 8 comparisions are, however, still
  3198. smalller than emitting the set.}
  3199. maxcompares:=5;
  3200. if cs_littlesize in aktswitches then
  3201. maxcompares:=8;
  3202. for i:=0 to 255 do
  3203. if i in byteset(Aset^) then
  3204. begin
  3205. if (numparts=0) or
  3206. (i<>setparts[numparts].stop+1) then
  3207. begin
  3208. {Set element is a separate element.}
  3209. inc(compares);
  3210. if compares>maxcompares then
  3211. exit;
  3212. inc(numparts);
  3213. setparts[numparts].range:=false;
  3214. setparts[numparts].stop:=i;
  3215. end
  3216. else
  3217. {Set element is part of a range.}
  3218. if not setparts[numparts].range then
  3219. begin
  3220. {Transform an element into a range.}
  3221. setparts[numparts].range:=true;
  3222. setparts[numparts].start:=
  3223. setparts[numparts].stop;
  3224. setparts[numparts].stop:=i;
  3225. inc(compares);
  3226. if compares>maxcompares then
  3227. exit;
  3228. end
  3229. else
  3230. begin
  3231. {Extend a range.}
  3232. setparts[numparts].stop:=i;
  3233. {A range of two elements can better
  3234. be checked as two separate ones.
  3235. When extending a range, our range
  3236. becomes larger than two elements.}
  3237. ranges:=true;
  3238. end;
  3239. end;
  3240. analizeset:=true;
  3241. end;
  3242. begin
  3243. if psetdef(p^.right^.resulttype)^.settype=smallset then
  3244. begin
  3245. if p^.left^.treetype=ordconstn then
  3246. begin
  3247. { only compulsory }
  3248. secondpass(p^.left);
  3249. secondpass(p^.right);
  3250. if codegenerror then
  3251. exit;
  3252. p^.location.resflags:=F_NE;
  3253. case p^.right^.location.loc of
  3254. LOC_REGISTER,LOC_CREGISTER:
  3255. begin
  3256. exprasmlist^.concat(new(pai386,op_const_reg(
  3257. A_TEST,S_L,1 shl (p^.left^.value and 31),
  3258. p^.right^.location.register)));
  3259. ungetregister32(p^.right^.location.register);
  3260. end
  3261. else
  3262. begin
  3263. exprasmlist^.concat(new(pai386,op_const_ref(A_TEST,S_L,1 shl (p^.left^.value and 31),
  3264. newreference(p^.right^.location.reference))));
  3265. del_reference(p^.right^.location.reference);
  3266. end;
  3267. end;
  3268. end
  3269. else
  3270. begin
  3271. { calculate both operators }
  3272. { the complex one first }
  3273. firstcomplex(p);
  3274. secondpass(p^.left);
  3275. { are too few registers free? }
  3276. pushed:=maybe_push(p^.right^.registers32,p^.left);
  3277. secondpass(p^.right);
  3278. if pushed then
  3279. restore(p^.left);
  3280. { of course not commutative }
  3281. if p^.swaped then
  3282. swaptree(p);
  3283. case p^.left^.location.loc of
  3284. LOC_REGISTER,
  3285. LOC_CREGISTER:
  3286. begin
  3287. hr:=p^.left^.location.register;
  3288. case p^.left^.location.register of
  3289. R_AX,R_BX,R_CX,R_DX,R_DI,R_SI,R_SP :
  3290. begin
  3291. hr:=reg16toreg32(p^.left^.location.register);
  3292. ungetregister32(hr);
  3293. exprasmlist^.concat(new(pai386,op_reg_reg(A_MOVZX,S_WL,
  3294. p^.left^.location.register,hr)));
  3295. end;
  3296. R_AL,R_BL,R_CL,R_DL :
  3297. begin
  3298. hr:=reg8toreg32(p^.left^.location.register);
  3299. ungetregister32(hr);
  3300. exprasmlist^.concat(new(pai386,op_reg_reg(A_MOVZX,S_BL,
  3301. p^.left^.location.register,hr)));
  3302. end;
  3303. end;
  3304. end;
  3305. else
  3306. begin
  3307. { the set element isn't never samller than a byte }
  3308. { and because it's a small set we need only 5 bits }
  3309. { but 8 bits are eaiser to load }
  3310. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOVZX,S_BL,
  3311. newreference(p^.left^.location.reference),R_EDI)));
  3312. hr:=R_EDI;
  3313. del_reference(p^.left^.location.reference);
  3314. end;
  3315. end;
  3316. case p^.right^.location.loc of
  3317. LOC_REGISTER,
  3318. LOC_CREGISTER:
  3319. exprasmlist^.concat(new(pai386,op_reg_reg(A_BT,S_L,hr,
  3320. p^.right^.location.register)));
  3321. else
  3322. begin
  3323. exprasmlist^.concat(new(pai386,op_reg_ref(A_BT,S_L,hr,
  3324. newreference(p^.right^.location.reference))));
  3325. del_reference(p^.right^.location.reference);
  3326. end;
  3327. end;
  3328. p^.location.loc:=LOC_FLAGS;
  3329. p^.location.resflags:=F_C;
  3330. end;
  3331. end
  3332. else
  3333. begin
  3334. if p^.left^.treetype=ordconstn then
  3335. begin
  3336. { only compulsory }
  3337. secondpass(p^.left);
  3338. secondpass(p^.right);
  3339. if codegenerror then
  3340. exit;
  3341. p^.location.resflags:=F_NE;
  3342. inc(p^.right^.location.reference.offset,p^.left^.value shr 3);
  3343. exprasmlist^.concat(new(pai386,op_const_ref(A_TEST,S_B,1 shl (p^.left^.value and 7),
  3344. newreference(p^.right^.location.reference))));
  3345. del_reference(p^.right^.location.reference);
  3346. end
  3347. else
  3348. begin
  3349. if (p^.right^.treetype=setconstrn) and
  3350. analizeset(p^.right^.constset) then
  3351. begin
  3352. {It gives us advantage to check for the set elements
  3353. separately instead of using the SET_IN_BYTE procedure.
  3354. To do: Build in support for LOC_JUMP.}
  3355. secondpass(p^.left);
  3356. {We won't do a second pass on p^.right, because
  3357. this will emit the constant set.}
  3358. {If register is used, use only lower 8 bits}
  3359. if p^.left^.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  3360. begin
  3361. pleftreg:=p^.left^.location.register;
  3362. if pleftreg in [R_AL..R_DH] then
  3363. begin
  3364. exprasmlist^.concat(new(pai386,op_const_reg(
  3365. A_AND,S_B,255,pleftreg)));
  3366. opsize:=S_B;
  3367. end
  3368. else
  3369. begin
  3370. exprasmlist^.concat(new(pai386,op_const_reg(
  3371. A_AND,S_L,255,pleftreg)));
  3372. if pleftreg in [R_EAX..R_EDI] then
  3373. opsize:=S_L
  3374. else
  3375. opsize:=S_W;
  3376. end;
  3377. end;
  3378. {Get a label to jump to the end.}
  3379. p^.location.loc:=LOC_FLAGS;
  3380. {It's better to use the zero flag when there are
  3381. no ranges.}
  3382. if ranges then
  3383. p^.location.resflags:=F_C
  3384. else
  3385. p^.location.resflags:=F_E;
  3386. href.symbol := nil;
  3387. clear_reference(href);
  3388. getlabel(l);
  3389. href.symbol:=stringdup(lab2str(l));
  3390. for i:=1 to numparts do
  3391. if setparts[i].range then
  3392. begin
  3393. {Check if left is in a range.}
  3394. {Get a label to jump over the check.}
  3395. href2.symbol := nil;
  3396. clear_reference(href2);
  3397. getlabel(l2);
  3398. href.symbol:=stringdup(lab2str(l2));
  3399. if setparts[i].start=setparts[i].stop-1 then
  3400. begin
  3401. case p^.left^.location.loc of
  3402. LOC_REGISTER,
  3403. LOC_CREGISTER :
  3404. exprasmlist^.concat(new(pai386,op_const_reg(A_CMP,opsize,
  3405. setparts[i].start,pleftreg)));
  3406. else
  3407. exprasmlist^.concat(new(pai386,op_const_ref(A_CMP,S_B,
  3408. setparts[i].start,newreference(p^.left^.location.reference))));
  3409. end;
  3410. {Result should be in carry flag when ranges are used.}
  3411. if ranges then
  3412. exprasmlist^.concat(new(pai386,op_none(A_STC,S_NO)));
  3413. {If found, jump to end.}
  3414. emitl(A_JE,l);
  3415. case p^.left^.location.loc of
  3416. LOC_REGISTER,
  3417. LOC_CREGISTER:
  3418. exprasmlist^.concat(new(pai386,op_const_reg(A_CMP,opsize,
  3419. setparts[i].stop,pleftreg)));
  3420. else
  3421. exprasmlist^.concat(new(pai386,op_const_ref(A_CMP,S_B,
  3422. setparts[i].stop,newreference(p^.left^.location.reference))));
  3423. end;
  3424. {Result should be in carry flag when ranges are used.}
  3425. if ranges then
  3426. exprasmlist^.concat(new(pai386,op_none(A_STC,S_NO)));
  3427. {If found, jump to end.}
  3428. emitl(A_JE,l);
  3429. end
  3430. else
  3431. begin
  3432. if setparts[i].start<>0 then
  3433. begin
  3434. { We only check for the lower bound if it is > 0, because
  3435. set elements lower than 0 do nt exist.}
  3436. case p^.left^.location.loc of
  3437. LOC_REGISTER,
  3438. LOC_CREGISTER :
  3439. exprasmlist^.concat(new(pai386,op_const_reg(A_CMP,opsize,
  3440. setparts[i].start,pleftreg)));
  3441. else
  3442. exprasmlist^.concat(new(pai386,op_const_ref(A_CMP,S_B,
  3443. setparts[i].start,newreference(p^.left^.location.reference))));
  3444. end;
  3445. {If lower, jump to next check.}
  3446. emitl(A_JB,l2);
  3447. end;
  3448. { We only check for the high bound if it is < 255, because
  3449. set elements higher than 255 do nt exist, the its always true,
  3450. so only a JMP is generated }
  3451. if setparts[i].stop<>255 then
  3452. begin
  3453. case p^.left^.location.loc of
  3454. LOC_REGISTER,
  3455. LOC_CREGISTER :
  3456. exprasmlist^.concat(new(pai386,op_const_reg(A_CMP,opsize,
  3457. setparts[i].stop+1,pleftreg)));
  3458. else
  3459. exprasmlist^.concat(new(pai386,op_const_ref(A_CMP,S_B,
  3460. setparts[i].stop+1,newreference(p^.left^.location.reference))));
  3461. end;
  3462. {If higher, element is in set.}
  3463. emitl(A_JB,l);
  3464. end
  3465. else
  3466. begin
  3467. exprasmlist^.concat(new(pai386,op_none(A_STC,S_NO)));
  3468. emitl(A_JMP,l);
  3469. end;
  3470. end;
  3471. {Emit the jump over label.}
  3472. exprasmlist^.concat(new(pai_label,init(l2)));
  3473. end
  3474. else
  3475. begin
  3476. {Emit code to check if left is an element.}
  3477. case p^.left^.location.loc of
  3478. LOC_REGISTER,
  3479. LOC_CREGISTER:
  3480. exprasmlist^.concat(new(pai386,op_const_reg(A_CMP,opsize,
  3481. setparts[i].stop,pleftreg)));
  3482. else
  3483. exprasmlist^.concat(new(pai386,op_const_ref(A_CMP,S_B,
  3484. setparts[i].stop,newreference(p^.left^.location.reference))));
  3485. end;
  3486. {Result should be in carry flag when ranges are used.}
  3487. if ranges then
  3488. exprasmlist^.concat(new(pai386,op_none(A_STC,S_NO)));
  3489. {If found, jump to end.}
  3490. emitl(A_JE,l);
  3491. end;
  3492. if ranges then
  3493. exprasmlist^.concat(new(pai386,op_none(A_CLC,S_NO)));
  3494. {To compensate for not doing a second pass.}
  3495. stringdispose(p^.right^.location.reference.symbol);
  3496. {Now place the end label.}
  3497. exprasmlist^.concat(new(pai_label,init(l)));
  3498. case p^.left^.location.loc of
  3499. LOC_REGISTER,
  3500. LOC_CREGISTER:
  3501. ungetregister32(pleftreg);
  3502. else
  3503. del_reference(p^.left^.location.reference);
  3504. end;
  3505. end
  3506. else
  3507. begin
  3508. { calculate both operators }
  3509. { the complex one first }
  3510. firstcomplex(p);
  3511. secondpass(p^.left);
  3512. { are too few registers free? }
  3513. pushed:=maybe_push(p^.right^.registers32,p);
  3514. secondpass(p^.right);
  3515. if pushed then restore(p);
  3516. { of course not commutative }
  3517. if p^.swaped then
  3518. swaptree(p);
  3519. pushsetelement(p^.left);
  3520. emitpushreferenceaddr(p^.right^.location.reference);
  3521. del_reference(p^.right^.location.reference);
  3522. { registers need not be save. that happens in SET_IN_BYTE }
  3523. { (EDI is changed) }
  3524. emitcall('SET_IN_BYTE',true);
  3525. { ungetiftemp(p^.right^.location.reference); }
  3526. p^.location.loc:=LOC_FLAGS;
  3527. p^.location.resflags:=F_C;
  3528. end;
  3529. end;
  3530. end;
  3531. end;
  3532. {***}
  3533. procedure secondstatement(var p : ptree);
  3534. var
  3535. hp : ptree;
  3536. begin
  3537. hp:=p;
  3538. while assigned(hp) do
  3539. begin
  3540. { assignments could be distance optimized }
  3541. if assigned(hp^.right) then
  3542. begin
  3543. cleartempgen;
  3544. secondpass(hp^.right);
  3545. end;
  3546. hp:=hp^.left;
  3547. end;
  3548. end;
  3549. procedure secondblockn(var p : ptree);
  3550. begin
  3551. { do second pass on left node }
  3552. if assigned(p^.left) then
  3553. secondpass(p^.left);
  3554. end;
  3555. procedure second_while_repeatn(var p : ptree);
  3556. var
  3557. l1,l2,l3,oldclabel,oldblabel : plabel;
  3558. otlabel,oflabel : plabel;
  3559. begin
  3560. getlabel(l1);
  3561. getlabel(l2);
  3562. { arrange continue and breaklabels: }
  3563. oldclabel:=aktcontinuelabel;
  3564. oldblabel:=aktbreaklabel;
  3565. if p^.treetype=repeatn then
  3566. begin
  3567. emitl(A_LABEL,l1);
  3568. aktcontinuelabel:=l1;
  3569. aktbreaklabel:=l2;
  3570. cleartempgen;
  3571. if assigned(p^.right) then
  3572. secondpass(p^.right);
  3573. otlabel:=truelabel;
  3574. oflabel:=falselabel;
  3575. truelabel:=l2;
  3576. falselabel:=l1;
  3577. cleartempgen;
  3578. secondpass(p^.left);
  3579. maketojumpbool(p^.left);
  3580. emitl(A_LABEL,l2);
  3581. truelabel:=otlabel;
  3582. falselabel:=oflabel;
  3583. end
  3584. else
  3585. begin
  3586. { handling code at the end as it is much more efficient }
  3587. emitl(A_JMP,l2);
  3588. emitl(A_LABEL,l1);
  3589. cleartempgen;
  3590. getlabel(l3);
  3591. aktcontinuelabel:=l2;
  3592. aktbreaklabel:=l3;
  3593. if assigned(p^.right) then
  3594. secondpass(p^.right);
  3595. emitl(A_LABEL,l2);
  3596. otlabel:=truelabel;
  3597. oflabel:=falselabel;
  3598. truelabel:=l1;
  3599. falselabel:=l3;
  3600. cleartempgen;
  3601. secondpass(p^.left);
  3602. maketojumpbool(p^.left);
  3603. emitl(A_LABEL,l3);
  3604. truelabel:=otlabel;
  3605. falselabel:=oflabel;
  3606. end;
  3607. aktcontinuelabel:=oldclabel;
  3608. aktbreaklabel:=oldblabel;
  3609. end;
  3610. procedure secondifn(var p : ptree);
  3611. var
  3612. hl,otlabel,oflabel : plabel;
  3613. begin
  3614. otlabel:=truelabel;
  3615. oflabel:=falselabel;
  3616. getlabel(truelabel);
  3617. getlabel(falselabel);
  3618. cleartempgen;
  3619. secondpass(p^.left);
  3620. maketojumpbool(p^.left);
  3621. if assigned(p^.right) then
  3622. begin
  3623. emitl(A_LABEL,truelabel);
  3624. cleartempgen;
  3625. secondpass(p^.right);
  3626. end;
  3627. if assigned(p^.t1) then
  3628. begin
  3629. if assigned(p^.right) then
  3630. begin
  3631. getlabel(hl);
  3632. emitl(A_JMP,hl);
  3633. end;
  3634. emitl(A_LABEL,falselabel);
  3635. cleartempgen;
  3636. secondpass(p^.t1);
  3637. if assigned(p^.right) then
  3638. emitl(A_LABEL,hl);
  3639. end
  3640. else
  3641. emitl(A_LABEL,falselabel);
  3642. if not(assigned(p^.right)) then
  3643. emitl(A_LABEL,truelabel);
  3644. truelabel:=otlabel;
  3645. falselabel:=oflabel;
  3646. end;
  3647. procedure secondbreakn(var p : ptree);
  3648. begin
  3649. if aktbreaklabel<>nil then
  3650. emitl(A_JMP,aktbreaklabel)
  3651. else
  3652. Message(cg_e_break_not_allowed);
  3653. end;
  3654. procedure secondcontinuen(var p : ptree);
  3655. begin
  3656. if aktcontinuelabel<>nil then
  3657. emitl(A_JMP,aktcontinuelabel)
  3658. else
  3659. Message(cg_e_continue_not_allowed);
  3660. end;
  3661. procedure secondfor(var p : ptree);
  3662. var
  3663. l3,oldclabel,oldblabel : plabel;
  3664. omitfirstcomp,temptovalue : boolean;
  3665. hs : byte;
  3666. temp1 : treference;
  3667. hop : tasmop;
  3668. cmpreg,cmp32 : tregister;
  3669. opsize : topsize;
  3670. count_var_is_signed : boolean;
  3671. begin
  3672. oldclabel:=aktcontinuelabel;
  3673. oldblabel:=aktbreaklabel;
  3674. getlabel(aktcontinuelabel);
  3675. getlabel(aktbreaklabel);
  3676. getlabel(l3);
  3677. { could we spare the first comparison ? }
  3678. omitfirstcomp:=false;
  3679. if p^.right^.treetype=ordconstn then
  3680. if p^.left^.right^.treetype=ordconstn then
  3681. omitfirstcomp:=(p^.backward and (p^.left^.right^.value>=p^.right^.value))
  3682. or (not(p^.backward) and (p^.left^.right^.value<=p^.right^.value));
  3683. { only calculate reference }
  3684. cleartempgen;
  3685. secondpass(p^.t2);
  3686. if not(simple_loadn) then
  3687. Message(cg_e_illegal_count_var);
  3688. { produce start assignment }
  3689. cleartempgen;
  3690. secondpass(p^.left);
  3691. count_var_is_signed:=is_signed(porddef(p^.t2^.resulttype));
  3692. hs:=p^.t2^.resulttype^.size;
  3693. cmp32:=getregister32;
  3694. case hs of
  3695. 1 : begin
  3696. opsize:=S_B;
  3697. cmpreg:=reg32toreg8(cmp32);
  3698. end;
  3699. 2 : begin
  3700. opsize:=S_W;
  3701. cmpreg:=reg32toreg16(cmp32);
  3702. end;
  3703. 4 : begin
  3704. opsize:=S_L;
  3705. cmpreg:=cmp32;
  3706. end;
  3707. end;
  3708. cleartempgen;
  3709. secondpass(p^.right);
  3710. { calculate pointer value and check if changeable and if so }
  3711. { load into temporary variable }
  3712. if p^.right^.treetype<>ordconstn then
  3713. begin
  3714. temp1.symbol:=nil;
  3715. gettempofsizereference(hs,temp1);
  3716. temptovalue:=true;
  3717. if (p^.right^.location.loc=LOC_REGISTER) or
  3718. (p^.right^.location.loc=LOC_CREGISTER) then
  3719. begin
  3720. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,opsize,p^.right^.location.register,
  3721. newreference(temp1))));
  3722. end
  3723. else
  3724. concatcopy(p^.right^.location.reference,temp1,hs,false);
  3725. end
  3726. else temptovalue:=false;
  3727. if temptovalue then
  3728. begin
  3729. if p^.t2^.location.loc=LOC_CREGISTER then
  3730. begin
  3731. exprasmlist^.concat(new(pai386,op_ref_reg(A_CMP,opsize,newreference(temp1),
  3732. p^.t2^.location.register)));
  3733. end
  3734. else
  3735. begin
  3736. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,opsize,newreference(p^.t2^.location.reference),
  3737. cmpreg)));
  3738. exprasmlist^.concat(new(pai386,op_ref_reg(A_CMP,opsize,newreference(temp1),
  3739. cmpreg)));
  3740. end;
  3741. end
  3742. else
  3743. begin
  3744. if not(omitfirstcomp) then
  3745. begin
  3746. if p^.t2^.location.loc=LOC_CREGISTER then
  3747. exprasmlist^.concat(new(pai386,op_const_reg(A_CMP,opsize,p^.right^.value,
  3748. p^.t2^.location.register)))
  3749. else
  3750. exprasmlist^.concat(new(pai386,op_const_ref(A_CMP,opsize,p^.right^.value,
  3751. newreference(p^.t2^.location.reference))));
  3752. end;
  3753. end;
  3754. if p^.backward then
  3755. if count_var_is_signed then
  3756. hop:=A_JL
  3757. else hop:=A_JB
  3758. else
  3759. if count_var_is_signed then
  3760. hop:=A_JG
  3761. else hop:=A_JA;
  3762. if not(omitfirstcomp) or temptovalue then
  3763. emitl(hop,aktbreaklabel);
  3764. emitl(A_LABEL,l3);
  3765. { help register must not be in instruction block }
  3766. cleartempgen;
  3767. if assigned(p^.t1) then
  3768. secondpass(p^.t1);
  3769. emitl(A_LABEL,aktcontinuelabel);
  3770. { makes no problems there }
  3771. cleartempgen;
  3772. { demand help register again }
  3773. cmp32:=getregister32;
  3774. case hs of
  3775. 1 : begin
  3776. opsize:=S_B;
  3777. cmpreg:=reg32toreg8(cmp32);
  3778. end;
  3779. 2 : begin
  3780. opsize:=S_W;
  3781. cmpreg:=reg32toreg16(cmp32);
  3782. end;
  3783. 4 : opsize:=S_L;
  3784. end;
  3785. { produce comparison and the corresponding }
  3786. { jump }
  3787. if temptovalue then
  3788. begin
  3789. if p^.t2^.location.loc=LOC_CREGISTER then
  3790. begin
  3791. exprasmlist^.concat(new(pai386,op_ref_reg(A_CMP,opsize,newreference(temp1),
  3792. p^.t2^.location.register)));
  3793. end
  3794. else
  3795. begin
  3796. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,opsize,newreference(p^.t2^.location.reference),
  3797. cmpreg)));
  3798. exprasmlist^.concat(new(pai386,op_ref_reg(A_CMP,opsize,newreference(temp1),
  3799. cmpreg)));
  3800. end;
  3801. end
  3802. else
  3803. begin
  3804. if p^.t2^.location.loc=LOC_CREGISTER then
  3805. exprasmlist^.concat(new(pai386,op_const_reg(A_CMP,opsize,p^.right^.value,
  3806. p^.t2^.location.register)))
  3807. else
  3808. exprasmlist^.concat(new(pai386,op_const_ref(A_CMP,opsize,p^.right^.value,
  3809. newreference(p^.t2^.location.reference))));
  3810. end;
  3811. if p^.backward then
  3812. if count_var_is_signed then
  3813. hop:=A_JLE
  3814. else
  3815. hop :=A_JBE
  3816. else
  3817. if count_var_is_signed then
  3818. hop:=A_JGE
  3819. else
  3820. hop:=A_JAE;
  3821. emitl(hop,aktbreaklabel);
  3822. { according to count direction DEC or INC... }
  3823. { must be after the test because of 0to 255 for bytes !! }
  3824. if p^.backward then
  3825. hop:=A_DEC
  3826. else hop:=A_INC;
  3827. if p^.t2^.location.loc=LOC_CREGISTER then
  3828. exprasmlist^.concat(new(pai386,op_reg(hop,opsize,p^.t2^.location.register)))
  3829. else
  3830. exprasmlist^.concat(new(pai386,op_ref(hop,opsize,newreference(p^.t2^.location.reference))));
  3831. emitl(A_JMP,l3);
  3832. { this is the break label: }
  3833. emitl(A_LABEL,aktbreaklabel);
  3834. ungetregister32(cmp32);
  3835. if temptovalue then
  3836. ungetiftemp(temp1);
  3837. aktcontinuelabel:=oldclabel;
  3838. aktbreaklabel:=oldblabel;
  3839. end;
  3840. { var
  3841. hs : string; }
  3842. procedure secondexitn(var p : ptree);
  3843. var
  3844. is_mem : boolean;
  3845. {op : tasmop;
  3846. s : topsize;}
  3847. otlabel,oflabel : plabel;
  3848. label
  3849. do_jmp;
  3850. begin
  3851. if assigned(p^.left) then
  3852. begin
  3853. otlabel:=truelabel;
  3854. oflabel:=falselabel;
  3855. getlabel(truelabel);
  3856. getlabel(falselabel);
  3857. secondpass(p^.left);
  3858. case p^.left^.location.loc of
  3859. LOC_FPU : goto do_jmp;
  3860. LOC_MEM,LOC_REFERENCE : is_mem:=true;
  3861. LOC_CREGISTER,
  3862. LOC_REGISTER : is_mem:=false;
  3863. LOC_FLAGS : begin
  3864. exprasmlist^.concat(new(pai386,op_reg(flag_2_set[p^.right^.location.resflags],S_B,R_AL)));
  3865. goto do_jmp;
  3866. end;
  3867. LOC_JUMP : begin
  3868. emitl(A_LABEL,truelabel);
  3869. exprasmlist^.concat(new(pai386,op_const_reg(A_MOV,S_B,1,R_AL)));
  3870. emitl(A_JMP,aktexit2label);
  3871. exprasmlist^.concat(new(pai386,op_reg_reg(A_XOR,S_B,R_AL,R_AL)));
  3872. goto do_jmp;
  3873. end;
  3874. else internalerror(2001);
  3875. end;
  3876. if (procinfo.retdef^.deftype=orddef) then
  3877. begin
  3878. case porddef(procinfo.retdef)^.typ of
  3879. s32bit,u32bit,bool32bit : if is_mem then
  3880. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  3881. newreference(p^.left^.location.reference),R_EAX)))
  3882. else
  3883. emit_reg_reg(A_MOV,S_L,p^.left^.location.register,R_EAX);
  3884. u8bit,s8bit,uchar,bool8bit : if is_mem then
  3885. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_B,
  3886. newreference(p^.left^.location.reference),R_AL)))
  3887. else
  3888. emit_reg_reg(A_MOV,S_B,p^.left^.location.register,R_AL);
  3889. s16bit,u16bit,bool16bit : if is_mem then
  3890. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_W,
  3891. newreference(p^.left^.location.reference),R_AX)))
  3892. else
  3893. emit_reg_reg(A_MOV,S_W,p^.left^.location.register,R_AX);
  3894. end;
  3895. end
  3896. else
  3897. if (procinfo.retdef^.deftype in [pointerdef,enumdef,procvardef]) then
  3898. begin
  3899. if is_mem then
  3900. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  3901. newreference(p^.left^.location.reference),R_EAX)))
  3902. else
  3903. exprasmlist^.concat(new(pai386,op_reg_reg(A_MOV,S_L,
  3904. p^.left^.location.register,R_EAX)));
  3905. end
  3906. else
  3907. if (procinfo.retdef^.deftype=floatdef) then
  3908. begin
  3909. if pfloatdef(procinfo.retdef)^.typ=f32bit then
  3910. begin
  3911. if is_mem then
  3912. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  3913. newreference(p^.left^.location.reference),R_EAX)))
  3914. else
  3915. emit_reg_reg(A_MOV,S_L,p^.left^.location.register,R_EAX);
  3916. end
  3917. else
  3918. if is_mem then
  3919. floatload(pfloatdef(procinfo.retdef)^.typ,p^.left^.location.reference);
  3920. end;
  3921. do_jmp:
  3922. truelabel:=otlabel;
  3923. falselabel:=oflabel;
  3924. emitl(A_JMP,aktexit2label);
  3925. end
  3926. else
  3927. begin
  3928. emitl(A_JMP,aktexitlabel);
  3929. end;
  3930. end;
  3931. procedure secondgoto(var p : ptree);
  3932. begin
  3933. emitl(A_JMP,p^.labelnr);
  3934. end;
  3935. procedure secondlabel(var p : ptree);
  3936. begin
  3937. emitl(A_LABEL,p^.labelnr);
  3938. cleartempgen;
  3939. secondpass(p^.left);
  3940. end;
  3941. procedure secondasm(var p : ptree);
  3942. begin
  3943. exprasmlist^.concatlist(p^.p_asm);
  3944. if not p^.object_preserved then
  3945. maybe_loadesi;
  3946. end;
  3947. procedure secondcase(var p : ptree);
  3948. var
  3949. with_sign : boolean;
  3950. opsize : topsize;
  3951. jmp_gt,jmp_le,jmp_lee : tasmop;
  3952. hp : ptree;
  3953. { register with case expression }
  3954. hregister : tregister;
  3955. endlabel,elselabel : plabel;
  3956. { true, if we can omit the range check of the jump table }
  3957. jumptable_no_range : boolean;
  3958. { where to put the jump table }
  3959. jumpsegment : paasmoutput;
  3960. procedure gentreejmp(p : pcaserecord);
  3961. var
  3962. lesslabel,greaterlabel : plabel;
  3963. begin
  3964. emitl(A_LABEL,p^._at);
  3965. { calculate labels for left and right }
  3966. if (p^.less=nil) then
  3967. lesslabel:=elselabel
  3968. else
  3969. lesslabel:=p^.less^._at;
  3970. if (p^.greater=nil) then
  3971. greaterlabel:=elselabel
  3972. else
  3973. greaterlabel:=p^.greater^._at;
  3974. { calculate labels for left and right }
  3975. { no range label: }
  3976. if p^._low=p^._high then
  3977. begin
  3978. exprasmlist^.concat(new(pai386,op_const_reg(A_CMP,opsize,p^._low,hregister)));
  3979. if greaterlabel=lesslabel then
  3980. begin
  3981. emitl(A_JNE,lesslabel);
  3982. end
  3983. else
  3984. begin
  3985. emitl(jmp_le,lesslabel);
  3986. emitl(jmp_gt,greaterlabel);
  3987. end;
  3988. emitl(A_JMP,p^.statement);
  3989. end
  3990. else
  3991. begin
  3992. exprasmlist^.concat(new(pai386,op_const_reg(A_CMP,opsize,p^._low,hregister)));
  3993. emitl(jmp_le,lesslabel);
  3994. exprasmlist^.concat(new(pai386,op_const_reg(A_CMP,opsize,p^._high,hregister)));
  3995. emitl(jmp_gt,greaterlabel);
  3996. emitl(A_JMP,p^.statement);
  3997. end;
  3998. if assigned(p^.less) then
  3999. gentreejmp(p^.less);
  4000. if assigned(p^.greater) then
  4001. gentreejmp(p^.greater);
  4002. end;
  4003. procedure genlinearlist(hp : pcaserecord);
  4004. var
  4005. first : boolean;
  4006. last : longint;
  4007. {helplabel : longint;}
  4008. procedure genitem(t : pcaserecord);
  4009. begin
  4010. if assigned(t^.less) then
  4011. genitem(t^.less);
  4012. if t^._low=t^._high then
  4013. begin
  4014. if t^._low-last=1 then
  4015. exprasmlist^.concat(new(pai386,op_reg(A_DEC,opsize,hregister)))
  4016. else if t^._low-last=0 then
  4017. exprasmlist^.concat(new(pai386,op_reg_reg(A_OR,opsize,hregister,hregister)))
  4018. else
  4019. exprasmlist^.concat(new(pai386,op_const_reg(A_SUB,opsize,t^._low-last,hregister)));
  4020. last:=t^._low;
  4021. emitl(A_JZ,t^.statement);
  4022. end
  4023. else
  4024. begin
  4025. { it begins with the smallest label, if the value }
  4026. { is even smaller then jump immediately to the }
  4027. { ELSE-label }
  4028. if first then
  4029. begin
  4030. if t^._low-1=1 then
  4031. exprasmlist^.concat(new(pai386,op_reg(A_DEC,opsize,
  4032. hregister)))
  4033. else if t^._low-1=0 then
  4034. exprasmlist^.concat(new(pai386,op_reg_reg(A_OR,opsize,
  4035. hregister,hregister)))
  4036. else
  4037. exprasmlist^.concat(new(pai386,op_const_reg(A_SUB,opsize,
  4038. t^._low-1,hregister)));
  4039. { work around: if the lower range=0 and we
  4040. do the subtraction we have to take care
  4041. of the sign!
  4042. }
  4043. if t^._low=0 then
  4044. emitl(A_JLE,elselabel)
  4045. else
  4046. emitl(jmp_lee,elselabel);
  4047. end
  4048. { if there is no unused label between the last and the }
  4049. { present label then the lower limit can be checked }
  4050. { immediately. else check the range in between: }
  4051. else if (t^._low-last>1)then
  4052. begin
  4053. if t^._low-last-1=1 then
  4054. exprasmlist^.concat(new(pai386,op_reg(A_DEC,opsize,hregister)))
  4055. else
  4056. exprasmlist^.concat(new(pai386,op_const_reg(A_SUB,opsize,t^._low-last-1,hregister)));
  4057. emitl(jmp_lee,elselabel);
  4058. end;
  4059. exprasmlist^.concat(new(pai386,op_const_reg(A_SUB,opsize,t^._high-t^._low+1,hregister)));
  4060. emitl(jmp_lee,t^.statement);
  4061. last:=t^._high;
  4062. end;
  4063. first:=false;
  4064. if assigned(t^.greater) then
  4065. genitem(t^.greater);
  4066. end;
  4067. var
  4068. hr : tregister;
  4069. begin
  4070. { case register is modified by the list evalution }
  4071. if (p^.left^.location.loc=LOC_CREGISTER) then
  4072. begin
  4073. hr:=getregister32;
  4074. case opsize of
  4075. S_B : hregister:=reg32toreg8(hr);
  4076. S_W : hregister:=reg32toreg16(hr);
  4077. S_L : hregister:=hr;
  4078. end;
  4079. end;
  4080. last:=0;
  4081. first:=true;
  4082. genitem(hp);
  4083. emitl(A_JMP,elselabel);
  4084. end;
  4085. procedure genjumptable(hp : pcaserecord;min_,max_ : longint);
  4086. var
  4087. table : plabel;
  4088. last : longint;
  4089. hr : preference;
  4090. procedure genitem(t : pcaserecord);
  4091. var
  4092. i : longint;
  4093. begin
  4094. if assigned(t^.less) then
  4095. genitem(t^.less);
  4096. { fill possible hole }
  4097. for i:=last+1 to t^._low-1 do
  4098. jumpsegment^.concat(new(pai_const,init_symbol(strpnew(lab2str
  4099. (elselabel)))));
  4100. for i:=t^._low to t^._high do
  4101. jumpsegment^.concat(new(pai_const,init_symbol(strpnew(lab2str
  4102. (t^.statement)))));
  4103. last:=t^._high;
  4104. if assigned(t^.greater) then
  4105. genitem(t^.greater);
  4106. end;
  4107. begin
  4108. if not(jumptable_no_range) then
  4109. begin
  4110. exprasmlist^.concat(new(pai386,op_const_reg(A_CMP,opsize,min_,hregister)));
  4111. { case expr less than min_ => goto elselabel }
  4112. emitl(jmp_le,elselabel);
  4113. exprasmlist^.concat(new(pai386,op_const_reg(A_CMP,opsize,max_,hregister)));
  4114. emitl(jmp_gt,elselabel);
  4115. end;
  4116. getlabel(table);
  4117. { extend with sign }
  4118. if opsize=S_W then
  4119. begin
  4120. exprasmlist^.concat(new(pai386,op_reg_reg(A_MOVZX,S_WL,hregister,
  4121. reg16toreg32(hregister))));
  4122. hregister:=reg16toreg32(hregister);
  4123. end
  4124. else if opsize=S_B then
  4125. begin
  4126. exprasmlist^.concat(new(pai386,op_reg_reg(A_MOVZX,S_BL,hregister,
  4127. reg8toreg32(hregister))));
  4128. hregister:=reg8toreg32(hregister);
  4129. end;
  4130. new(hr);
  4131. reset_reference(hr^);
  4132. hr^.symbol:=stringdup(lab2str(table));
  4133. hr^.offset:=(-min_)*4;
  4134. hr^.index:=hregister;
  4135. hr^.scalefactor:=4;
  4136. exprasmlist^.concat(new(pai386,op_ref(A_JMP,S_NO,hr)));
  4137. { !!!!! generate tables
  4138. if not(cs_littlesize in aktswitches^ ) then
  4139. jumpsegment^.concat(new(pai386,op_const(A_ALIGN,S_NO,4)));
  4140. }
  4141. jumpsegment^.concat(new(pai_label,init(table)));
  4142. last:=min_;
  4143. genitem(hp);
  4144. { !!!!!!!
  4145. if not(cs_littlesize in aktswitches^ ) then
  4146. exprasmlist^.concat(new(pai386,op_const(A_ALIGN,S_NO,4)));
  4147. }
  4148. end;
  4149. var
  4150. lv,hv,min_label,max_label,labels : longint;
  4151. max_linear_list : longint;
  4152. begin
  4153. getlabel(endlabel);
  4154. getlabel(elselabel);
  4155. if (cs_smartlink in aktswitches) then
  4156. jumpsegment:=procinfo.aktlocaldata
  4157. else
  4158. jumpsegment:=datasegment;
  4159. with_sign:=is_signed(p^.left^.resulttype);
  4160. if with_sign then
  4161. begin
  4162. jmp_gt:=A_JG;
  4163. jmp_le:=A_JL;
  4164. jmp_lee:=A_JLE;
  4165. end
  4166. else
  4167. begin
  4168. jmp_gt:=A_JA;
  4169. jmp_le:=A_JB;
  4170. jmp_lee:=A_JBE;
  4171. end;
  4172. cleartempgen;
  4173. secondpass(p^.left);
  4174. { determines the size of the operand }
  4175. { determines the size of the operand }
  4176. opsize:=bytes2Sxx[p^.left^.resulttype^.size];
  4177. { copy the case expression to a register }
  4178. { copy the case expression to a register }
  4179. case p^.left^.location.loc of
  4180. LOC_REGISTER,
  4181. LOC_CREGISTER:
  4182. hregister:=p^.left^.location.register;
  4183. LOC_MEM,LOC_REFERENCE : begin
  4184. del_reference(p^.left^.location.reference);
  4185. hregister:=getregister32;
  4186. case opsize of
  4187. S_B : hregister:=reg32toreg8(hregister);
  4188. S_W : hregister:=reg32toreg16(hregister);
  4189. end;
  4190. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,opsize,newreference(
  4191. p^.left^.location.reference),hregister)));
  4192. end;
  4193. else internalerror(2002);
  4194. end;
  4195. { now generate the jumps }
  4196. if cs_optimize in aktswitches then
  4197. begin
  4198. { procedures are empirically passed on }
  4199. { consumption can also be calculated }
  4200. { but does it pay on the different }
  4201. { processors? }
  4202. { moreover can the size only be appro- }
  4203. { ximated as it is not known if rel8, }
  4204. { rel16 or rel32 jumps are used }
  4205. min_label:=case_get_min(p^.nodes);
  4206. max_label:=case_get_max(p^.nodes);
  4207. labels:=case_count_labels(p^.nodes);
  4208. { can we omit the range check of the jump table }
  4209. getrange(p^.left^.resulttype,lv,hv);
  4210. jumptable_no_range:=(lv=min_label) and (hv=max_label);
  4211. { optimize for size ? }
  4212. if cs_littlesize in aktswitches then
  4213. begin
  4214. if (labels<=2) or ((max_label-min_label)>3*labels) then
  4215. { a linear list is always smaller than a jump tree }
  4216. genlinearlist(p^.nodes)
  4217. else
  4218. { if the labels less or more a continuum then }
  4219. genjumptable(p^.nodes,min_label,max_label);
  4220. end
  4221. else
  4222. begin
  4223. if jumptable_no_range then
  4224. max_linear_list:=4
  4225. else
  4226. max_linear_list:=2;
  4227. { a jump table crashes the pipeline! }
  4228. if aktoptprocessor=i486 then
  4229. inc(max_linear_list,3);
  4230. if aktoptprocessor=pentium then
  4231. inc(max_linear_list,6);
  4232. if aktoptprocessor>=pentiumpro then
  4233. inc(max_linear_list,9);
  4234. if (labels<=max_linear_list) then
  4235. genlinearlist(p^.nodes)
  4236. else
  4237. begin
  4238. if ((max_label-min_label)>4*labels) then
  4239. begin
  4240. if labels>16 then
  4241. gentreejmp(p^.nodes)
  4242. else
  4243. genlinearlist(p^.nodes);
  4244. end
  4245. else
  4246. genjumptable(p^.nodes,min_label,max_label);
  4247. end;
  4248. end;
  4249. end
  4250. else
  4251. { it's always not bad }
  4252. genlinearlist(p^.nodes);
  4253. { now generate the instructions }
  4254. hp:=p^.right;
  4255. while assigned(hp) do
  4256. begin
  4257. cleartempgen;
  4258. secondpass(hp^.right);
  4259. emitl(A_JMP,endlabel);
  4260. hp:=hp^.left;
  4261. end;
  4262. emitl(A_LABEL,elselabel);
  4263. { ...and the else block }
  4264. if assigned(p^.elseblock) then
  4265. begin
  4266. cleartempgen;
  4267. secondpass(p^.elseblock);
  4268. end;
  4269. emitl(A_LABEL,endlabel);
  4270. end;
  4271. { generates the code for a raise statement }
  4272. procedure secondraise(var p : ptree);
  4273. var
  4274. a : plabel;
  4275. begin
  4276. if assigned(p^.left) then
  4277. begin
  4278. { generate the address }
  4279. if assigned(p^.right) then
  4280. begin
  4281. secondpass(p^.right);
  4282. if codegenerror then
  4283. exit;
  4284. end
  4285. else
  4286. begin
  4287. getlabel(a);
  4288. emitl(A_LABEL,a);
  4289. exprasmlist^.concat(new(pai386,
  4290. op_csymbol(A_PUSH,S_L,newcsymbol(lab2str(a),0))));
  4291. end;
  4292. secondpass(p^.left);
  4293. if codegenerror then
  4294. exit;
  4295. case p^.left^.location.loc of
  4296. LOC_MEM,LOC_REFERENCE : emitpushreferenceaddr(p^.left^.location.reference);
  4297. LOC_CREGISTER,LOC_REGISTER : exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,
  4298. p^.left^.location.register)));
  4299. else Message(sym_e_type_mismatch);
  4300. end;
  4301. emitcall('DO_RAISE',true);
  4302. end
  4303. else
  4304. emitcall('DO_RERAISE',true);
  4305. end;
  4306. procedure secondtryexcept(var p : ptree);
  4307. begin
  4308. end;
  4309. procedure secondtryfinally(var p : ptree);
  4310. begin
  4311. end;
  4312. procedure secondfail(var p : ptree);
  4313. var hp : preference;
  4314. begin
  4315. {if procinfo.exceptions then
  4316. aktproccode.concat(gennasmrec(CALL,S_NO,'HELP_DESTRUCTOR_E'))
  4317. else }
  4318. { we should know if the constructor is called with a new or not,
  4319. how can we do that ???
  4320. exprasmlist^.concat(new(pai386,op_csymbol(A_CALL,S_NO,newcsymbol('HELP_DESTRUCTOR',0))));
  4321. }
  4322. exprasmlist^.concat(new(pai386,op_reg_reg(A_XOR,S_L,R_ESI,R_ESI)));
  4323. { also reset to zero in the stack }
  4324. new(hp);
  4325. reset_reference(hp^);
  4326. hp^.offset:=procinfo.ESI_offset;
  4327. hp^.base:=procinfo.framepointer;
  4328. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_ESI,hp)));
  4329. exprasmlist^.concat(new(pai_labeled,init(A_JMP,quickexitlabel)));
  4330. end;
  4331. procedure secondwith(var p : ptree);
  4332. var
  4333. ref : treference;
  4334. symtable : psymtable;
  4335. i : longint;
  4336. begin
  4337. if assigned(p^.left) then
  4338. begin
  4339. secondpass(p^.left);
  4340. ref.symbol:=nil;
  4341. gettempofsizereference(4,ref);
  4342. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,
  4343. newreference(p^.left^.location.reference),R_EDI)));
  4344. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
  4345. R_EDI,newreference(ref))));
  4346. del_reference(p^.left^.location.reference);
  4347. { the offset relative to (%ebp) is only needed here! }
  4348. symtable:=p^.withsymtable;
  4349. for i:=1 to p^.tablecount do
  4350. begin
  4351. symtable^.datasize:=ref.offset;
  4352. symtable:=symtable^.next;
  4353. end;
  4354. { p^.right can be optimize out !!! }
  4355. if p^.right<>nil then
  4356. secondpass(p^.right);
  4357. { clear some stuff }
  4358. ungetiftemp(ref);
  4359. end;
  4360. end;
  4361. { implementation not complete yet }
  4362. var addr_correction : longint;
  4363. procedure correct_address(p : psym);
  4364. begin
  4365. if p^.typ=varsym then
  4366. begin
  4367. inc(pvarsym(p)^.address,addr_correction);
  4368. {$ifdef extdebug}
  4369. Comment(V_debug,pvarsym(p)^.name+' is at offset -'
  4370. +tostr(pvarsym(p)^.address));
  4371. exprasmlist^.concat(new(pai_asm_comment,init(
  4372. strpnew(pvarsym(p)^.name+' is at offset -'
  4373. +tostr(pvarsym(p)^.address)))));
  4374. {$endif extdebug}
  4375. end;
  4376. end;
  4377. procedure secondprocinline(var p : ptree);
  4378. var st : psymtable;
  4379. oldprocsym : pprocsym;
  4380. para_size : longint;
  4381. oldprocinfo : tprocinfo;
  4382. { just dummies for genentrycode }
  4383. nostackframe,make_global : boolean;
  4384. proc_names : tstringcontainer;
  4385. inlineentrycode,inlineexitcode : paasmoutput;
  4386. oldexitlabel,oldexit2label,oldquickexitlabel:Plabel;
  4387. begin
  4388. oldexitlabel:=aktexitlabel;
  4389. oldexit2label:=aktexit2label;
  4390. oldquickexitlabel:=quickexitlabel;
  4391. getlabel(aktexitlabel);
  4392. getlabel(aktexit2label);
  4393. oldprocsym:=aktprocsym;
  4394. oldprocinfo:=procinfo;
  4395. { set the return value }
  4396. procinfo.retdef:=p^.inlineprocdef^.retdef;
  4397. procinfo.retoffset:=p^.retoffset;
  4398. { arg space has been filled by the parent secondcall }
  4399. st:=p^.inlineprocdef^.localst;
  4400. { set it to the same lexical level }
  4401. st^.symtablelevel:=
  4402. oldprocsym^.definition^.localst^.symtablelevel;
  4403. if st^.datasize>0 then
  4404. st^.call_offset:=gettempofsizepersistant(st^.datasize);
  4405. {$ifdef extdebug}
  4406. Comment(V_debug,'local symtable is at offset '
  4407. +tostr(st^.call_offset));
  4408. exprasmlist^.concat(new(pai_asm_comment,init(
  4409. strpnew('local symtable is at offset '
  4410. +tostr(st^.call_offset)))));
  4411. {$endif extdebug}
  4412. addr_correction:=-st^.call_offset-st^.datasize;
  4413. st^.foreach(correct_address);
  4414. {$ifdef extdebug}
  4415. exprasmlist^.concat(new(pai_asm_comment,init('Start of inlined proc')));
  4416. {$endif extdebug}
  4417. { takes care of local data initialization }
  4418. inlineentrycode:=new(paasmoutput,init);
  4419. inlineexitcode:=new(paasmoutput,init);
  4420. proc_names.init;
  4421. para_size:=p^.para_size;
  4422. make_global:=false; { to avoid warning }
  4423. genentrycode(inlineentrycode,proc_names,make_global,
  4424. 0,para_size,nostackframe,true);
  4425. exprasmlist^.concatlist(inlineentrycode);
  4426. secondpass(p^.left);
  4427. genexitcode(inlineexitcode,0,false,true);
  4428. exprasmlist^.concatlist(inlineexitcode);
  4429. {$ifdef extdebug}
  4430. exprasmlist^.concat(new(pai_asm_comment,init('End of inlined proc')));
  4431. {$endif extdebug}
  4432. {we can free the local data now }
  4433. if st^.datasize>0 then
  4434. ungetpersistanttemp(st^.call_offset,st^.datasize);
  4435. { set the real address again }
  4436. addr_correction:=-addr_correction;
  4437. st^.foreach(correct_address);
  4438. aktprocsym:=oldprocsym;
  4439. aktexitlabel:=oldexitlabel;
  4440. aktexit2label:=oldexit2label;
  4441. quickexitlabel:=oldquickexitlabel;
  4442. procinfo:=oldprocinfo;
  4443. end;
  4444. procedure secondpass(var p : ptree);
  4445. const
  4446. procedures : array[ttreetyp] of secondpassproc =
  4447. (secondadd,secondadd,secondadd,secondmoddiv,secondadd,
  4448. secondmoddiv,secondassignment,secondload,secondnothing,
  4449. secondadd,secondadd,secondadd,secondadd,
  4450. secondadd,secondadd,secondin,secondadd,
  4451. secondadd,secondshlshr,secondshlshr,secondadd,
  4452. secondadd,secondsubscriptn,secondderef,secondaddr,
  4453. seconddoubleaddr,
  4454. secondordconst,secondtypeconv,secondcalln,secondnothing,
  4455. secondrealconst,secondfixconst,secondumminus,
  4456. secondasm,secondvecn,
  4457. secondstringconst,secondfuncret,secondselfn,
  4458. secondnot,secondinline,secondniln,seconderror,
  4459. secondnothing,secondhnewn,secondhdisposen,secondnewn,
  4460. secondsimplenewdispose,secondnothing,secondsetcons,secondblockn,
  4461. secondstatement,secondnothing,secondifn,secondbreakn,
  4462. secondcontinuen,second_while_repeatn,second_while_repeatn,secondfor,
  4463. secondexitn,secondwith,secondcase,secondlabel,
  4464. secondgoto,secondsimplenewdispose,secondtryexcept,secondraise,
  4465. secondnothing,secondtryfinally,secondis,secondas,seconderror,
  4466. secondfail,secondadd,secondprocinline,
  4467. secondnothing,secondloadvmt);
  4468. var
  4469. oldcodegenerror : boolean;
  4470. oldswitches : Tcswitches;
  4471. oldis : pinputfile;
  4472. oldnr : longint;
  4473. begin
  4474. oldcodegenerror:=codegenerror;
  4475. oldswitches:=aktswitches;
  4476. oldis:=current_module^.current_inputfile;
  4477. oldnr:=current_module^.current_inputfile^.line_no;
  4478. codegenerror:=false;
  4479. current_module^.current_inputfile:=
  4480. pinputfile(current_module^.sourcefiles.get_file(p^.fileinfo.fileindex));
  4481. current_module^.current_inputfile^.line_no:=p^.fileinfo.line;
  4482. aktswitches:=p^.pragmas;
  4483. if not(p^.error) then
  4484. begin
  4485. procedures[p^.treetype](p);
  4486. p^.error:=codegenerror;
  4487. codegenerror:=codegenerror or oldcodegenerror;
  4488. end
  4489. else
  4490. codegenerror:=true;
  4491. aktswitches:=oldswitches;
  4492. current_module^.current_inputfile:=oldis;
  4493. current_module^.current_inputfile^.line_no:=oldnr;
  4494. end;
  4495. function do_secondpass(var p : ptree) : boolean;
  4496. begin
  4497. codegenerror:=false;
  4498. if not(p^.error) then
  4499. secondpass(p);
  4500. do_secondpass:=codegenerror;
  4501. end;
  4502. var
  4503. regvars : array[1..maxvarregs] of pvarsym;
  4504. regvars_para : array[1..maxvarregs] of boolean;
  4505. regvars_refs : array[1..maxvarregs] of longint;
  4506. parasym : boolean;
  4507. procedure searchregvars(p : psym);
  4508. var
  4509. i,j,k : longint;
  4510. begin
  4511. if (p^.typ=varsym) and (pvarsym(p)^.regable) then
  4512. begin
  4513. { walk through all momentary register variables }
  4514. for i:=1 to maxvarregs do
  4515. begin
  4516. { free register ? }
  4517. if regvars[i]=nil then
  4518. begin
  4519. regvars[i]:=pvarsym(p);
  4520. regvars_para[i]:=parasym;
  4521. break;
  4522. end;
  4523. { else throw out a variable ? }
  4524. j:=pvarsym(p)^.refs;
  4525. { parameter get a less value }
  4526. if parasym then
  4527. begin
  4528. if cs_littlesize in aktswitches then
  4529. dec(j,1)
  4530. else
  4531. dec(j,100);
  4532. end;
  4533. if (j>regvars_refs[i]) and (j>0) then
  4534. begin
  4535. for k:=maxvarregs-1 downto i do
  4536. begin
  4537. regvars[k+1]:=regvars[k];
  4538. regvars_para[k+1]:=regvars_para[k];
  4539. end;
  4540. { calc the new refs
  4541. pvarsym(p)^.refs:=j; }
  4542. regvars[i]:=pvarsym(p);
  4543. regvars_para[i]:=parasym;
  4544. regvars_refs[i]:=j;
  4545. break;
  4546. end;
  4547. end;
  4548. end;
  4549. end;
  4550. procedure generatecode(var p : ptree);
  4551. var
  4552. { *pass modifies with every node aktlinenr and current_module^.current_inputfile, }
  4553. { to constantly contain the right line numbers }
  4554. oldis : pinputfile;
  4555. oldnr,i : longint;
  4556. regsize : topsize;
  4557. regi : tregister;
  4558. hr : preference;
  4559. label
  4560. nextreg;
  4561. begin
  4562. cleartempgen;
  4563. oldis:=current_module^.current_inputfile;
  4564. oldnr:=current_module^.current_inputfile^.line_no;
  4565. { when size optimization only count occurrence }
  4566. if cs_littlesize in aktswitches then
  4567. t_times:=1
  4568. else
  4569. { reference for repetition is 100 }
  4570. t_times:=100;
  4571. { clear register count }
  4572. {$ifdef SUPPORT_MMX}
  4573. for regi:=R_EAX to R_MM6 do
  4574. begin
  4575. reg_pushes[regi]:=0;
  4576. is_reg_var[regi]:=false;
  4577. end;
  4578. {$else SUPPORT_MMX}
  4579. for regi:=R_EAX to R_EDI do
  4580. begin
  4581. reg_pushes[regi]:=0;
  4582. is_reg_var[regi]:=false;
  4583. end;
  4584. {$endif SUPPORT_MMX}
  4585. use_esp_stackframe:=false;
  4586. if not(do_firstpass(p)) then
  4587. begin
  4588. { max. optimizations }
  4589. { only if no asm is used }
  4590. if (cs_maxoptimieren in aktswitches) and
  4591. ((procinfo.flags and pi_uses_asm)=0) then
  4592. begin
  4593. { can we omit the stack frame ? }
  4594. { conditions:
  4595. 1. procedure (not main block)
  4596. 2. no constructor or destructor
  4597. 3. no call to other procedures
  4598. 4. no interrupt handler
  4599. }
  4600. if assigned(aktprocsym) then
  4601. begin
  4602. if (aktprocsym^.definition^.options and
  4603. poconstructor+podestructor{+poinline}+pointerrupt=0) and
  4604. ((procinfo.flags and pi_do_call)=0) and (lexlevel>1) then
  4605. begin
  4606. { use ESP as frame pointer }
  4607. procinfo.framepointer:=R_ESP;
  4608. use_esp_stackframe:=true;
  4609. { calc parameter distance new }
  4610. dec(procinfo.framepointer_offset,4);
  4611. dec(procinfo.ESI_offset,4);
  4612. { is this correct ???}
  4613. { retoffset can be negativ for results in eax !! }
  4614. { the value should be decreased only if positive }
  4615. if procinfo.retoffset>=0 then
  4616. dec(procinfo.retoffset,4);
  4617. dec(procinfo.call_offset,4);
  4618. aktprocsym^.definition^.parast^.call_offset:=procinfo.call_offset;
  4619. end;
  4620. end;
  4621. if (p^.registers32<4) then
  4622. begin
  4623. for i:=1 to maxvarregs do
  4624. regvars[i]:=nil;
  4625. parasym:=false;
  4626. {$ifdef tp}
  4627. symtablestack^.foreach(searchregvars);
  4628. {$else}
  4629. symtablestack^.foreach(@searchregvars);
  4630. {$endif}
  4631. { copy parameter into a register ? }
  4632. parasym:=true;
  4633. {$ifdef tp}
  4634. symtablestack^.next^.foreach(searchregvars);
  4635. {$else}
  4636. symtablestack^.next^.foreach(@searchregvars);
  4637. {$endif}
  4638. { hold needed registers free }
  4639. for i:=maxvarregs downto maxvarregs-p^.registers32+1 do
  4640. regvars[i]:=nil;
  4641. { now assign register }
  4642. for i:=1 to maxvarregs-p^.registers32 do
  4643. begin
  4644. if assigned(regvars[i]) then
  4645. begin
  4646. { it is nonsens, to copy the variable to }
  4647. { a register because we need then much }
  4648. { pushes ? }
  4649. if reg_pushes[varregs[i]]>=regvars[i]^.refs then
  4650. begin
  4651. regvars[i]:=nil;
  4652. goto nextreg;
  4653. end;
  4654. { register is no longer available for }
  4655. { expressions }
  4656. { search the register which is the most }
  4657. { unused }
  4658. usableregs:=usableregs-[varregs[i]];
  4659. is_reg_var[varregs[i]]:=true;
  4660. dec(c_usableregs);
  4661. { possibly no 32 bit register are needed }
  4662. if (regvars[i]^.definition^.deftype=orddef) and
  4663. (porddef(regvars[i]^.definition)^.typ in [bool8bit,uchar,u8bit,s8bit]) then
  4664. begin
  4665. regvars[i]^.reg:=reg32toreg8(varregs[i]);
  4666. regsize:=S_B;
  4667. end
  4668. else if (regvars[i]^.definition^.deftype=orddef) and
  4669. (porddef(regvars[i]^.definition)^.typ in [bool16bit,u16bit,s16bit]) then
  4670. begin
  4671. regvars[i]^.reg:=reg32toreg16(varregs[i]);
  4672. regsize:=S_W;
  4673. end
  4674. else
  4675. begin
  4676. regvars[i]^.reg:=varregs[i];
  4677. regsize:=S_L;
  4678. end;
  4679. { parameter must be load }
  4680. if regvars_para[i] then
  4681. begin
  4682. { procinfo is there actual, }
  4683. { because we can't never be in a }
  4684. { nested procedure }
  4685. { when loading parameter to reg }
  4686. new(hr);
  4687. reset_reference(hr^);
  4688. hr^.offset:=pvarsym(regvars[i])^.address+procinfo.call_offset;
  4689. hr^.base:=procinfo.framepointer;
  4690. procinfo.aktentrycode^.concat(new(pai386,op_ref_reg(A_MOV,regsize,
  4691. hr,regvars[i]^.reg)));
  4692. unused:=unused - [regvars[i]^.reg];
  4693. end;
  4694. { procedure uses this register }
  4695. usedinproc:=usedinproc or ($80 shr byte(varregs[i]));
  4696. end;
  4697. nextreg:
  4698. { dummy }
  4699. regsize:=S_W;
  4700. end;
  4701. if (verbosity and v_debug)=v_debug then
  4702. begin
  4703. for i:=1 to maxvarregs do
  4704. begin
  4705. if assigned(regvars[i]) then
  4706. Message3(cg_d_register_weight,reg2str(regvars[i]^.reg),
  4707. tostr(regvars[i]^.refs),regvars[i]^.name);
  4708. end;
  4709. end;
  4710. end;
  4711. end;
  4712. do_secondpass(p);
  4713. {$ifdef StoreFPULevel}
  4714. if assigned(aktprocsym) then
  4715. aktprocsym^.fpu_used:=p^.registersfpu;
  4716. {$endif StoreFPULevel}
  4717. { all registers can be used again }
  4718. usableregs:=[R_EAX,R_EBX,R_ECX,R_EDX];
  4719. {$ifdef SUPPORT_MMX}
  4720. usableregs:=usableregs+[R_MM0..R_MM6];
  4721. {$endif SUPPORT_MMX}
  4722. c_usableregs:=4;
  4723. end;
  4724. procinfo.aktproccode^.concatlist(exprasmlist);
  4725. current_module^.current_inputfile:=oldis;
  4726. current_module^.current_inputfile^.line_no:=oldnr;
  4727. end;
  4728. end.
  4729. {
  4730. $Log$
  4731. Revision 1.33 1998-06-04 23:51:37 peter
  4732. * m68k compiles
  4733. + .def file creation moved to gendef.pas so it could also be used
  4734. for win32
  4735. Revision 1.32 1998/06/04 09:55:35 pierre
  4736. * demangled name of procsym reworked to become independant of the mangling scheme
  4737. Come test_funcret improvements (not yet working)S: ----------------------------------------------------------------------
  4738. Revision 1.31 1998/06/03 22:48:52 peter
  4739. + wordbool,longbool
  4740. * rename bis,von -> high,low
  4741. * moved some systemunit loading/creating to psystem.pas
  4742. Revision 1.30 1998/06/02 17:03:00 pierre
  4743. * with node corrected for objects
  4744. * small bugs for SUPPORT_MMX fixed
  4745. Revision 1.29 1998/06/01 16:50:18 peter
  4746. + boolean -> ord conversion
  4747. * fixed ord -> boolean conversion
  4748. Revision 1.28 1998/05/28 17:26:47 peter
  4749. * fixed -R switch, it didn't work after my previous akt/init patch
  4750. * fixed bugs 110,130,136
  4751. Revision 1.27 1998/05/25 17:11:38 pierre
  4752. * firstpasscount bug fixed
  4753. now all is already set correctly the first time
  4754. under EXTDEBUG try -gp to skip all other firstpasses
  4755. it works !!
  4756. * small bug fixes
  4757. - for smallsets with -dTESTSMALLSET
  4758. - some warnings removed (by correcting code !)
  4759. Revision 1.26 1998/05/23 01:21:03 peter
  4760. + aktasmmode, aktoptprocessor, aktoutputformat
  4761. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  4762. + $LIBNAME to set the library name where the unit will be put in
  4763. * splitted cgi386 a bit (codeseg to large for bp7)
  4764. * nasm, tasm works again. nasm moved to ag386nsm.pas
  4765. Revision 1.25 1998/05/21 19:33:31 peter
  4766. + better procedure directive handling and only one table
  4767. Revision 1.24 1998/05/20 09:42:33 pierre
  4768. + UseTokenInfo now default
  4769. * unit in interface uses and implementation uses gives error now
  4770. * only one error for unknown symbol (uses lastsymknown boolean)
  4771. the problem came from the label code !
  4772. + first inlined procedures and function work
  4773. (warning there might be allowed cases were the result is still wrong !!)
  4774. * UseBrower updated gives a global list of all position of all used symbols
  4775. with switch -gb
  4776. Revision 1.23 1998/05/12 10:46:58 peter
  4777. * moved printstatus to verb_def
  4778. + V_Normal which is between V_Error and V_Warning and doesn't have a
  4779. prefix like error: warning: and is included in V_Default
  4780. * fixed some messages
  4781. * first time parameter scan is only for -v and -T
  4782. - removed old style messages
  4783. Revision 1.22 1998/05/07 00:17:00 peter
  4784. * smartlinking for sets
  4785. + consts labels are now concated/generated in hcodegen
  4786. * moved some cpu code to cga and some none cpu depended code from cga
  4787. to tree and hcodegen and cleanup of hcodegen
  4788. * assembling .. output reduced for smartlinking ;)
  4789. Revision 1.21 1998/05/06 08:38:36 pierre
  4790. * better position info with UseTokenInfo
  4791. UseTokenInfo greatly simplified
  4792. + added check for changed tree after first time firstpass
  4793. (if we could remove all the cases were it happen
  4794. we could skip all firstpass if firstpasscount > 1)
  4795. Only with ExtDebug
  4796. Revision 1.20 1998/05/01 16:38:44 florian
  4797. * handling of private and protected fixed
  4798. + change_keywords_to_tp implemented to remove
  4799. keywords which aren't supported by tp
  4800. * break and continue are now symbols of the system unit
  4801. + widestring, longstring and ansistring type released
  4802. Revision 1.19 1998/04/30 15:59:39 pierre
  4803. * GDB works again better :
  4804. correct type info in one pass
  4805. + UseTokenInfo for better source position
  4806. * fixed one remaining bug in scanner for line counts
  4807. * several little fixes
  4808. Revision 1.18 1998/04/29 10:33:48 pierre
  4809. + added some code for ansistring (not complete nor working yet)
  4810. * corrected operator overloading
  4811. * corrected nasm output
  4812. + started inline procedures
  4813. + added starstarn : use ** for exponentiation (^ gave problems)
  4814. + started UseTokenInfo cond to get accurate positions
  4815. Revision 1.17 1998/04/27 23:10:27 peter
  4816. + new scanner
  4817. * $makelib -> if smartlink
  4818. * small filename fixes pmodule.setfilename
  4819. * moved import from files.pas -> import.pas
  4820. Revision 1.16 1998/04/23 21:52:08 florian
  4821. * fixes of Jonas applied
  4822. Revision 1.15 1998/04/22 21:06:49 florian
  4823. * last fixes before the release:
  4824. - veryyyy slow firstcall fixed
  4825. Revision 1.14 1998/04/21 10:16:47 peter
  4826. * patches from strasbourg
  4827. * objects is not used anymore in the fpc compiled version
  4828. Revision 1.13 1998/04/14 23:27:02 florian
  4829. + exclude/include with constant second parameter added
  4830. Revision 1.12 1998/04/13 21:15:41 florian
  4831. * error handling of pass_1 and cgi386 fixed
  4832. * the following bugs fixed: 0117, 0118, 0119 and 0129, 0122 was already
  4833. fixed, verified
  4834. Revision 1.11 1998/04/13 08:42:51 florian
  4835. * call by reference and call by value open arrays fixed
  4836. Revision 1.10 1998/04/12 22:39:43 florian
  4837. * problem with read access to properties solved
  4838. * correct handling of hidding methods via virtual (COM)
  4839. * correct result type of constructor calls (COM), the resulttype
  4840. depends now on the type of the class reference
  4841. Revision 1.9 1998/04/10 21:36:55 florian
  4842. + some stuff to support method pointers (procedure of object) added
  4843. (declaration, parameter handling)
  4844. Revision 1.8 1998/04/09 22:16:33 florian
  4845. * problem with previous REGALLOC solved
  4846. * improved property support
  4847. Revision 1.7 1998/04/09 14:28:05 jonas
  4848. + basic k6 and 6x86 optimizing support (-O7 and -O8)
  4849. Revision 1.6 1998/04/08 11:34:20 peter
  4850. * nasm works (linux only tested)
  4851. Revision 1.5 1998/04/07 22:45:04 florian
  4852. * bug0092, bug0115 and bug0121 fixed
  4853. + packed object/class/array
  4854. Revision 1.4 1998/04/07 13:19:42 pierre
  4855. * bugfixes for reset_gdb_info
  4856. in MEM parsing for go32v2
  4857. better external symbol creation
  4858. support for rhgdb.exe (lowercase file names)
  4859. }