cgai386.pas 156 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. Helper routines for the i386 code generator
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************}
  17. unit cgai386;
  18. interface
  19. uses
  20. cobjects,tree,
  21. cpubase,cpuasm,
  22. symconst,symtable,aasm;
  23. {$define TESTGETTEMP to store const that
  24. are written into temps for later release PM }
  25. function def_opsize(p1:pdef):topsize;
  26. function def2def_opsize(p1,p2:pdef):topsize;
  27. function def_getreg(p1:pdef):tregister;
  28. function makereg8(r:tregister):tregister;
  29. function makereg16(r:tregister):tregister;
  30. function makereg32(r:tregister):tregister;
  31. procedure locflags2reg(var l:tlocation;opsize:topsize);
  32. procedure locjump2reg(var l:tlocation;opsize:topsize; otl, ofl: pasmlabel);
  33. procedure emitlab(var l : pasmlabel);
  34. procedure emitjmp(c : tasmcond;var l : pasmlabel);
  35. procedure emit_flag2reg(flag:tresflags;hregister:tregister);
  36. procedure emit_none(i : tasmop;s : topsize);
  37. procedure emit_const(i : tasmop;s : topsize;c : longint);
  38. procedure emit_reg(i : tasmop;s : topsize;reg : tregister);
  39. procedure emit_ref(i : tasmop;s : topsize;ref : preference);
  40. procedure emit_const_reg(i : tasmop;s : topsize;c : longint;reg : tregister);
  41. procedure emit_const_ref(i : tasmop;s : topsize;c : longint;ref : preference);
  42. procedure emit_ref_reg(i : tasmop;s : topsize;ref : preference;reg : tregister);
  43. procedure emit_reg_ref(i : tasmop;s : topsize;reg : tregister;ref : preference);
  44. procedure emit_reg_reg(i : tasmop;s : topsize;reg1,reg2 : tregister);
  45. procedure emit_const_reg_reg(i : tasmop;s : topsize;c : longint;reg1,reg2 : tregister);
  46. procedure emit_reg_reg_reg(i : tasmop;s : topsize;reg1,reg2,reg3 : tregister);
  47. procedure emit_sym(i : tasmop;s : topsize;op : pasmsymbol);
  48. procedure emit_sym_ofs(i : tasmop;s : topsize;op : pasmsymbol;ofs : longint);
  49. procedure emit_sym_ofs_reg(i : tasmop;s : topsize;op : pasmsymbol;ofs:longint;reg : tregister);
  50. procedure emit_sym_ofs_ref(i : tasmop;s : topsize;op : pasmsymbol;ofs:longint;ref : preference);
  51. procedure emitcall(const routine:string);
  52. procedure emit_mov_loc_ref(const t:tlocation;const ref:treference;siz:topsize;freetemp:boolean);
  53. procedure emit_mov_loc_reg(const t:tlocation;reg:tregister);
  54. procedure emit_mov_ref_reg64(r : treference;rl,rh : tregister);
  55. procedure emit_lea_loc_ref(const t:tlocation;const ref:treference;freetemp:boolean);
  56. procedure emit_lea_loc_reg(const t:tlocation;reg:tregister;freetemp:boolean);
  57. procedure emit_push_loc(const t:tlocation);
  58. procedure emit_push_mem_size(const t: treference; size: longint);
  59. { pushes qword location to the stack }
  60. procedure emit_pushq_loc(const t : tlocation);
  61. procedure release_qword_loc(const t : tlocation);
  62. { remove non regvar registers in loc from regs (in the format }
  63. { pushusedregisters uses) }
  64. procedure remove_non_regvars_from_loc(const t: tlocation; var regs: byte);
  65. { releases the registers of a location }
  66. procedure release_loc(const t : tlocation);
  67. procedure emit_pushw_loc(const t:tlocation);
  68. procedure emit_push_lea_loc(const t:tlocation;freetemp:boolean);
  69. procedure emit_to_mem(var p:ptree);
  70. procedure emit_to_reg16(var hr:tregister);
  71. procedure emit_to_reg32(var hr:tregister);
  72. procedure emit_mov_reg_loc(reg: TRegister; const t:tlocation);
  73. procedure emit_movq_reg_loc(reghigh,reglow: TRegister;t:tlocation);
  74. procedure copyshortstring(const dref,sref : treference;len : byte;
  75. loadref, del_sref: boolean);
  76. procedure loadansistring(p : ptree);
  77. procedure finalize(t : pdef;const ref : treference;is_already_ref : boolean);
  78. procedure incrstringref(t : pdef;const ref : treference);
  79. procedure decrstringref(t : pdef;const ref : treference);
  80. function maybe_push(needed : byte;p : ptree;isint64 : boolean) : boolean;
  81. procedure push_int(l : longint);
  82. procedure emit_push_mem(const ref : treference);
  83. procedure emitpushreferenceaddr(const ref : treference);
  84. procedure pushsetelement(p : ptree);
  85. procedure restore(p : ptree;isint64 : boolean);
  86. procedure push_value_para(p:ptree;inlined,is_cdecl:boolean;
  87. para_offset:longint;alignment : longint);
  88. {$ifdef TEMPS_NOT_PUSH}
  89. { does the same as restore/ , but uses temp. space instead of pushing }
  90. function maybe_push(needed : byte;p : ptree;isint64 : boolean) : boolean;
  91. procedure restorefromtemp(p : ptree;isint64 : boolean);
  92. {$endif TEMPS_NOT_PUSH}
  93. procedure floatload(t : tfloattype;const ref : treference);
  94. procedure floatstore(t : tfloattype;const ref : treference);
  95. procedure floatloadops(t : tfloattype;var op : tasmop;var s : topsize);
  96. procedure floatstoreops(t : tfloattype;var op : tasmop;var s : topsize);
  97. procedure maybe_loadesi;
  98. procedure maketojumpbool(p : ptree);
  99. procedure emitloadord2reg(const location:Tlocation;orddef:Porddef;destreg:Tregister;delloc:boolean);
  100. procedure emitoverflowcheck(p:ptree);
  101. procedure emitrangecheck(p:ptree;todef:pdef);
  102. procedure concatcopy(source,dest : treference;size : longint;delsource : boolean;loadref:boolean);
  103. procedure firstcomplex(p : ptree);
  104. procedure genentrycode(alist : paasmoutput;const proc_names:Tstringcontainer;make_global:boolean;
  105. stackframe:longint;
  106. var parasize:longint;var nostackframe:boolean;
  107. inlined : boolean);
  108. procedure genexitcode(alist : paasmoutput;parasize:longint;
  109. nostackframe,inlined:boolean);
  110. { if a unit doesn't have a explicit init/final code, }
  111. { we've to generate one, if the units has ansistrings }
  112. { in the interface or implementation }
  113. procedure genimplicitunitfinal(alist : paasmoutput);
  114. procedure genimplicitunitinit(alist : paasmoutput);
  115. {$ifdef test_dest_loc}
  116. const
  117. { used to avoid temporary assignments }
  118. dest_loc_known : boolean = false;
  119. in_dest_loc : boolean = false;
  120. dest_loc_tree : ptree = nil;
  121. var
  122. dest_loc : tlocation;
  123. procedure mov_reg_to_dest(p : ptree; s : topsize; reg : tregister);
  124. {$endif test_dest_loc}
  125. implementation
  126. uses
  127. strings,globtype,systems,globals,verbose,files,types,pbase,
  128. tgeni386,temp_gen,hcodegen,ppu,regvars
  129. {$ifdef GDB}
  130. ,gdb
  131. {$endif}
  132. {$ifndef NOTARGETWIN32}
  133. ,t_win32
  134. {$endif}
  135. ;
  136. {*****************************************************************************
  137. Helpers
  138. *****************************************************************************}
  139. function def_opsize(p1:pdef):topsize;
  140. begin
  141. case p1^.size of
  142. 1 : def_opsize:=S_B;
  143. 2 : def_opsize:=S_W;
  144. 4 : def_opsize:=S_L;
  145. else
  146. internalerror(78);
  147. end;
  148. end;
  149. function def2def_opsize(p1,p2:pdef):topsize;
  150. var
  151. o1 : topsize;
  152. begin
  153. case p1^.size of
  154. 1 : o1:=S_B;
  155. 2 : o1:=S_W;
  156. 4 : o1:=S_L;
  157. { I don't know if we need it (FK) }
  158. 8 : o1:=S_L;
  159. else
  160. internalerror(78);
  161. end;
  162. if assigned(p2) then
  163. begin
  164. case p2^.size of
  165. 1 : o1:=S_B;
  166. 2 : begin
  167. if o1=S_B then
  168. o1:=S_BW
  169. else
  170. o1:=S_W;
  171. end;
  172. 4,8:
  173. begin
  174. case o1 of
  175. S_B : o1:=S_BL;
  176. S_W : o1:=S_WL;
  177. end;
  178. end;
  179. end;
  180. end;
  181. def2def_opsize:=o1;
  182. end;
  183. function def_getreg(p1:pdef):tregister;
  184. begin
  185. case p1^.size of
  186. 1 : def_getreg:=reg32toreg8(getregister32);
  187. 2 : def_getreg:=reg32toreg16(getregister32);
  188. 4 : def_getreg:=getregister32;
  189. else
  190. internalerror(78);
  191. end;
  192. end;
  193. function makereg8(r:tregister):tregister;
  194. begin
  195. case r of
  196. R_EAX,R_EBX,R_ECX,R_EDX,R_EDI,R_ESI,R_ESP :
  197. makereg8:=reg32toreg8(r);
  198. R_AX,R_BX,R_CX,R_DX,R_DI,R_SI,R_SP :
  199. makereg8:=reg16toreg8(r);
  200. R_AL,R_BL,R_CL,R_DL :
  201. makereg8:=r;
  202. end;
  203. end;
  204. function makereg16(r:tregister):tregister;
  205. begin
  206. case r of
  207. R_EAX,R_EBX,R_ECX,R_EDX,R_EDI,R_ESI,R_ESP :
  208. makereg16:=reg32toreg16(r);
  209. R_AX,R_BX,R_CX,R_DX,R_DI,R_SI,R_SP :
  210. makereg16:=r;
  211. R_AL,R_BL,R_CL,R_DL :
  212. makereg16:=reg8toreg16(r);
  213. end;
  214. end;
  215. function makereg32(r:tregister):tregister;
  216. begin
  217. case r of
  218. R_EAX,R_EBX,R_ECX,R_EDX,R_EDI,R_ESI,R_ESP :
  219. makereg32:=r;
  220. R_AX,R_BX,R_CX,R_DX,R_DI,R_SI,R_SP :
  221. makereg32:=reg16toreg32(r);
  222. R_AL,R_BL,R_CL,R_DL :
  223. makereg32:=reg8toreg32(r);
  224. end;
  225. end;
  226. procedure locflags2reg(var l:tlocation;opsize:topsize);
  227. var
  228. hregister : tregister;
  229. begin
  230. if (l.loc=LOC_FLAGS) then
  231. begin
  232. hregister:=getregister32;
  233. case opsize of
  234. S_W : hregister:=reg32toreg16(hregister);
  235. S_B : hregister:=reg32toreg8(hregister);
  236. end;
  237. emit_flag2reg(l.resflags,hregister);
  238. l.loc:=LOC_REGISTER;
  239. l.register:=hregister;
  240. end
  241. else internalerror(270720001);
  242. end;
  243. procedure locjump2reg(var l:tlocation;opsize:topsize; otl, ofl: pasmlabel);
  244. var
  245. hregister : tregister;
  246. hl : pasmlabel;
  247. begin
  248. if l.loc = LOC_JUMP then
  249. begin
  250. hregister:=getregister32;
  251. case opsize of
  252. S_W : hregister:=reg32toreg16(hregister);
  253. S_B : hregister:=reg32toreg8(hregister);
  254. end;
  255. l.loc:=LOC_REGISTER;
  256. l.register:=hregister;
  257. emitlab(truelabel);
  258. truelabel:=otl;
  259. emit_const_reg(A_MOV,opsize,1,hregister);
  260. getlabel(hl);
  261. emitjmp(C_None,hl);
  262. emitlab(falselabel);
  263. falselabel:=ofl;
  264. emit_reg_reg(A_XOR,S_L,makereg32(hregister),
  265. makereg32(hregister));
  266. emitlab(hl);
  267. end
  268. else internalerror(270720002);
  269. end;
  270. {*****************************************************************************
  271. Emit Assembler
  272. *****************************************************************************}
  273. procedure emitlab(var l : pasmlabel);
  274. begin
  275. if not l^.is_set then
  276. exprasmlist^.concat(new(pai_label,init(l)))
  277. else
  278. internalerror(7453984);
  279. end;
  280. {$ifdef nojmpfix}
  281. procedure emitjmp(c : tasmcond;var l : pasmlabel);
  282. var
  283. ai : Paicpu;
  284. begin
  285. if c=C_None then
  286. exprasmlist^.concat(new(paicpu,op_sym(A_JMP,S_NO,l)))
  287. else
  288. begin
  289. ai:=new(paicpu,op_sym(A_Jcc,S_NO,l));
  290. ai^.SetCondition(c);
  291. ai^.is_jmp:=true;
  292. exprasmlist^.concat(ai);
  293. end;
  294. end;
  295. {$else nojmpfix}
  296. procedure emitjmp(c : tasmcond;var l : pasmlabel);
  297. var
  298. ai : Paicpu;
  299. begin
  300. if c=C_None then
  301. ai := new(paicpu,op_sym(A_JMP,S_NO,l))
  302. else
  303. begin
  304. ai:=new(paicpu,op_sym(A_Jcc,S_NO,l));
  305. ai^.SetCondition(c);
  306. end;
  307. ai^.is_jmp:=true;
  308. exprasmlist^.concat(ai);
  309. end;
  310. {$endif nojmpfix}
  311. procedure emit_flag2reg(flag:tresflags;hregister:tregister);
  312. var
  313. ai : paicpu;
  314. hreg : tregister;
  315. begin
  316. hreg:=makereg8(hregister);
  317. ai:=new(paicpu,op_reg(A_Setcc,S_B,hreg));
  318. ai^.SetCondition(flag_2_cond[flag]);
  319. exprasmlist^.concat(ai);
  320. if hreg<>hregister then
  321. begin
  322. if hregister in regset16bit then
  323. emit_to_reg16(hreg)
  324. else
  325. emit_to_reg32(hreg);
  326. end;
  327. end;
  328. procedure emit_none(i : tasmop;s : topsize);
  329. begin
  330. exprasmlist^.concat(new(paicpu,op_none(i,s)));
  331. end;
  332. procedure emit_reg(i : tasmop;s : topsize;reg : tregister);
  333. begin
  334. exprasmlist^.concat(new(paicpu,op_reg(i,s,reg)));
  335. end;
  336. procedure emit_ref(i : tasmop;s : topsize;ref : preference);
  337. begin
  338. exprasmlist^.concat(new(paicpu,op_ref(i,s,ref)));
  339. end;
  340. procedure emit_const(i : tasmop;s : topsize;c : longint);
  341. begin
  342. exprasmlist^.concat(new(paicpu,op_const(i,s,c)));
  343. end;
  344. procedure emit_const_reg(i : tasmop;s : topsize;c : longint;reg : tregister);
  345. begin
  346. exprasmlist^.concat(new(paicpu,op_const_reg(i,s,c,reg)));
  347. end;
  348. procedure emit_const_ref(i : tasmop;s : topsize;c : longint;ref : preference);
  349. begin
  350. exprasmlist^.concat(new(paicpu,op_const_ref(i,s,c,ref)));
  351. end;
  352. procedure emit_ref_reg(i : tasmop;s : topsize;ref : preference;reg : tregister);
  353. begin
  354. exprasmlist^.concat(new(paicpu,op_ref_reg(i,s,ref,reg)));
  355. end;
  356. procedure emit_reg_ref(i : tasmop;s : topsize;reg : tregister;ref : preference);
  357. begin
  358. exprasmlist^.concat(new(paicpu,op_reg_ref(i,s,reg,ref)));
  359. end;
  360. procedure emit_reg_reg(i : tasmop;s : topsize;reg1,reg2 : tregister);
  361. begin
  362. if (reg1<>reg2) or (i<>A_MOV) then
  363. exprasmlist^.concat(new(paicpu,op_reg_reg(i,s,reg1,reg2)));
  364. end;
  365. procedure emit_const_reg_reg(i : tasmop;s : topsize;c : longint;reg1,reg2 : tregister);
  366. begin
  367. exprasmlist^.concat(new(paicpu,op_const_reg_reg(i,s,c,reg1,reg2)));
  368. end;
  369. procedure emit_reg_reg_reg(i : tasmop;s : topsize;reg1,reg2,reg3 : tregister);
  370. begin
  371. exprasmlist^.concat(new(paicpu,op_reg_reg_reg(i,s,reg1,reg2,reg3)));
  372. end;
  373. procedure emit_sym(i : tasmop;s : topsize;op : pasmsymbol);
  374. begin
  375. exprasmlist^.concat(new(paicpu,op_sym(i,s,op)));
  376. end;
  377. procedure emit_sym_ofs(i : tasmop;s : topsize;op : pasmsymbol;ofs : longint);
  378. begin
  379. exprasmlist^.concat(new(paicpu,op_sym_ofs(i,s,op,ofs)));
  380. end;
  381. procedure emit_sym_ofs_reg(i : tasmop;s : topsize;op : pasmsymbol;ofs:longint;reg : tregister);
  382. begin
  383. exprasmlist^.concat(new(paicpu,op_sym_ofs_reg(i,s,op,ofs,reg)));
  384. end;
  385. procedure emit_sym_ofs_ref(i : tasmop;s : topsize;op : pasmsymbol;ofs:longint;ref : preference);
  386. begin
  387. exprasmlist^.concat(new(paicpu,op_sym_ofs_ref(i,s,op,ofs,ref)));
  388. end;
  389. procedure emitcall(const routine:string);
  390. begin
  391. exprasmlist^.concat(new(paicpu,op_sym(A_CALL,S_NO,newasmsymbol(routine))));
  392. end;
  393. { only usefull in startup code }
  394. procedure emitinsertcall(const routine:string);
  395. begin
  396. exprasmlist^.insert(new(paicpu,op_sym(A_CALL,S_NO,newasmsymbol(routine))));
  397. end;
  398. procedure emit_mov_loc_ref(const t:tlocation;const ref:treference;siz:topsize;freetemp:boolean);
  399. var
  400. hreg : tregister;
  401. pushedeax : boolean;
  402. begin
  403. pushedeax:=false;
  404. case t.loc of
  405. LOC_REGISTER,
  406. LOC_CREGISTER : begin
  407. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,siz,
  408. t.register,newreference(ref))));
  409. ungetregister32(t.register); { the register is not needed anymore }
  410. end;
  411. LOC_MEM,
  412. LOC_REFERENCE : begin
  413. if t.reference.is_immediate then
  414. emit_const_ref(A_MOV,siz,
  415. t.reference.offset,newreference(ref))
  416. else
  417. begin
  418. case siz of
  419. S_B : begin
  420. { we can't do a getregister in the code generator }
  421. { without problems!!! }
  422. if usablereg32>0 then
  423. hreg:=reg32toreg8(getregister32)
  424. else
  425. begin
  426. emit_reg(A_PUSH,S_L,R_EAX);
  427. pushedeax:=true;
  428. hreg:=R_AL;
  429. end;
  430. end;
  431. S_W : hreg:=R_DI;
  432. S_L : hreg:=R_EDI;
  433. end;
  434. {$ifndef noAllocEdi}
  435. if hreg in [R_DI,R_EDI] then
  436. getexplicitregister32(R_EDI);
  437. {$endif noAllocEdi}
  438. emit_ref_reg(A_MOV,siz,
  439. newreference(t.reference),hreg);
  440. del_reference(t.reference);
  441. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,siz,
  442. hreg,newreference(ref))));
  443. if siz=S_B then
  444. begin
  445. if pushedeax then
  446. emit_reg(A_POP,S_L,R_EAX)
  447. else
  448. ungetregister(hreg);
  449. end;
  450. {$ifndef noAllocEdi}
  451. if hreg in [R_DI,R_EDI] then
  452. ungetregister32(R_EDI);
  453. {$endif noAllocEdi}
  454. { we can release the registers }
  455. { but only AFTER the MOV! Important for the optimizer!
  456. (JM)}
  457. del_reference(ref);
  458. end;
  459. if freetemp then
  460. ungetiftemp(t.reference);
  461. end;
  462. else
  463. internalerror(330);
  464. end;
  465. end;
  466. procedure emit_mov_loc_reg(const t:tlocation;reg:tregister);
  467. begin
  468. case t.loc of
  469. LOC_REGISTER,
  470. LOC_CREGISTER : begin
  471. emit_reg_reg(A_MOV,S_L,t.register,reg);
  472. ungetregister32(t.register); { the register is not needed anymore }
  473. end;
  474. LOC_MEM,
  475. LOC_REFERENCE : begin
  476. if t.reference.is_immediate then
  477. emit_const_reg(A_MOV,S_L,
  478. t.reference.offset,reg)
  479. else
  480. begin
  481. emit_ref_reg(A_MOV,S_L,
  482. newreference(t.reference),reg);
  483. end;
  484. end;
  485. else
  486. internalerror(330);
  487. end;
  488. end;
  489. procedure emit_mov_reg_loc(reg: TRegister; const t:tlocation);
  490. begin
  491. case t.loc of
  492. LOC_REGISTER,
  493. LOC_CREGISTER : begin
  494. emit_reg_reg(A_MOV,RegSize(Reg),
  495. reg,t.register);
  496. end;
  497. LOC_MEM,
  498. LOC_REFERENCE : begin
  499. if t.reference.is_immediate then
  500. internalerror(334)
  501. else
  502. begin
  503. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,RegSize(Reg),
  504. Reg,newreference(t.reference))));
  505. end;
  506. end;
  507. else
  508. internalerror(330);
  509. end;
  510. end;
  511. procedure emit_lea_loc_reg(const t:tlocation;reg:tregister;freetemp:boolean);
  512. begin
  513. case t.loc of
  514. LOC_MEM,
  515. LOC_REFERENCE : begin
  516. if t.reference.is_immediate then
  517. internalerror(331)
  518. else
  519. begin
  520. emit_ref_reg(A_LEA,S_L,
  521. newreference(t.reference),reg);
  522. end;
  523. if freetemp then
  524. ungetiftemp(t.reference);
  525. end;
  526. else
  527. internalerror(332);
  528. end;
  529. end;
  530. procedure emit_movq_reg_loc(reghigh,reglow: TRegister;t:tlocation);
  531. begin
  532. case t.loc of
  533. LOC_REGISTER,
  534. LOC_CREGISTER : begin
  535. emit_reg_reg(A_MOV,S_L,
  536. reglow,t.registerlow);
  537. emit_reg_reg(A_MOV,S_L,
  538. reghigh,t.registerhigh);
  539. end;
  540. LOC_MEM,
  541. LOC_REFERENCE : begin
  542. if t.reference.is_immediate then
  543. internalerror(334)
  544. else
  545. begin
  546. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,
  547. Reglow,newreference(t.reference))));
  548. inc(t.reference.offset,4);
  549. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,
  550. Reghigh,newreference(t.reference))));
  551. end;
  552. end;
  553. else
  554. internalerror(330);
  555. end;
  556. end;
  557. procedure emit_pushq_loc(const t : tlocation);
  558. var
  559. hr : preference;
  560. begin
  561. case t.loc of
  562. LOC_REGISTER,
  563. LOC_CREGISTER:
  564. begin
  565. exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,
  566. t.registerhigh)));
  567. exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,
  568. t.registerlow)));
  569. end;
  570. LOC_MEM,
  571. LOC_REFERENCE:
  572. begin
  573. hr:=newreference(t.reference);
  574. inc(hr^.offset,4);
  575. exprasmlist^.concat(new(paicpu,op_ref(A_PUSH,S_L,
  576. hr)));
  577. exprasmlist^.concat(new(paicpu,op_ref(A_PUSH,S_L,
  578. newreference(t.reference))));
  579. ungetiftemp(t.reference);
  580. end;
  581. else internalerror(331);
  582. end;
  583. end;
  584. procedure remove_non_regvars_from_loc(const t: tlocation; var regs: byte);
  585. begin
  586. case t.loc of
  587. LOC_REGISTER:
  588. { can't be a regvar, since it would be LOC_CREGISTER then }
  589. regs := regs and not($80 shr byte(t.register));
  590. LOC_MEM,LOC_REFERENCE:
  591. begin
  592. if not(cs_regalloc in aktglobalswitches) or
  593. (t.reference.base in usableregs) then
  594. regs := regs and
  595. not($80 shr byte(t.reference.base));
  596. if not(cs_regalloc in aktglobalswitches) or
  597. (t.reference.index in usableregs) then
  598. regs := regs and
  599. not($80 shr byte(t.reference.index));
  600. end;
  601. end;
  602. end;
  603. procedure release_loc(const t : tlocation);
  604. begin
  605. case t.loc of
  606. LOC_REGISTER,
  607. LOC_CREGISTER:
  608. begin
  609. ungetregister32(t.register);
  610. end;
  611. LOC_MEM,
  612. LOC_REFERENCE:
  613. del_reference(t.reference);
  614. else internalerror(332);
  615. end;
  616. end;
  617. procedure release_qword_loc(const t : tlocation);
  618. begin
  619. case t.loc of
  620. LOC_REGISTER,
  621. LOC_CREGISTER:
  622. begin
  623. ungetregister32(t.registerhigh);
  624. ungetregister32(t.registerlow);
  625. end;
  626. LOC_MEM,
  627. LOC_REFERENCE:
  628. del_reference(t.reference);
  629. else internalerror(331);
  630. end;
  631. end;
  632. procedure emit_push_loc(const t:tlocation);
  633. begin
  634. case t.loc of
  635. LOC_REGISTER,
  636. LOC_CREGISTER : begin
  637. exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,makereg32(t.register))));
  638. ungetregister(t.register); { the register is not needed anymore }
  639. end;
  640. LOC_MEM,
  641. LOC_REFERENCE : begin
  642. if t.reference.is_immediate then
  643. exprasmlist^.concat(new(paicpu,op_const(A_PUSH,S_L,t.reference.offset)))
  644. else
  645. exprasmlist^.concat(new(paicpu,op_ref(A_PUSH,S_L,newreference(t.reference))));
  646. del_reference(t.reference);
  647. ungetiftemp(t.reference);
  648. end;
  649. else
  650. internalerror(330);
  651. end;
  652. end;
  653. procedure emit_pushw_loc(const t:tlocation);
  654. var
  655. opsize : topsize;
  656. begin
  657. case t.loc of
  658. LOC_REGISTER,
  659. LOC_CREGISTER : begin
  660. if target_os.stackalignment=4 then
  661. exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,makereg32(t.register))))
  662. else
  663. exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_W,makereg16(t.register))));
  664. ungetregister(t.register); { the register is not needed anymore }
  665. end;
  666. LOC_MEM,
  667. LOC_REFERENCE : begin
  668. if target_os.stackalignment=4 then
  669. opsize:=S_L
  670. else
  671. opsize:=S_W;
  672. if t.reference.is_immediate then
  673. exprasmlist^.concat(new(paicpu,op_const(A_PUSH,opsize,t.reference.offset)))
  674. else
  675. exprasmlist^.concat(new(paicpu,op_ref(A_PUSH,opsize,newreference(t.reference))));
  676. del_reference(t.reference);
  677. ungetiftemp(t.reference);
  678. end;
  679. else
  680. internalerror(330);
  681. end;
  682. end;
  683. procedure emit_lea_loc_ref(const t:tlocation;const ref:treference;freetemp:boolean);
  684. begin
  685. case t.loc of
  686. LOC_MEM,
  687. LOC_REFERENCE : begin
  688. if t.reference.is_immediate then
  689. internalerror(331)
  690. else
  691. begin
  692. {$ifndef noAllocEdi}
  693. getexplicitregister32(R_EDI);
  694. {$endif noAllocEdi}
  695. emit_ref_reg(A_LEA,S_L,
  696. newreference(t.reference),R_EDI);
  697. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,
  698. R_EDI,newreference(ref))));
  699. {$ifndef noAllocEdi}
  700. ungetregister32(R_EDI);
  701. {$endif noAllocEdi}
  702. end;
  703. { release the registers }
  704. del_reference(t.reference);
  705. if freetemp then
  706. ungetiftemp(t.reference);
  707. end;
  708. else
  709. internalerror(332);
  710. end;
  711. end;
  712. procedure emit_push_lea_loc(const t:tlocation;freetemp:boolean);
  713. begin
  714. case t.loc of
  715. LOC_MEM,
  716. LOC_REFERENCE : begin
  717. if t.reference.is_immediate then
  718. internalerror(331)
  719. else
  720. begin
  721. {$ifndef noAllocEdi}
  722. getexplicitregister32(R_EDI);
  723. {$endif noAllocEdi}
  724. emit_ref_reg(A_LEA,S_L,
  725. newreference(t.reference),R_EDI);
  726. exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,R_EDI)));
  727. {$ifndef noAllocEdi}
  728. ungetregister32(R_EDI);
  729. {$endif noAllocEdi}
  730. end;
  731. if freetemp then
  732. ungetiftemp(t.reference);
  733. end;
  734. else
  735. internalerror(332);
  736. end;
  737. end;
  738. procedure emit_push_mem_size(const t: treference; size: longint);
  739. var
  740. s: topsize;
  741. begin
  742. if t.is_immediate then
  743. begin
  744. if (size=4) or
  745. (target_os.stackalignment=4) then
  746. exprasmlist^.concat(new(paicpu,op_const(A_PUSH,S_L,t.offset)))
  747. else
  748. exprasmlist^.concat(new(paicpu,op_const(A_PUSH,S_W,t.offset)));
  749. end
  750. else
  751. if size < 4 then
  752. begin
  753. getexplicitregister32(R_EDI);
  754. case size of
  755. 1: s := S_BL;
  756. 2: s := S_WL;
  757. else internalerror(200008071);
  758. end;
  759. exprasmlist^.concat(new(paicpu,op_ref_reg(A_MOVZX,s,
  760. newreference(t),R_EDI)));
  761. if target_os.stackalignment=4 then
  762. exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,R_EDI)))
  763. else
  764. exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_W,R_DI)));
  765. ungetregister32(R_EDI);
  766. end
  767. else
  768. if size = 4 then
  769. emit_push_mem(t)
  770. else
  771. internalerror(200008072);
  772. end;
  773. procedure emit_to_mem(var p:ptree);
  774. var
  775. r : treference;
  776. begin
  777. case p^.location.loc of
  778. LOC_FPU : begin
  779. reset_reference(p^.location.reference);
  780. gettempofsizereference(10,p^.location.reference);
  781. floatstore(pfloatdef(p^.resulttype)^.typ,p^.location.reference);
  782. { This can't be never a l-value! (FK)
  783. p^.location.loc:=LOC_REFERENCE; }
  784. end;
  785. LOC_REGISTER:
  786. begin
  787. if is_64bitint(p^.resulttype) then
  788. begin
  789. gettempofsizereference(8,r);
  790. emit_reg_ref(A_MOV,S_L,p^.location.registerlow,
  791. newreference(r));
  792. inc(r.offset,4);
  793. emit_reg_ref(A_MOV,S_L,p^.location.registerhigh,
  794. newreference(r));
  795. dec(r.offset,4);
  796. p^.location.reference:=r;
  797. end
  798. else
  799. internalerror(1405001);
  800. end;
  801. LOC_MEM,
  802. LOC_REFERENCE : ;
  803. LOC_CFPUREGISTER : begin
  804. emit_reg(A_FLD,S_NO,correct_fpuregister(p^.location.register,fpuvaroffset));
  805. inc(fpuvaroffset);
  806. reset_reference(p^.location.reference);
  807. gettempofsizereference(10,p^.location.reference);
  808. floatstore(pfloatdef(p^.resulttype)^.typ,p^.location.reference);
  809. { This can't be never a l-value! (FK)
  810. p^.location.loc:=LOC_REFERENCE; }
  811. end;
  812. else
  813. internalerror(333);
  814. end;
  815. p^.location.loc:=LOC_MEM;
  816. end;
  817. procedure emit_to_reg16(var hr:tregister);
  818. begin
  819. { ranges are a little bit bug sensitive ! }
  820. case hr of
  821. R_EAX,R_EBX,R_ECX,R_EDX,R_EDI,R_ESI,R_ESP,R_EBP:
  822. begin
  823. hr:=reg32toreg16(hr);
  824. end;
  825. R_AL,R_BL,R_CL,R_DL:
  826. begin
  827. hr:=reg8toreg16(hr);
  828. emit_const_reg(A_AND,S_W,$ff,hr);
  829. end;
  830. R_AH,R_BH,R_CH,R_DH:
  831. begin
  832. hr:=reg8toreg16(hr);
  833. emit_const_reg(A_AND,S_W,$ff00,hr);
  834. end;
  835. end;
  836. end;
  837. procedure emit_to_reg32(var hr:tregister);
  838. begin
  839. { ranges are a little bit bug sensitive ! }
  840. case hr of
  841. R_AX,R_BX,R_CX,R_DX,R_DI,R_SI,R_SP,R_BP:
  842. begin
  843. hr:=reg16toreg32(hr);
  844. emit_const_reg(A_AND,S_L,$ffff,hr);
  845. end;
  846. R_AL,R_BL,R_CL,R_DL:
  847. begin
  848. hr:=reg8toreg32(hr);
  849. emit_const_reg(A_AND,S_L,$ff,hr);
  850. end;
  851. R_AH,R_BH,R_CH,R_DH:
  852. begin
  853. hr:=reg8toreg32(hr);
  854. emit_const_reg(A_AND,S_L,$ff00,hr);
  855. end;
  856. end;
  857. end;
  858. procedure emit_mov_ref_reg64(r : treference;rl,rh : tregister);
  859. var
  860. hr : preference;
  861. begin
  862. { if we load a 64 bit reference, we must be careful because }
  863. { we could overwrite the registers of the reference by }
  864. { accident }
  865. if r.base=rl then
  866. begin
  867. emit_reg_reg(A_MOV,S_L,r.base,
  868. R_EDI);
  869. r.base:=R_EDI;
  870. end
  871. else if r.index=rl then
  872. begin
  873. emit_reg_reg(A_MOV,S_L,r.index,
  874. R_EDI);
  875. r.index:=R_EDI;
  876. end;
  877. emit_ref_reg(A_MOV,S_L,
  878. newreference(r),rl);
  879. hr:=newreference(r);
  880. inc(hr^.offset,4);
  881. emit_ref_reg(A_MOV,S_L,
  882. hr,rh);
  883. end;
  884. {*****************************************************************************
  885. Emit String Functions
  886. *****************************************************************************}
  887. procedure copyshortstring(const dref,sref : treference;len : byte;
  888. loadref, del_sref: boolean);
  889. begin
  890. emitpushreferenceaddr(dref);
  891. { if it's deleted right before it's used, the optimizer can move }
  892. { the reg deallocations to the right places (JM) }
  893. if del_sref then
  894. del_reference(sref);
  895. if loadref then
  896. emit_push_mem(sref)
  897. else
  898. emitpushreferenceaddr(sref);
  899. push_int(len);
  900. emitcall('FPC_SHORTSTR_COPY');
  901. maybe_loadesi;
  902. end;
  903. procedure copylongstring(const dref,sref : treference;len : longint;loadref:boolean);
  904. begin
  905. emitpushreferenceaddr(dref);
  906. if loadref then
  907. emit_push_mem(sref)
  908. else
  909. emitpushreferenceaddr(sref);
  910. push_int(len);
  911. emitcall('FPC_LONGSTR_COPY');
  912. maybe_loadesi;
  913. end;
  914. procedure incrstringref(t : pdef;const ref : treference);
  915. var
  916. pushedregs : tpushed;
  917. begin
  918. pushusedregisters(pushedregs,$ff);
  919. emitpushreferenceaddr(ref);
  920. if is_ansistring(t) then
  921. begin
  922. emitcall('FPC_ANSISTR_INCR_REF');
  923. end
  924. else if is_widestring(t) then
  925. begin
  926. emitcall('FPC_WIDESTR_INCR_REF');
  927. end
  928. else internalerror(1859);
  929. popusedregisters(pushedregs);
  930. end;
  931. procedure decrstringref(t : pdef;const ref : treference);
  932. var
  933. pushedregs : tpushed;
  934. begin
  935. pushusedregisters(pushedregs,$ff);
  936. emitpushreferenceaddr(ref);
  937. if is_ansistring(t) then
  938. begin
  939. emitcall('FPC_ANSISTR_DECR_REF');
  940. end
  941. else if is_widestring(t) then
  942. begin
  943. emitcall('FPC_WIDESTR_DECR_REF');
  944. end
  945. else internalerror(1859);
  946. popusedregisters(pushedregs);
  947. end;
  948. procedure loadansistring(p : ptree);
  949. {
  950. copies an ansistring from p^.right to p^.left, we
  951. assume, that both sides are ansistring, firstassignement have
  952. to take care of that, an ansistring can't be a register variable
  953. }
  954. var
  955. pushed : tpushed;
  956. regs_to_push: byte;
  957. ungettemp : boolean;
  958. begin
  959. { before pushing any parameter, we have to save all used }
  960. { registers, but before that we have to release the }
  961. { registers of that node to save uneccessary pushed }
  962. { so be careful, if you think you can optimize that code (FK) }
  963. { nevertheless, this has to be changed, because otherwise the }
  964. { register is released before it's contents are pushed -> }
  965. { problems with the optimizer (JM) }
  966. del_reference(p^.left^.location.reference);
  967. ungettemp:=false;
  968. { Find out which registers have to be pushed (JM) }
  969. regs_to_push := $ff;
  970. remove_non_regvars_from_loc(p^.right^.location,regs_to_push);
  971. { And push them (JM) }
  972. pushusedregisters(pushed,regs_to_push);
  973. case p^.right^.location.loc of
  974. LOC_REGISTER,LOC_CREGISTER:
  975. begin
  976. exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,p^.right^.location.register)));
  977. ungetregister32(p^.right^.location.register);
  978. end;
  979. LOC_REFERENCE,LOC_MEM:
  980. begin
  981. { First release the registers because emit_push_mem may }
  982. { load the reference in edi before pushing and then the }
  983. { dealloc is too late (and optimizations are missed (JM) }
  984. del_reference(p^.right^.location.reference);
  985. { This one doesn't need extra registers (JM) }
  986. emit_push_mem(p^.right^.location.reference);
  987. ungettemp:=true;
  988. end;
  989. end;
  990. emitpushreferenceaddr(p^.left^.location.reference);
  991. del_reference(p^.left^.location.reference);
  992. emitcall('FPC_ANSISTR_ASSIGN');
  993. maybe_loadesi;
  994. popusedregisters(pushed);
  995. if ungettemp then
  996. ungetiftemp(p^.right^.location.reference);
  997. end;
  998. {*****************************************************************************
  999. Emit Push Functions
  1000. *****************************************************************************}
  1001. function maybe_push(needed : byte;p : ptree;isint64 : boolean) : boolean;
  1002. var
  1003. pushed : boolean;
  1004. {hregister : tregister; }
  1005. {$ifdef TEMPS_NOT_PUSH}
  1006. href : treference;
  1007. {$endif TEMPS_NOT_PUSH}
  1008. begin
  1009. if needed>usablereg32 then
  1010. begin
  1011. if (p^.location.loc=LOC_REGISTER) then
  1012. begin
  1013. if isint64 then
  1014. begin
  1015. {$ifdef TEMPS_NOT_PUSH}
  1016. gettempofsizereference(href,8);
  1017. p^.temp_offset:=href.offset;
  1018. href.offset:=href.offset+4;
  1019. exprasmlist^.concat(new(paicpu,op_reg(A_MOV,S_L,p^.location.registerhigh,href)));
  1020. href.offset:=href.offset-4;
  1021. {$else TEMPS_NOT_PUSH}
  1022. exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,p^.location.registerhigh)));
  1023. {$endif TEMPS_NOT_PUSH}
  1024. ungetregister32(p^.location.registerhigh);
  1025. end
  1026. {$ifdef TEMPS_NOT_PUSH}
  1027. else
  1028. begin
  1029. gettempofsizereference(href,4);
  1030. p^.temp_offset:=href.offset;
  1031. end
  1032. {$endif TEMPS_NOT_PUSH}
  1033. ;
  1034. pushed:=true;
  1035. {$ifdef TEMPS_NOT_PUSH}
  1036. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,p^.location.register,href)));
  1037. {$else TEMPS_NOT_PUSH}
  1038. exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,p^.location.register)));
  1039. {$endif TEMPS_NOT_PUSH}
  1040. ungetregister32(p^.location.register);
  1041. end
  1042. else if (p^.location.loc in [LOC_MEM,LOC_REFERENCE]) and
  1043. ((p^.location.reference.base<>R_NO) or
  1044. (p^.location.reference.index<>R_NO)
  1045. ) then
  1046. begin
  1047. del_reference(p^.location.reference);
  1048. {$ifndef noAllocEdi}
  1049. getexplicitregister32(R_EDI);
  1050. {$endif noAllocEdi}
  1051. emit_ref_reg(A_LEA,S_L,newreference(p^.location.reference),
  1052. R_EDI);
  1053. {$ifdef TEMPS_NOT_PUSH}
  1054. gettempofsizereference(href,4);
  1055. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,R_EDI,href)));
  1056. p^.temp_offset:=href.offset;
  1057. {$else TEMPS_NOT_PUSH}
  1058. exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,R_EDI)));
  1059. {$endif TEMPS_NOT_PUSH}
  1060. {$ifndef noAllocEdi}
  1061. ungetregister32(R_EDI);
  1062. {$endif noAllocEdi}
  1063. pushed:=true;
  1064. end
  1065. else pushed:=false;
  1066. end
  1067. else pushed:=false;
  1068. maybe_push:=pushed;
  1069. end;
  1070. {$ifdef TEMPS_NOT_PUSH}
  1071. function maybe_savetotemp(needed : byte;p : ptree;isint64 : boolean) : boolean;
  1072. var
  1073. pushed : boolean;
  1074. href : treference;
  1075. begin
  1076. if needed>usablereg32 then
  1077. begin
  1078. if (p^.location.loc=LOC_REGISTER) then
  1079. begin
  1080. if isint64(p^.resulttype) then
  1081. begin
  1082. gettempofsizereference(href,8);
  1083. p^.temp_offset:=href.offset;
  1084. href.offset:=href.offset+4;
  1085. exprasmlist^.concat(new(paicpu,op_reg(A_MOV,S_L,p^.location.registerhigh,href)));
  1086. href.offset:=href.offset-4;
  1087. ungetregister32(p^.location.registerhigh);
  1088. end
  1089. else
  1090. begin
  1091. gettempofsizereference(href,4);
  1092. p^.temp_offset:=href.offset;
  1093. end;
  1094. pushed:=true;
  1095. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,p^.location.register,href)));
  1096. ungetregister32(p^.location.register);
  1097. end
  1098. else if (p^.location.loc in [LOC_MEM,LOC_REFERENCE]) and
  1099. ((p^.location.reference.base<>R_NO) or
  1100. (p^.location.reference.index<>R_NO)
  1101. ) then
  1102. begin
  1103. del_reference(p^.location.reference);
  1104. {$ifndef noAllocEdi}
  1105. getexplicitregister32(R_EDI);
  1106. {$endif noAllocEdi}
  1107. emit_ref_reg(A_LEA,S_L,newreference(p^.location.reference),
  1108. R_EDI);
  1109. gettempofsizereference(href,4);
  1110. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,R_EDI,href)));
  1111. {$ifndef noAllocEdi}
  1112. ungetregister32(R_EDI);
  1113. {$endif noAllocEdi}
  1114. p^.temp_offset:=href.offset;
  1115. pushed:=true;
  1116. end
  1117. else pushed:=false;
  1118. end
  1119. else pushed:=false;
  1120. maybe_push:=pushed;
  1121. end;
  1122. {$endif TEMPS_NOT_PUSH}
  1123. procedure push_int(l : longint);
  1124. begin
  1125. if (l = 0) and
  1126. not(aktoptprocessor in [Class386, ClassP6]) and
  1127. not(cs_littlesize in aktglobalswitches)
  1128. Then
  1129. begin
  1130. {$ifndef noAllocEdi}
  1131. getexplicitregister32(R_EDI);
  1132. {$endif noAllocEdi}
  1133. emit_reg_reg(A_XOR,S_L,R_EDI,R_EDI);
  1134. exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,R_EDI)));
  1135. {$ifndef noAllocEdi}
  1136. ungetregister32(R_EDI);
  1137. {$endif noAllocEdi}
  1138. end
  1139. else
  1140. exprasmlist^.concat(new(paicpu,op_const(A_PUSH,S_L,l)));
  1141. end;
  1142. procedure emit_push_mem(const ref : treference);
  1143. begin
  1144. if ref.is_immediate then
  1145. push_int(ref.offset)
  1146. else
  1147. begin
  1148. if not(aktoptprocessor in [Class386, ClassP6]) and
  1149. not(cs_littlesize in aktglobalswitches)
  1150. then
  1151. begin
  1152. {$ifndef noAllocEdi}
  1153. getexplicitregister32(R_EDI);
  1154. {$endif noAllocEdi}
  1155. emit_ref_reg(A_MOV,S_L,newreference(ref),R_EDI);
  1156. exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,R_EDI)));
  1157. {$ifndef noAllocEdi}
  1158. ungetregister32(R_EDI);
  1159. {$endif noAllocEdi}
  1160. end
  1161. else exprasmlist^.concat(new(paicpu,op_ref(A_PUSH,S_L,newreference(ref))));
  1162. end;
  1163. end;
  1164. procedure emitpushreferenceaddr(const ref : treference);
  1165. var
  1166. href : treference;
  1167. begin
  1168. { this will fail for references to other segments !!! }
  1169. if ref.is_immediate then
  1170. { is this right ? }
  1171. begin
  1172. { push_int(ref.offset)}
  1173. gettempofsizereference(4,href);
  1174. emit_const_ref(A_MOV,S_L,ref.offset,newreference(href));
  1175. emitpushreferenceaddr(href);
  1176. del_reference(href);
  1177. end
  1178. else
  1179. begin
  1180. if ref.segment<>R_NO then
  1181. CGMessage(cg_e_cant_use_far_pointer_there);
  1182. if (ref.base=R_NO) and (ref.index=R_NO) then
  1183. exprasmlist^.concat(new(paicpu,op_sym_ofs(A_PUSH,S_L,ref.symbol,ref.offset)))
  1184. else if (ref.base=R_NO) and (ref.index<>R_NO) and
  1185. (ref.offset=0) and (ref.scalefactor=0) and (ref.symbol=nil) then
  1186. exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,ref.index)))
  1187. else if (ref.base<>R_NO) and (ref.index=R_NO) and
  1188. (ref.offset=0) and (ref.symbol=nil) then
  1189. exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,ref.base)))
  1190. else
  1191. begin
  1192. {$ifndef noAllocEdi}
  1193. getexplicitregister32(R_EDI);
  1194. {$endif noAllocEdi}
  1195. emit_ref_reg(A_LEA,S_L,newreference(ref),R_EDI);
  1196. exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,R_EDI)));
  1197. {$ifndef noAllocEdi}
  1198. ungetregister32(R_EDI);
  1199. {$endif noAllocEdi}
  1200. end;
  1201. end;
  1202. end;
  1203. procedure pushsetelement(p : ptree);
  1204. {
  1205. copies p a set element on the stack
  1206. }
  1207. var
  1208. hr,hr16,hr32 : tregister;
  1209. begin
  1210. { copy the element on the stack, slightly complicated }
  1211. if p^.treetype=ordconstn then
  1212. begin
  1213. if target_os.stackalignment=4 then
  1214. exprasmlist^.concat(new(paicpu,op_const(A_PUSH,S_L,p^.value)))
  1215. else
  1216. exprasmlist^.concat(new(paicpu,op_const(A_PUSH,S_W,p^.value)));
  1217. end
  1218. else
  1219. begin
  1220. case p^.location.loc of
  1221. LOC_REGISTER,
  1222. LOC_CREGISTER :
  1223. begin
  1224. hr:=p^.location.register;
  1225. case hr of
  1226. R_EAX,R_EBX,R_ECX,R_EDX,R_EDI,R_ESI,R_ESP :
  1227. begin
  1228. hr16:=reg32toreg16(hr);
  1229. hr32:=hr;
  1230. end;
  1231. R_AX,R_BX,R_CX,R_DX,R_DI,R_SI,R_SP :
  1232. begin
  1233. hr16:=hr;
  1234. hr32:=reg16toreg32(hr);
  1235. end;
  1236. R_AL,R_BL,R_CL,R_DL :
  1237. begin
  1238. hr16:=reg8toreg16(hr);
  1239. hr32:=reg8toreg32(hr);
  1240. end;
  1241. end;
  1242. if target_os.stackalignment=4 then
  1243. exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,hr32)))
  1244. else
  1245. exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_W,hr16)));
  1246. ungetregister32(hr32);
  1247. end;
  1248. else
  1249. begin
  1250. { you can't push more bytes than the size of the element, }
  1251. { because this may cross a page boundary and you'll get a }
  1252. { sigsegv (JM) }
  1253. emit_push_mem_size(p^.location.reference,1);
  1254. del_reference(p^.location.reference);
  1255. end;
  1256. end;
  1257. end;
  1258. end;
  1259. procedure restore(p : ptree;isint64 : boolean);
  1260. var
  1261. hregister : tregister;
  1262. {$ifdef TEMPS_NOT_PUSH}
  1263. href : treference;
  1264. {$endif TEMPS_NOT_PUSH}
  1265. begin
  1266. hregister:=getregister32;
  1267. {$ifdef TEMPS_NOT_PUSH}
  1268. reset_reference(href);
  1269. href.base:=procinfo^.frame_pointer;
  1270. href.offset:=p^.temp_offset;
  1271. emit_ref_reg(A_MOV,S_L,href,hregister);
  1272. {$else TEMPS_NOT_PUSH}
  1273. exprasmlist^.concat(new(paicpu,op_reg(A_POP,S_L,hregister)));
  1274. {$endif TEMPS_NOT_PUSH}
  1275. if (p^.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  1276. begin
  1277. p^.location.register:=hregister;
  1278. if isint64 then
  1279. begin
  1280. p^.location.registerhigh:=getregister32;
  1281. {$ifdef TEMPS_NOT_PUSH}
  1282. href.offset:=p^.temp_offset+4;
  1283. emit_ref_reg(A_MOV,S_L,p^.location.registerhigh);
  1284. { set correctly for release ! }
  1285. href.offset:=p^.temp_offset;
  1286. {$else TEMPS_NOT_PUSH}
  1287. exprasmlist^.concat(new(paicpu,op_reg(A_POP,S_L,p^.location.registerhigh)));
  1288. {$endif TEMPS_NOT_PUSH}
  1289. end;
  1290. end
  1291. else
  1292. begin
  1293. reset_reference(p^.location.reference);
  1294. { any reasons why this was moved into the index register ? }
  1295. { normally usage of base register is much better (FK) }
  1296. p^.location.reference.base:=hregister;
  1297. { Why is this done? We can never be sure about p^.left
  1298. because otherwise secondload fails !!!
  1299. set_location(p^.left^.location,p^.location);}
  1300. end;
  1301. {$ifdef TEMPS_NOT_PUSH}
  1302. ungetiftemp(href);
  1303. {$endif TEMPS_NOT_PUSH}
  1304. end;
  1305. {$ifdef TEMPS_NOT_PUSH}
  1306. procedure restorefromtemp(p : ptree;isint64 : boolean);
  1307. var
  1308. hregister : tregister;
  1309. href : treference;
  1310. begin
  1311. hregister:=getregister32;
  1312. reset_reference(href);
  1313. href.base:=procinfo^.frame_pointer;
  1314. href.offset:=p^.temp_offset;
  1315. emit_ref_reg(A_MOV,S_L,href,hregister);
  1316. if (p^.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  1317. begin
  1318. p^.location.register:=hregister;
  1319. if isint64 then
  1320. begin
  1321. p^.location.registerhigh:=getregister32;
  1322. href.offset:=p^.temp_offset+4;
  1323. emit_ref_reg(A_MOV,S_L,p^.location.registerhigh);
  1324. { set correctly for release ! }
  1325. href.offset:=p^.temp_offset;
  1326. end;
  1327. end
  1328. else
  1329. begin
  1330. reset_reference(p^.location.reference);
  1331. p^.location.reference.base:=hregister;
  1332. { Why is this done? We can never be sure about p^.left
  1333. because otherwise secondload fails PM
  1334. set_location(p^.left^.location,p^.location);}
  1335. end;
  1336. ungetiftemp(href);
  1337. end;
  1338. {$endif TEMPS_NOT_PUSH}
  1339. procedure push_value_para(p:ptree;inlined,is_cdecl:boolean;
  1340. para_offset:longint;alignment : longint);
  1341. var
  1342. tempreference : treference;
  1343. r : preference;
  1344. opsize : topsize;
  1345. op : tasmop;
  1346. hreg : tregister;
  1347. size : longint;
  1348. hlabel : pasmlabel;
  1349. begin
  1350. case p^.location.loc of
  1351. LOC_REGISTER,
  1352. LOC_CREGISTER:
  1353. begin
  1354. case p^.location.register of
  1355. R_EAX,R_EBX,R_ECX,R_EDX,R_ESI,
  1356. R_EDI,R_ESP,R_EBP :
  1357. begin
  1358. if p^.resulttype^.size=8 then
  1359. begin
  1360. inc(pushedparasize,8);
  1361. if inlined then
  1362. begin
  1363. r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
  1364. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,
  1365. p^.location.registerlow,r)));
  1366. r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize+4);
  1367. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,
  1368. p^.location.registerhigh,r)));
  1369. end
  1370. else
  1371. exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,p^.location.registerhigh)));
  1372. ungetregister32(p^.location.registerhigh);
  1373. exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,p^.location.registerlow)));
  1374. ungetregister32(p^.location.registerlow);
  1375. end
  1376. else
  1377. begin
  1378. inc(pushedparasize,4);
  1379. if inlined then
  1380. begin
  1381. r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
  1382. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,
  1383. p^.location.register,r)));
  1384. end
  1385. else
  1386. exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,p^.location.register)));
  1387. ungetregister32(p^.location.register);
  1388. end;
  1389. end;
  1390. R_AX,R_BX,R_CX,R_DX,R_SI,R_DI:
  1391. begin
  1392. if alignment=4 then
  1393. begin
  1394. opsize:=S_L;
  1395. hreg:=reg16toreg32(p^.location.register);
  1396. inc(pushedparasize,4);
  1397. end
  1398. else
  1399. begin
  1400. opsize:=S_W;
  1401. hreg:=p^.location.register;
  1402. inc(pushedparasize,2);
  1403. end;
  1404. if inlined then
  1405. begin
  1406. r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
  1407. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,opsize,hreg,r)));
  1408. end
  1409. else
  1410. exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,opsize,hreg)));
  1411. ungetregister32(reg16toreg32(p^.location.register));
  1412. end;
  1413. R_AL,R_BL,R_CL,R_DL:
  1414. begin
  1415. if alignment=4 then
  1416. begin
  1417. opsize:=S_L;
  1418. hreg:=reg8toreg32(p^.location.register);
  1419. inc(pushedparasize,4);
  1420. end
  1421. else
  1422. begin
  1423. opsize:=S_W;
  1424. hreg:=reg8toreg16(p^.location.register);
  1425. inc(pushedparasize,2);
  1426. end;
  1427. { we must push always 16 bit }
  1428. if inlined then
  1429. begin
  1430. r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
  1431. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,opsize,hreg,r)));
  1432. end
  1433. else
  1434. exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,opsize,hreg)));
  1435. ungetregister32(reg8toreg32(p^.location.register));
  1436. end;
  1437. else internalerror(1899);
  1438. end;
  1439. end;
  1440. LOC_FPU:
  1441. begin
  1442. size:=align(pfloatdef(p^.resulttype)^.size,alignment);
  1443. inc(pushedparasize,size);
  1444. if not inlined then
  1445. emit_const_reg(A_SUB,S_L,size,R_ESP);
  1446. {$ifdef GDB}
  1447. if (cs_debuginfo in aktmoduleswitches) and
  1448. (exprasmlist^.first=exprasmlist^.last) then
  1449. exprasmlist^.concat(new(pai_force_line,init));
  1450. {$endif GDB}
  1451. r:=new_reference(R_ESP,0);
  1452. floatstoreops(pfloatdef(p^.resulttype)^.typ,op,opsize);
  1453. { this is the easiest case for inlined !! }
  1454. if inlined then
  1455. begin
  1456. r^.base:=procinfo^.framepointer;
  1457. r^.offset:=para_offset-pushedparasize;
  1458. end;
  1459. exprasmlist^.concat(new(paicpu,op_ref(op,opsize,r)));
  1460. dec(fpuvaroffset);
  1461. end;
  1462. LOC_CFPUREGISTER:
  1463. begin
  1464. exprasmlist^.concat(new(paicpu,op_reg(A_FLD,S_NO,
  1465. correct_fpuregister(p^.location.register,fpuvaroffset))));
  1466. size:=align(pfloatdef(p^.resulttype)^.size,alignment);
  1467. inc(pushedparasize,size);
  1468. if not inlined then
  1469. emit_const_reg(A_SUB,S_L,size,R_ESP);
  1470. {$ifdef GDB}
  1471. if (cs_debuginfo in aktmoduleswitches) and
  1472. (exprasmlist^.first=exprasmlist^.last) then
  1473. exprasmlist^.concat(new(pai_force_line,init));
  1474. {$endif GDB}
  1475. r:=new_reference(R_ESP,0);
  1476. floatstoreops(pfloatdef(p^.resulttype)^.typ,op,opsize);
  1477. { this is the easiest case for inlined !! }
  1478. if inlined then
  1479. begin
  1480. r^.base:=procinfo^.framepointer;
  1481. r^.offset:=para_offset-pushedparasize;
  1482. end;
  1483. exprasmlist^.concat(new(paicpu,op_ref(op,opsize,r)));
  1484. end;
  1485. LOC_REFERENCE,LOC_MEM:
  1486. begin
  1487. tempreference:=p^.location.reference;
  1488. del_reference(p^.location.reference);
  1489. case p^.resulttype^.deftype of
  1490. enumdef,
  1491. orddef :
  1492. begin
  1493. case p^.resulttype^.size of
  1494. 8 : begin
  1495. inc(pushedparasize,8);
  1496. if inlined then
  1497. begin
  1498. {$ifndef noAllocEdi}
  1499. getexplicitregister32(R_EDI);
  1500. {$endif noAllocEdi}
  1501. emit_ref_reg(A_MOV,S_L,
  1502. newreference(tempreference),R_EDI);
  1503. r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
  1504. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,R_EDI,r)));
  1505. {$ifndef noAllocEdi}
  1506. ungetregister32(R_EDI);
  1507. getexplicitregister32(R_EDI);
  1508. {$endif noAllocEdi}
  1509. inc(tempreference.offset,4);
  1510. emit_ref_reg(A_MOV,S_L,
  1511. newreference(tempreference),R_EDI);
  1512. r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize+4);
  1513. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,R_EDI,r)));
  1514. {$ifndef noAllocEdi}
  1515. ungetregister32(R_EDI);
  1516. {$endif noAllocEdi}
  1517. end
  1518. else
  1519. begin
  1520. inc(tempreference.offset,4);
  1521. emit_push_mem(tempreference);
  1522. dec(tempreference.offset,4);
  1523. emit_push_mem(tempreference);
  1524. end;
  1525. end;
  1526. 4 : begin
  1527. inc(pushedparasize,4);
  1528. if inlined then
  1529. begin
  1530. {$ifndef noAllocEdi}
  1531. getexplicitregister32(R_EDI);
  1532. {$endif noAllocEdi}
  1533. emit_ref_reg(A_MOV,S_L,
  1534. newreference(tempreference),R_EDI);
  1535. r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
  1536. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,R_EDI,r)));
  1537. {$ifndef noAllocEdi}
  1538. ungetregister32(R_EDI);
  1539. {$endif noAllocEdi}
  1540. end
  1541. else
  1542. emit_push_mem(tempreference);
  1543. end;
  1544. 1,2 : begin
  1545. if alignment=4 then
  1546. begin
  1547. opsize:=S_L;
  1548. hreg:=R_EDI;
  1549. inc(pushedparasize,4);
  1550. end
  1551. else
  1552. begin
  1553. opsize:=S_W;
  1554. hreg:=R_DI;
  1555. inc(pushedparasize,2);
  1556. end;
  1557. if inlined then
  1558. begin
  1559. {$ifndef noAllocEdi}
  1560. getexplicitregister32(R_EDI);
  1561. {$endif noAllocEdi}
  1562. emit_ref_reg(A_MOV,opsize,
  1563. newreference(tempreference),hreg);
  1564. r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
  1565. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,opsize,hreg,r)));
  1566. {$ifndef noAllocEdi}
  1567. ungetregister32(R_EDI);
  1568. {$endif noAllocEdi}
  1569. end
  1570. else
  1571. emit_push_mem_size(tempreference,p^.resulttype^.size);
  1572. end;
  1573. else
  1574. internalerror(234231);
  1575. end;
  1576. end;
  1577. floatdef :
  1578. begin
  1579. case pfloatdef(p^.resulttype)^.typ of
  1580. f32bit,
  1581. s32real :
  1582. begin
  1583. inc(pushedparasize,4);
  1584. if inlined then
  1585. begin
  1586. {$ifndef noAllocEdi}
  1587. getexplicitregister32(R_EDI);
  1588. {$endif noAllocEdi}
  1589. emit_ref_reg(A_MOV,S_L,
  1590. newreference(tempreference),R_EDI);
  1591. r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
  1592. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,R_EDI,r)));
  1593. {$ifndef noAllocEdi}
  1594. ungetregister32(R_EDI);
  1595. {$endif noAllocEdi}
  1596. end
  1597. else
  1598. emit_push_mem(tempreference);
  1599. end;
  1600. s64real,
  1601. s64comp :
  1602. begin
  1603. inc(pushedparasize,4);
  1604. inc(tempreference.offset,4);
  1605. if inlined then
  1606. begin
  1607. {$ifndef noAllocEdi}
  1608. getexplicitregister32(R_EDI);
  1609. {$endif noAllocEdi}
  1610. emit_ref_reg(A_MOV,S_L,
  1611. newreference(tempreference),R_EDI);
  1612. r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
  1613. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,R_EDI,r)));
  1614. {$ifndef noAllocEdi}
  1615. ungetregister32(R_EDI);
  1616. {$endif noAllocEdi}
  1617. end
  1618. else
  1619. emit_push_mem(tempreference);
  1620. inc(pushedparasize,4);
  1621. dec(tempreference.offset,4);
  1622. if inlined then
  1623. begin
  1624. {$ifndef noAllocEdi}
  1625. getexplicitregister32(R_EDI);
  1626. {$endif noAllocEdi}
  1627. emit_ref_reg(A_MOV,S_L,
  1628. newreference(tempreference),R_EDI);
  1629. r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
  1630. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,R_EDI,r)));
  1631. {$ifndef noAllocEdi}
  1632. ungetregister32(R_EDI);
  1633. {$endif noAllocEdi}
  1634. end
  1635. else
  1636. emit_push_mem(tempreference);
  1637. end;
  1638. s80real :
  1639. begin
  1640. inc(pushedparasize,4);
  1641. if alignment=4 then
  1642. inc(tempreference.offset,8)
  1643. else
  1644. inc(tempreference.offset,6);
  1645. if inlined then
  1646. begin
  1647. {$ifndef noAllocEdi}
  1648. getexplicitregister32(R_EDI);
  1649. {$endif noAllocEdi}
  1650. emit_ref_reg(A_MOV,S_L,
  1651. newreference(tempreference),R_EDI);
  1652. r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
  1653. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,R_EDI,r)));
  1654. {$ifndef noAllocEdi}
  1655. ungetregister32(R_EDI);
  1656. {$endif noAllocEdi}
  1657. end
  1658. else
  1659. emit_push_mem(tempreference);
  1660. dec(tempreference.offset,4);
  1661. inc(pushedparasize,4);
  1662. if inlined then
  1663. begin
  1664. {$ifndef noAllocEdi}
  1665. getexplicitregister32(R_EDI);
  1666. {$endif noAllocEdi}
  1667. emit_ref_reg(A_MOV,S_L,
  1668. newreference(tempreference),R_EDI);
  1669. r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
  1670. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,R_EDI,r)));
  1671. {$ifndef noAllocEdi}
  1672. ungetregister32(R_EDI);
  1673. {$endif noAllocEdi}
  1674. end
  1675. else
  1676. emit_push_mem(tempreference);
  1677. if alignment=4 then
  1678. begin
  1679. opsize:=S_L;
  1680. hreg:=R_EDI;
  1681. inc(pushedparasize,4);
  1682. dec(tempreference.offset,4);
  1683. end
  1684. else
  1685. begin
  1686. opsize:=S_W;
  1687. hreg:=R_DI;
  1688. inc(pushedparasize,2);
  1689. dec(tempreference.offset,2);
  1690. end;
  1691. if inlined then
  1692. begin
  1693. {$ifndef noAllocEdi}
  1694. getexplicitregister32(R_EDI);
  1695. {$endif noAllocEdi}
  1696. emit_ref_reg(A_MOV,opsize,
  1697. newreference(tempreference),hreg);
  1698. r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
  1699. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,opsize,hreg,r)));
  1700. {$ifndef noAllocEdi}
  1701. ungetregister32(R_EDI);
  1702. {$endif noAllocEdi}
  1703. end
  1704. else
  1705. exprasmlist^.concat(new(paicpu,op_ref(A_PUSH,opsize,
  1706. newreference(tempreference))));
  1707. end;
  1708. end;
  1709. end;
  1710. pointerdef,
  1711. procvardef,
  1712. classrefdef:
  1713. begin
  1714. inc(pushedparasize,4);
  1715. if inlined then
  1716. begin
  1717. {$ifndef noAllocEdi}
  1718. getexplicitregister32(R_EDI);
  1719. {$endif noAllocEdi}
  1720. emit_ref_reg(A_MOV,S_L,
  1721. newreference(tempreference),R_EDI);
  1722. r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
  1723. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,R_EDI,r)));
  1724. {$ifndef noAllocEdi}
  1725. ungetregister32(R_EDI);
  1726. {$endif noAllocEdi}
  1727. end
  1728. else
  1729. emit_push_mem(tempreference);
  1730. end;
  1731. arraydef,
  1732. recorddef,
  1733. stringdef,
  1734. setdef,
  1735. objectdef :
  1736. begin
  1737. { even some structured types are 32 bit }
  1738. if is_widestring(p^.resulttype) or
  1739. is_ansistring(p^.resulttype) or
  1740. is_smallset(p^.resulttype) or
  1741. ((p^.resulttype^.deftype in [recorddef,arraydef]) and (p^.resulttype^.size<=4)
  1742. and ((p^.resulttype^.deftype<>arraydef) or not
  1743. (parraydef(p^.resulttype)^.IsConstructor or
  1744. parraydef(p^.resulttype)^.isArrayOfConst or
  1745. is_open_array(p^.resulttype)))
  1746. ) or
  1747. ((p^.resulttype^.deftype=objectdef) and
  1748. pobjectdef(p^.resulttype)^.is_class) then
  1749. begin
  1750. if (p^.resulttype^.size>2) or
  1751. ((alignment=4) and (p^.resulttype^.size>0)) then
  1752. begin
  1753. inc(pushedparasize,4);
  1754. if inlined then
  1755. begin
  1756. r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
  1757. concatcopy(tempreference,r^,4,false,false);
  1758. end
  1759. else
  1760. emit_push_mem(tempreference);
  1761. end
  1762. else
  1763. begin
  1764. if p^.resulttype^.size>0 then
  1765. begin
  1766. inc(pushedparasize,2);
  1767. if inlined then
  1768. begin
  1769. r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
  1770. concatcopy(tempreference,r^,2,false,false);
  1771. end
  1772. else
  1773. exprasmlist^.concat(new(paicpu,op_ref(A_PUSH,S_W,newreference(tempreference))));
  1774. end;
  1775. end;
  1776. end
  1777. { call by value open array ? }
  1778. else if is_cdecl then
  1779. begin
  1780. { push on stack }
  1781. size:=align(p^.resulttype^.size,alignment);
  1782. inc(pushedparasize,size);
  1783. emit_const_reg(A_SUB,S_L,size,R_ESP);
  1784. r:=new_reference(R_ESP,0);
  1785. concatcopy(tempreference,r^,size,false,false);
  1786. end
  1787. else
  1788. internalerror(8954);
  1789. end;
  1790. else
  1791. CGMessage(cg_e_illegal_expression);
  1792. end;
  1793. end;
  1794. LOC_JUMP:
  1795. begin
  1796. getlabel(hlabel);
  1797. if alignment=4 then
  1798. begin
  1799. opsize:=S_L;
  1800. inc(pushedparasize,4);
  1801. end
  1802. else
  1803. begin
  1804. opsize:=S_W;
  1805. inc(pushedparasize,2);
  1806. end;
  1807. emitlab(truelabel);
  1808. if inlined then
  1809. begin
  1810. r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
  1811. emit_const_ref(A_MOV,opsize,1,r);
  1812. end
  1813. else
  1814. exprasmlist^.concat(new(paicpu,op_const(A_PUSH,opsize,1)));
  1815. emitjmp(C_None,hlabel);
  1816. emitlab(falselabel);
  1817. if inlined then
  1818. begin
  1819. r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
  1820. emit_const_ref(A_MOV,opsize,0,r);
  1821. end
  1822. else
  1823. exprasmlist^.concat(new(paicpu,op_const(A_PUSH,opsize,0)));
  1824. emitlab(hlabel);
  1825. end;
  1826. LOC_FLAGS:
  1827. begin
  1828. if not(R_EAX in unused) then
  1829. begin
  1830. {$ifndef noAllocEdi}
  1831. getexplicitregister32(R_EDI);
  1832. {$endif noAllocEdi}
  1833. emit_reg_reg(A_MOV,S_L,R_EAX,R_EDI);
  1834. end;
  1835. emit_flag2reg(p^.location.resflags,R_AL);
  1836. emit_reg_reg(A_MOVZX,S_BW,R_AL,R_AX);
  1837. if alignment=4 then
  1838. begin
  1839. opsize:=S_L;
  1840. hreg:=R_EAX;
  1841. inc(pushedparasize,4);
  1842. end
  1843. else
  1844. begin
  1845. opsize:=S_W;
  1846. hreg:=R_AX;
  1847. inc(pushedparasize,2);
  1848. end;
  1849. if inlined then
  1850. begin
  1851. r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
  1852. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,opsize,hreg,r)));
  1853. end
  1854. else
  1855. exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,opsize,hreg)));
  1856. if not(R_EAX in unused) then
  1857. begin
  1858. emit_reg_reg(A_MOV,S_L,R_EDI,R_EAX);
  1859. {$ifndef noAllocEdi}
  1860. ungetregister32(R_EDI);
  1861. {$endif noAllocEdi}
  1862. end;
  1863. end;
  1864. {$ifdef SUPPORT_MMX}
  1865. LOC_MMXREGISTER,
  1866. LOC_CMMXREGISTER:
  1867. begin
  1868. inc(pushedparasize,8); { was missing !!! (PM) }
  1869. emit_const_reg(
  1870. A_SUB,S_L,8,R_ESP);
  1871. {$ifdef GDB}
  1872. if (cs_debuginfo in aktmoduleswitches) and
  1873. (exprasmlist^.first=exprasmlist^.last) then
  1874. exprasmlist^.concat(new(pai_force_line,init));
  1875. {$endif GDB}
  1876. if inlined then
  1877. begin
  1878. r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
  1879. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOVQ,S_NO,
  1880. p^.location.register,r)));
  1881. end
  1882. else
  1883. begin
  1884. r:=new_reference(R_ESP,0);
  1885. exprasmlist^.concat(new(paicpu,op_reg_ref(
  1886. A_MOVQ,S_NO,p^.location.register,r)));
  1887. end;
  1888. end;
  1889. {$endif SUPPORT_MMX}
  1890. end;
  1891. end;
  1892. {*****************************************************************************
  1893. Emit Float Functions
  1894. *****************************************************************************}
  1895. procedure floatloadops(t : tfloattype;var op : tasmop;var s : topsize);
  1896. begin
  1897. case t of
  1898. s32real : begin
  1899. op:=A_FLD;
  1900. s:=S_FS;
  1901. end;
  1902. s64real : begin
  1903. op:=A_FLD;
  1904. { ???? }
  1905. s:=S_FL;
  1906. end;
  1907. s80real : begin
  1908. op:=A_FLD;
  1909. s:=S_FX;
  1910. end;
  1911. s64comp : begin
  1912. op:=A_FILD;
  1913. s:=S_IQ;
  1914. end;
  1915. else internalerror(17);
  1916. end;
  1917. end;
  1918. procedure floatload(t : tfloattype;const ref : treference);
  1919. var
  1920. op : tasmop;
  1921. s : topsize;
  1922. begin
  1923. floatloadops(t,op,s);
  1924. exprasmlist^.concat(new(paicpu,op_ref(op,s,
  1925. newreference(ref))));
  1926. inc(fpuvaroffset);
  1927. end;
  1928. procedure floatstoreops(t : tfloattype;var op : tasmop;var s : topsize);
  1929. begin
  1930. case t of
  1931. s32real : begin
  1932. op:=A_FSTP;
  1933. s:=S_FS;
  1934. end;
  1935. s64real : begin
  1936. op:=A_FSTP;
  1937. s:=S_FL;
  1938. end;
  1939. s80real : begin
  1940. op:=A_FSTP;
  1941. s:=S_FX;
  1942. end;
  1943. s64comp : begin
  1944. op:=A_FISTP;
  1945. s:=S_IQ;
  1946. end;
  1947. else
  1948. internalerror(17);
  1949. end;
  1950. end;
  1951. procedure floatstore(t : tfloattype;const ref : treference);
  1952. var
  1953. op : tasmop;
  1954. s : topsize;
  1955. begin
  1956. floatstoreops(t,op,s);
  1957. exprasmlist^.concat(new(paicpu,op_ref(op,s,
  1958. newreference(ref))));
  1959. dec(fpuvaroffset);
  1960. end;
  1961. {*****************************************************************************
  1962. Emit Functions
  1963. *****************************************************************************}
  1964. procedure maketojumpbool(p : ptree);
  1965. {
  1966. produces jumps to true respectively false labels using boolean expressions
  1967. }
  1968. var
  1969. opsize : topsize;
  1970. storepos : tfileposinfo;
  1971. begin
  1972. if p^.error then
  1973. exit;
  1974. storepos:=aktfilepos;
  1975. aktfilepos:=p^.fileinfo;
  1976. if is_boolean(p^.resulttype) then
  1977. begin
  1978. if is_constboolnode(p) then
  1979. begin
  1980. if p^.value<>0 then
  1981. emitjmp(C_None,truelabel)
  1982. else
  1983. emitjmp(C_None,falselabel);
  1984. end
  1985. else
  1986. begin
  1987. opsize:=def_opsize(p^.resulttype);
  1988. case p^.location.loc of
  1989. LOC_CREGISTER,LOC_REGISTER : begin
  1990. emit_reg_reg(A_OR,opsize,p^.location.register,
  1991. p^.location.register);
  1992. ungetregister(p^.location.register);
  1993. emitjmp(C_NZ,truelabel);
  1994. emitjmp(C_None,falselabel);
  1995. end;
  1996. LOC_MEM,LOC_REFERENCE : begin
  1997. emit_const_ref(
  1998. A_CMP,opsize,0,newreference(p^.location.reference));
  1999. del_reference(p^.location.reference);
  2000. emitjmp(C_NZ,truelabel);
  2001. emitjmp(C_None,falselabel);
  2002. end;
  2003. LOC_FLAGS : begin
  2004. emitjmp(flag_2_cond[p^.location.resflags],truelabel);
  2005. emitjmp(C_None,falselabel);
  2006. end;
  2007. end;
  2008. end;
  2009. end
  2010. else
  2011. CGMessage(type_e_mismatch);
  2012. aktfilepos:=storepos;
  2013. end;
  2014. { produces if necessary overflowcode }
  2015. procedure emitoverflowcheck(p:ptree);
  2016. var
  2017. hl : pasmlabel;
  2018. begin
  2019. if not(cs_check_overflow in aktlocalswitches) then
  2020. exit;
  2021. getlabel(hl);
  2022. if not ((p^.resulttype^.deftype=pointerdef) or
  2023. ((p^.resulttype^.deftype=orddef) and
  2024. (porddef(p^.resulttype)^.typ in [u64bit,u16bit,u32bit,u8bit,uchar,
  2025. bool8bit,bool16bit,bool32bit]))) then
  2026. emitjmp(C_NO,hl)
  2027. else
  2028. emitjmp(C_NB,hl);
  2029. emitcall('FPC_OVERFLOW');
  2030. emitlab(hl);
  2031. end;
  2032. { produces range check code, while one of the operands is a 64 bit
  2033. integer }
  2034. procedure emitrangecheck64(p : ptree;todef : pdef);
  2035. begin
  2036. CGMessage(cg_w_64bit_range_check_not_supported);
  2037. {internalerror(28699);}
  2038. end;
  2039. { produces if necessary rangecheckcode }
  2040. procedure emitrangecheck(p:ptree;todef:pdef);
  2041. {
  2042. generate range checking code for the value at location t. The
  2043. type used is the checked against todefs ranges. fromdef (p.resulttype)
  2044. is the original type used at that location, when both defs are
  2045. equal the check is also insert (needed for succ,pref,inc,dec)
  2046. }
  2047. var
  2048. neglabel,
  2049. poslabel : pasmlabel;
  2050. href : treference;
  2051. rstr : string;
  2052. hreg : tregister;
  2053. opsize : topsize;
  2054. op : tasmop;
  2055. fromdef : pdef;
  2056. lto,hto,
  2057. lfrom,hfrom : longint;
  2058. doublebound,
  2059. is_reg,
  2060. popecx : boolean;
  2061. begin
  2062. { range checking on and range checkable value? }
  2063. if not(cs_check_range in aktlocalswitches) or
  2064. not(todef^.deftype in [orddef,enumdef,arraydef]) then
  2065. exit;
  2066. { only check when assigning to scalar, subranges are different,
  2067. when todef=fromdef then the check is always generated }
  2068. fromdef:=p^.resulttype;
  2069. if is_64bitint(fromdef) or is_64bitint(todef) then
  2070. begin
  2071. emitrangecheck64(p,todef);
  2072. exit;
  2073. end;
  2074. {we also need lto and hto when checking if we need to use doublebound!
  2075. (JM)}
  2076. getrange(todef,lto,hto);
  2077. if todef<>fromdef then
  2078. begin
  2079. getrange(p^.resulttype,lfrom,hfrom);
  2080. { first check for not being u32bit, then if the to is bigger than
  2081. from }
  2082. if (lto<hto) and (lfrom<hfrom) and
  2083. (lto<=lfrom) and (hto>=hfrom) then
  2084. exit;
  2085. end;
  2086. { generate the rangecheck code for the def where we are going to
  2087. store the result }
  2088. doublebound:=false;
  2089. case todef^.deftype of
  2090. orddef :
  2091. begin
  2092. porddef(todef)^.genrangecheck;
  2093. rstr:=porddef(todef)^.getrangecheckstring;
  2094. doublebound:=(porddef(todef)^.typ=u32bit) and (lto>hto);
  2095. end;
  2096. enumdef :
  2097. begin
  2098. penumdef(todef)^.genrangecheck;
  2099. rstr:=penumdef(todef)^.getrangecheckstring;
  2100. end;
  2101. arraydef :
  2102. begin
  2103. parraydef(todef)^.genrangecheck;
  2104. rstr:=parraydef(todef)^.getrangecheckstring;
  2105. doublebound:=(lto>hto);
  2106. end;
  2107. end;
  2108. { get op and opsize }
  2109. opsize:=def2def_opsize(fromdef,u32bitdef);
  2110. if opsize in [S_B,S_W,S_L] then
  2111. op:=A_MOV
  2112. else
  2113. if is_signed(fromdef) then
  2114. op:=A_MOVSX
  2115. else
  2116. op:=A_MOVZX;
  2117. is_reg:=(p^.location.loc in [LOC_REGISTER,LOC_CREGISTER]);
  2118. if is_reg then
  2119. hreg:=p^.location.register;
  2120. if not target_os.use_bound_instruction then
  2121. begin
  2122. { FPC_BOUNDCHECK needs to be called with
  2123. %ecx - value
  2124. %edi - pointer to the ranges }
  2125. popecx:=false;
  2126. if not(is_reg) or
  2127. (p^.location.register<>R_ECX) then
  2128. begin
  2129. if not(R_ECX in unused) then
  2130. begin
  2131. exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,R_ECX)));
  2132. popecx:=true;
  2133. end
  2134. else exprasmlist^.concat(new(pairegalloc,alloc(R_ECX)));
  2135. if is_reg then
  2136. emit_reg_reg(op,opsize,p^.location.register,R_ECX)
  2137. else
  2138. emit_ref_reg(op,opsize,newreference(p^.location.reference),R_ECX);
  2139. end;
  2140. if doublebound then
  2141. begin
  2142. getlabel(neglabel);
  2143. getlabel(poslabel);
  2144. emit_reg_reg(A_OR,S_L,R_ECX,R_ECX);
  2145. emitjmp(C_L,neglabel);
  2146. end;
  2147. { insert bound instruction only }
  2148. {$ifndef noAllocEdi}
  2149. getexplicitregister32(R_EDI);
  2150. {$endif noAllocEdi}
  2151. exprasmlist^.concat(new(paicpu,op_sym_ofs_reg(A_MOV,S_L,newasmsymbol(rstr),0,R_EDI)));
  2152. emitcall('FPC_BOUNDCHECK');
  2153. {$ifndef noAllocEdi}
  2154. ungetregister32(R_EDI);
  2155. {$endif noAllocEdi}
  2156. { u32bit needs 2 checks }
  2157. if doublebound then
  2158. begin
  2159. emitjmp(C_None,poslabel);
  2160. emitlab(neglabel);
  2161. {$ifndef noAllocEdi}
  2162. getexplicitregister32(R_EDI);
  2163. {$endif noAllocEdi}
  2164. exprasmlist^.concat(new(paicpu,op_sym_ofs_reg(A_MOV,S_L,newasmsymbol(rstr),8,R_EDI)));
  2165. emitcall('FPC_BOUNDCHECK');
  2166. {$ifndef noAllocEdi}
  2167. ungetregister32(R_EDI);
  2168. {$endif noAllocEdi}
  2169. emitlab(poslabel);
  2170. end;
  2171. if popecx then
  2172. exprasmlist^.concat(new(paicpu,op_reg(A_POP,S_L,R_ECX)))
  2173. else exprasmlist^.concat(new(pairegalloc,dealloc(R_ECX)));
  2174. end
  2175. else
  2176. begin
  2177. reset_reference(href);
  2178. href.symbol:=newasmsymbol(rstr);
  2179. { load the value in a register }
  2180. if is_reg then
  2181. begin
  2182. { be sure that hreg is a 32 bit reg, if not load it in %edi }
  2183. if p^.location.register in [R_EAX..R_EDI] then
  2184. hreg:=p^.location.register
  2185. else
  2186. begin
  2187. {$ifndef noAllocEdi}
  2188. getexplicitregister32(R_EDI);
  2189. {$endif noAllocEdi}
  2190. emit_reg_reg(op,opsize,p^.location.register,R_EDI);
  2191. hreg:=R_EDI;
  2192. end;
  2193. end
  2194. else
  2195. begin
  2196. {$ifndef noAllocEdi}
  2197. getexplicitregister32(R_EDI);
  2198. {$endif noAllocEdi}
  2199. emit_ref_reg(op,opsize,newreference(p^.location.reference),R_EDI);
  2200. hreg:=R_EDI;
  2201. end;
  2202. if doublebound then
  2203. begin
  2204. getlabel(neglabel);
  2205. getlabel(poslabel);
  2206. emit_reg_reg(A_TEST,S_L,hreg,hreg);
  2207. emitjmp(C_L,neglabel);
  2208. end;
  2209. { insert bound instruction only }
  2210. exprasmlist^.concat(new(paicpu,op_reg_ref(A_BOUND,S_L,hreg,newreference(href))));
  2211. { u32bit needs 2 checks }
  2212. if doublebound then
  2213. begin
  2214. href.offset:=8;
  2215. emitjmp(C_None,poslabel);
  2216. emitlab(neglabel);
  2217. exprasmlist^.concat(new(paicpu,op_reg_ref(A_BOUND,S_L,hreg,newreference(href))));
  2218. emitlab(poslabel);
  2219. end;
  2220. {$ifndef noAllocEdi}
  2221. if hreg = R_EDI then
  2222. ungetregister32(R_EDI);
  2223. {$endif noAllocEdi}
  2224. end;
  2225. end;
  2226. procedure concatcopy(source,dest : treference;size : longint;delsource,loadref : boolean);
  2227. const
  2228. isizes : array[0..3] of topsize=(S_L,S_B,S_W,S_B);
  2229. ishr : array[0..3] of byte=(2,0,1,0);
  2230. var
  2231. ecxpushed : boolean;
  2232. helpsize : longint;
  2233. i : byte;
  2234. reg8,reg32 : tregister;
  2235. swap : boolean;
  2236. procedure maybepushecx;
  2237. begin
  2238. if not(R_ECX in unused) then
  2239. begin
  2240. exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,R_ECX)));
  2241. ecxpushed:=true;
  2242. end
  2243. else getexplicitregister32(R_ECX);
  2244. end;
  2245. begin
  2246. {$IfNDef regallocfix}
  2247. If delsource then
  2248. del_reference(source);
  2249. {$EndIf regallocfix}
  2250. if (not loadref) and
  2251. ((size<=8) or
  2252. (not(cs_littlesize in aktglobalswitches ) and (size<=12))) then
  2253. begin
  2254. helpsize:=size shr 2;
  2255. {$ifndef noAllocEdi}
  2256. getexplicitregister32(R_EDI);
  2257. {$endif noAllocEdi}
  2258. for i:=1 to helpsize do
  2259. begin
  2260. emit_ref_reg(A_MOV,S_L,newreference(source),R_EDI);
  2261. {$ifdef regallocfix}
  2262. If (size = 4) and delsource then
  2263. del_reference(source);
  2264. {$endif regallocfix}
  2265. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,R_EDI,newreference(dest))));
  2266. inc(source.offset,4);
  2267. inc(dest.offset,4);
  2268. dec(size,4);
  2269. end;
  2270. if size>1 then
  2271. begin
  2272. emit_ref_reg(A_MOV,S_W,newreference(source),R_DI);
  2273. {$ifdef regallocfix}
  2274. If (size = 2) and delsource then
  2275. del_reference(source);
  2276. {$endif regallocfix}
  2277. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_W,R_DI,newreference(dest))));
  2278. inc(source.offset,2);
  2279. inc(dest.offset,2);
  2280. dec(size,2);
  2281. end;
  2282. {$ifndef noAllocEdi}
  2283. ungetregister32(R_EDI);
  2284. {$endif noAllocEdi}
  2285. if size>0 then
  2286. begin
  2287. { and now look for an 8 bit register }
  2288. swap:=false;
  2289. if R_EAX in unused then reg8:=reg32toreg8(getexplicitregister32(R_EAX))
  2290. else if R_EDX in unused then reg8:=reg32toreg8(getexplicitregister32(R_EDX))
  2291. else if R_EBX in unused then reg8:=reg32toreg8(getexplicitregister32(R_EBX))
  2292. else if R_ECX in unused then reg8:=reg32toreg8(getexplicitregister32(R_ECX))
  2293. else
  2294. begin
  2295. swap:=true;
  2296. { we need only to check 3 registers, because }
  2297. { one is always not index or base }
  2298. if (dest.base<>R_EAX) and (dest.index<>R_EAX) then
  2299. begin
  2300. reg8:=R_AL;
  2301. reg32:=R_EAX;
  2302. end
  2303. else if (dest.base<>R_EBX) and (dest.index<>R_EBX) then
  2304. begin
  2305. reg8:=R_BL;
  2306. reg32:=R_EBX;
  2307. end
  2308. else if (dest.base<>R_ECX) and (dest.index<>R_ECX) then
  2309. begin
  2310. reg8:=R_CL;
  2311. reg32:=R_ECX;
  2312. end;
  2313. end;
  2314. if swap then
  2315. { was earlier XCHG, of course nonsense }
  2316. begin
  2317. {$ifndef noAllocEdi}
  2318. getexplicitregister32(R_EDI);
  2319. {$endif noAllocEdi}
  2320. emit_reg_reg(A_MOV,S_L,reg32,R_EDI);
  2321. end;
  2322. emit_ref_reg(A_MOV,S_B,newreference(source),reg8);
  2323. {$ifdef regallocfix}
  2324. If delsource then
  2325. del_reference(source);
  2326. {$endif regallocfix}
  2327. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_B,reg8,newreference(dest))));
  2328. if swap then
  2329. begin
  2330. emit_reg_reg(A_MOV,S_L,R_EDI,reg32);
  2331. {$ifndef noAllocEdi}
  2332. ungetregister32(R_EDI);
  2333. {$endif noAllocEdi}
  2334. end
  2335. else
  2336. ungetregister(reg8);
  2337. end;
  2338. end
  2339. else
  2340. begin
  2341. {$ifndef noAllocEdi}
  2342. getexplicitregister32(R_EDI);
  2343. {$endif noAllocEdi}
  2344. emit_ref_reg(A_LEA,S_L,newreference(dest),R_EDI);
  2345. {$ifdef regallocfix}
  2346. {is this ok?? (JM)}
  2347. del_reference(dest);
  2348. {$endif regallocfix}
  2349. {$ifndef noAllocEdi}
  2350. exprasmlist^.concat(new(pairegalloc,alloc(R_ESI)));
  2351. {$endif noAllocEdi}
  2352. if loadref then
  2353. emit_ref_reg(A_MOV,S_L,newreference(source),R_ESI)
  2354. else
  2355. begin
  2356. emit_ref_reg(A_LEA,S_L,newreference(source),R_ESI);
  2357. {$ifdef regallocfix}
  2358. if delsource then
  2359. del_reference(source);
  2360. {$endif regallocfix}
  2361. end;
  2362. exprasmlist^.concat(new(paicpu,op_none(A_CLD,S_NO)));
  2363. ecxpushed:=false;
  2364. if cs_littlesize in aktglobalswitches then
  2365. begin
  2366. maybepushecx;
  2367. emit_const_reg(A_MOV,S_L,size,R_ECX);
  2368. exprasmlist^.concat(new(paicpu,op_none(A_REP,S_NO)));
  2369. exprasmlist^.concat(new(paicpu,op_none(A_MOVSB,S_NO)));
  2370. end
  2371. else
  2372. begin
  2373. helpsize:=size shr 2;
  2374. size:=size and 3;
  2375. if helpsize>1 then
  2376. begin
  2377. maybepushecx;
  2378. emit_const_reg(A_MOV,S_L,helpsize,R_ECX);
  2379. exprasmlist^.concat(new(paicpu,op_none(A_REP,S_NO)));
  2380. end;
  2381. if helpsize>0 then
  2382. exprasmlist^.concat(new(paicpu,op_none(A_MOVSD,S_NO)));
  2383. if size>1 then
  2384. begin
  2385. dec(size,2);
  2386. exprasmlist^.concat(new(paicpu,op_none(A_MOVSW,S_NO)));
  2387. end;
  2388. if size=1 then
  2389. exprasmlist^.concat(new(paicpu,op_none(A_MOVSB,S_NO)));
  2390. end;
  2391. {$ifndef noAllocEdi}
  2392. ungetregister32(R_EDI);
  2393. exprasmlist^.concat(new(pairegalloc,dealloc(R_ESI)));
  2394. {$endif noAllocEdi}
  2395. if ecxpushed then
  2396. exprasmlist^.concat(new(paicpu,op_reg(A_POP,S_L,R_ECX)))
  2397. else
  2398. ungetregister32(R_ECX);
  2399. { loading SELF-reference again }
  2400. maybe_loadesi;
  2401. end;
  2402. if delsource then
  2403. ungetiftemp(source);
  2404. end;
  2405. procedure emitloadord2reg(const location:Tlocation;orddef:Porddef;
  2406. destreg:Tregister;delloc:boolean);
  2407. {A lot smaller and less bug sensitive than the original unfolded loads.}
  2408. var tai:Paicpu;
  2409. r:Preference;
  2410. begin
  2411. tai := nil;
  2412. case location.loc of
  2413. LOC_REGISTER,LOC_CREGISTER:
  2414. begin
  2415. case orddef^.typ of
  2416. u8bit:
  2417. tai:=new(paicpu,op_reg_reg(A_MOVZX,S_BL,location.register,destreg));
  2418. s8bit:
  2419. tai:=new(paicpu,op_reg_reg(A_MOVSX,S_BL,location.register,destreg));
  2420. u16bit:
  2421. tai:=new(paicpu,op_reg_reg(A_MOVZX,S_WL,location.register,destreg));
  2422. s16bit:
  2423. tai:=new(paicpu,op_reg_reg(A_MOVSX,S_WL,location.register,destreg));
  2424. u32bit,s32bit:
  2425. if location.register <> destreg then
  2426. tai:=new(paicpu,op_reg_reg(A_MOV,S_L,location.register,destreg));
  2427. end;
  2428. if delloc then
  2429. ungetregister(location.register);
  2430. end;
  2431. LOC_MEM,
  2432. LOC_REFERENCE:
  2433. begin
  2434. if location.reference.is_immediate then
  2435. tai:=new(paicpu,op_const_reg(A_MOV,S_L,location.reference.offset,destreg))
  2436. else
  2437. begin
  2438. r:=newreference(location.reference);
  2439. case orddef^.typ of
  2440. u8bit:
  2441. tai:=new(paicpu,op_ref_reg(A_MOVZX,S_BL,r,destreg));
  2442. s8bit:
  2443. tai:=new(paicpu,op_ref_reg(A_MOVSX,S_BL,r,destreg));
  2444. u16bit:
  2445. tai:=new(paicpu,op_ref_reg(A_MOVZX,S_WL,r,destreg));
  2446. s16bit:
  2447. tai:=new(paicpu,op_ref_reg(A_MOVSX,S_WL,r,destreg));
  2448. u32bit:
  2449. tai:=new(paicpu,op_ref_reg(A_MOV,S_L,r,destreg));
  2450. s32bit:
  2451. tai:=new(paicpu,op_ref_reg(A_MOV,S_L,r,destreg));
  2452. end;
  2453. end;
  2454. if delloc then
  2455. del_reference(location.reference);
  2456. end
  2457. else
  2458. internalerror(6);
  2459. end;
  2460. if assigned(tai) then
  2461. exprasmlist^.concat(tai);
  2462. end;
  2463. { if necessary ESI is reloaded after a call}
  2464. procedure maybe_loadesi;
  2465. var
  2466. hp : preference;
  2467. p : pprocinfo;
  2468. i : longint;
  2469. begin
  2470. if assigned(procinfo^._class) then
  2471. begin
  2472. {$ifndef noAllocEdi}
  2473. exprasmlist^.concat(new(pairegalloc,alloc(R_ESI)));
  2474. {$endif noAllocEdi}
  2475. if lexlevel>normal_function_level then
  2476. begin
  2477. new(hp);
  2478. reset_reference(hp^);
  2479. hp^.offset:=procinfo^.framepointer_offset;
  2480. hp^.base:=procinfo^.framepointer;
  2481. emit_ref_reg(A_MOV,S_L,hp,R_ESI);
  2482. p:=procinfo^.parent;
  2483. for i:=3 to lexlevel-1 do
  2484. begin
  2485. new(hp);
  2486. reset_reference(hp^);
  2487. hp^.offset:=p^.framepointer_offset;
  2488. hp^.base:=R_ESI;
  2489. emit_ref_reg(A_MOV,S_L,hp,R_ESI);
  2490. p:=p^.parent;
  2491. end;
  2492. new(hp);
  2493. reset_reference(hp^);
  2494. hp^.offset:=p^.selfpointer_offset;
  2495. hp^.base:=R_ESI;
  2496. emit_ref_reg(A_MOV,S_L,hp,R_ESI);
  2497. end
  2498. else
  2499. begin
  2500. new(hp);
  2501. reset_reference(hp^);
  2502. hp^.offset:=procinfo^.selfpointer_offset;
  2503. hp^.base:=procinfo^.framepointer;
  2504. emit_ref_reg(A_MOV,S_L,hp,R_ESI);
  2505. end;
  2506. end;
  2507. end;
  2508. { DO NOT RELY on the fact that the ptree is not yet swaped
  2509. because of inlining code PM }
  2510. procedure firstcomplex(p : ptree);
  2511. var
  2512. hp : ptree;
  2513. begin
  2514. { always calculate boolean AND and OR from left to right }
  2515. if (p^.treetype in [orn,andn]) and
  2516. (p^.left^.resulttype^.deftype=orddef) and
  2517. (porddef(p^.left^.resulttype)^.typ in [bool8bit,bool16bit,bool32bit]) then
  2518. begin
  2519. { p^.swaped:=false}
  2520. if p^.swaped then
  2521. internalerror(234234);
  2522. end
  2523. else
  2524. if (p^.left^.registers32<p^.right^.registers32) and
  2525. { the following check is appropriate, because all }
  2526. { 4 registers are rarely used and it is thereby }
  2527. { achieved that the extra code is being dropped }
  2528. { by exchanging not commutative operators }
  2529. (p^.right^.registers32<=4) then
  2530. begin
  2531. hp:=p^.left;
  2532. p^.left:=p^.right;
  2533. p^.right:=hp;
  2534. p^.swaped:=not p^.swaped;
  2535. end;
  2536. {else
  2537. p^.swaped:=false; do not modify }
  2538. end;
  2539. {*****************************************************************************
  2540. Entry/Exit Code Functions
  2541. *****************************************************************************}
  2542. procedure genprofilecode;
  2543. var
  2544. pl : pasmlabel;
  2545. begin
  2546. if (po_assembler in aktprocsym^.definition^.procoptions) then
  2547. exit;
  2548. case target_info.target of
  2549. target_i386_linux:
  2550. begin
  2551. getaddrlabel(pl);
  2552. emitcall('mcount');
  2553. exprasmlist^.insert(new(paicpu,op_sym_ofs_reg(A_MOV,S_L,pl,0,R_EDX)));
  2554. exprasmlist^.insert(new(pai_section,init(sec_code)));
  2555. exprasmlist^.insert(new(pai_const,init_32bit(0)));
  2556. exprasmlist^.insert(new(pai_label,init(pl)));
  2557. exprasmlist^.insert(new(pai_align,init(4)));
  2558. exprasmlist^.insert(new(pai_section,init(sec_data)));
  2559. end;
  2560. target_i386_go32v2:
  2561. begin
  2562. emitinsertcall('MCOUNT');
  2563. end;
  2564. end;
  2565. end;
  2566. procedure generate_interrupt_stackframe_entry;
  2567. begin
  2568. { save the registers of an interrupt procedure }
  2569. exprasmlist^.insert(new(paicpu,op_reg(A_PUSH,S_L,R_EAX)));
  2570. exprasmlist^.insert(new(paicpu,op_reg(A_PUSH,S_L,R_EBX)));
  2571. exprasmlist^.insert(new(paicpu,op_reg(A_PUSH,S_L,R_ECX)));
  2572. exprasmlist^.insert(new(paicpu,op_reg(A_PUSH,S_L,R_EDX)));
  2573. exprasmlist^.insert(new(paicpu,op_reg(A_PUSH,S_L,R_ESI)));
  2574. exprasmlist^.insert(new(paicpu,op_reg(A_PUSH,S_L,R_EDI)));
  2575. { .... also the segment registers }
  2576. exprasmlist^.insert(new(paicpu,op_reg(A_PUSH,S_W,R_DS)));
  2577. exprasmlist^.insert(new(paicpu,op_reg(A_PUSH,S_W,R_ES)));
  2578. exprasmlist^.insert(new(paicpu,op_reg(A_PUSH,S_W,R_FS)));
  2579. exprasmlist^.insert(new(paicpu,op_reg(A_PUSH,S_W,R_GS)));
  2580. end;
  2581. procedure generate_interrupt_stackframe_exit;
  2582. begin
  2583. { restore the registers of an interrupt procedure }
  2584. { this was all with entrycode instead of exitcode !!}
  2585. procinfo^.aktexitcode^.concat(new(paicpu,op_reg(A_POP,S_L,R_EAX)));
  2586. procinfo^.aktexitcode^.concat(new(paicpu,op_reg(A_POP,S_L,R_EBX)));
  2587. procinfo^.aktexitcode^.concat(new(paicpu,op_reg(A_POP,S_L,R_ECX)));
  2588. procinfo^.aktexitcode^.concat(new(paicpu,op_reg(A_POP,S_L,R_EDX)));
  2589. procinfo^.aktexitcode^.concat(new(paicpu,op_reg(A_POP,S_L,R_ESI)));
  2590. procinfo^.aktexitcode^.concat(new(paicpu,op_reg(A_POP,S_L,R_EDI)));
  2591. { .... also the segment registers }
  2592. procinfo^.aktexitcode^.concat(new(paicpu,op_reg(A_POP,S_W,R_DS)));
  2593. procinfo^.aktexitcode^.concat(new(paicpu,op_reg(A_POP,S_W,R_ES)));
  2594. procinfo^.aktexitcode^.concat(new(paicpu,op_reg(A_POP,S_W,R_FS)));
  2595. procinfo^.aktexitcode^.concat(new(paicpu,op_reg(A_POP,S_W,R_GS)));
  2596. { this restores the flags }
  2597. procinfo^.aktexitcode^.concat(new(paicpu,op_none(A_IRET,S_NO)));
  2598. end;
  2599. { generates the code for threadvar initialisation }
  2600. procedure initialize_threadvar(p : pnamedindexobject);{$ifndef FPC}far;{$endif}
  2601. var
  2602. hr : treference;
  2603. begin
  2604. if (psym(p)^.typ=varsym) and
  2605. (vo_is_thread_var in pvarsym(p)^.varoptions) then
  2606. begin
  2607. exprasmlist^.concat(new(paicpu,op_const(A_PUSH,S_L,pvarsym(p)^.getsize)));
  2608. reset_reference(hr);
  2609. hr.symbol:=newasmsymbol(pvarsym(p)^.mangledname);
  2610. emitpushreferenceaddr(hr);
  2611. emitcall('FPC_INIT_THREADVAR');
  2612. end;
  2613. end;
  2614. { initilizes data of type t }
  2615. { if is_already_ref is true then the routines assumes }
  2616. { that r points to the data to initialize }
  2617. procedure initialize(t : pdef;const ref : treference;is_already_ref : boolean);
  2618. var
  2619. hr : treference;
  2620. begin
  2621. if is_ansistring(t) or
  2622. is_widestring(t) then
  2623. begin
  2624. emit_const_ref(A_MOV,S_L,0,
  2625. newreference(ref));
  2626. end
  2627. else
  2628. begin
  2629. reset_reference(hr);
  2630. hr.symbol:=t^.get_inittable_label;
  2631. emitpushreferenceaddr(hr);
  2632. if is_already_ref then
  2633. exprasmlist^.concat(new(paicpu,op_ref(A_PUSH,S_L,
  2634. newreference(ref))))
  2635. else
  2636. emitpushreferenceaddr(ref);
  2637. emitcall('FPC_INITIALIZE');
  2638. end;
  2639. end;
  2640. { finalizes data of type t }
  2641. { if is_already_ref is true then the routines assumes }
  2642. { that r points to the data to finalizes }
  2643. procedure finalize(t : pdef;const ref : treference;is_already_ref : boolean);
  2644. var
  2645. r : treference;
  2646. begin
  2647. if is_ansistring(t) or
  2648. is_widestring(t) then
  2649. begin
  2650. decrstringref(t,ref);
  2651. end
  2652. else
  2653. begin
  2654. reset_reference(r);
  2655. r.symbol:=t^.get_inittable_label;
  2656. emitpushreferenceaddr(r);
  2657. if is_already_ref then
  2658. exprasmlist^.concat(new(paicpu,op_ref(A_PUSH,S_L,
  2659. newreference(ref))))
  2660. else
  2661. emitpushreferenceaddr(ref);
  2662. emitcall('FPC_FINALIZE');
  2663. end;
  2664. end;
  2665. { generates the code for initialisation of local data }
  2666. procedure initialize_data(p : pnamedindexobject);{$ifndef FPC}far;{$endif}
  2667. var
  2668. hr : treference;
  2669. begin
  2670. if (psym(p)^.typ=varsym) and
  2671. assigned(pvarsym(p)^.vartype.def) and
  2672. not((pvarsym(p)^.vartype.def^.deftype=objectdef) and
  2673. pobjectdef(pvarsym(p)^.vartype.def)^.is_class) and
  2674. pvarsym(p)^.vartype.def^.needs_inittable then
  2675. begin
  2676. if assigned(procinfo) then
  2677. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  2678. reset_reference(hr);
  2679. if psym(p)^.owner^.symtabletype in [localsymtable,inlinelocalsymtable] then
  2680. begin
  2681. hr.base:=procinfo^.framepointer;
  2682. hr.offset:=-pvarsym(p)^.address+pvarsym(p)^.owner^.address_fixup;
  2683. end
  2684. else
  2685. begin
  2686. hr.symbol:=newasmsymbol(pvarsym(p)^.mangledname);
  2687. end;
  2688. initialize(pvarsym(p)^.vartype.def,hr,false);
  2689. end;
  2690. end;
  2691. { generates the code for incrementing the reference count of parameters }
  2692. procedure incr_data(p : pnamedindexobject);{$ifndef FPC}far;{$endif}
  2693. var
  2694. hr : treference;
  2695. begin
  2696. if (psym(p)^.typ=varsym) and
  2697. not((pvarsym(p)^.vartype.def^.deftype=objectdef) and
  2698. pobjectdef(pvarsym(p)^.vartype.def)^.is_class) and
  2699. pvarsym(p)^.vartype.def^.needs_inittable and
  2700. ((pvarsym(p)^.varspez=vs_value) {or
  2701. (pvarsym(p)^.varspez=vs_const) and
  2702. not(dont_copy_const_param(pvarsym(p)^.definition))}) then
  2703. begin
  2704. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  2705. reset_reference(hr);
  2706. hr.symbol:=pvarsym(p)^.vartype.def^.get_inittable_label;
  2707. emitpushreferenceaddr(hr);
  2708. reset_reference(hr);
  2709. hr.base:=procinfo^.framepointer;
  2710. hr.offset:=pvarsym(p)^.address+procinfo^.para_offset;
  2711. emitpushreferenceaddr(hr);
  2712. reset_reference(hr);
  2713. emitcall('FPC_ADDREF');
  2714. end;
  2715. end;
  2716. { generates the code for finalisation of local data }
  2717. procedure finalize_data(p : pnamedindexobject);{$ifndef FPC}far;{$endif}
  2718. var
  2719. hr : treference;
  2720. begin
  2721. if (psym(p)^.typ=varsym) and
  2722. assigned(pvarsym(p)^.vartype.def) and
  2723. not((pvarsym(p)^.vartype.def^.deftype=objectdef) and
  2724. pobjectdef(pvarsym(p)^.vartype.def)^.is_class) and
  2725. pvarsym(p)^.vartype.def^.needs_inittable then
  2726. begin
  2727. { not all kind of parameters need to be finalized }
  2728. if (psym(p)^.owner^.symtabletype=parasymtable) and
  2729. (pvarsym(p)^.varspez in [vs_out,vs_var,vs_const]) then
  2730. exit;
  2731. if assigned(procinfo) then
  2732. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  2733. reset_reference(hr);
  2734. case psym(p)^.owner^.symtabletype of
  2735. localsymtable,inlinelocalsymtable:
  2736. begin
  2737. hr.base:=procinfo^.framepointer;
  2738. hr.offset:=-pvarsym(p)^.address+pvarsym(p)^.owner^.address_fixup;
  2739. end;
  2740. parasymtable,inlineparasymtable:
  2741. begin
  2742. hr.base:=procinfo^.framepointer;
  2743. hr.offset:=pvarsym(p)^.address+procinfo^.para_offset;
  2744. end;
  2745. else
  2746. hr.symbol:=newasmsymbol(pvarsym(p)^.mangledname);
  2747. end;
  2748. finalize(pvarsym(p)^.vartype.def,hr,false);
  2749. end;
  2750. end;
  2751. { generates the code to make local copies of the value parameters }
  2752. procedure copyvalueparas(p : pnamedindexobject);{$ifndef fpc}far;{$endif}
  2753. var
  2754. href1,href2 : treference;
  2755. r : preference;
  2756. power,len : longint;
  2757. opsize : topsize;
  2758. again,ok : pasmlabel;
  2759. begin
  2760. if (psym(p)^.typ=varsym) and
  2761. (pvarsym(p)^.varspez=vs_value) and
  2762. (push_addr_param(pvarsym(p)^.vartype.def)) then
  2763. begin
  2764. if is_open_array(pvarsym(p)^.vartype.def) or
  2765. is_array_of_const(pvarsym(p)^.vartype.def) then
  2766. begin
  2767. { get stack space }
  2768. new(r);
  2769. reset_reference(r^);
  2770. r^.base:=procinfo^.framepointer;
  2771. r^.offset:=pvarsym(p)^.address+4+procinfo^.para_offset;
  2772. {$ifndef noAllocEdi}
  2773. getexplicitregister32(R_EDI);
  2774. {$endif noAllocEdi}
  2775. exprasmlist^.concat(new(paicpu,
  2776. op_ref_reg(A_MOV,S_L,r,R_EDI)));
  2777. exprasmlist^.concat(new(paicpu,
  2778. op_reg(A_INC,S_L,R_EDI)));
  2779. if (parraydef(pvarsym(p)^.vartype.def)^.elesize<>1) then
  2780. begin
  2781. if ispowerof2(parraydef(pvarsym(p)^.vartype.def)^.elesize, power) then
  2782. exprasmlist^.concat(new(paicpu,
  2783. op_const_reg(A_SHL,S_L,
  2784. power,R_EDI)))
  2785. else
  2786. exprasmlist^.concat(new(paicpu,
  2787. op_const_reg(A_IMUL,S_L,
  2788. parraydef(pvarsym(p)^.vartype.def)^.elesize,R_EDI)));
  2789. end;
  2790. {$ifndef NOTARGETWIN32}
  2791. { windows guards only a few pages for stack growing, }
  2792. { so we have to access every page first }
  2793. if target_os.id=os_i386_win32 then
  2794. begin
  2795. getlabel(again);
  2796. getlabel(ok);
  2797. emitlab(again);
  2798. exprasmlist^.concat(new(paicpu,
  2799. op_const_reg(A_CMP,S_L,winstackpagesize,R_EDI)));
  2800. emitjmp(C_C,ok);
  2801. exprasmlist^.concat(new(paicpu,
  2802. op_const_reg(A_SUB,S_L,winstackpagesize-4,R_ESP)));
  2803. exprasmlist^.concat(new(paicpu,
  2804. op_reg(A_PUSH,S_L,R_EAX)));
  2805. exprasmlist^.concat(new(paicpu,
  2806. op_const_reg(A_SUB,S_L,winstackpagesize,R_EDI)));
  2807. emitjmp(C_None,again);
  2808. emitlab(ok);
  2809. exprasmlist^.concat(new(paicpu,
  2810. op_reg_reg(A_SUB,S_L,R_EDI,R_ESP)));
  2811. {$ifndef noAllocEdi}
  2812. ungetregister32(R_EDI);
  2813. {$endif noAllocEdi}
  2814. { now reload EDI }
  2815. new(r);
  2816. reset_reference(r^);
  2817. r^.base:=procinfo^.framepointer;
  2818. r^.offset:=pvarsym(p)^.address+4+procinfo^.para_offset;
  2819. {$ifndef noAllocEdi}
  2820. getexplicitregister32(R_EDI);
  2821. {$endif noAllocEdi}
  2822. exprasmlist^.concat(new(paicpu,
  2823. op_ref_reg(A_MOV,S_L,r,R_EDI)));
  2824. exprasmlist^.concat(new(paicpu,
  2825. op_reg(A_INC,S_L,R_EDI)));
  2826. if (parraydef(pvarsym(p)^.vartype.def)^.elesize<>1) then
  2827. begin
  2828. if ispowerof2(parraydef(pvarsym(p)^.vartype.def)^.elesize, power) then
  2829. exprasmlist^.concat(new(paicpu,
  2830. op_const_reg(A_SHL,S_L,
  2831. power,R_EDI)))
  2832. else
  2833. exprasmlist^.concat(new(paicpu,
  2834. op_const_reg(A_IMUL,S_L,
  2835. parraydef(pvarsym(p)^.vartype.def)^.elesize,R_EDI)));
  2836. end;
  2837. end
  2838. else
  2839. {$endif NOTARGETWIN32}
  2840. exprasmlist^.concat(new(paicpu,
  2841. op_reg_reg(A_SUB,S_L,R_EDI,R_ESP)));
  2842. { load destination }
  2843. exprasmlist^.concat(new(paicpu,
  2844. op_reg_reg(A_MOV,S_L,R_ESP,R_EDI)));
  2845. { don't destroy the registers! }
  2846. exprasmlist^.concat(new(paicpu,
  2847. op_reg(A_PUSH,S_L,R_ECX)));
  2848. exprasmlist^.concat(new(paicpu,
  2849. op_reg(A_PUSH,S_L,R_ESI)));
  2850. { load count }
  2851. new(r);
  2852. reset_reference(r^);
  2853. r^.base:=procinfo^.framepointer;
  2854. r^.offset:=pvarsym(p)^.address+4+procinfo^.para_offset;
  2855. exprasmlist^.concat(new(paicpu,
  2856. op_ref_reg(A_MOV,S_L,r,R_ECX)));
  2857. { load source }
  2858. new(r);
  2859. reset_reference(r^);
  2860. r^.base:=procinfo^.framepointer;
  2861. r^.offset:=pvarsym(p)^.address+procinfo^.para_offset;
  2862. exprasmlist^.concat(new(paicpu,
  2863. op_ref_reg(A_MOV,S_L,r,R_ESI)));
  2864. { scheduled .... }
  2865. exprasmlist^.concat(new(paicpu,
  2866. op_reg(A_INC,S_L,R_ECX)));
  2867. { calculate size }
  2868. len:=parraydef(pvarsym(p)^.vartype.def)^.elesize;
  2869. opsize:=S_B;
  2870. if (len and 3)=0 then
  2871. begin
  2872. opsize:=S_L;
  2873. len:=len shr 2;
  2874. end
  2875. else
  2876. if (len and 1)=0 then
  2877. begin
  2878. opsize:=S_W;
  2879. len:=len shr 1;
  2880. end;
  2881. if ispowerof2(len, power) then
  2882. exprasmlist^.concat(new(paicpu,
  2883. op_const_reg(A_SHL,S_L,
  2884. power,R_ECX)))
  2885. else
  2886. exprasmlist^.concat(new(paicpu,
  2887. op_const_reg(A_IMUL,S_L,len,R_ECX)));
  2888. exprasmlist^.concat(new(paicpu,
  2889. op_none(A_REP,S_NO)));
  2890. case opsize of
  2891. S_B : exprasmlist^.concat(new(paicpu,op_none(A_MOVSB,S_NO)));
  2892. S_W : exprasmlist^.concat(new(paicpu,op_none(A_MOVSW,S_NO)));
  2893. S_L : exprasmlist^.concat(new(paicpu,op_none(A_MOVSD,S_NO)));
  2894. end;
  2895. {$ifndef noAllocEdi}
  2896. ungetregister32(R_EDI);
  2897. {$endif noAllocEdi}
  2898. exprasmlist^.concat(new(paicpu,
  2899. op_reg(A_POP,S_L,R_ESI)));
  2900. exprasmlist^.concat(new(paicpu,
  2901. op_reg(A_POP,S_L,R_ECX)));
  2902. { patch the new address }
  2903. new(r);
  2904. reset_reference(r^);
  2905. r^.base:=procinfo^.framepointer;
  2906. r^.offset:=pvarsym(p)^.address+procinfo^.para_offset;
  2907. exprasmlist^.concat(new(paicpu,
  2908. op_reg_ref(A_MOV,S_L,R_ESP,r)));
  2909. end
  2910. else
  2911. if is_shortstring(pvarsym(p)^.vartype.def) then
  2912. begin
  2913. reset_reference(href1);
  2914. href1.base:=procinfo^.framepointer;
  2915. href1.offset:=pvarsym(p)^.address+procinfo^.para_offset;
  2916. reset_reference(href2);
  2917. href2.base:=procinfo^.framepointer;
  2918. href2.offset:=-pvarsym(p)^.localvarsym^.address+pvarsym(p)^.localvarsym^.owner^.address_fixup;
  2919. copyshortstring(href2,href1,pstringdef(pvarsym(p)^.vartype.def)^.len,true,false);
  2920. end
  2921. else
  2922. begin
  2923. reset_reference(href1);
  2924. href1.base:=procinfo^.framepointer;
  2925. href1.offset:=pvarsym(p)^.address+procinfo^.para_offset;
  2926. reset_reference(href2);
  2927. href2.base:=procinfo^.framepointer;
  2928. href2.offset:=-pvarsym(p)^.localvarsym^.address+pvarsym(p)^.localvarsym^.owner^.address_fixup;
  2929. concatcopy(href1,href2,pvarsym(p)^.vartype.def^.size,true,true);
  2930. end;
  2931. end;
  2932. end;
  2933. procedure inittempansistrings;
  2934. var
  2935. hp : ptemprecord;
  2936. r : preference;
  2937. begin
  2938. hp:=templist;
  2939. while assigned(hp) do
  2940. begin
  2941. if hp^.temptype in [tt_ansistring,tt_freeansistring] then
  2942. begin
  2943. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  2944. new(r);
  2945. reset_reference(r^);
  2946. r^.base:=procinfo^.framepointer;
  2947. r^.offset:=hp^.pos;
  2948. emit_const_ref(A_MOV,S_L,0,r);
  2949. end;
  2950. hp:=hp^.next;
  2951. end;
  2952. end;
  2953. procedure finalizetempansistrings;
  2954. var
  2955. hp : ptemprecord;
  2956. hr : treference;
  2957. begin
  2958. hp:=templist;
  2959. while assigned(hp) do
  2960. begin
  2961. if hp^.temptype in [tt_ansistring,tt_freeansistring] then
  2962. begin
  2963. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  2964. reset_reference(hr);
  2965. hr.base:=procinfo^.framepointer;
  2966. hr.offset:=hp^.pos;
  2967. emitpushreferenceaddr(hr);
  2968. emitcall('FPC_ANSISTR_DECR_REF');
  2969. end;
  2970. hp:=hp^.next;
  2971. end;
  2972. end;
  2973. var
  2974. ls : longint;
  2975. procedure largest_size(p : pnamedindexobject);{$ifndef FPC}far;{$endif}
  2976. begin
  2977. if (psym(p)^.typ=varsym) and
  2978. (pvarsym(p)^.getvaluesize>ls) then
  2979. ls:=pvarsym(p)^.getvaluesize;
  2980. end;
  2981. procedure alignstack(alist : paasmoutput);
  2982. begin
  2983. {$ifdef dummy}
  2984. if (cs_optimize in aktglobalswitches) and
  2985. (aktoptprocessor in [classp5,classp6]) then
  2986. begin
  2987. ls:=0;
  2988. aktprocsym^.definition^.localst^.foreach({$ifndef TP}@{$endif}largest_size);
  2989. if ls>=8 then
  2990. alist^.insert(new(paicpu,op_const_reg(A_AND,S_L,-8,R_ESP)));
  2991. end;
  2992. {$endif dummy}
  2993. end;
  2994. procedure genentrycode(alist : paasmoutput;const proc_names:Tstringcontainer;make_global:boolean;
  2995. stackframe:longint;
  2996. var parasize:longint;var nostackframe:boolean;
  2997. inlined : boolean);
  2998. {
  2999. Generates the entry code for a procedure
  3000. }
  3001. var
  3002. hs : string;
  3003. {$ifdef GDB}
  3004. stab_function_name : Pai_stab_function_name;
  3005. {$endif GDB}
  3006. hr : preference;
  3007. p : psymtable;
  3008. r : treference;
  3009. oldlist,
  3010. oldexprasmlist : paasmoutput;
  3011. again : pasmlabel;
  3012. i : longint;
  3013. begin
  3014. oldexprasmlist:=exprasmlist;
  3015. exprasmlist:=alist;
  3016. if (not inlined) and (aktprocsym^.definition^.proctypeoption=potype_proginit) then
  3017. begin
  3018. emitinsertcall('FPC_INITIALIZEUNITS');
  3019. if target_info.target=target_I386_WIN32 then
  3020. begin
  3021. new(hr);
  3022. reset_reference(hr^);
  3023. hr^.symbol:=newasmsymbol(
  3024. 'U_SYSWIN32_ISCONSOLE');
  3025. if apptype=at_cui then
  3026. exprasmlist^.insert(new(paicpu,op_const_ref(A_MOV,S_B,
  3027. 1,hr)))
  3028. else
  3029. exprasmlist^.insert(new(paicpu,op_const_ref(A_MOV,S_B,
  3030. 0,hr)));
  3031. end;
  3032. oldlist:=exprasmlist;
  3033. exprasmlist:=new(paasmoutput,init);
  3034. p:=symtablestack;
  3035. while assigned(p) do
  3036. begin
  3037. p^.foreach({$ifndef TP}@{$endif}initialize_threadvar);
  3038. p:=p^.next;
  3039. end;
  3040. oldlist^.insertlist(exprasmlist);
  3041. dispose(exprasmlist,done);
  3042. exprasmlist:=oldlist;
  3043. end;
  3044. {$ifdef GDB}
  3045. if (not inlined) and (cs_debuginfo in aktmoduleswitches) then
  3046. exprasmlist^.insert(new(pai_force_line,init));
  3047. {$endif GDB}
  3048. { a constructor needs a help procedure }
  3049. if (aktprocsym^.definition^.proctypeoption=potype_constructor) then
  3050. begin
  3051. if procinfo^._class^.is_class then
  3052. begin
  3053. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  3054. exprasmlist^.insert(new(paicpu,op_cond_sym(A_Jcc,C_Z,S_NO,faillabel)));
  3055. emitinsertcall('FPC_NEW_CLASS');
  3056. end
  3057. else
  3058. begin
  3059. exprasmlist^.insert(new(paicpu,op_cond_sym(A_Jcc,C_Z,S_NO,faillabel)));
  3060. emitinsertcall('FPC_HELP_CONSTRUCTOR');
  3061. {$ifndef noAllocEdi}
  3062. getexplicitregister32(R_EDI);
  3063. {$endif noAllocEdi}
  3064. exprasmlist^.insert(new(paicpu,op_const_reg(A_MOV,S_L,procinfo^._class^.vmt_offset,R_EDI)));
  3065. end;
  3066. end;
  3067. { don't load ESI, does the caller }
  3068. { we must do it for local function }
  3069. { that can be called from a foreach }
  3070. { of another object than self !! PM }
  3071. if assigned(procinfo^._class) and
  3072. (lexlevel>normal_function_level) then
  3073. maybe_loadesi;
  3074. { When message method contains self as a parameter,
  3075. we must load it into ESI }
  3076. If (po_containsself in aktprocsym^.definition^.procoptions) then
  3077. begin
  3078. new(hr);
  3079. reset_reference(hr^);
  3080. hr^.offset:=procinfo^.selfpointer_offset;
  3081. hr^.base:=procinfo^.framepointer;
  3082. exprasmlist^.insert(new(paicpu,op_ref_reg(A_MOV,S_L,hr,R_ESI)));
  3083. {$ifndef noAllocEdi}
  3084. exprasmlist^.insert(new(pairegalloc,alloc(R_ESI)));
  3085. {$endif noAllocEdi}
  3086. end;
  3087. { should we save edi,esi,ebx like C ? }
  3088. if (po_savestdregs in aktprocsym^.definition^.procoptions) then
  3089. begin
  3090. if (aktprocsym^.definition^.usedregisters and ($80 shr byte(R_EBX)))<>0 then
  3091. exprasmlist^.insert(new(paicpu,op_reg(A_PUSH,S_L,R_EBX)));
  3092. exprasmlist^.insert(new(paicpu,op_reg(A_PUSH,S_L,R_ESI)));
  3093. exprasmlist^.insert(new(paicpu,op_reg(A_PUSH,S_L,R_EDI)));
  3094. end;
  3095. { for the save all registers we can simply use a pusha,popa which
  3096. push edi,esi,ebp,esp(ignored),ebx,edx,ecx,eax }
  3097. if (po_saveregisters in aktprocsym^.definition^.procoptions) then
  3098. begin
  3099. exprasmlist^.insert(new(paicpu,op_none(A_PUSHA,S_L)));
  3100. end;
  3101. { omit stack frame ? }
  3102. if not inlined then
  3103. if procinfo^.framepointer=stack_pointer then
  3104. begin
  3105. CGMessage(cg_d_stackframe_omited);
  3106. nostackframe:=true;
  3107. if (aktprocsym^.definition^.proctypeoption in [potype_unitinit,potype_proginit,potype_unitfinalize]) then
  3108. parasize:=0
  3109. else
  3110. parasize:=aktprocsym^.definition^.parast^.datasize+procinfo^.para_offset-4;
  3111. if stackframe<>0 then
  3112. exprasmlist^.insert(new(paicpu,
  3113. op_const_reg(A_SUB,S_L,stackframe,R_ESP)));
  3114. end
  3115. else
  3116. begin
  3117. alignstack(alist);
  3118. if (aktprocsym^.definition^.proctypeoption in [potype_unitinit,potype_proginit,potype_unitfinalize]) then
  3119. parasize:=0
  3120. else
  3121. parasize:=aktprocsym^.definition^.parast^.datasize+procinfo^.para_offset-8;
  3122. nostackframe:=false;
  3123. if stackframe<>0 then
  3124. begin
  3125. {$ifdef unused}
  3126. if (cs_littlesize in aktglobalswitches) and (stackframe<=65535) then
  3127. begin
  3128. if (cs_check_stack in aktlocalswitches) and
  3129. not(target_info.target in [target_i386_linux,target_i386_win32]) then
  3130. begin
  3131. emitinsertcall('FPC_STACKCHECK');
  3132. exprasmlist^.insert(new(paicpu,op_const(A_PUSH,S_L,stackframe)));
  3133. end;
  3134. if cs_profile in aktmoduleswitches then
  3135. genprofilecode;
  3136. { %edi is already saved when pocdecl is used
  3137. if (target_info.target=target_linux) and
  3138. ((aktprocsym^.definition^.options and poexports)<>0) then
  3139. exprasmlist^.insert(new(Paicpu,op_reg(A_PUSH,S_L,R_EDI))); }
  3140. { ATTENTION:
  3141. never use ENTER in linux !!!
  3142. the stack page fault does not support it PM }
  3143. exprasmlist^.insert(new(paicpu,op_const_const(A_ENTER,S_NO,stackframe,0)))
  3144. end
  3145. else
  3146. {$endif unused}
  3147. begin
  3148. { windows guards only a few pages for stack growing, }
  3149. { so we have to access every page first }
  3150. if (target_os.id=os_i386_win32) and
  3151. (stackframe>=winstackpagesize) then
  3152. begin
  3153. if stackframe div winstackpagesize<=5 then
  3154. begin
  3155. exprasmlist^.insert(new(paicpu,op_const_reg(A_SUB,S_L,stackframe-4,R_ESP)));
  3156. for i:=1 to stackframe div winstackpagesize do
  3157. begin
  3158. hr:=new_reference(R_ESP,stackframe-i*winstackpagesize);
  3159. exprasmlist^.concat(new(paicpu,
  3160. op_const_ref(A_MOV,S_L,0,hr)));
  3161. end;
  3162. exprasmlist^.concat(new(paicpu,
  3163. op_reg(A_PUSH,S_L,R_EAX)));
  3164. end
  3165. else
  3166. begin
  3167. getlabel(again);
  3168. {$ifndef noAllocEdi}
  3169. getexplicitregister32(R_EDI);
  3170. {$endif noAllocEdi}
  3171. exprasmlist^.concat(new(paicpu,
  3172. op_const_reg(A_MOV,S_L,stackframe div winstackpagesize,R_EDI)));
  3173. emitlab(again);
  3174. exprasmlist^.concat(new(paicpu,
  3175. op_const_reg(A_SUB,S_L,winstackpagesize-4,R_ESP)));
  3176. exprasmlist^.concat(new(paicpu,
  3177. op_reg(A_PUSH,S_L,R_EAX)));
  3178. exprasmlist^.concat(new(paicpu,
  3179. op_reg(A_DEC,S_L,R_EDI)));
  3180. emitjmp(C_NZ,again);
  3181. {$ifndef noAllocEdi}
  3182. ungetregister32(R_EDI);
  3183. {$endif noAllocEdi}
  3184. exprasmlist^.concat(new(paicpu,
  3185. op_const_reg(A_SUB,S_L,stackframe mod winstackpagesize,R_ESP)));
  3186. end
  3187. end
  3188. else
  3189. exprasmlist^.insert(new(paicpu,op_const_reg(A_SUB,S_L,stackframe,R_ESP)));
  3190. if (cs_check_stack in aktlocalswitches) and
  3191. not(target_info.target in [target_i386_linux,target_i386_win32]) then
  3192. begin
  3193. emitinsertcall('FPC_STACKCHECK');
  3194. exprasmlist^.insert(new(paicpu,op_const(A_PUSH,S_L,stackframe)));
  3195. end;
  3196. if cs_profile in aktmoduleswitches then
  3197. genprofilecode;
  3198. exprasmlist^.insert(new(paicpu,op_reg_reg(A_MOV,S_L,R_ESP,R_EBP)));
  3199. exprasmlist^.insert(new(paicpu,op_reg(A_PUSH,S_L,R_EBP)));
  3200. end;
  3201. end { endif stackframe <> 0 }
  3202. else
  3203. begin
  3204. if cs_profile in aktmoduleswitches then
  3205. genprofilecode;
  3206. exprasmlist^.insert(new(paicpu,op_reg_reg(A_MOV,S_L,R_ESP,R_EBP)));
  3207. exprasmlist^.insert(new(paicpu,op_reg(A_PUSH,S_L,R_EBP)));
  3208. end;
  3209. end;
  3210. if (po_interrupt in aktprocsym^.definition^.procoptions) then
  3211. generate_interrupt_stackframe_entry;
  3212. { initialize return value }
  3213. if (procinfo^.returntype.def<>pdef(voiddef)) and
  3214. (procinfo^.returntype.def^.needs_inittable) and
  3215. ((procinfo^.returntype.def^.deftype<>objectdef) or
  3216. not(pobjectdef(procinfo^.returntype.def)^.is_class)) then
  3217. begin
  3218. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  3219. reset_reference(r);
  3220. r.offset:=procinfo^.return_offset;
  3221. r.base:=procinfo^.framepointer;
  3222. initialize(procinfo^.returntype.def,r,ret_in_param(procinfo^.returntype.def));
  3223. end;
  3224. { generate copies of call by value parameters }
  3225. if not(po_assembler in aktprocsym^.definition^.procoptions) and
  3226. not (pocall_cdecl in aktprocsym^.definition^.proccalloptions) then
  3227. aktprocsym^.definition^.parast^.foreach({$ifndef TP}@{$endif}copyvalueparas);
  3228. { initialisize local data like ansistrings }
  3229. case aktprocsym^.definition^.proctypeoption of
  3230. potype_unitinit:
  3231. begin
  3232. { using current_module^.globalsymtable is hopefully }
  3233. { more robust than symtablestack and symtablestack^.next }
  3234. psymtable(current_module^.globalsymtable)^.foreach({$ifndef TP}@{$endif}initialize_data);
  3235. psymtable(current_module^.localsymtable)^.foreach({$ifndef TP}@{$endif}initialize_data);
  3236. end;
  3237. { units have seperate code for initilization and finalization }
  3238. potype_unitfinalize: ;
  3239. else
  3240. aktprocsym^.definition^.localst^.foreach({$ifndef TP}@{$endif}initialize_data);
  3241. end;
  3242. { add a reference to all call by value/const parameters }
  3243. aktprocsym^.definition^.parast^.foreach({$ifndef TP}@{$endif}incr_data);
  3244. { initialisizes temp. ansi/wide string data }
  3245. inittempansistrings;
  3246. { do we need an exception frame because of ansi/widestrings ? }
  3247. if not inlined and
  3248. ((procinfo^.flags and pi_needs_implicit_finally)<>0) and
  3249. { but it's useless in init/final code of units }
  3250. not(aktprocsym^.definition^.proctypeoption in [potype_unitfinalize,potype_unitinit]) then
  3251. begin
  3252. usedinproc:=usedinproc or ($80 shr byte(R_EAX));
  3253. { Type of stack-frame must be pushed}
  3254. exprasmlist^.concat(new(paicpu,op_const(A_PUSH,S_L,1)));
  3255. emitcall('FPC_PUSHEXCEPTADDR');
  3256. exprasmlist^.concat(new(paicpu,
  3257. op_reg(A_PUSH,S_L,R_EAX)));
  3258. emitcall('FPC_SETJMP');
  3259. exprasmlist^.concat(new(paicpu,
  3260. op_reg(A_PUSH,S_L,R_EAX)));
  3261. exprasmlist^.concat(new(paicpu,
  3262. op_reg_reg(A_TEST,S_L,R_EAX,R_EAX)));
  3263. emitjmp(C_NE,aktexitlabel);
  3264. { probably we've to reload self here }
  3265. maybe_loadesi;
  3266. end;
  3267. if not inlined then
  3268. begin
  3269. if (cs_profile in aktmoduleswitches) or
  3270. (aktprocsym^.definition^.owner^.symtabletype=globalsymtable) or
  3271. (assigned(procinfo^._class) and (procinfo^._class^.owner^.symtabletype=globalsymtable)) then
  3272. make_global:=true;
  3273. hs:=proc_names.get;
  3274. {$ifdef GDB}
  3275. if (cs_debuginfo in aktmoduleswitches) and target_os.use_function_relative_addresses then
  3276. stab_function_name := new(pai_stab_function_name,init(strpnew(hs)));
  3277. {$EndIf GDB}
  3278. while hs<>'' do
  3279. begin
  3280. if make_global then
  3281. exprasmlist^.insert(new(pai_symbol,initname_global(hs,0)))
  3282. else
  3283. exprasmlist^.insert(new(pai_symbol,initname(hs,0)));
  3284. {$ifdef GDB}
  3285. if (cs_debuginfo in aktmoduleswitches) and
  3286. target_os.use_function_relative_addresses then
  3287. exprasmlist^.insert(new(pai_stab_function_name,init(strpnew(hs))));
  3288. {$endif GDB}
  3289. hs:=proc_names.get;
  3290. end;
  3291. if make_global or ((procinfo^.flags and pi_is_global) <> 0) then
  3292. aktprocsym^.is_global := True;
  3293. {$ifdef GDB}
  3294. if (cs_debuginfo in aktmoduleswitches) then
  3295. begin
  3296. if target_os.use_function_relative_addresses then
  3297. exprasmlist^.insert(stab_function_name);
  3298. exprasmlist^.insert(new(pai_stabs,init(aktprocsym^.stabstring)));
  3299. aktprocsym^.isstabwritten:=true;
  3300. end;
  3301. {$endif GDB}
  3302. { Align, gprof uses 16 byte granularity }
  3303. if (cs_profile in aktmoduleswitches) then
  3304. exprasmlist^.insert(new(pai_align,init_op(16,$90)))
  3305. else
  3306. if not(cs_littlesize in aktglobalswitches) then
  3307. exprasmlist^.insert(new(pai_align,init(16)));
  3308. end;
  3309. if inlined then
  3310. load_regvars(exprasmlist,nil);
  3311. exprasmlist:=oldexprasmlist;
  3312. end;
  3313. procedure handle_return_value(inlined : boolean;var uses_eax,uses_edx : boolean);
  3314. var
  3315. hr : preference;
  3316. op : Tasmop;
  3317. s : Topsize;
  3318. begin
  3319. uses_eax:=false;
  3320. uses_edx:=false;
  3321. if procinfo^.returntype.def<>pdef(voiddef) then
  3322. begin
  3323. {if ((procinfo^.flags and pi_operator)<>0) and
  3324. assigned(opsym) then
  3325. procinfo^.funcret_is_valid:=
  3326. procinfo^.funcret_is_valid or (opsym^.refs>0);}
  3327. if (procinfo^.funcret_state<>vs_assigned) and not inlined { and
  3328. ((procinfo^.flags and pi_uses_asm)=0)} then
  3329. CGMessage(sym_w_function_result_not_set);
  3330. hr:=new_reference(procinfo^.framepointer,procinfo^.return_offset);
  3331. if (procinfo^.returntype.def^.deftype in [orddef,enumdef]) then
  3332. begin
  3333. uses_eax:=true;
  3334. case procinfo^.returntype.def^.size of
  3335. 8:
  3336. begin
  3337. emit_ref_reg(A_MOV,S_L,hr,R_EAX);
  3338. hr:=new_reference(procinfo^.framepointer,procinfo^.return_offset+4);
  3339. emit_ref_reg(A_MOV,S_L,hr,R_EDX);
  3340. uses_edx:=true;
  3341. end;
  3342. 4:
  3343. emit_ref_reg(A_MOV,S_L,hr,R_EAX);
  3344. 2:
  3345. emit_ref_reg(A_MOV,S_W,hr,R_AX);
  3346. 1:
  3347. emit_ref_reg(A_MOV,S_B,hr,R_AL);
  3348. end;
  3349. end
  3350. else
  3351. if ret_in_acc(procinfo^.returntype.def) then
  3352. begin
  3353. uses_eax:=true;
  3354. emit_ref_reg(A_MOV,S_L,hr,R_EAX);
  3355. end
  3356. else
  3357. if (procinfo^.returntype.def^.deftype=floatdef) then
  3358. begin
  3359. floatloadops(pfloatdef(procinfo^.returntype.def)^.typ,op,s);
  3360. exprasmlist^.concat(new(paicpu,op_ref(op,s,hr)))
  3361. end
  3362. else
  3363. dispose(hr);
  3364. end
  3365. end;
  3366. procedure genexitcode(alist : paasmoutput;parasize:longint;nostackframe,inlined:boolean);
  3367. var
  3368. {$ifdef GDB}
  3369. mangled_length : longint;
  3370. p : pchar;
  3371. {$endif GDB}
  3372. nofinal,okexitlabel,noreraiselabel,nodestroycall : pasmlabel;
  3373. hr : treference;
  3374. uses_eax,uses_edx,uses_esi : boolean;
  3375. oldexprasmlist : paasmoutput;
  3376. ai : paicpu;
  3377. pd : pprocdef;
  3378. begin
  3379. oldexprasmlist:=exprasmlist;
  3380. exprasmlist:=alist;
  3381. if aktexitlabel^.is_used then
  3382. exprasmlist^.insert(new(pai_label,init(aktexitlabel)));
  3383. { call the destructor help procedure }
  3384. if (aktprocsym^.definition^.proctypeoption=potype_destructor) and
  3385. assigned(procinfo^._class) then
  3386. begin
  3387. if procinfo^._class^.is_class then
  3388. begin
  3389. emitinsertcall('FPC_DISPOSE_CLASS');
  3390. end
  3391. else
  3392. begin
  3393. emitinsertcall('FPC_HELP_DESTRUCTOR');
  3394. {$ifndef noAllocEdi}
  3395. getexplicitregister32(R_EDI);
  3396. {$endif noAllocEdi}
  3397. exprasmlist^.insert(new(paicpu,op_const_reg(A_MOV,S_L,procinfo^._class^.vmt_offset,R_EDI)));
  3398. { must the object be finalized ? }
  3399. if procinfo^._class^.needs_inittable then
  3400. begin
  3401. getlabel(nofinal);
  3402. exprasmlist^.insert(new(pai_label,init(nofinal)));
  3403. emitinsertcall('FPC_FINALIZE');
  3404. {$ifndef noAllocEdi}
  3405. ungetregister32(R_EDI);
  3406. {$endif noAllocEdi}
  3407. exprasmlist^.insert(new(paicpu,op_reg(A_PUSH,S_L,R_ESI)));
  3408. exprasmlist^.insert(new(paicpu,op_sym(A_PUSH,S_L,procinfo^._class^.get_inittable_label)));
  3409. ai:=new(paicpu,op_sym(A_Jcc,S_NO,nofinal));
  3410. ai^.SetCondition(C_Z);
  3411. exprasmlist^.insert(ai);
  3412. reset_reference(hr);
  3413. hr.base:=R_EBP;
  3414. hr.offset:=8;
  3415. exprasmlist^.insert(new(paicpu,op_const_ref(A_CMP,S_L,0,newreference(hr))));
  3416. end;
  3417. end;
  3418. end;
  3419. { finalize temporary data }
  3420. finalizetempansistrings;
  3421. { finalize local data like ansistrings}
  3422. case aktprocsym^.definition^.proctypeoption of
  3423. potype_unitfinalize:
  3424. begin
  3425. { using current_module^.globalsymtable is hopefully }
  3426. { more robust than symtablestack and symtablestack^.next }
  3427. psymtable(current_module^.globalsymtable)^.foreach({$ifndef TP}@{$endif}finalize_data);
  3428. psymtable(current_module^.localsymtable)^.foreach({$ifndef TP}@{$endif}finalize_data);
  3429. end;
  3430. { units have seperate code for initialization and finalization }
  3431. potype_unitinit: ;
  3432. else
  3433. aktprocsym^.definition^.localst^.foreach({$ifndef TP}@{$endif}finalize_data);
  3434. end;
  3435. { finalize paras data }
  3436. if assigned(aktprocsym^.definition^.parast) then
  3437. aktprocsym^.definition^.parast^.foreach({$ifndef TP}@{$endif}finalize_data);
  3438. { do we need to handle exceptions because of ansi/widestrings ? }
  3439. if not inlined and
  3440. ((procinfo^.flags and pi_needs_implicit_finally)<>0) and
  3441. { but it's useless in init/final code of units }
  3442. not(aktprocsym^.definition^.proctypeoption in [potype_unitfinalize,potype_unitinit]) then
  3443. begin
  3444. { the exception helper routines modify all registers }
  3445. aktprocsym^.definition^.usedregisters:=$ff;
  3446. getlabel(noreraiselabel);
  3447. emitcall('FPC_POPADDRSTACK');
  3448. exprasmlist^.concat(new(paicpu,
  3449. op_reg(A_POP,S_L,R_EAX)));
  3450. exprasmlist^.concat(new(paicpu,
  3451. op_reg_reg(A_TEST,S_L,R_EAX,R_EAX)));
  3452. emitjmp(C_E,noreraiselabel);
  3453. if (aktprocsym^.definition^.proctypeoption=potype_constructor) then
  3454. begin
  3455. if assigned(procinfo^._class) then
  3456. begin
  3457. pd:=procinfo^._class^.searchdestructor;
  3458. if assigned(pd) then
  3459. begin
  3460. getlabel(nodestroycall);
  3461. emit_const_ref(A_CMP,S_L,0,new_reference(procinfo^.framepointer,
  3462. procinfo^.selfpointer_offset));
  3463. emitjmp(C_E,nodestroycall);
  3464. if procinfo^._class^.is_class then
  3465. begin
  3466. emit_const(A_PUSH,S_L,1);
  3467. emit_reg(A_PUSH,S_L,R_ESI);
  3468. end
  3469. else
  3470. begin
  3471. emit_reg(A_PUSH,S_L,R_ESI);
  3472. emit_sym(A_PUSH,S_L,newasmsymbol(procinfo^._class^.vmt_mangledname));
  3473. end;
  3474. if (po_virtualmethod in pd^.procoptions) then
  3475. begin
  3476. emit_ref_reg(A_MOV,S_L,new_reference(R_ESI,0),R_EDI);
  3477. emit_ref(A_CALL,S_NO,new_reference(R_EDI,procinfo^._class^.vmtmethodoffset(pd^.extnumber)));
  3478. end
  3479. else
  3480. emitcall(pd^.mangledname);
  3481. { not necessary because the result is never assigned in the
  3482. case of an exception (FK)
  3483. emit_const_reg(A_MOV,S_L,0,R_ESI);
  3484. emit_const_ref(A_MOV,S_L,0,new_reference(procinfo^.framepointer,8));
  3485. }
  3486. emitlab(nodestroycall);
  3487. end;
  3488. end
  3489. end
  3490. else
  3491. { must be the return value finalized before reraising the exception? }
  3492. if (procinfo^.returntype.def<>pdef(voiddef)) and
  3493. (procinfo^.returntype.def^.needs_inittable) and
  3494. ((procinfo^.returntype.def^.deftype<>objectdef) or
  3495. not(pobjectdef(procinfo^.returntype.def)^.is_class)) then
  3496. begin
  3497. reset_reference(hr);
  3498. hr.offset:=procinfo^.return_offset;
  3499. hr.base:=procinfo^.framepointer;
  3500. finalize(procinfo^.returntype.def,hr,ret_in_param(procinfo^.returntype.def));
  3501. end;
  3502. emitcall('FPC_RERAISE');
  3503. emitlab(noreraiselabel);
  3504. end;
  3505. { call __EXIT for main program }
  3506. if (not DLLsource) and (not inlined) and (aktprocsym^.definition^.proctypeoption=potype_proginit) then
  3507. begin
  3508. emitcall('FPC_DO_EXIT');
  3509. end;
  3510. { handle return value }
  3511. uses_eax:=false;
  3512. uses_edx:=false;
  3513. uses_esi:=false;
  3514. if not(po_assembler in aktprocsym^.definition^.procoptions) then
  3515. if (aktprocsym^.definition^.proctypeoption<>potype_constructor) then
  3516. handle_return_value(inlined,uses_eax,uses_edx)
  3517. else
  3518. begin
  3519. { successful constructor deletes the zero flag }
  3520. { and returns self in eax }
  3521. { eax must be set to zero if the allocation failed !!! }
  3522. getlabel(okexitlabel);
  3523. emitjmp(C_NONE,okexitlabel);
  3524. emitlab(faillabel);
  3525. if procinfo^._class^.is_class then
  3526. begin
  3527. emit_ref_reg(A_MOV,S_L,new_reference(procinfo^.framepointer,8),R_ESI);
  3528. emitcall('FPC_HELP_FAIL_CLASS');
  3529. end
  3530. else
  3531. begin
  3532. emit_ref_reg(A_MOV,S_L,new_reference(procinfo^.framepointer,12),R_ESI);
  3533. {$ifndef noAllocEdi}
  3534. getexplicitregister32(R_EDI);
  3535. {$endif noAllocEdi}
  3536. emit_const_reg(A_MOV,S_L,procinfo^._class^.vmt_offset,R_EDI);
  3537. emitcall('FPC_HELP_FAIL');
  3538. {$ifndef noAllocEdi}
  3539. ungetregister32(R_EDI);
  3540. {$endif noAllocEdi}
  3541. end;
  3542. emitlab(okexitlabel);
  3543. emit_reg_reg(A_MOV,S_L,R_ESI,R_EAX);
  3544. emit_reg_reg(A_TEST,S_L,R_ESI,R_ESI);
  3545. uses_eax:=true;
  3546. uses_esi:=true;
  3547. end;
  3548. { stabs uses the label also ! }
  3549. if aktexit2label^.is_used or
  3550. ((cs_debuginfo in aktmoduleswitches) and not inlined) then
  3551. emitlab(aktexit2label);
  3552. { gives problems for long mangled names }
  3553. {list^.concat(new(pai_symbol,init(aktprocsym^.definition^.mangledname+'_end')));}
  3554. { should we restore edi ? }
  3555. { for all i386 gcc implementations }
  3556. if (po_savestdregs in aktprocsym^.definition^.procoptions) then
  3557. begin
  3558. if (aktprocsym^.definition^.usedregisters and ($80 shr byte(R_EBX)))<>0 then
  3559. exprasmlist^.concat(new(paicpu,op_reg(A_POP,S_L,R_EBX)));
  3560. exprasmlist^.concat(new(paicpu,op_reg(A_POP,S_L,R_ESI)));
  3561. exprasmlist^.concat(new(paicpu,op_reg(A_POP,S_L,R_EDI)));
  3562. { here we could reset R_EBX
  3563. but that is risky because it only works
  3564. if genexitcode is called after genentrycode
  3565. so lets skip this for the moment PM
  3566. aktprocsym^.definition^.usedregisters:=
  3567. aktprocsym^.definition^.usedregisters or not ($80 shr byte(R_EBX));
  3568. }
  3569. end;
  3570. { for the save all registers we can simply use a pusha,popa which
  3571. push edi,esi,ebp,esp(ignored),ebx,edx,ecx,eax }
  3572. if (po_saveregisters in aktprocsym^.definition^.procoptions) then
  3573. begin
  3574. if uses_esi then
  3575. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,R_ESI,new_reference(R_ESP,4))));
  3576. if uses_edx then
  3577. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,R_EDX,new_reference(R_ESP,20))));
  3578. if uses_eax then
  3579. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,R_EAX,new_reference(R_ESP,28))));
  3580. exprasmlist^.concat(new(paicpu,op_none(A_POPA,S_L)))
  3581. end;
  3582. if not(nostackframe) then
  3583. begin
  3584. if not inlined then
  3585. exprasmlist^.concat(new(paicpu,op_none(A_LEAVE,S_NO)));
  3586. end
  3587. else
  3588. begin
  3589. if (gettempsize<>0) and not inlined then
  3590. exprasmlist^.insert(new(paicpu,
  3591. op_const_reg(A_ADD,S_L,gettempsize,R_ESP)));
  3592. end;
  3593. { parameters are limited to 65535 bytes because }
  3594. { ret allows only imm16 }
  3595. if (parasize>65535) and not(pocall_clearstack in aktprocsym^.definition^.proccalloptions) then
  3596. CGMessage(cg_e_parasize_too_big);
  3597. { at last, the return is generated }
  3598. if not inlined then
  3599. if (po_interrupt in aktprocsym^.definition^.procoptions) then
  3600. begin
  3601. if uses_esi then
  3602. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,R_ESI,new_reference(R_ESP,16))));
  3603. if uses_edx then
  3604. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,R_EDX,new_reference(R_ESP,12))));
  3605. if uses_eax then
  3606. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,R_EAX,new_reference(R_ESP,0))));
  3607. generate_interrupt_stackframe_exit;
  3608. end
  3609. else
  3610. begin
  3611. {Routines with the poclearstack flag set use only a ret.}
  3612. { also routines with parasize=0 }
  3613. if (pocall_clearstack in aktprocsym^.definition^.proccalloptions) then
  3614. begin
  3615. {$ifndef OLD_C_STACK}
  3616. { complex return values are removed from stack in C code PM }
  3617. if ret_in_param(aktprocsym^.definition^.rettype.def) then
  3618. exprasmlist^.concat(new(paicpu,op_const(A_RET,S_NO,4)))
  3619. else
  3620. {$endif not OLD_C_STACK}
  3621. exprasmlist^.concat(new(paicpu,op_none(A_RET,S_NO)));
  3622. end
  3623. else if (parasize=0) then
  3624. exprasmlist^.concat(new(paicpu,op_none(A_RET,S_NO)))
  3625. else
  3626. exprasmlist^.concat(new(paicpu,op_const(A_RET,S_NO,parasize)));
  3627. end;
  3628. if not inlined then
  3629. exprasmlist^.concat(new(pai_symbol_end,initname(aktprocsym^.definition^.mangledname)));
  3630. {$ifdef GDB}
  3631. if (cs_debuginfo in aktmoduleswitches) and not inlined then
  3632. begin
  3633. aktprocsym^.concatstabto(exprasmlist);
  3634. if assigned(procinfo^._class) then
  3635. if (not assigned(procinfo^.parent) or
  3636. not assigned(procinfo^.parent^._class)) then
  3637. exprasmlist^.concat(new(pai_stabs,init(strpnew(
  3638. '"$t:v'+procinfo^._class^.numberstring+'",'+
  3639. tostr(N_PSYM)+',0,0,'+tostr(procinfo^.selfpointer_offset)))))
  3640. else
  3641. exprasmlist^.concat(new(pai_stabs,init(strpnew(
  3642. '"$t:r*'+procinfo^._class^.numberstring+'",'+
  3643. tostr(N_RSYM)+',0,0,'+tostr(GDB_i386index[R_ESI])))));
  3644. { define calling EBP as pseudo local var PM }
  3645. { this enables test if the function is a local one !! }
  3646. if assigned(procinfo^.parent) and (lexlevel>normal_function_level) then
  3647. exprasmlist^.concat(new(pai_stabs,init(strpnew(
  3648. '"parent_ebp:'+voidpointerdef^.numberstring+'",'+
  3649. tostr(N_LSYM)+',0,0,'+tostr(procinfo^.framepointer_offset)))));
  3650. if (pdef(aktprocsym^.definition^.rettype.def) <> pdef(voiddef)) then
  3651. begin
  3652. if ret_in_param(aktprocsym^.definition^.rettype.def) then
  3653. exprasmlist^.concat(new(pai_stabs,init(strpnew(
  3654. '"'+aktprocsym^.name+':X*'+aktprocsym^.definition^.rettype.def^.numberstring+'",'+
  3655. tostr(N_PSYM)+',0,0,'+tostr(procinfo^.return_offset)))))
  3656. else
  3657. exprasmlist^.concat(new(pai_stabs,init(strpnew(
  3658. '"'+aktprocsym^.name+':X'+aktprocsym^.definition^.rettype.def^.numberstring+'",'+
  3659. tostr(N_PSYM)+',0,0,'+tostr(procinfo^.return_offset)))));
  3660. if (m_result in aktmodeswitches) then
  3661. if ret_in_param(aktprocsym^.definition^.rettype.def) then
  3662. exprasmlist^.concat(new(pai_stabs,init(strpnew(
  3663. '"RESULT:X*'+aktprocsym^.definition^.rettype.def^.numberstring+'",'+
  3664. tostr(N_PSYM)+',0,0,'+tostr(procinfo^.return_offset)))))
  3665. else
  3666. exprasmlist^.concat(new(pai_stabs,init(strpnew(
  3667. '"RESULT:X'+aktprocsym^.definition^.rettype.def^.numberstring+'",'+
  3668. tostr(N_PSYM)+',0,0,'+tostr(procinfo^.return_offset)))));
  3669. end;
  3670. mangled_length:=length(aktprocsym^.definition^.mangledname);
  3671. getmem(p,2*mangled_length+50);
  3672. strpcopy(p,'192,0,0,');
  3673. strpcopy(strend(p),aktprocsym^.definition^.mangledname);
  3674. if (target_os.use_function_relative_addresses) then
  3675. begin
  3676. strpcopy(strend(p),'-');
  3677. strpcopy(strend(p),aktprocsym^.definition^.mangledname);
  3678. end;
  3679. exprasmlist^.concat(new(pai_stabn,init(strnew(p))));
  3680. {list^.concat(new(pai_stabn,init(strpnew('192,0,0,'
  3681. +aktprocsym^.definition^.mangledname))));
  3682. p[0]:='2';p[1]:='2';p[2]:='4';
  3683. strpcopy(strend(p),'_end');}
  3684. strpcopy(p,'224,0,0,'+aktexit2label^.name);
  3685. if (target_os.use_function_relative_addresses) then
  3686. begin
  3687. strpcopy(strend(p),'-');
  3688. strpcopy(strend(p),aktprocsym^.definition^.mangledname);
  3689. end;
  3690. exprasmlist^.concatlist(withdebuglist);
  3691. exprasmlist^.concat(new(pai_stabn,init(
  3692. strnew(p))));
  3693. { strpnew('224,0,0,'
  3694. +aktprocsym^.definition^.mangledname+'_end'))));}
  3695. freemem(p,2*mangled_length+50);
  3696. end;
  3697. {$endif GDB}
  3698. if inlined then
  3699. cleanup_regvars(exprasmlist);
  3700. exprasmlist:=oldexprasmlist;
  3701. end;
  3702. procedure genimplicitunitfinal(alist : paasmoutput);
  3703. begin
  3704. { using current_module^.globalsymtable is hopefully }
  3705. { more robust than symtablestack and symtablestack^.next }
  3706. psymtable(current_module^.globalsymtable)^.foreach({$ifndef TP}@{$endif}finalize_data);
  3707. psymtable(current_module^.localsymtable)^.foreach({$ifndef TP}@{$endif}finalize_data);
  3708. exprasmlist^.insert(new(pai_symbol,initname_global('FINALIZE$$'+current_module^.modulename^,0)));
  3709. exprasmlist^.insert(new(pai_symbol,initname_global(target_os.cprefix+current_module^.modulename^+'_finalize',0)));
  3710. {$ifdef GDB}
  3711. if (cs_debuginfo in aktmoduleswitches) and
  3712. target_os.use_function_relative_addresses then
  3713. exprasmlist^.insert(new(pai_stab_function_name,init(strpnew('FINALIZE$$'+current_module^.modulename^))));
  3714. {$endif GDB}
  3715. exprasmlist^.concat(new(paicpu,op_none(A_RET,S_NO)));
  3716. alist^.concatlist(exprasmlist);
  3717. end;
  3718. procedure genimplicitunitinit(alist : paasmoutput);
  3719. begin
  3720. { using current_module^.globalsymtable is hopefully }
  3721. { more robust than symtablestack and symtablestack^.next }
  3722. psymtable(current_module^.globalsymtable)^.foreach({$ifndef TP}@{$endif}finalize_data);
  3723. psymtable(current_module^.localsymtable)^.foreach({$ifndef TP}@{$endif}finalize_data);
  3724. exprasmlist^.insert(new(pai_symbol,initname_global('INIT$$'+current_module^.modulename^,0)));
  3725. exprasmlist^.insert(new(pai_symbol,initname_global(target_os.cprefix+current_module^.modulename^+'_init',0)));
  3726. {$ifdef GDB}
  3727. if (cs_debuginfo in aktmoduleswitches) and
  3728. target_os.use_function_relative_addresses then
  3729. exprasmlist^.insert(new(pai_stab_function_name,init(strpnew('INIT$$'+current_module^.modulename^))));
  3730. {$endif GDB}
  3731. exprasmlist^.concat(new(paicpu,op_none(A_RET,S_NO)));
  3732. alist^.concatlist(exprasmlist);
  3733. end;
  3734. {$ifdef test_dest_loc}
  3735. procedure mov_reg_to_dest(p : ptree; s : topsize; reg : tregister);
  3736. begin
  3737. if (dest_loc.loc=LOC_CREGISTER) or (dest_loc.loc=LOC_REGISTER) then
  3738. begin
  3739. emit_reg_reg(A_MOV,s,reg,dest_loc.register);
  3740. set_location(p^.location,dest_loc);
  3741. in_dest_loc:=true;
  3742. end
  3743. else
  3744. if (dest_loc.loc=LOC_REFERENCE) or (dest_loc.loc=LOC_MEM) then
  3745. begin
  3746. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,s,reg,newreference(dest_loc.reference))));
  3747. set_location(p^.location,dest_loc);
  3748. in_dest_loc:=true;
  3749. end
  3750. else
  3751. internalerror(20080);
  3752. end;
  3753. {$endif test_dest_loc}
  3754. end.
  3755. {
  3756. $Log$
  3757. Revision 1.9 2000-08-10 18:42:03 peter
  3758. * fixed for constants in emit_push_mem_size for go32v2 (merged)
  3759. Revision 1.8 2000/08/07 11:29:40 jonas
  3760. + emit_push_mem_size() which pushes a value in memory of a certain size
  3761. * pushsetelement() and pushvaluepara() use this new procedure, because
  3762. otherwise they could sometimes try to push data past the end of the
  3763. heap, causing a crash
  3764. (merged from fixes branch)
  3765. Revision 1.7 2000/08/03 13:17:25 jonas
  3766. + allow regvars to be used inside inlined procs, which required the
  3767. following changes:
  3768. + load regvars in genentrycode/free them in genexitcode (cgai386)
  3769. * moved all regvar related code to new regvars unit
  3770. + added pregvarinfo type to hcodegen
  3771. + added regvarinfo field to tprocinfo (symdef/symdefh)
  3772. * deallocate the regvars of the caller in secondprocinline before
  3773. inlining the called procedure and reallocate them afterwards
  3774. Revision 1.6 2000/08/02 08:05:04 jonas
  3775. * fixed web bug1087
  3776. * allocate R_ECX explicitely if it's used
  3777. (merged from fixes branch)
  3778. Revision 1.5 2000/07/27 09:25:05 jonas
  3779. * moved locflags2reg() procedure from cg386add to cgai386
  3780. + added locjump2reg() procedure to cgai386
  3781. * fixed internalerror(2002) when the result of a case expression has
  3782. LOC_JUMP
  3783. (all merged from fixes branch)
  3784. Revision 1.4 2000/07/21 15:14:02 jonas
  3785. + added is_addr field for labels, if they are only used for getting the address
  3786. (e.g. for io checks) and corresponding getaddrlabel() procedure
  3787. Revision 1.3 2000/07/13 12:08:25 michael
  3788. + patched to 1.1.0 with former 1.09patch from peter
  3789. Revision 1.2 2000/07/13 11:32:37 michael
  3790. + removed logs
  3791. }