cgi386.pas 223 KB

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