cgai386.pas 134 KB

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